Epoch chain: update reference to head block. (#4249)

* Update reference to head block.

* Fix block retrieve.

Co-authored-by: Konstantin <k.potapov@softpro.com>
pull/4251/head
Konstantin 2 years ago committed by GitHub
parent 9df446a23d
commit 93b14ddca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      core/epochchain.go
  2. 10
      core/rawdb/accessors_chain.go
  3. 2
      p2p/stream/protocols/sync/chain.go

@ -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")
}
@ -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
}

@ -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)

@ -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 {

Loading…
Cancel
Save