Skip to content

Commit

Permalink
Epoch chain: update reference to head block. (#4249)
Browse files Browse the repository at this point in the history
* Update reference to head block.

* Fix block retrieve.

Co-authored-by: Konstantin <[email protected]>
  • Loading branch information
Frozen and Konstantin authored Jul 29, 2022
1 parent 9df446a commit 93b14dd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
14 changes: 13 additions & 1 deletion core/epochchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewEpochChain(db ethdb.Database, chainConfig *params.ChainConfig,
return nil, err
}
} else {
header := bc.GetHeaderByHash(hash)
header := bc.GetHeaderByHash(head)
if header == nil {
return nil, errors.New("failed to initialize: missing header")
}
Expand Down Expand Up @@ -148,6 +148,18 @@ func (bc *EpochChain) InsertChain(blocks types.Blocks, _ bool) (int, error) {
if err != nil {
return i, err
}
err = rawdb.WriteHeadHeaderHash(batch, block.Hash())
if err != nil {
return i, err
}
err = rawdb.WriteHeaderNumber(batch, block.Hash(), block.NumberU64())
if err != nil {
return i, err
}
err = rawdb.WriteHeader(batch, block.Header())
if err != nil {
return i, err
}
if err := batch.Write(); err != nil {
return i, err
}
Expand Down
10 changes: 10 additions & 0 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ func ReadHeaderNumber(db DatabaseReader, hash common.Hash) *uint64 {
return &number
}

// WriteHeaderNumber stores reference from hash to number.
func WriteHeaderNumber(db DatabaseWriter, hash common.Hash, number uint64) error {
var (
key = headerNumberKey(hash)
encoded = encodeBlockNumber(number)
)

return db.Put(key, encoded)
}

// ReadHeadHeaderHash retrieves the hash of the current canonical head header.
func ReadHeadHeaderHash(db DatabaseReader) common.Hash {
data, _ := db.Get(headHeaderKey)
Expand Down
2 changes: 1 addition & 1 deletion p2p/stream/protocols/sync/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (ch *chainHelperImpl) getBlockWithSigByHeader(header *block.Header) (*types
rs, err := ch.keyLocker.Lock(header.Number().Uint64(), func() (interface{}, error) {
b := ch.chain.GetBlock(header.Hash(), header.Number().Uint64())
if b == nil {
return nil, nil
return nil, errors.Errorf("block %d not found", header.Number().Uint64())
}
commitSig, err := ch.getBlockSigAndBitmap(header)
if err != nil {
Expand Down

0 comments on commit 93b14dd

Please sign in to comment.