Skip to content

Commit 6289137

Browse files
authored
consensus/clique, core: API cleanup (#23100)
This removes some code: - The clique engine calculated the snapshot twice when verifying headers/blocks. - The method GetBlockHashesFromHash in Header/Block/Lightchain was only used by tests. It is now removed from the API. - The method GetTdByHash internally looked up the number before calling GetTd(hash, num). In many cases, callers already had the number, and used this method just because it has a shorter name. I have removed the method to make the API surface smaller.
1 parent da3da7c commit 6289137

File tree

9 files changed

+38
-84
lines changed

9 files changed

+38
-84
lines changed

consensus/clique/clique.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header
363363
}
364364
}
365365
// All basic checks passed, verify the seal and return
366-
return c.verifySeal(chain, header, parents)
366+
return c.verifySeal(snap, header, parents)
367367
}
368368

369369
// snapshot retrieves the authorization snapshot at a given point in time.
@@ -460,18 +460,12 @@ func (c *Clique) VerifyUncles(chain consensus.ChainReader, block *types.Block) e
460460
// consensus protocol requirements. The method accepts an optional list of parent
461461
// headers that aren't yet part of the local blockchain to generate the snapshots
462462
// from.
463-
func (c *Clique) verifySeal(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error {
463+
func (c *Clique) verifySeal(snap *Snapshot, header *types.Header, parents []*types.Header) error {
464464
// Verifying the genesis block is not supported
465465
number := header.Number.Uint64()
466466
if number == 0 {
467467
return errUnknownBlock
468468
}
469-
// Retrieve the snapshot needed to verify this header and cache it
470-
snap, err := c.snapshot(chain, number-1, header.ParentHash, parents)
471-
if err != nil {
472-
return err
473-
}
474-
475469
// Resolve the authorization key and check against signers
476470
signer, err := ecrecover(header, c.signatures)
477471
if err != nil {

core/blockchain.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,12 +2411,6 @@ func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int {
24112411
return bc.hc.GetTd(hash, number)
24122412
}
24132413

2414-
// GetTdByHash retrieves a block's total difficulty in the canonical chain from the
2415-
// database by hash, caching it if found.
2416-
func (bc *BlockChain) GetTdByHash(hash common.Hash) *big.Int {
2417-
return bc.hc.GetTdByHash(hash)
2418-
}
2419-
24202414
// GetHeader retrieves a block header from the database by hash and number,
24212415
// caching it if found.
24222416
func (bc *BlockChain) GetHeader(hash common.Hash, number uint64) *types.Header {
@@ -2450,12 +2444,6 @@ func (bc *BlockChain) GetCanonicalHash(number uint64) common.Hash {
24502444
return bc.hc.GetCanonicalHash(number)
24512445
}
24522446

2453-
// GetBlockHashesFromHash retrieves a number of block hashes starting at a given
2454-
// hash, fetching towards the genesis block.
2455-
func (bc *BlockChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
2456-
return bc.hc.GetBlockHashesFromHash(hash, max)
2457-
}
2458-
24592447
// GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
24602448
// a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
24612449
// number of blocks to be individually checked before we reach the canonical chain.

core/blockchain_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,21 @@ func testFork(t *testing.T, blockchain *BlockChain, i, n int, full bool, compara
118118
var tdPre, tdPost *big.Int
119119

120120
if full {
121-
tdPre = blockchain.GetTdByHash(blockchain.CurrentBlock().Hash())
121+
cur := blockchain.CurrentBlock()
122+
tdPre = blockchain.GetTd(cur.Hash(), cur.NumberU64())
122123
if err := testBlockChainImport(blockChainB, blockchain); err != nil {
123124
t.Fatalf("failed to import forked block chain: %v", err)
124125
}
125-
tdPost = blockchain.GetTdByHash(blockChainB[len(blockChainB)-1].Hash())
126+
last := blockChainB[len(blockChainB)-1]
127+
tdPost = blockchain.GetTd(last.Hash(), last.NumberU64())
126128
} else {
127-
tdPre = blockchain.GetTdByHash(blockchain.CurrentHeader().Hash())
129+
cur := blockchain.CurrentHeader()
130+
tdPre = blockchain.GetTd(cur.Hash(), cur.Number.Uint64())
128131
if err := testHeaderChainImport(headerChainB, blockchain); err != nil {
129132
t.Fatalf("failed to import forked header chain: %v", err)
130133
}
131-
tdPost = blockchain.GetTdByHash(headerChainB[len(headerChainB)-1].Hash())
134+
last := headerChainB[len(headerChainB)-1]
135+
tdPost = blockchain.GetTd(last.Hash(), last.Number.Uint64())
132136
}
133137
// Compare the total difficulties of the chains
134138
comparator(tdPre, tdPost)
@@ -165,7 +169,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
165169
}
166170

167171
blockchain.chainmu.MustLock()
168-
rawdb.WriteTd(blockchain.db, block.Hash(), block.NumberU64(), new(big.Int).Add(block.Difficulty(), blockchain.GetTdByHash(block.ParentHash())))
172+
rawdb.WriteTd(blockchain.db, block.Hash(), block.NumberU64(), new(big.Int).Add(block.Difficulty(), blockchain.GetTd(block.ParentHash(), block.NumberU64()-1)))
169173
rawdb.WriteBlock(blockchain.db, block)
170174
statedb.Commit(false)
171175
blockchain.chainmu.Unlock()
@@ -183,7 +187,7 @@ func testHeaderChainImport(chain []*types.Header, blockchain *BlockChain) error
183187
}
184188
// Manually insert the header into the database, but don't reorganise (allows subsequent testing)
185189
blockchain.chainmu.MustLock()
186-
rawdb.WriteTd(blockchain.db, header.Hash(), header.Number.Uint64(), new(big.Int).Add(header.Difficulty, blockchain.GetTdByHash(header.ParentHash)))
190+
rawdb.WriteTd(blockchain.db, header.Hash(), header.Number.Uint64(), new(big.Int).Add(header.Difficulty, blockchain.GetTd(header.ParentHash, header.Number.Uint64()-1)))
187191
rawdb.WriteHeader(blockchain.db, header)
188192
blockchain.chainmu.Unlock()
189193
}
@@ -436,11 +440,13 @@ func testReorg(t *testing.T, first, second []int64, td int64, full bool) {
436440
// Make sure the chain total difficulty is the correct one
437441
want := new(big.Int).Add(blockchain.genesisBlock.Difficulty(), big.NewInt(td))
438442
if full {
439-
if have := blockchain.GetTdByHash(blockchain.CurrentBlock().Hash()); have.Cmp(want) != 0 {
443+
cur := blockchain.CurrentBlock()
444+
if have := blockchain.GetTd(cur.Hash(), cur.NumberU64()); have.Cmp(want) != 0 {
440445
t.Errorf("total difficulty mismatch: have %v, want %v", have, want)
441446
}
442447
} else {
443-
if have := blockchain.GetTdByHash(blockchain.CurrentHeader().Hash()); have.Cmp(want) != 0 {
448+
cur := blockchain.CurrentHeader()
449+
if have := blockchain.GetTd(cur.Hash(), cur.Number.Uint64()); have.Cmp(want) != 0 {
444450
t.Errorf("total difficulty mismatch: have %v, want %v", have, want)
445451
}
446452
}
@@ -676,10 +682,10 @@ func TestFastVsFullChains(t *testing.T) {
676682
for i := 0; i < len(blocks); i++ {
677683
num, hash := blocks[i].NumberU64(), blocks[i].Hash()
678684

679-
if ftd, atd := fast.GetTdByHash(hash), archive.GetTdByHash(hash); ftd.Cmp(atd) != 0 {
685+
if ftd, atd := fast.GetTd(hash, num), archive.GetTd(hash, num); ftd.Cmp(atd) != 0 {
680686
t.Errorf("block #%d [%x]: td mismatch: fastdb %v, archivedb %v", num, hash, ftd, atd)
681687
}
682-
if antd, artd := ancient.GetTdByHash(hash), archive.GetTdByHash(hash); antd.Cmp(artd) != 0 {
688+
if antd, artd := ancient.GetTd(hash, num), archive.GetTd(hash, num); antd.Cmp(artd) != 0 {
683689
t.Errorf("block #%d [%x]: td mismatch: ancientdb %v, archivedb %v", num, hash, antd, artd)
684690
}
685691
if fheader, aheader := fast.GetHeaderByHash(hash), archive.GetHeaderByHash(hash); fheader.Hash() != aheader.Hash() {

core/headerchain.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -394,29 +394,6 @@ func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, start time.Time)
394394
return res.status, err
395395
}
396396

397-
// GetBlockHashesFromHash retrieves a number of block hashes starting at a given
398-
// hash, fetching towards the genesis block.
399-
func (hc *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
400-
// Get the origin header from which to fetch
401-
header := hc.GetHeaderByHash(hash)
402-
if header == nil {
403-
return nil
404-
}
405-
// Iterate the headers until enough is collected or the genesis reached
406-
chain := make([]common.Hash, 0, max)
407-
for i := uint64(0); i < max; i++ {
408-
next := header.ParentHash
409-
if header = hc.GetHeader(next, header.Number.Uint64()-1); header == nil {
410-
break
411-
}
412-
chain = append(chain, next)
413-
if header.Number.Sign() == 0 {
414-
break
415-
}
416-
}
417-
return chain
418-
}
419-
420397
// GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
421398
// a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
422399
// number of blocks to be individually checked before we reach the canonical chain.
@@ -472,16 +449,6 @@ func (hc *HeaderChain) GetTd(hash common.Hash, number uint64) *big.Int {
472449
return td
473450
}
474451

475-
// GetTdByHash retrieves a block's total difficulty in the canonical chain from the
476-
// database by hash, caching it if found.
477-
func (hc *HeaderChain) GetTdByHash(hash common.Hash) *big.Int {
478-
number := hc.GetBlockNumber(hash)
479-
if number == nil {
480-
return nil
481-
}
482-
return hc.GetTd(hash, *number)
483-
}
484-
485452
// GetHeader retrieves a block header from the database by hash and number,
486453
// caching it if found.
487454
func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.Header {

eth/api_backend.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*typ
194194
}
195195

196196
func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
197-
return b.eth.blockchain.GetTdByHash(hash)
197+
if header := b.eth.blockchain.GetHeaderByHash(hash); header != nil {
198+
return b.eth.blockchain.GetTd(hash, header.Number.Uint64())
199+
}
200+
return nil
198201
}
199202

200203
func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {

eth/protocols/eth/handler_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
126126
for i := range unknown {
127127
unknown[i] = byte(i)
128128
}
129+
getHashes := func(from, limit uint64) (hashes []common.Hash) {
130+
for i := uint64(0); i < limit; i++ {
131+
hashes = append(hashes, backend.chain.GetCanonicalHash(from-1-i))
132+
}
133+
return hashes
134+
}
129135
// Create a batch of tests for various scenarios
130136
limit := uint64(maxHeadersServe)
131137
tests := []struct {
@@ -183,7 +189,7 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
183189
// Ensure protocol limits are honored
184190
{
185191
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: backend.chain.CurrentBlock().NumberU64() - 1}, Amount: limit + 10, Reverse: true},
186-
backend.chain.GetBlockHashesFromHash(backend.chain.CurrentBlock().Hash(), limit),
192+
getHashes(backend.chain.CurrentBlock().NumberU64(), limit),
187193
},
188194
// Check that requesting more than available is handled gracefully
189195
{

eth/sync.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,15 @@ func (cs *chainSyncer) modeAndLocalHead() (downloader.SyncMode, *big.Int) {
182182
// If we're in fast sync mode, return that directly
183183
if atomic.LoadUint32(&cs.handler.fastSync) == 1 {
184184
block := cs.handler.chain.CurrentFastBlock()
185-
td := cs.handler.chain.GetTdByHash(block.Hash())
185+
td := cs.handler.chain.GetTd(block.Hash(), block.NumberU64())
186186
return downloader.FastSync, td
187187
}
188188
// We are probably in full sync, but we might have rewound to before the
189189
// fast sync pivot, check if we should reenable
190190
if pivot := rawdb.ReadLastPivotNumber(cs.handler.database); pivot != nil {
191191
if head := cs.handler.chain.CurrentBlock(); head.NumberU64() < *pivot {
192192
block := cs.handler.chain.CurrentFastBlock()
193-
td := cs.handler.chain.GetTdByHash(block.Hash())
193+
td := cs.handler.chain.GetTd(block.Hash(), block.NumberU64())
194194
return downloader.FastSync, td
195195
}
196196
}

light/lightchain.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,6 @@ func (lc *LightChain) GetTd(hash common.Hash, number uint64) *big.Int {
430430
return lc.hc.GetTd(hash, number)
431431
}
432432

433-
// GetTdByHash retrieves a block's total difficulty in the canonical chain from the
434-
// database by hash, caching it if found.
435-
func (lc *LightChain) GetTdByHash(hash common.Hash) *big.Int {
436-
return lc.hc.GetTdByHash(hash)
437-
}
438-
439433
// GetHeaderByNumberOdr retrieves the total difficult from the database or
440434
// network by hash and number, caching it (associated with its hash) if found.
441435
func (lc *LightChain) GetTdOdr(ctx context.Context, hash common.Hash, number uint64) *big.Int {
@@ -470,12 +464,6 @@ func (bc *LightChain) GetCanonicalHash(number uint64) common.Hash {
470464
return bc.hc.GetCanonicalHash(number)
471465
}
472466

473-
// GetBlockHashesFromHash retrieves a number of block hashes starting at a given
474-
// hash, fetching towards the genesis block.
475-
func (lc *LightChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
476-
return lc.hc.GetBlockHashesFromHash(hash, max)
477-
}
478-
479467
// GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
480468
// a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
481469
// number of blocks to be individually checked before we reach the canonical chain.

light/lightchain_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ func testFork(t *testing.T, LightChain *LightChain, i, n int, comparator func(td
104104
}
105105
// Sanity check that the forked chain can be imported into the original
106106
var tdPre, tdPost *big.Int
107-
108-
tdPre = LightChain.GetTdByHash(LightChain.CurrentHeader().Hash())
107+
cur := LightChain.CurrentHeader()
108+
tdPre = LightChain.GetTd(cur.Hash(), cur.Number.Uint64())
109109
if err := testHeaderChainImport(headerChainB, LightChain); err != nil {
110110
t.Fatalf("failed to import forked header chain: %v", err)
111111
}
112-
tdPost = LightChain.GetTdByHash(headerChainB[len(headerChainB)-1].Hash())
112+
last := headerChainB[len(headerChainB)-1]
113+
tdPost = LightChain.GetTd(last.Hash(), last.Number.Uint64())
113114
// Compare the total difficulties of the chains
114115
comparator(tdPre, tdPost)
115116
}
@@ -124,7 +125,8 @@ func testHeaderChainImport(chain []*types.Header, lightchain *LightChain) error
124125
}
125126
// Manually insert the header into the database, but don't reorganize (allows subsequent testing)
126127
lightchain.chainmu.Lock()
127-
rawdb.WriteTd(lightchain.chainDb, header.Hash(), header.Number.Uint64(), new(big.Int).Add(header.Difficulty, lightchain.GetTdByHash(header.ParentHash)))
128+
rawdb.WriteTd(lightchain.chainDb, header.Hash(), header.Number.Uint64(),
129+
new(big.Int).Add(header.Difficulty, lightchain.GetTd(header.ParentHash, header.Number.Uint64()-1)))
128130
rawdb.WriteHeader(lightchain.chainDb, header)
129131
lightchain.chainmu.Unlock()
130132
}
@@ -309,7 +311,7 @@ func testReorg(t *testing.T, first, second []int, td int64) {
309311
}
310312
// Make sure the chain total difficulty is the correct one
311313
want := new(big.Int).Add(bc.genesisBlock.Difficulty(), big.NewInt(td))
312-
if have := bc.GetTdByHash(bc.CurrentHeader().Hash()); have.Cmp(want) != 0 {
314+
if have := bc.GetTd(bc.CurrentHeader().Hash(), bc.CurrentHeader().Number.Uint64()); have.Cmp(want) != 0 {
313315
t.Errorf("total difficulty mismatch: have %v, want %v", have, want)
314316
}
315317
}

0 commit comments

Comments
 (0)