Skip to content

Commit 28c5a8a

Browse files
authored
les: implement new les fetcher (#20692)
* cmd, consensus, eth, les: implement light fetcher * les: address comment * les: address comment * les: address comments * les: check td after delivery * les: add linearExpiredValue for error counter * les: fix import * les: fix dead lock * les: order announces by td * les: encapsulate invalid counter * les: address comment * les: add more checks during the delivery * les: fix log * eth, les: fix lint * eth/fetcher: address comment
1 parent 93da0cf commit 28c5a8a

22 files changed

Lines changed: 1318 additions & 1014 deletions

‎cmd/geth/retesteth.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,23 @@ func (e *NoRewardEngine) Author(header *types.Header) (common.Address, error) {
200200
return e.inner.Author(header)
201201
}
202202

203-
func (e *NoRewardEngine) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error {
203+
func (e *NoRewardEngine) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error {
204204
return e.inner.VerifyHeader(chain, header, seal)
205205
}
206206

207-
func (e *NoRewardEngine) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
207+
func (e *NoRewardEngine) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
208208
return e.inner.VerifyHeaders(chain, headers, seals)
209209
}
210210

211211
func (e *NoRewardEngine) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
212212
return e.inner.VerifyUncles(chain, block)
213213
}
214214

215-
func (e *NoRewardEngine) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
215+
func (e *NoRewardEngine) VerifySeal(chain consensus.ChainHeaderReader, header *types.Header) error {
216216
return e.inner.VerifySeal(chain, header)
217217
}
218218

219-
func (e *NoRewardEngine) Prepare(chain consensus.ChainReader, header *types.Header) error {
219+
func (e *NoRewardEngine) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
220220
return e.inner.Prepare(chain, header)
221221
}
222222

@@ -229,7 +229,7 @@ func (e *NoRewardEngine) accumulateRewards(config *params.ChainConfig, state *st
229229
state.AddBalance(header.Coinbase, reward)
230230
}
231231

232-
func (e *NoRewardEngine) Finalize(chain consensus.ChainReader, header *types.Header, statedb *state.StateDB, txs []*types.Transaction,
232+
func (e *NoRewardEngine) Finalize(chain consensus.ChainHeaderReader, header *types.Header, statedb *state.StateDB, txs []*types.Transaction,
233233
uncles []*types.Header) {
234234
if e.rewardsOn {
235235
e.inner.Finalize(chain, header, statedb, txs, uncles)
@@ -239,7 +239,7 @@ func (e *NoRewardEngine) Finalize(chain consensus.ChainReader, header *types.Hea
239239
}
240240
}
241241

242-
func (e *NoRewardEngine) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, statedb *state.StateDB, txs []*types.Transaction,
242+
func (e *NoRewardEngine) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, statedb *state.StateDB, txs []*types.Transaction,
243243
uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
244244
if e.rewardsOn {
245245
return e.inner.FinalizeAndAssemble(chain, header, statedb, txs, uncles, receipts)
@@ -252,19 +252,19 @@ func (e *NoRewardEngine) FinalizeAndAssemble(chain consensus.ChainReader, header
252252
}
253253
}
254254

255-
func (e *NoRewardEngine) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
255+
func (e *NoRewardEngine) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
256256
return e.inner.Seal(chain, block, results, stop)
257257
}
258258

259259
func (e *NoRewardEngine) SealHash(header *types.Header) common.Hash {
260260
return e.inner.SealHash(header)
261261
}
262262

263-
func (e *NoRewardEngine) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
263+
func (e *NoRewardEngine) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
264264
return e.inner.CalcDifficulty(chain, time, parent)
265265
}
266266

267-
func (e *NoRewardEngine) APIs(chain consensus.ChainReader) []rpc.API {
267+
func (e *NoRewardEngine) APIs(chain consensus.ChainHeaderReader) []rpc.API {
268268
return e.inner.APIs(chain)
269269
}
270270

‎consensus/clique/api.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
// API is a user facing RPC API to allow controlling the signer and voting
2929
// mechanisms of the proof-of-authority scheme.
3030
type API struct {
31-
chain consensus.ChainReader
31+
chain consensus.ChainHeaderReader
3232
clique *Clique
3333
}
3434

‎consensus/clique/clique.go‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,14 @@ func (c *Clique) Author(header *types.Header) (common.Address, error) {
213213
}
214214

215215
// VerifyHeader checks whether a header conforms to the consensus rules.
216-
func (c *Clique) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error {
216+
func (c *Clique) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error {
217217
return c.verifyHeader(chain, header, nil)
218218
}
219219

220220
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The
221221
// method returns a quit channel to abort the operations and a results channel to
222222
// retrieve the async verifications (the order is that of the input slice).
223-
func (c *Clique) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
223+
func (c *Clique) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
224224
abort := make(chan struct{})
225225
results := make(chan error, len(headers))
226226

@@ -242,7 +242,7 @@ func (c *Clique) VerifyHeaders(chain consensus.ChainReader, headers []*types.Hea
242242
// caller may optionally pass in a batch of parents (ascending order) to avoid
243243
// looking those up from the database. This is useful for concurrently verifying
244244
// a batch of new headers.
245-
func (c *Clique) verifyHeader(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error {
245+
func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error {
246246
if header.Number == nil {
247247
return errUnknownBlock
248248
}
@@ -305,7 +305,7 @@ func (c *Clique) verifyHeader(chain consensus.ChainReader, header *types.Header,
305305
// rather depend on a batch of previous headers. The caller may optionally pass
306306
// in a batch of parents (ascending order) to avoid looking those up from the
307307
// database. This is useful for concurrently verifying a batch of new headers.
308-
func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error {
308+
func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error {
309309
// The genesis block is the always valid dead-end
310310
number := header.Number.Uint64()
311311
if number == 0 {
@@ -345,7 +345,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *type
345345
}
346346

347347
// snapshot retrieves the authorization snapshot at a given point in time.
348-
func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) {
348+
func (c *Clique) snapshot(chain consensus.ChainHeaderReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) {
349349
// Search for a snapshot in memory or on disk for checkpoints
350350
var (
351351
headers []*types.Header
@@ -436,15 +436,15 @@ func (c *Clique) VerifyUncles(chain consensus.ChainReader, block *types.Block) e
436436

437437
// VerifySeal implements consensus.Engine, checking whether the signature contained
438438
// in the header satisfies the consensus protocol requirements.
439-
func (c *Clique) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
439+
func (c *Clique) VerifySeal(chain consensus.ChainHeaderReader, header *types.Header) error {
440440
return c.verifySeal(chain, header, nil)
441441
}
442442

443443
// verifySeal checks whether the signature contained in the header satisfies the
444444
// consensus protocol requirements. The method accepts an optional list of parent
445445
// headers that aren't yet part of the local blockchain to generate the snapshots
446446
// from.
447-
func (c *Clique) verifySeal(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error {
447+
func (c *Clique) verifySeal(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error {
448448
// Verifying the genesis block is not supported
449449
number := header.Number.Uint64()
450450
if number == 0 {
@@ -487,7 +487,7 @@ func (c *Clique) verifySeal(chain consensus.ChainReader, header *types.Header, p
487487

488488
// Prepare implements consensus.Engine, preparing all the consensus fields of the
489489
// header for running the transactions on top.
490-
func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) error {
490+
func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
491491
// If the block isn't a checkpoint, cast a random vote (good enough for now)
492492
header.Coinbase = common.Address{}
493493
header.Nonce = types.BlockNonce{}
@@ -552,15 +552,15 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro
552552

553553
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
554554
// rewards given.
555-
func (c *Clique) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
555+
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
556556
// No block rewards in PoA, so the state remains as is and uncles are dropped
557557
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
558558
header.UncleHash = types.CalcUncleHash(nil)
559559
}
560560

561561
// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
562562
// nor block rewards given, and returns the final block.
563-
func (c *Clique) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
563+
func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
564564
// No block rewards in PoA, so the state remains as is and uncles are dropped
565565
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
566566
header.UncleHash = types.CalcUncleHash(nil)
@@ -581,7 +581,7 @@ func (c *Clique) Authorize(signer common.Address, signFn SignerFn) {
581581

582582
// Seal implements consensus.Engine, attempting to create a sealed block using
583583
// the local signing credentials.
584-
func (c *Clique) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
584+
func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
585585
header := block.Header()
586586

587587
// Sealing the genesis block is not supported
@@ -654,7 +654,7 @@ func (c *Clique) Seal(chain consensus.ChainReader, block *types.Block, results c
654654
// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
655655
// that a new block should have based on the previous blocks in the chain and the
656656
// current signer.
657-
func (c *Clique) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
657+
func (c *Clique) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
658658
snap, err := c.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil)
659659
if err != nil {
660660
return nil
@@ -684,7 +684,7 @@ func (c *Clique) Close() error {
684684

685685
// APIs implements consensus.Engine, returning the user facing RPC API to allow
686686
// controlling the signer voting.
687-
func (c *Clique) APIs(chain consensus.ChainReader) []rpc.API {
687+
func (c *Clique) APIs(chain consensus.ChainHeaderReader) []rpc.API {
688688
return []rpc.API{{
689689
Namespace: "clique",
690690
Version: "1.0",

‎consensus/consensus.go‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
"github.com/ethereum/go-ethereum/rpc"
2828
)
2929

30-
// ChainReader defines a small collection of methods needed to access the local
31-
// blockchain during header and/or uncle verification.
32-
type ChainReader interface {
30+
// ChainHeaderReader defines a small collection of methods needed to access the local
31+
// blockchain during header verification.
32+
type ChainHeaderReader interface {
3333
// Config retrieves the blockchain's chain configuration.
3434
Config() *params.ChainConfig
3535

@@ -44,6 +44,12 @@ type ChainReader interface {
4444

4545
// GetHeaderByHash retrieves a block header from the database by its hash.
4646
GetHeaderByHash(hash common.Hash) *types.Header
47+
}
48+
49+
// ChainReader defines a small collection of methods needed to access the local
50+
// blockchain during header and/or uncle verification.
51+
type ChainReader interface {
52+
ChainHeaderReader
4753

4854
// GetBlock retrieves a block from the database by hash and number.
4955
GetBlock(hash common.Hash, number uint64) *types.Block
@@ -59,58 +65,58 @@ type Engine interface {
5965
// VerifyHeader checks whether a header conforms to the consensus rules of a
6066
// given engine. Verifying the seal may be done optionally here, or explicitly
6167
// via the VerifySeal method.
62-
VerifyHeader(chain ChainReader, header *types.Header, seal bool) error
68+
VerifyHeader(chain ChainHeaderReader, header *types.Header, seal bool) error
6369

6470
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
6571
// concurrently. The method returns a quit channel to abort the operations and
6672
// a results channel to retrieve the async verifications (the order is that of
6773
// the input slice).
68-
VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
74+
VerifyHeaders(chain ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
6975

7076
// VerifyUncles verifies that the given block's uncles conform to the consensus
7177
// rules of a given engine.
7278
VerifyUncles(chain ChainReader, block *types.Block) error
7379

7480
// VerifySeal checks whether the crypto seal on a header is valid according to
7581
// the consensus rules of the given engine.
76-
VerifySeal(chain ChainReader, header *types.Header) error
82+
VerifySeal(chain ChainHeaderReader, header *types.Header) error
7783

7884
// Prepare initializes the consensus fields of a block header according to the
7985
// rules of a particular engine. The changes are executed inline.
80-
Prepare(chain ChainReader, header *types.Header) error
86+
Prepare(chain ChainHeaderReader, header *types.Header) error
8187

8288
// Finalize runs any post-transaction state modifications (e.g. block rewards)
8389
// but does not assemble the block.
8490
//
8591
// Note: The block header and state database might be updated to reflect any
8692
// consensus rules that happen at finalization (e.g. block rewards).
87-
Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
93+
Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
8894
uncles []*types.Header)
8995

9096
// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
9197
// rewards) and assembles the final block.
9298
//
9399
// Note: The block header and state database might be updated to reflect any
94100
// consensus rules that happen at finalization (e.g. block rewards).
95-
FinalizeAndAssemble(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
101+
FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
96102
uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)
97103

98104
// Seal generates a new sealing request for the given input block and pushes
99105
// the result into the given channel.
100106
//
101107
// Note, the method returns immediately and will send the result async. More
102108
// than one result may also be returned depending on the consensus algorithm.
103-
Seal(chain ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
109+
Seal(chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
104110

105111
// SealHash returns the hash of a block prior to it being sealed.
106112
SealHash(header *types.Header) common.Hash
107113

108114
// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
109115
// that a new block should have.
110-
CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int
116+
CalcDifficulty(chain ChainHeaderReader, time uint64, parent *types.Header) *big.Int
111117

112118
// APIs returns the RPC APIs this consensus engine provides.
113-
APIs(chain ChainReader) []rpc.API
119+
APIs(chain ChainHeaderReader) []rpc.API
114120

115121
// Close terminates any background threads maintained by the consensus engine.
116122
Close() error

0 commit comments

Comments
 (0)