Skip to content

Commit b978ed3

Browse files
committed
put difflayer into verifyManage cache when node restart
1 parent 1aaab76 commit b978ed3

6 files changed

+70
-37
lines changed

core/block_validator.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,21 @@ type BlockValidator struct {
4141
}
4242

4343
// NewBlockValidator returns a new block validator which is safe for re-use
44-
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine, mode VerifyMode, peers verifyPeers) *BlockValidator {
44+
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine, mode VerifyMode, peers verifyPeers) (*BlockValidator, error) {
4545
validator := &BlockValidator{
4646
config: config,
4747
engine: engine,
4848
bc: blockchain,
4949
}
5050
if mode.NeedRemoteVerify() {
51-
validator.remoteValidator = NewVerifyManager(blockchain, peers, mode == InsecureVerify)
51+
remoteValidator, err := NewVerifyManager(blockchain, peers, mode == InsecureVerify)
52+
if err != nil {
53+
return nil, err
54+
}
55+
validator.remoteValidator = remoteValidator
5256
go validator.remoteValidator.mainLoop()
5357
}
54-
return validator
58+
return validator, nil
5559
}
5660

5761
// ValidateBody validates the given block's uncles and verifies the block

core/blockchain.go

+27-22
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ var defaultCacheConfig = &CacheConfig{
164164
SnapshotWait: true,
165165
}
166166

167-
type BlockChainOption func(*BlockChain) *BlockChain
167+
type BlockChainOption func(*BlockChain) (*BlockChain, error)
168168

169169
// BlockChain represents the canonical chain given a database with a genesis
170170
// block. The Blockchain manages chain imports, reverts, chain reorganisations.
@@ -198,16 +198,16 @@ type BlockChain struct {
198198
txLookupLimit uint64
199199
triesInMemory uint64
200200

201-
hc *HeaderChain
202-
rmLogsFeed event.Feed
203-
chainFeed event.Feed
204-
chainSideFeed event.Feed
205-
chainHeadFeed event.Feed
201+
hc *HeaderChain
202+
rmLogsFeed event.Feed
203+
chainFeed event.Feed
204+
chainSideFeed event.Feed
205+
chainHeadFeed event.Feed
206206
chainBlockFeed event.Feed
207-
logsFeed event.Feed
208-
blockProcFeed event.Feed
209-
scope event.SubscriptionScope
210-
genesisBlock *types.Block
207+
logsFeed event.Feed
208+
blockProcFeed event.Feed
209+
scope event.SubscriptionScope
210+
genesisBlock *types.Block
211211

212212
chainmu sync.RWMutex // blockchain insertion lock
213213

@@ -451,7 +451,10 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
451451
}
452452
// do options before start any routine
453453
for _, option := range options {
454-
bc = option(bc)
454+
bc, err = option(bc)
455+
if err != nil {
456+
return nil, err
457+
}
455458
}
456459
// Take ownership of this particular state
457460
go bc.update()
@@ -525,7 +528,6 @@ func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer, sorted bool) {
525528
bc.diffLayerCache.RemoveOldest()
526529
}
527530

528-
//json.MarshalIndent()
529531
bc.diffLayerCache.Add(diffLayer.BlockHash, diffLayer)
530532
if cached, ok := bc.diffLayerChanCache.Get(diffLayer.BlockHash); ok {
531533
diffLayerCh := cached.(chan struct{})
@@ -3145,27 +3147,31 @@ func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscr
31453147
}
31463148

31473149
// Options
3148-
func EnableLightProcessor(bc *BlockChain) *BlockChain {
3150+
func EnableLightProcessor(bc *BlockChain) (*BlockChain, error) {
31493151
bc.processor = NewLightStateProcessor(bc.Config(), bc, bc.engine)
3150-
return bc
3152+
return bc, nil
31513153
}
31523154

3153-
func EnablePipelineCommit(bc *BlockChain) *BlockChain {
3155+
func EnablePipelineCommit(bc *BlockChain) (*BlockChain, error) {
31543156
bc.pipeCommit = true
3155-
return bc
3157+
return bc, nil
31563158
}
31573159

31583160
func EnablePersistDiff(limit uint64) BlockChainOption {
3159-
return func(chain *BlockChain) *BlockChain {
3161+
return func(chain *BlockChain) (*BlockChain, error) {
31603162
chain.diffLayerFreezerBlockLimit = limit
3161-
return chain
3163+
return chain, nil
31623164
}
31633165
}
31643166

31653167
func EnableBlockValidator(chainConfig *params.ChainConfig, engine consensus.Engine, mode VerifyMode, peers verifyPeers) BlockChainOption {
3166-
return func(bc *BlockChain) *BlockChain {
3167-
bc.validator = NewBlockValidator(chainConfig, bc, engine, mode, peers)
3168-
return bc
3168+
return func(bc *BlockChain) (*BlockChain, error) {
3169+
validator, err := NewBlockValidator(chainConfig, bc, engine, mode, peers)
3170+
if err != nil {
3171+
return bc, err
3172+
}
3173+
bc.validator = validator
3174+
return bc, nil
31693175
}
31703176
}
31713177

@@ -3289,7 +3295,6 @@ func (bc *BlockChain) GenerateDiffLayer(blockHash common.Hash) (*types.DiffLayer
32893295
if diffLayer != nil {
32903296
diffLayer.BlockHash = blockHash
32913297
diffLayer.Number = block.NumberU64()
3292-
32933298
bc.cacheDiffLayer(diffLayer, true)
32943299
}
32953300

core/blockchain_diff_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ func newTwoForkedBlockchains(len1, len2 int) (chain1 *BlockChain, chain2 *BlockC
499499
Config: params.TestChainConfig,
500500
Alloc: GenesisAlloc{testAddr: {Balance: big.NewInt(100000000000000000)}},
501501
}).MustCommit(db1)
502-
503-
chain1, _ = NewBlockChain(db1, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil, EnablePersistDiff(860000))
502+
engine1 := ethash.NewFaker()
503+
chain1, _ = NewBlockChain(db1, nil, params.TestChainConfig, engine1, vm.Config{}, nil, nil, EnablePersistDiff(860000), EnableBlockValidator(params.TestChainConfig, engine1, 0, nil))
504504
generator1 := func(i int, block *BlockGen) {
505505
// The chain maker doesn't have access to a chain, so the difficulty will be
506506
// lets unset (nil). Set it here to the correct value.
@@ -555,7 +555,8 @@ func newTwoForkedBlockchains(len1, len2 int) (chain1 *BlockChain, chain2 *BlockC
555555
Config: params.TestChainConfig,
556556
Alloc: GenesisAlloc{testAddr: {Balance: big.NewInt(100000000000000000)}},
557557
}).MustCommit(db2)
558-
chain2, _ = NewBlockChain(db2, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil, EnablePersistDiff(860000))
558+
engine2 := ethash.NewFaker()
559+
chain2, _ = NewBlockChain(db2, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil, EnablePersistDiff(860000), EnableBlockValidator(params.TestChainConfig, engine2, 0, nil))
559560
generator2 := func(i int, block *BlockGen) {
560561
// The chain maker doesn't have access to a chain, so the difficulty will be
561562
// lets unset (nil). Set it here to the correct value.
@@ -659,7 +660,7 @@ func newBlockChainWithCliqueEngine(blocks int) *BlockChain {
659660
copy(genspec.ExtraData[32:], testAddr[:])
660661
genesis := genspec.MustCommit(db)
661662

662-
chain, _ := NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil)
663+
chain, _ := NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil, EnableBlockValidator(params.AllCliqueProtocolChanges, engine, 0 /*LocalVerify*/, nil))
663664
generator := func(i int, block *BlockGen) {
664665
// The chain maker doesn't have access to a chain, so the difficulty will be
665666
// lets unset (nil). Set it here to the correct value.

core/blockchain_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func TestBlockImportVerification(t *testing.T) {
252252
}
253253
defer processor.Stop()
254254
// Start fork from current height
255-
processor = EnablePipelineCommit(processor)
255+
processor, _ = EnablePipelineCommit(processor)
256256
testInvalidStateRootBlockImport(t, processor, length, 10, true)
257257
}
258258

core/error.go

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ var (
3535
// ErrDiffLayerNotFound is returned when diff layer not found.
3636
ErrDiffLayerNotFound = errors.New("diff layer not found")
3737

38+
// ErrCurrentBlockNotFound is returned when current block not found.
39+
ErrCurrentBlockNotFound = errors.New("current block not found")
40+
3841
// ErrKnownBadBlock is return when the block is a known bad block
3942
ErrKnownBadBlock = errors.New("already known bad block")
4043
)

core/remote_state_verifier.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"fmt"
5+
"math/big"
56
"math/rand"
67
"time"
78

@@ -45,16 +46,35 @@ type remoteVerifyManager struct {
4546
allowInsecure bool
4647

4748
// Subscription
48-
chainBlockCh chan ChainHeadEvent
49+
chainBlockCh chan ChainHeadEvent
4950
chainHeadSub event.Subscription
5051

5152
// Channels
5253
verifyCh chan common.Hash
5354
messageCh chan verifyMessage
5455
}
5556

56-
func NewVerifyManager(blockchain *BlockChain, peers verifyPeers, allowInsecure bool) *remoteVerifyManager {
57+
func NewVerifyManager(blockchain *BlockChain, peers verifyPeers, allowInsecure bool) (*remoteVerifyManager, error) {
5758
verifiedCache, _ := lru.New(verifiedCacheSize)
59+
block := blockchain.CurrentBlock()
60+
if block == nil {
61+
return nil, ErrCurrentBlockNotFound
62+
}
63+
number := block.Number()
64+
for i := maxForkHeight; i >= 0; i-- {
65+
if new(big.Int).Sub(number, big.NewInt(int64(i))).Cmp(common.Big0) <= 0 {
66+
continue
67+
}
68+
oldBlock := blockchain.GetBlockByNumber(number.Uint64() - uint64(i))
69+
if oldBlock == nil {
70+
return nil, fmt.Errorf("block is nil, number: %d", number)
71+
}
72+
_, err := blockchain.GenerateDiffLayer(oldBlock.Hash())
73+
if err != nil {
74+
return nil, err
75+
}
76+
}
77+
5878
vm := &remoteVerifyManager{
5979
bc: blockchain,
6080
tasks: make(map[common.Hash]*verifyTask),
@@ -63,11 +83,11 @@ func NewVerifyManager(blockchain *BlockChain, peers verifyPeers, allowInsecure b
6383
allowInsecure: allowInsecure,
6484

6585
chainBlockCh: make(chan ChainHeadEvent, chainHeadChanSize),
66-
verifyCh: make(chan common.Hash, maxForkHeight),
67-
messageCh: make(chan verifyMessage),
86+
verifyCh: make(chan common.Hash, maxForkHeight),
87+
messageCh: make(chan verifyMessage),
6888
}
6989
vm.chainHeadSub = blockchain.SubscribeChainBlockEvent(vm.chainBlockCh)
70-
return vm
90+
return vm, nil
7191
}
7292

7393
func (vm *remoteVerifyManager) mainLoop() {
@@ -219,7 +239,7 @@ type verifyTask struct {
219239
candidatePeers verifyPeers
220240
badPeers map[string]struct{}
221241
startAt time.Time
222-
allowInsecure bool
242+
allowInsecure bool
223243

224244
messageCh chan verifyMessage
225245
terminalCh chan struct{}
@@ -288,7 +308,7 @@ func (vt *verifyTask) sendVerifyRequest(n int) {
288308
}
289309
// if has not valid peer, log warning.
290310
if len(validPeers) == 0 {
291-
log.Warn("there is no valid peer for block", vt.blockHeader.Number)
311+
log.Warn("there is no valid peer for block", "number", vt.blockHeader.Number)
292312
return
293313
}
294314

0 commit comments

Comments
 (0)