Skip to content

Commit

Permalink
Removed the number from the external block cache, rawdb, GetExternalB…
Browse files Browse the repository at this point in the history
…lock methods
  • Loading branch information
shreekarashastry committed Jun 30, 2022
1 parent 8aa6a34 commit 8339997
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 46 deletions.
10 changes: 5 additions & 5 deletions consensus/blake3/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func (blake3 *Blake3) PrimeTraceBranch(chain consensus.ChainHeaderReader, header
// Obtain the external block on the branch we are currently tracing.
var extBlock *types.ExternalBlock
var err error
extBlock, err = chain.GetExternalBlock(header.Hash(), header.Number[context].Uint64(), header.Location, uint64(context))
extBlock, err = chain.GetExternalBlock(header.Hash(), header.Location, uint64(context))
if err != nil {
log.Info("Trace Branch: External Block not found for header", "number", header.Number, "context", context, "hash", header.Hash(), "location", header.Location)
return nil, err
Expand All @@ -488,7 +488,7 @@ func (blake3 *Blake3) PrimeTraceBranch(chain consensus.ChainHeaderReader, header
}

// Retrieve the previous header as an external block.
prevHeader, err := chain.GetExternalBlock(header.ParentHash[context], header.Number[context].Uint64()-1, header.Location, uint64(context))
prevHeader, err := chain.GetExternalBlock(header.ParentHash[context], header.Location, uint64(context))
if err != nil {
log.Info("Trace Branch: External Block not found for previous header", "number", header.Number[context].Int64()-1, "context", context, "hash", header.ParentHash[context], "location", header.Location)
return nil, err
Expand Down Expand Up @@ -570,7 +570,7 @@ func (blake3 *Blake3) RegionTraceBranch(chain consensus.ChainHeaderReader, heade
// Obtain the external block on the branch we are currently tracing.
var extBlock *types.ExternalBlock
var err error
extBlock, err = chain.GetExternalBlock(header.Hash(), header.Number[context].Uint64(), header.Location, uint64(context))
extBlock, err = chain.GetExternalBlock(header.Hash(), header.Location, uint64(context))
if err != nil {
log.Info("Trace Branch: External Block not found for header", "number", header.Number, "context", context, "hash", header.Hash(), "location", header.Location)
return nil, err
Expand All @@ -585,7 +585,7 @@ func (blake3 *Blake3) RegionTraceBranch(chain consensus.ChainHeaderReader, heade
}

// Retrieve the previous header as an external block.
prevHeader, err := chain.GetExternalBlock(header.ParentHash[context], header.Number[context].Uint64()-1, header.Location, uint64(context))
prevHeader, err := chain.GetExternalBlock(header.ParentHash[context], header.Location, uint64(context))
if err != nil {
log.Info("Trace Branch: External Block not found for previous header", "number", header.Number[context].Int64()-1, "context", context, "hash", header.ParentHash[context], "location", header.Location)
return nil, err
Expand Down Expand Up @@ -729,7 +729,7 @@ func (blake3 *Blake3) PreviousCoincidentOnPath(chain consensus.ChainHeaderReader
header = prevHeader
} else {
// Get previous header on external chain by hash
prevExtBlock, err := chain.GetExternalBlock(header.ParentHash[path], header.Number[path].Uint64()-1, header.Location, uint64(path))
prevExtBlock, err := chain.GetExternalBlock(header.ParentHash[path], header.Location, uint64(path))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type ChainHeaderReader interface {
GetLinkExternalBlocks(header *types.Header) ([]*types.ExternalBlock, error)

// GetExternalBlock retrieves an external block header by its hash and context.
GetExternalBlock(hash common.Hash, number uint64, location []byte, context uint64) (*types.ExternalBlock, error)
GetExternalBlock(hash common.Hash, location []byte, context uint64) (*types.ExternalBlock, error)

// QueueAndRetrieveExtBlocks passes external blocks to the queue and returns the amount available for this block
QueueAndRetrieveExtBlocks(externalBlocks []*types.ExternalBlock, header *types.Header) []*types.ExternalBlock
Expand Down
25 changes: 12 additions & 13 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ func (bc *BlockChain) NdToTd(header *types.Header, nD []*big.Int) ([]*big.Int, e
prevHeader := bc.GetHeaderByHash(header.ParentHash[params.PRIME])
if prevHeader == nil {
// Get previous header on external chain by hash
prevExtBlock, err := bc.GetExternalBlock(header.ParentHash[params.PRIME], header.Number[params.PRIME].Uint64()-1, header.Location, uint64(params.PRIME))
prevExtBlock, err := bc.GetExternalBlock(header.ParentHash[params.PRIME], header.Location, uint64(params.PRIME))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2817,19 +2817,19 @@ func (bc *BlockChain) GetHeaderByHash(hash common.Hash) *types.Header {
}

// GetExternalBlock retrieves an external block from either the ext block cache or rawdb.
func (bc *BlockChain) GetExternalBlock(hash common.Hash, number uint64, location []byte, context uint64) (*types.ExternalBlock, error) {
func (bc *BlockChain) GetExternalBlock(hash common.Hash, location []byte, context uint64) (*types.ExternalBlock, error) {
// Lookup block in externalBlocks cache
key := types.ExtBlockCacheKey(number, context, hash)
key := types.ExtBlockCacheKey(context, hash)

if block, ok := bc.externalBlocks.HasGet(nil, key); ok {
var blockDecoded *types.ExternalBlock
rlp.DecodeBytes(block, &blockDecoded)
return blockDecoded, nil
}
block := rawdb.ReadExternalBlock(bc.db, hash, number, context)
block := rawdb.ReadExternalBlock(bc.db, hash, context)

if block == nil {
block = bc.requestExternalBlock(hash, number, location, context)
block = bc.requestExternalBlock(hash, location, context)
if block == nil {
return &types.ExternalBlock{}, errors.New("error finding external block by context and hash")
}
Expand All @@ -2838,18 +2838,18 @@ func (bc *BlockChain) GetExternalBlock(hash common.Hash, number uint64, location
}

// requestExternalBlock sends an external block event to the missingExternalBlockFeed in order to be fulfilled by a manager or client.
func (bc *BlockChain) requestExternalBlock(hash common.Hash, number uint64, location []byte, context uint64) *types.ExternalBlock {
func (bc *BlockChain) requestExternalBlock(hash common.Hash, location []byte, context uint64) *types.ExternalBlock {
bc.missingExternalBlockFeed.Send(MissingExternalBlock{Hash: hash, Location: location, Context: int(context)})
for i := 0; i < params.ExternalBlockLookupLimit; i++ {
time.Sleep(time.Duration(params.ExternalBlockLookupDelay) * time.Millisecond)
// Lookup block in externalBlocks cache
key := types.ExtBlockCacheKey(number, context, hash)
key := types.ExtBlockCacheKey(context, hash)
if block, ok := bc.externalBlocks.HasGet(nil, key); ok {
var blockDecoded *types.ExternalBlock
rlp.DecodeBytes(block, &blockDecoded)
return blockDecoded
}
block := rawdb.ReadExternalBlock(bc.db, hash, number, context)
block := rawdb.ReadExternalBlock(bc.db, hash, context)
if block != nil {
return block
}
Expand All @@ -2860,14 +2860,14 @@ func (bc *BlockChain) requestExternalBlock(hash common.Hash, number uint64, loca
// GetExternalBlockTraceSet checks if the ExternalBlock for the given header is present in the cache and returns the externalBlock
func (bc *BlockChain) GetExternalBlockTraceSet(header *types.Header, context int) (*types.ExternalBlock, error) {
// Lookup block in externalBlocks cache
key := types.ExtBlockCacheKey(header.Number[context].Uint64(), uint64(context), header.Hash())
key := types.ExtBlockCacheKey(uint64(context), header.Hash())

if extBlock, ok := bc.externalBlocks.HasGet(nil, key); ok {
var extBlockDecoded *types.ExternalBlock
rlp.DecodeBytes(extBlock, &extBlockDecoded)
return extBlockDecoded, nil
}
extBlock := rawdb.ReadExternalBlock(bc.db, header.Hash(), header.Number[context].Uint64(), uint64(context))
extBlock := rawdb.ReadExternalBlock(bc.db, header.Hash(), uint64(context))

return extBlock, nil
}
Expand All @@ -2877,11 +2877,10 @@ func (bc *BlockChain) StoreExternalBlocks(blocks []*types.ExternalBlock) error {

for i := 0; i < len(blocks); i++ {
context := blocks[i].Context().Uint64()
number := blocks[i].Header().Number[context].Uint64()
hash := blocks[i].Hash()

// Lookup block in externalBlocks cache
key := types.ExtBlockCacheKey(number, context, hash)
key := types.ExtBlockCacheKey(context, hash)
bc.externalBlocks.Del(key)

rawdb.WriteExternalBlock(bc.db, blocks[i])
Expand Down Expand Up @@ -3164,7 +3163,7 @@ func (bc *BlockChain) CalcHLCRNetDifficulty(terminalHash common.Hash, header *ty
prevHeader := bc.GetHeaderByHash(header.ParentHash[order])
if prevHeader == nil {
// Get previous header on external chain by hash
prevExtBlock, err := bc.GetExternalBlock(header.ParentHash[order], header.Number[order].Uint64()-1, header.Location, uint64(order))
prevExtBlock, err := bc.GetExternalBlock(header.ParentHash[order], header.Location, uint64(order))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (cr *fakeChainReader) GetHeaderByNumber(number uint64) *types.Header
func (cr *fakeChainReader) GetHeaderByHash(hash common.Hash) *types.Header { return nil }
func (cr *fakeChainReader) GetHeader(hash common.Hash, number uint64) *types.Header { return nil }
func (cr *fakeChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { return nil }
func (cr *fakeChainReader) GetExternalBlock(hash common.Hash, number uint64, location []byte, context uint64) (*types.ExternalBlock, error) {
func (cr *fakeChainReader) GetExternalBlock(hash common.Hash, location []byte, context uint64) (*types.ExternalBlock, error) {
return nil, nil
}
func (cr *fakeChainReader) QueueAndRetrieveExtBlocks(blocks []*types.ExternalBlock, header *types.Header) []*types.ExternalBlock {
Expand Down
6 changes: 3 additions & 3 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ func (hc *HeaderChain) GetLinkExternalBlocks(header *types.Header) ([]*types.Ext

// GetExternalBlock is not applicable in the header chain since the BlockChain contains
// the external blocks cache.
func (hc *HeaderChain) GetExternalBlock(hash common.Hash, number uint64, location []byte, context uint64) (*types.ExternalBlock, error) {
func (hc *HeaderChain) GetExternalBlock(hash common.Hash, location []byte, context uint64) (*types.ExternalBlock, error) {
return nil, nil
}

Expand Down Expand Up @@ -739,7 +739,7 @@ func (hc *HeaderChain) NdToTd(header *types.Header, nD []*big.Int) ([]*big.Int,
prevHeader := hc.GetHeaderByHash(header.ParentHash[params.PRIME])
if prevHeader == nil {
// Get previous header on external chain by hash
prevExtBlock, err := hc.GetExternalBlock(header.ParentHash[params.PRIME], header.Number[params.PRIME].Uint64()-1, header.Location, uint64(params.PRIME))
prevExtBlock, err := hc.GetExternalBlock(header.ParentHash[params.PRIME], header.Location, uint64(params.PRIME))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -853,7 +853,7 @@ func (hc *HeaderChain) CalcHLCRNetDifficulty(terminalHash common.Hash, header *t
prevHeader := hc.GetHeaderByHash(header.ParentHash[order])
if prevHeader == nil {
// Get previous header on external chain by hash
prevExtBlock, err := hc.GetExternalBlock(header.ParentHash[order], header.Number[order].Uint64()-1, header.Location, uint64(order))
prevExtBlock, err := hc.GetExternalBlock(header.ParentHash[order], header.Location, uint64(order))
if err != nil {
return nil, err
}
Expand Down
29 changes: 14 additions & 15 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ func ReadHeaderRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValu
}

// ReadExternalHeaderRLP retrieves a block header in its raw RLP database encoding.
func ReadExternalHeaderRLP(db ethdb.Reader, hash common.Hash, number uint64, context uint64) rlp.RawValue {
func ReadExternalHeaderRLP(db ethdb.Reader, hash common.Hash, context uint64) rlp.RawValue {
// look up the data in leveldb.
data, _ := db.Get(extHeaderKey(number, context, hash))
data, _ := db.Get(extHeaderKey(context, hash))
if len(data) > 0 {
return data
}
Expand Down Expand Up @@ -363,8 +363,8 @@ func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header
}

// ReadExternalHeader retrieves the block header corresponding to the hash.
func ReadExternalHeader(db ethdb.Reader, hash common.Hash, number uint64, context uint64) *types.Header {
data := ReadExternalHeaderRLP(db, hash, number, context)
func ReadExternalHeader(db ethdb.Reader, hash common.Hash, context uint64) *types.Header {
data := ReadExternalHeaderRLP(db, hash, context)
if len(data) == 0 {
return nil
}
Expand Down Expand Up @@ -401,16 +401,15 @@ func WriteHeader(db ethdb.KeyValueWriter, header *types.Header) {
// to-number mapping.
func WriteExternalHeader(db ethdb.KeyValueWriter, header *types.Header, context uint64) {
var (
hash = header.Hash()
number = header.Number[context].Uint64()
hash = header.Hash()
)

// Write the encoded header
data, err := rlp.EncodeToBytes(header)
if err != nil {
log.Crit("Failed to RLP encode header", "err", err)
}
key := extHeaderKey(number, context, hash)
key := extHeaderKey(context, hash)
if err := db.Put(key, data); err != nil {
log.Crit("Failed to store header", "err", err)
}
Expand Down Expand Up @@ -464,9 +463,9 @@ func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue
}

// ReadExternalBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
func ReadExternalBodyRLP(db ethdb.Reader, hash common.Hash, number uint64, context uint64) rlp.RawValue {
func ReadExternalBodyRLP(db ethdb.Reader, hash common.Hash, context uint64) rlp.RawValue {
// Then try to look up the data in leveldb.
data, _ := db.Get(extBlockBodyKey(number, context, hash))
data, _ := db.Get(extBlockBodyKey(context, hash))
if len(data) > 0 {
return data
}
Expand Down Expand Up @@ -501,7 +500,7 @@ func WriteBodyRLP(db ethdb.KeyValueWriter, hash common.Hash, number uint64, rlp

// WriteBodyRLP stores an RLP encoded block body into the database.
func WriteExternalBodyRLP(db ethdb.KeyValueWriter, hash common.Hash, number uint64, context uint64, rlp rlp.RawValue) {
if err := db.Put(extBlockBodyKey(number, context, hash), rlp); err != nil {
if err := db.Put(extBlockBodyKey(context, hash), rlp); err != nil {
log.Crit("Failed to store block body", "err", err)
}
}
Expand Down Expand Up @@ -541,8 +540,8 @@ func WriteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64, body *t
}

// ReadExternalBody retrieves the block body corresponding to the hash.
func ReadExternalBody(db ethdb.Reader, hash common.Hash, number uint64, context uint64) *types.ExternalBody {
data := ReadExternalBodyRLP(db, hash, number, context)
func ReadExternalBody(db ethdb.Reader, hash common.Hash, context uint64) *types.ExternalBody {
data := ReadExternalBodyRLP(db, hash, context)
if len(data) == 0 {
return nil
}
Expand Down Expand Up @@ -854,12 +853,12 @@ func WriteBlock(db ethdb.KeyValueWriter, block *types.Block) {
// ReadExternalBlock retrieves an entire external block corresponding to the hash, assembling it
// back from the stored header and body. If either the header or body could not
// be retrieved nil is returned.
func ReadExternalBlock(db ethdb.Reader, hash common.Hash, number uint64, context uint64) *types.ExternalBlock {
header := ReadExternalHeader(db, hash, number, context)
func ReadExternalBlock(db ethdb.Reader, hash common.Hash, context uint64) *types.ExternalBlock {
header := ReadExternalHeader(db, hash, context)
if header == nil {
return nil
}
body := ReadExternalBody(db, hash, number, context)
body := ReadExternalBody(db, hash, context)
if body == nil {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func headerKey(number uint64, hash common.Hash) []byte {
}

// extHeaderKey = headerPrefix + num (uint64 big endian) + location + context + hash
func extHeaderKey(number uint64, context uint64, hash common.Hash) []byte {
return append(append(append(headerPrefix, encodeBlockNumber(number)...), encodeBlockNumber(context)...), hash.Bytes()...)
func extHeaderKey(context uint64, hash common.Hash) []byte {
return append(append(headerPrefix, encodeBlockNumber(context)...), hash.Bytes()...)
}

// headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
Expand All @@ -178,8 +178,8 @@ func blockBodyKey(number uint64, hash common.Hash) []byte {
}

// extBlockBodyKey = blockBodyPrefix + num (uint64 big endian) + location + context + hash
func extBlockBodyKey(number uint64, context uint64, hash common.Hash) []byte {
return append(append(append(blockBodyPrefix, encodeBlockNumber(number)...), encodeBlockNumber(context)...), hash.Bytes()...)
func extBlockBodyKey(context uint64, hash common.Hash) []byte {
return append(append(blockBodyPrefix, encodeBlockNumber(context)...), hash.Bytes()...)
}

// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
Expand Down
6 changes: 3 additions & 3 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (b *ExternalBlock) Receipts() Receipts { return b.receipts }
func (b *ExternalBlock) Context() *big.Int { return b.context }
func (b *ExternalBlock) CacheKey() []byte {
hash := b.header.Hash()
return ExtBlockCacheKey(b.header.Number[b.context.Int64()].Uint64(), b.context.Uint64(), hash)
return ExtBlockCacheKey(b.context.Uint64(), hash)
}

// Returns current MapContext for a given block.
Expand All @@ -348,8 +348,8 @@ func encodeBlockNumber(number uint64) []byte {
}

// extBlockBodyKey = blockBodyPrefix + num (uint64 big endian) + location + context + hash
func ExtBlockCacheKey(number uint64, context uint64, hash common.Hash) []byte {
return append(append(append([]byte("e"), encodeBlockNumber(number)...), encodeBlockNumber(context)...), hash.Bytes()...)
func ExtBlockCacheKey(context uint64, hash common.Hash) []byte {
return append(append([]byte("e"), encodeBlockNumber(context)...), hash.Bytes()...)
}

// Body returns the non-header content of the block.
Expand Down
2 changes: 1 addition & 1 deletion light/lightchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ func (lc *LightChain) GetLinkExternalBlocks(header *types.Header) ([]*types.Exte

// GetExternalBlock is not applicable in the header chain since the BlockChain contains
// the external blocks cache.
func (lc *LightChain) GetExternalBlock(hash common.Hash, number uint64, location []byte, context uint64) (*types.ExternalBlock, error) {
func (lc *LightChain) GetExternalBlock(hash common.Hash, location []byte, context uint64) (*types.ExternalBlock, error) {
return nil, nil
}

Expand Down

0 comments on commit 8339997

Please sign in to comment.