Skip to content

Commit 0b296e8

Browse files
committed
fix misc bugs of verify node
1 parent 6306002 commit 0b296e8

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
@@ -1705,7 +1705,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
17051705
cfg.RPCTxFeeCap = ctx.GlobalFloat64(RPCGlobalTxFeeCapFlag.Name)
17061706
}
17071707
if ctx.GlobalIsSet(NoDiscoverFlag.Name) {
1708-
cfg.EthDiscoveryURLs, cfg.SnapDiscoveryURLs = []string{}, []string{}
1708+
cfg.EthDiscoveryURLs, cfg.SnapDiscoveryURLs, cfg.TrustDiscoveryURLs = []string{}, []string{}, []string{}
17091709
} else if ctx.GlobalIsSet(DNSDiscoveryFlag.Name) {
17101710
urls := ctx.GlobalString(DNSDiscoveryFlag.Name)
17111711
if urls == "" {
@@ -1811,6 +1811,7 @@ func SetDNSDiscoveryDefaults(cfg *ethconfig.Config, genesis common.Hash) {
18111811
if url := params.KnownDNSNetwork(genesis, protocol); url != "" {
18121812
cfg.EthDiscoveryURLs = []string{url}
18131813
cfg.SnapDiscoveryURLs = cfg.EthDiscoveryURLs
1814+
cfg.TrustDiscoveryURLs = cfg.EthDiscoveryURLs
18141815
}
18151816
}
18161817

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
@@ -488,7 +488,22 @@ func (bc *BlockChain) cacheReceipts(hash common.Hash, receipts types.Receipts) {
488488
bc.receiptsCache.Add(hash, receipts)
489489
}
490490

491-
func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer) {
491+
func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer, sorted bool) {
492+
if !sorted {
493+
sort.SliceStable(diffLayer.Codes, func(i, j int) bool {
494+
return diffLayer.Codes[i].Hash.Hex() < diffLayer.Codes[j].Hash.Hex()
495+
})
496+
sort.SliceStable(diffLayer.Destructs, func(i, j int) bool {
497+
return diffLayer.Destructs[i].Hex() < (diffLayer.Destructs[j].Hex())
498+
})
499+
sort.SliceStable(diffLayer.Accounts, func(i, j int) bool {
500+
return diffLayer.Accounts[i].Account.Hex() < diffLayer.Accounts[j].Account.Hex()
501+
})
502+
sort.SliceStable(diffLayer.Storages, func(i, j int) bool {
503+
return diffLayer.Storages[i].Account.Hex() < diffLayer.Storages[j].Account.Hex()
504+
})
505+
}
506+
492507
if bc.diffLayerCache.Len() >= diffLayerCacheLimit {
493508
bc.diffLayerCache.RemoveOldest()
494509
}
@@ -1690,12 +1705,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
16901705
diffLayer.BlockHash = block.Hash()
16911706
diffLayer.Number = block.NumberU64()
16921707

1693-
sort.Sort(types.DiffCodeSlice(diffLayer.Codes))
1694-
sort.Sort(common.AddressSlice(diffLayer.Destructs))
1695-
sort.Sort(types.DiffAccountSlice(diffLayer.Accounts))
1696-
sort.Sort(types.DiffStorageSlice(diffLayer.Storages))
1697-
1698-
bc.cacheDiffLayer(diffLayer)
1708+
go bc.cacheDiffLayer(diffLayer, false)
16991709
}
17001710
triedb := bc.stateCache.TrieDB()
17011711

@@ -2730,9 +2740,14 @@ func (bc *BlockChain) HandleDiffLayer(diffLayer *types.DiffLayer, pid string, fu
27302740
return nil
27312741
}
27322742

2743+
diffHash := common.Hash{}
2744+
if diffLayer.DiffHash.Load() != nil {
2745+
diffHash = diffLayer.DiffHash.Load().(common.Hash)
2746+
}
2747+
27332748
bc.diffMux.Lock()
27342749
defer bc.diffMux.Unlock()
2735-
if blockHash, exist := bc.diffHashToBlockHash[diffLayer.DiffHash]; exist && blockHash == diffLayer.BlockHash {
2750+
if blockHash, exist := bc.diffHashToBlockHash[diffHash]; exist && blockHash == diffLayer.BlockHash {
27362751
return nil
27372752
}
27382753

@@ -2746,28 +2761,28 @@ func (bc *BlockChain) HandleDiffLayer(diffLayer *types.DiffLayer, pid string, fu
27462761
return nil
27472762
}
27482763
if _, exist := bc.diffPeersToDiffHashes[pid]; exist {
2749-
if _, alreadyHas := bc.diffPeersToDiffHashes[pid][diffLayer.DiffHash]; alreadyHas {
2764+
if _, alreadyHas := bc.diffPeersToDiffHashes[pid][diffHash]; alreadyHas {
27502765
return nil
27512766
}
27522767
} else {
27532768
bc.diffPeersToDiffHashes[pid] = make(map[common.Hash]struct{})
27542769
}
2755-
bc.diffPeersToDiffHashes[pid][diffLayer.DiffHash] = struct{}{}
2770+
bc.diffPeersToDiffHashes[pid][diffHash] = struct{}{}
27562771
if _, exist := bc.diffNumToBlockHashes[diffLayer.Number]; !exist {
27572772
bc.diffNumToBlockHashes[diffLayer.Number] = make(map[common.Hash]struct{})
27582773
}
27592774
bc.diffNumToBlockHashes[diffLayer.Number][diffLayer.BlockHash] = struct{}{}
27602775

2761-
if _, exist := bc.diffHashToPeers[diffLayer.DiffHash]; !exist {
2762-
bc.diffHashToPeers[diffLayer.DiffHash] = make(map[string]struct{})
2776+
if _, exist := bc.diffHashToPeers[diffHash]; !exist {
2777+
bc.diffHashToPeers[diffHash] = make(map[string]struct{})
27632778
}
2764-
bc.diffHashToPeers[diffLayer.DiffHash][pid] = struct{}{}
2779+
bc.diffHashToPeers[diffHash][pid] = struct{}{}
27652780

27662781
if _, exist := bc.blockHashToDiffLayers[diffLayer.BlockHash]; !exist {
27672782
bc.blockHashToDiffLayers[diffLayer.BlockHash] = make(map[common.Hash]*types.DiffLayer)
27682783
}
2769-
bc.blockHashToDiffLayers[diffLayer.BlockHash][diffLayer.DiffHash] = diffLayer
2770-
bc.diffHashToBlockHash[diffLayer.DiffHash] = diffLayer.BlockHash
2784+
bc.blockHashToDiffLayers[diffLayer.BlockHash][diffHash] = diffLayer
2785+
bc.diffHashToBlockHash[diffHash] = diffLayer.BlockHash
27712786

27722787
return nil
27732788
}
@@ -3035,60 +3050,55 @@ func EnablePersistDiff(limit uint64) BlockChainOption {
30353050
}
30363051
}
30373052

3038-
func (bc *BlockChain) GetRootByDiffHash(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) (*types.VerifyResult, error) {
3053+
func (bc *BlockChain) GetRootByDiffHash(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) *types.VerifyResult {
30393054
var res types.VerifyResult
30403055
res.BlockNumber = blockNumber
30413056
res.BlockHash = blockHash
30423057

3043-
if blockNumber > bc.CurrentHeader().Number.Uint64()+11 {
3058+
if blockNumber > bc.CurrentHeader().Number.Uint64()+maxDiffForkDist {
30443059
res.Status = types.StatusBlockTooNew
3045-
return &res, nil
3060+
return &res
30463061
} else if blockNumber > bc.CurrentHeader().Number.Uint64() {
30473062
res.Status = types.StatusBlockNewer
3048-
return &res, nil
3063+
return &res
3064+
}
3065+
3066+
header := bc.GetHeaderByHash(blockHash)
3067+
if header == nil {
3068+
if blockNumber > bc.CurrentHeader().Number.Uint64()-maxDiffForkDist {
3069+
res.Status = types.StatusPossibleFork
3070+
return &res
3071+
}
3072+
3073+
res.Status = types.StatusImpossibleFork
3074+
return &res
30493075
}
30503076

30513077
diff := bc.GetTrustedDiffLayer(blockHash)
30523078
if diff != nil {
3053-
if diff.DiffHash == (common.Hash{}) {
3054-
hash, err := GetTrustedDiffHash(diff)
3079+
if diff.DiffHash.Load() == nil {
3080+
hash, err := CalculateDiffHash(diff)
30553081
if err != nil {
30563082
res.Status = types.StatusUnexpectedError
3057-
return &res, err
3083+
return &res
30583084
}
30593085

3060-
diff.DiffHash = hash
3086+
diff.DiffHash.Store(hash)
30613087
}
30623088

3063-
if diffHash != diff.DiffHash {
3089+
if diffHash != diff.DiffHash.Load().(common.Hash) {
30643090
res.Status = types.StatusDiffHashMismatch
3065-
return &res, nil
3091+
return &res
30663092
}
30673093

3068-
header := bc.GetHeaderByHash(blockHash)
3069-
if header == nil {
3070-
res.Status = types.StatusUnexpectedError
3071-
return &res, fmt.Errorf("unexpected error, header not found")
3072-
}
30733094
res.Status = types.StatusFullVerified
30743095
res.Root = header.Root
3075-
return &res, nil
3076-
}
3077-
3078-
header := bc.GetHeaderByHash(blockHash)
3079-
if header == nil {
3080-
if blockNumber > bc.CurrentHeader().Number.Uint64()-11 {
3081-
res.Status = types.StatusPossibleFork
3082-
return &res, nil
3083-
}
3084-
3085-
res.Status = types.StatusImpossibleFork
3086-
return &res, nil
3096+
return &res
30873097
}
30883098

3089-
res.Status = types.StatusUntrustedVerified
3099+
res.Status = types.StatusPartiallyVerified
30903100
res.Root = header.Root
3091-
return &res, nil
3101+
return &res
30923102
}
30933103

30943104
func (bc *BlockChain) GetTrustedDiffLayer(blockHash common.Hash) *types.DiffLayer {
@@ -3160,12 +3170,18 @@ func (bc *BlockChain) GenerateDiffLayer(blockHash common.Hash) (*types.DiffLayer
31603170
if diffLayer != nil {
31613171
diffLayer.BlockHash = blockHash
31623172
diffLayer.Number = block.NumberU64()
3173+
3174+
bc.cacheDiffLayer(diffLayer, true)
31633175
}
31643176

31653177
return diffLayer, nil
31663178
}
31673179

3168-
func GetTrustedDiffHash(d *types.DiffLayer) (common.Hash, error) {
3180+
func CalculateDiffHash(d *types.DiffLayer) (common.Hash, error) {
3181+
if d == nil {
3182+
return common.Hash{}, fmt.Errorf("nil diff layer")
3183+
}
3184+
31693185
diff := &types.ExtDiffLayer{
31703186
BlockHash: d.BlockHash,
31713187
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)