Avoid a redundant rlpHash() in FeedHeader() (#2959)

This commit is contained in:
TBC Dev 2021-11-15 16:52:14 +08:00 committed by GitHub
parent bca0d1d473
commit 5ddbf843d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 16 deletions

View File

@ -36,17 +36,22 @@ func TestInserter1(t *testing.T) {
}
defer tx.Rollback()
hi := NewHeaderInserter("headers", big.NewInt(0), 0)
var h1, h2 types.Header
h1.Number = big.NewInt(1)
h1.Difficulty = big.NewInt(10)
h1.ParentHash = genesis.Hash()
h2.Number = big.NewInt(2)
h2.Difficulty = big.NewInt(1010)
h2.ParentHash = h1.Hash()
if err = hi.FeedHeader(tx, &h1, 1); err != nil {
h1 := types.Header{
Number: big.NewInt(1),
Difficulty: big.NewInt(10),
ParentHash: genesis.Hash(),
}
h1Hash := h1.Hash()
h2 := types.Header{
Number: big.NewInt(2),
Difficulty: big.NewInt(1010),
ParentHash: h1Hash,
}
h2Hash := h2.Hash()
if err = hi.FeedHeader(tx, &h1, h1Hash, 1); err != nil {
t.Errorf("feed empty header 1: %v", err)
}
if err = hi.FeedHeader(tx, &h2, 2); err != nil {
if err = hi.FeedHeader(tx, &h2, h2Hash, 2); err != nil {
t.Errorf("feed empty header 2: %v", err)
}
}

View File

@ -591,7 +591,7 @@ func (hd *HeaderDownload) RequestSkeleton() *HeaderRequest {
// InsertHeaders attempts to insert headers into the database, verifying them first
// It returns true in the first return value if the system is "in sync"
func (hd *HeaderDownload) InsertHeaders(hf func(header *types.Header, blockHeight uint64) error, logPrefix string, logChannel <-chan time.Time) (bool, error) {
func (hd *HeaderDownload) InsertHeaders(hf func(header *types.Header, hash common.Hash, blockHeight uint64) error, logPrefix string, logChannel <-chan time.Time) (bool, error) {
hd.lock.Lock()
defer hd.lock.Unlock()
var linksInFuture []*Link // Here we accumulate links that fail validation as "in the future"
@ -635,7 +635,7 @@ func (hd *HeaderDownload) InsertHeaders(hf func(header *types.Header, blockHeigh
delete(hd.links, link.hash)
continue
}
if err := hf(link.header, link.blockHeight); err != nil {
if err := hf(link.header, link.hash, link.blockHeight); err != nil {
return false, err
}
if link.blockHeight > hd.highestInDb {
@ -717,15 +717,14 @@ func (hd *HeaderDownload) addHeaderAsLink(header *types.Header, persisted bool)
return link
}
func (hi *HeaderInserter) FeedHeaderFunc(db kv.StatelessRwTx) func(header *types.Header, blockHeight uint64) error {
return func(header *types.Header, blockHeight uint64) error {
return hi.FeedHeader(db, header, blockHeight)
func (hi *HeaderInserter) FeedHeaderFunc(db kv.StatelessRwTx) func(header *types.Header, hash common.Hash, blockHeight uint64) error {
return func(header *types.Header, hash common.Hash, blockHeight uint64) error {
return hi.FeedHeader(db, header, hash, blockHeight)
}
}
func (hi *HeaderInserter) FeedHeader(db kv.StatelessRwTx, header *types.Header, blockHeight uint64) error {
hash := header.Hash()
func (hi *HeaderInserter) FeedHeader(db kv.StatelessRwTx, header *types.Header, hash common.Hash, blockHeight uint64) error {
if hash == hi.prevHash {
// Skip duplicates
return nil