Skip to content

Commit 529e66e

Browse files
committed
fix misc bugs of verify node
1 parent 0405b68 commit 529e66e

File tree

13 files changed

+105
-139
lines changed

13 files changed

+105
-139
lines changed

cmd/utils/flags.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
17121712
cfg.RPCTxFeeCap = ctx.GlobalFloat64(RPCGlobalTxFeeCapFlag.Name)
17131713
}
17141714
if ctx.GlobalIsSet(NoDiscoverFlag.Name) {
1715-
cfg.EthDiscoveryURLs, cfg.SnapDiscoveryURLs = []string{}, []string{}
1715+
cfg.EthDiscoveryURLs, cfg.SnapDiscoveryURLs, cfg.TrustDiscoveryURLs = []string{}, []string{}, []string{}
17161716
} else if ctx.GlobalIsSet(DNSDiscoveryFlag.Name) {
17171717
urls := ctx.GlobalString(DNSDiscoveryFlag.Name)
17181718
if urls == "" {
@@ -1818,6 +1818,7 @@ func SetDNSDiscoveryDefaults(cfg *ethconfig.Config, genesis common.Hash) {
18181818
if url := params.KnownDNSNetwork(genesis, protocol); url != "" {
18191819
cfg.EthDiscoveryURLs = []string{url}
18201820
cfg.SnapDiscoveryURLs = cfg.EthDiscoveryURLs
1821+
cfg.TrustDiscoveryURLs = cfg.EthDiscoveryURLs
18211822
}
18221823
}
18231824

common/types.go

-15
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,6 @@ func (a *Address) UnmarshalGraphQL(input interface{}) error {
354354
return err
355355
}
356356

357-
// AddressSlice is used for sort
358-
type AddressSlice []Address
359-
360-
func (s AddressSlice) Len() int {
361-
return len(s)
362-
}
363-
364-
func (s AddressSlice) Less(i, j int) bool {
365-
return s[i].Hex() < s[j].Hex()
366-
}
367-
368-
func (s AddressSlice) Swap(i, j int) {
369-
s[i], s[j] = s[j], s[i]
370-
}
371-
372357
// UnprefixedAddress allows marshaling an Address without 0x prefix.
373358
type UnprefixedAddress Address
374359

core/blockchain.go

+61-45
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,22 @@ func (bc *BlockChain) cacheReceipts(hash common.Hash, receipts types.Receipts) {
501501
bc.receiptsCache.Add(hash, receipts)
502502
}
503503

504-
func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer) {
504+
func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer, sorted bool) {
505+
if !sorted {
506+
sort.SliceStable(diffLayer.Codes, func(i, j int) bool {
507+
return diffLayer.Codes[i].Hash.Hex() < diffLayer.Codes[j].Hash.Hex()
508+
})
509+
sort.SliceStable(diffLayer.Destructs, func(i, j int) bool {
510+
return diffLayer.Destructs[i].Hex() < (diffLayer.Destructs[j].Hex())
511+
})
512+
sort.SliceStable(diffLayer.Accounts, func(i, j int) bool {
513+
return diffLayer.Accounts[i].Account.Hex() < diffLayer.Accounts[j].Account.Hex()
514+
})
515+
sort.SliceStable(diffLayer.Storages, func(i, j int) bool {
516+
return diffLayer.Storages[i].Account.Hex() < diffLayer.Storages[j].Account.Hex()
517+
})
518+
}
519+
505520
if bc.diffLayerCache.Len() >= diffLayerCacheLimit {
506521
bc.diffLayerCache.RemoveOldest()
507522
}
@@ -1801,12 +1816,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
18011816
diffLayer.BlockHash = block.Hash()
18021817
diffLayer.Number = block.NumberU64()
18031818

1804-
sort.Sort(types.DiffCodeSlice(diffLayer.Codes))
1805-
sort.Sort(common.AddressSlice(diffLayer.Destructs))
1806-
sort.Sort(types.DiffAccountSlice(diffLayer.Accounts))
1807-
sort.Sort(types.DiffStorageSlice(diffLayer.Storages))
1808-
1809-
bc.cacheDiffLayer(diffLayer)
1819+
go bc.cacheDiffLayer(diffLayer, false)
18101820
}
18111821

18121822
wg.Wait()
@@ -2798,9 +2808,14 @@ func (bc *BlockChain) HandleDiffLayer(diffLayer *types.DiffLayer, pid string, fu
27982808
return nil
27992809
}
28002810

2811+
diffHash := common.Hash{}
2812+
if diffLayer.DiffHash.Load() != nil {
2813+
diffHash = diffLayer.DiffHash.Load().(common.Hash)
2814+
}
2815+
28012816
bc.diffMux.Lock()
28022817
defer bc.diffMux.Unlock()
2803-
if blockHash, exist := bc.diffHashToBlockHash[diffLayer.DiffHash]; exist && blockHash == diffLayer.BlockHash {
2818+
if blockHash, exist := bc.diffHashToBlockHash[diffHash]; exist && blockHash == diffLayer.BlockHash {
28042819
return nil
28052820
}
28062821

@@ -2814,28 +2829,28 @@ func (bc *BlockChain) HandleDiffLayer(diffLayer *types.DiffLayer, pid string, fu
28142829
return nil
28152830
}
28162831
if _, exist := bc.diffPeersToDiffHashes[pid]; exist {
2817-
if _, alreadyHas := bc.diffPeersToDiffHashes[pid][diffLayer.DiffHash]; alreadyHas {
2832+
if _, alreadyHas := bc.diffPeersToDiffHashes[pid][diffHash]; alreadyHas {
28182833
return nil
28192834
}
28202835
} else {
28212836
bc.diffPeersToDiffHashes[pid] = make(map[common.Hash]struct{})
28222837
}
2823-
bc.diffPeersToDiffHashes[pid][diffLayer.DiffHash] = struct{}{}
2838+
bc.diffPeersToDiffHashes[pid][diffHash] = struct{}{}
28242839
if _, exist := bc.diffNumToBlockHashes[diffLayer.Number]; !exist {
28252840
bc.diffNumToBlockHashes[diffLayer.Number] = make(map[common.Hash]struct{})
28262841
}
28272842
bc.diffNumToBlockHashes[diffLayer.Number][diffLayer.BlockHash] = struct{}{}
28282843

2829-
if _, exist := bc.diffHashToPeers[diffLayer.DiffHash]; !exist {
2830-
bc.diffHashToPeers[diffLayer.DiffHash] = make(map[string]struct{})
2844+
if _, exist := bc.diffHashToPeers[diffHash]; !exist {
2845+
bc.diffHashToPeers[diffHash] = make(map[string]struct{})
28312846
}
2832-
bc.diffHashToPeers[diffLayer.DiffHash][pid] = struct{}{}
2847+
bc.diffHashToPeers[diffHash][pid] = struct{}{}
28332848

28342849
if _, exist := bc.blockHashToDiffLayers[diffLayer.BlockHash]; !exist {
28352850
bc.blockHashToDiffLayers[diffLayer.BlockHash] = make(map[common.Hash]*types.DiffLayer)
28362851
}
2837-
bc.blockHashToDiffLayers[diffLayer.BlockHash][diffLayer.DiffHash] = diffLayer
2838-
bc.diffHashToBlockHash[diffLayer.DiffHash] = diffLayer.BlockHash
2852+
bc.blockHashToDiffLayers[diffLayer.BlockHash][diffHash] = diffLayer
2853+
bc.diffHashToBlockHash[diffHash] = diffLayer.BlockHash
28392854

28402855
return nil
28412856
}
@@ -3120,60 +3135,55 @@ func EnablePersistDiff(limit uint64) BlockChainOption {
31203135
}
31213136
}
31223137

3123-
func (bc *BlockChain) GetRootByDiffHash(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) (*types.VerifyResult, error) {
3138+
func (bc *BlockChain) GetRootByDiffHash(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) *types.VerifyResult {
31243139
var res types.VerifyResult
31253140
res.BlockNumber = blockNumber
31263141
res.BlockHash = blockHash
31273142

3128-
if blockNumber > bc.CurrentHeader().Number.Uint64()+11 {
3143+
if blockNumber > bc.CurrentHeader().Number.Uint64()+maxDiffForkDist {
31293144
res.Status = types.StatusBlockTooNew
3130-
return &res, nil
3145+
return &res
31313146
} else if blockNumber > bc.CurrentHeader().Number.Uint64() {
31323147
res.Status = types.StatusBlockNewer
3133-
return &res, nil
3148+
return &res
3149+
}
3150+
3151+
header := bc.GetHeaderByHash(blockHash)
3152+
if header == nil {
3153+
if blockNumber > bc.CurrentHeader().Number.Uint64()-maxDiffForkDist {
3154+
res.Status = types.StatusPossibleFork
3155+
return &res
3156+
}
3157+
3158+
res.Status = types.StatusImpossibleFork
3159+
return &res
31343160
}
31353161

31363162
diff := bc.GetTrustedDiffLayer(blockHash)
31373163
if diff != nil {
3138-
if diff.DiffHash == (common.Hash{}) {
3139-
hash, err := GetTrustedDiffHash(diff)
3164+
if diff.DiffHash.Load() == nil {
3165+
hash, err := CalculateDiffHash(diff)
31403166
if err != nil {
31413167
res.Status = types.StatusUnexpectedError
3142-
return &res, err
3168+
return &res
31433169
}
31443170

3145-
diff.DiffHash = hash
3171+
diff.DiffHash.Store(hash)
31463172
}
31473173

3148-
if diffHash != diff.DiffHash {
3174+
if diffHash != diff.DiffHash.Load().(common.Hash) {
31493175
res.Status = types.StatusDiffHashMismatch
3150-
return &res, nil
3176+
return &res
31513177
}
31523178

3153-
header := bc.GetHeaderByHash(blockHash)
3154-
if header == nil {
3155-
res.Status = types.StatusUnexpectedError
3156-
return &res, fmt.Errorf("unexpected error, header not found")
3157-
}
31583179
res.Status = types.StatusFullVerified
31593180
res.Root = header.Root
3160-
return &res, nil
3161-
}
3162-
3163-
header := bc.GetHeaderByHash(blockHash)
3164-
if header == nil {
3165-
if blockNumber > bc.CurrentHeader().Number.Uint64()-11 {
3166-
res.Status = types.StatusPossibleFork
3167-
return &res, nil
3168-
}
3169-
3170-
res.Status = types.StatusImpossibleFork
3171-
return &res, nil
3181+
return &res
31723182
}
31733183

3174-
res.Status = types.StatusUntrustedVerified
3184+
res.Status = types.StatusPartiallyVerified
31753185
res.Root = header.Root
3176-
return &res, nil
3186+
return &res
31773187
}
31783188

31793189
func (bc *BlockChain) GetTrustedDiffLayer(blockHash common.Hash) *types.DiffLayer {
@@ -3245,12 +3255,18 @@ func (bc *BlockChain) GenerateDiffLayer(blockHash common.Hash) (*types.DiffLayer
32453255
if diffLayer != nil {
32463256
diffLayer.BlockHash = blockHash
32473257
diffLayer.Number = block.NumberU64()
3258+
3259+
bc.cacheDiffLayer(diffLayer, true)
32483260
}
32493261

32503262
return diffLayer, nil
32513263
}
32523264

3253-
func GetTrustedDiffHash(d *types.DiffLayer) (common.Hash, error) {
3265+
func CalculateDiffHash(d *types.DiffLayer) (common.Hash, error) {
3266+
if d == nil {
3267+
return common.Hash{}, fmt.Errorf("nil diff layer")
3268+
}
3269+
32543270
diff := &types.ExtDiffLayer{
32553271
BlockHash: d.BlockHash,
32563272
Receipts: make([]*types.ReceiptForStorage, 0),

core/blockchain_diff_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func rawDataToDiffLayer(data rlp.RawValue) (*types.DiffLayer, error) {
288288
hasher.Write(data)
289289
var diffHash common.Hash
290290
hasher.Sum(diffHash[:0])
291-
diff.DiffHash = diffHash
291+
diff.DiffHash.Store(diffHash)
292292
hasher.Reset()
293293
return &diff, nil
294294
}
@@ -600,21 +600,21 @@ func testGetRootByDiffHash(t *testing.T, chain1, chain2 *BlockChain, blockNumber
600600
diffHash2 := types.EmptyRootHash
601601
if status != types.StatusDiffHashMismatch {
602602
var err error
603-
diffHash2, err = GetTrustedDiffHash(diffLayer2)
603+
diffHash2, err = CalculateDiffHash(diffLayer2)
604604
if err != nil {
605605
t.Fatalf("failed to compute diff hash: %v", err)
606606
}
607607
}
608608

609-
if status == types.StatusUntrustedVerified {
609+
if status == types.StatusPartiallyVerified {
610610
block1 := chain1.GetBlockByNumber(blockNumber)
611611
if block1 == nil {
612612
t.Fatalf("failed to find block, number: %v", blockNumber)
613613
}
614614
chain1.diffLayerCache.Remove(block1.Hash())
615615
}
616616

617-
result, _ := chain1.GetRootByDiffHash(blockNumber, block2.Hash(), diffHash2)
617+
result := chain1.GetRootByDiffHash(blockNumber, block2.Hash(), diffHash2)
618618
if result.Status != expect.Status {
619619
t.Fatalf("failed to verify block, number: %v, expect status: %v, real status: %v", blockNumber, expect.Status, result.Status)
620620
}
@@ -639,7 +639,7 @@ func TestGetRootByDiffHash(t *testing.T) {
639639
}
640640

641641
testGetRootByDiffHash(t, chain1, chain2, 10, types.StatusFullVerified)
642-
testGetRootByDiffHash(t, chain1, chain2, 2, types.StatusUntrustedVerified)
642+
testGetRootByDiffHash(t, chain1, chain2, 2, types.StatusPartiallyVerified)
643643
testGetRootByDiffHash(t, chain1, chain2, 10, types.StatusDiffHashMismatch)
644644
testGetRootByDiffHash(t, chain1, chain2, 12, types.StatusImpossibleFork)
645645
testGetRootByDiffHash(t, chain1, chain2, 20, types.StatusPossibleFork)
@@ -743,7 +743,7 @@ func TestGenerateDiffLayer(t *testing.T) {
743743
}
744744
t.Fatalf("unexpected nil diff layer, block number: %v, block hash: %v", blockNr, block.Hash())
745745
}
746-
expDiffHash, err := GetTrustedDiffHash(expDiffLayer)
746+
expDiffHash, err := CalculateDiffHash(expDiffLayer)
747747
if err != nil {
748748
t.Fatalf("compute diff hash failed: %v", err)
749749
}
@@ -752,7 +752,7 @@ func TestGenerateDiffLayer(t *testing.T) {
752752
if err != nil || diffLayer == nil {
753753
t.Fatalf("generate diff layer failed: %v", err)
754754
}
755-
diffHash, err := GetTrustedDiffHash(diffLayer)
755+
diffHash, err := CalculateDiffHash(diffLayer)
756756
if err != nil {
757757
t.Fatalf("compute diff hash failed: %v", err)
758758
}

core/state_processor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (p *LightStateProcessor) Process(block *types.Block, statedb *state.StateDB
118118
return statedb, receipts, logs, gasUsed, nil
119119
}
120120
log.Error("do light process err at block", "num", block.NumberU64(), "err", err)
121-
p.bc.removeDiffLayers(diffLayer.DiffHash)
121+
p.bc.removeDiffLayers(diffLayer.DiffHash.Load().(common.Hash))
122122
// prepare new statedb
123123
statedb.StopPrefetcher()
124124
parent := p.bc.GetHeader(block.ParentHash(), block.NumberU64()-1)

0 commit comments

Comments
 (0)