Skip to content

Commit

Permalink
fix misc bugs of verify node
Browse files Browse the repository at this point in the history
  • Loading branch information
keefel committed Jan 26, 2022
1 parent 6306002 commit 0b296e8
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 139 deletions.
3 changes: 2 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
cfg.RPCTxFeeCap = ctx.GlobalFloat64(RPCGlobalTxFeeCapFlag.Name)
}
if ctx.GlobalIsSet(NoDiscoverFlag.Name) {
cfg.EthDiscoveryURLs, cfg.SnapDiscoveryURLs = []string{}, []string{}
cfg.EthDiscoveryURLs, cfg.SnapDiscoveryURLs, cfg.TrustDiscoveryURLs = []string{}, []string{}, []string{}
} else if ctx.GlobalIsSet(DNSDiscoveryFlag.Name) {
urls := ctx.GlobalString(DNSDiscoveryFlag.Name)
if urls == "" {
Expand Down Expand Up @@ -1811,6 +1811,7 @@ func SetDNSDiscoveryDefaults(cfg *ethconfig.Config, genesis common.Hash) {
if url := params.KnownDNSNetwork(genesis, protocol); url != "" {
cfg.EthDiscoveryURLs = []string{url}
cfg.SnapDiscoveryURLs = cfg.EthDiscoveryURLs
cfg.TrustDiscoveryURLs = cfg.EthDiscoveryURLs
}
}

Expand Down
15 changes: 0 additions & 15 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,21 +354,6 @@ func (a *Address) UnmarshalGraphQL(input interface{}) error {
return err
}

// AddressSlice is used for sort
type AddressSlice []Address

func (s AddressSlice) Len() int {
return len(s)
}

func (s AddressSlice) Less(i, j int) bool {
return s[i].Hex() < s[j].Hex()
}

func (s AddressSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// UnprefixedAddress allows marshaling an Address without 0x prefix.
type UnprefixedAddress Address

Expand Down
106 changes: 61 additions & 45 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,22 @@ func (bc *BlockChain) cacheReceipts(hash common.Hash, receipts types.Receipts) {
bc.receiptsCache.Add(hash, receipts)
}

func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer) {
func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer, sorted bool) {
if !sorted {
sort.SliceStable(diffLayer.Codes, func(i, j int) bool {
return diffLayer.Codes[i].Hash.Hex() < diffLayer.Codes[j].Hash.Hex()
})
sort.SliceStable(diffLayer.Destructs, func(i, j int) bool {
return diffLayer.Destructs[i].Hex() < (diffLayer.Destructs[j].Hex())
})
sort.SliceStable(diffLayer.Accounts, func(i, j int) bool {
return diffLayer.Accounts[i].Account.Hex() < diffLayer.Accounts[j].Account.Hex()
})
sort.SliceStable(diffLayer.Storages, func(i, j int) bool {
return diffLayer.Storages[i].Account.Hex() < diffLayer.Storages[j].Account.Hex()
})
}

if bc.diffLayerCache.Len() >= diffLayerCacheLimit {
bc.diffLayerCache.RemoveOldest()
}
Expand Down Expand Up @@ -1690,12 +1705,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
diffLayer.BlockHash = block.Hash()
diffLayer.Number = block.NumberU64()

sort.Sort(types.DiffCodeSlice(diffLayer.Codes))
sort.Sort(common.AddressSlice(diffLayer.Destructs))
sort.Sort(types.DiffAccountSlice(diffLayer.Accounts))
sort.Sort(types.DiffStorageSlice(diffLayer.Storages))

bc.cacheDiffLayer(diffLayer)
go bc.cacheDiffLayer(diffLayer, false)
}
triedb := bc.stateCache.TrieDB()

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

diffHash := common.Hash{}
if diffLayer.DiffHash.Load() != nil {
diffHash = diffLayer.DiffHash.Load().(common.Hash)
}

bc.diffMux.Lock()
defer bc.diffMux.Unlock()
if blockHash, exist := bc.diffHashToBlockHash[diffLayer.DiffHash]; exist && blockHash == diffLayer.BlockHash {
if blockHash, exist := bc.diffHashToBlockHash[diffHash]; exist && blockHash == diffLayer.BlockHash {
return nil
}

Expand All @@ -2746,28 +2761,28 @@ func (bc *BlockChain) HandleDiffLayer(diffLayer *types.DiffLayer, pid string, fu
return nil
}
if _, exist := bc.diffPeersToDiffHashes[pid]; exist {
if _, alreadyHas := bc.diffPeersToDiffHashes[pid][diffLayer.DiffHash]; alreadyHas {
if _, alreadyHas := bc.diffPeersToDiffHashes[pid][diffHash]; alreadyHas {
return nil
}
} else {
bc.diffPeersToDiffHashes[pid] = make(map[common.Hash]struct{})
}
bc.diffPeersToDiffHashes[pid][diffLayer.DiffHash] = struct{}{}
bc.diffPeersToDiffHashes[pid][diffHash] = struct{}{}
if _, exist := bc.diffNumToBlockHashes[diffLayer.Number]; !exist {
bc.diffNumToBlockHashes[diffLayer.Number] = make(map[common.Hash]struct{})
}
bc.diffNumToBlockHashes[diffLayer.Number][diffLayer.BlockHash] = struct{}{}

if _, exist := bc.diffHashToPeers[diffLayer.DiffHash]; !exist {
bc.diffHashToPeers[diffLayer.DiffHash] = make(map[string]struct{})
if _, exist := bc.diffHashToPeers[diffHash]; !exist {
bc.diffHashToPeers[diffHash] = make(map[string]struct{})
}
bc.diffHashToPeers[diffLayer.DiffHash][pid] = struct{}{}
bc.diffHashToPeers[diffHash][pid] = struct{}{}

if _, exist := bc.blockHashToDiffLayers[diffLayer.BlockHash]; !exist {
bc.blockHashToDiffLayers[diffLayer.BlockHash] = make(map[common.Hash]*types.DiffLayer)
}
bc.blockHashToDiffLayers[diffLayer.BlockHash][diffLayer.DiffHash] = diffLayer
bc.diffHashToBlockHash[diffLayer.DiffHash] = diffLayer.BlockHash
bc.blockHashToDiffLayers[diffLayer.BlockHash][diffHash] = diffLayer
bc.diffHashToBlockHash[diffHash] = diffLayer.BlockHash

return nil
}
Expand Down Expand Up @@ -3035,60 +3050,55 @@ func EnablePersistDiff(limit uint64) BlockChainOption {
}
}

func (bc *BlockChain) GetRootByDiffHash(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) (*types.VerifyResult, error) {
func (bc *BlockChain) GetRootByDiffHash(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) *types.VerifyResult {
var res types.VerifyResult
res.BlockNumber = blockNumber
res.BlockHash = blockHash

if blockNumber > bc.CurrentHeader().Number.Uint64()+11 {
if blockNumber > bc.CurrentHeader().Number.Uint64()+maxDiffForkDist {
res.Status = types.StatusBlockTooNew
return &res, nil
return &res
} else if blockNumber > bc.CurrentHeader().Number.Uint64() {
res.Status = types.StatusBlockNewer
return &res, nil
return &res
}

header := bc.GetHeaderByHash(blockHash)
if header == nil {
if blockNumber > bc.CurrentHeader().Number.Uint64()-maxDiffForkDist {
res.Status = types.StatusPossibleFork
return &res
}

res.Status = types.StatusImpossibleFork
return &res
}

diff := bc.GetTrustedDiffLayer(blockHash)
if diff != nil {
if diff.DiffHash == (common.Hash{}) {
hash, err := GetTrustedDiffHash(diff)
if diff.DiffHash.Load() == nil {
hash, err := CalculateDiffHash(diff)
if err != nil {
res.Status = types.StatusUnexpectedError
return &res, err
return &res
}

diff.DiffHash = hash
diff.DiffHash.Store(hash)
}

if diffHash != diff.DiffHash {
if diffHash != diff.DiffHash.Load().(common.Hash) {
res.Status = types.StatusDiffHashMismatch
return &res, nil
return &res
}

header := bc.GetHeaderByHash(blockHash)
if header == nil {
res.Status = types.StatusUnexpectedError
return &res, fmt.Errorf("unexpected error, header not found")
}
res.Status = types.StatusFullVerified
res.Root = header.Root
return &res, nil
}

header := bc.GetHeaderByHash(blockHash)
if header == nil {
if blockNumber > bc.CurrentHeader().Number.Uint64()-11 {
res.Status = types.StatusPossibleFork
return &res, nil
}

res.Status = types.StatusImpossibleFork
return &res, nil
return &res
}

res.Status = types.StatusUntrustedVerified
res.Status = types.StatusPartiallyVerified
res.Root = header.Root
return &res, nil
return &res
}

func (bc *BlockChain) GetTrustedDiffLayer(blockHash common.Hash) *types.DiffLayer {
Expand Down Expand Up @@ -3160,12 +3170,18 @@ func (bc *BlockChain) GenerateDiffLayer(blockHash common.Hash) (*types.DiffLayer
if diffLayer != nil {
diffLayer.BlockHash = blockHash
diffLayer.Number = block.NumberU64()

bc.cacheDiffLayer(diffLayer, true)
}

return diffLayer, nil
}

func GetTrustedDiffHash(d *types.DiffLayer) (common.Hash, error) {
func CalculateDiffHash(d *types.DiffLayer) (common.Hash, error) {
if d == nil {
return common.Hash{}, fmt.Errorf("nil diff layer")
}

diff := &types.ExtDiffLayer{
BlockHash: d.BlockHash,
Receipts: make([]*types.ReceiptForStorage, 0),
Expand Down
14 changes: 7 additions & 7 deletions core/blockchain_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func rawDataToDiffLayer(data rlp.RawValue) (*types.DiffLayer, error) {
hasher.Write(data)
var diffHash common.Hash
hasher.Sum(diffHash[:0])
diff.DiffHash = diffHash
diff.DiffHash.Store(diffHash)
hasher.Reset()
return &diff, nil
}
Expand Down Expand Up @@ -600,21 +600,21 @@ func testGetRootByDiffHash(t *testing.T, chain1, chain2 *BlockChain, blockNumber
diffHash2 := types.EmptyRootHash
if status != types.StatusDiffHashMismatch {
var err error
diffHash2, err = GetTrustedDiffHash(diffLayer2)
diffHash2, err = CalculateDiffHash(diffLayer2)
if err != nil {
t.Fatalf("failed to compute diff hash: %v", err)
}
}

if status == types.StatusUntrustedVerified {
if status == types.StatusPartiallyVerified {
block1 := chain1.GetBlockByNumber(blockNumber)
if block1 == nil {
t.Fatalf("failed to find block, number: %v", blockNumber)
}
chain1.diffLayerCache.Remove(block1.Hash())
}

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

testGetRootByDiffHash(t, chain1, chain2, 10, types.StatusFullVerified)
testGetRootByDiffHash(t, chain1, chain2, 2, types.StatusUntrustedVerified)
testGetRootByDiffHash(t, chain1, chain2, 2, types.StatusPartiallyVerified)
testGetRootByDiffHash(t, chain1, chain2, 10, types.StatusDiffHashMismatch)
testGetRootByDiffHash(t, chain1, chain2, 12, types.StatusImpossibleFork)
testGetRootByDiffHash(t, chain1, chain2, 20, types.StatusPossibleFork)
Expand Down Expand Up @@ -743,7 +743,7 @@ func TestGenerateDiffLayer(t *testing.T) {
}
t.Fatalf("unexpected nil diff layer, block number: %v, block hash: %v", blockNr, block.Hash())
}
expDiffHash, err := GetTrustedDiffHash(expDiffLayer)
expDiffHash, err := CalculateDiffHash(expDiffLayer)
if err != nil {
t.Fatalf("compute diff hash failed: %v", err)
}
Expand All @@ -752,7 +752,7 @@ func TestGenerateDiffLayer(t *testing.T) {
if err != nil || diffLayer == nil {
t.Fatalf("generate diff layer failed: %v", err)
}
diffHash, err := GetTrustedDiffHash(diffLayer)
diffHash, err := CalculateDiffHash(diffLayer)
if err != nil {
t.Fatalf("compute diff hash failed: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (p *LightStateProcessor) Process(block *types.Block, statedb *state.StateDB
return statedb, receipts, logs, gasUsed, nil
}
log.Error("do light process err at block", "num", block.NumberU64(), "err", err)
p.bc.removeDiffLayers(diffLayer.DiffHash)
p.bc.removeDiffLayers(diffLayer.DiffHash.Load().(common.Hash))
// prepare new statedb
statedb.StopPrefetcher()
parent := p.bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
Expand Down
Loading

0 comments on commit 0b296e8

Please sign in to comment.