Skip to content

Commit ae3bdea

Browse files
committed
fix to resolve comments
Signed-off-by: cryyl <[email protected]>
1 parent 2dbc8c5 commit ae3bdea

11 files changed

+54
-60
lines changed

cmd/utils/flags.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ var (
275275
defaultVerifyMode = ethconfig.Defaults.TriesVerifyMode
276276
TriesVerifyModeFlag = TextMarshalerFlag{
277277
Name: "tries-verify-mode",
278-
Usage: `tries verify mode: "local", "full", "insecure", "none"`,
278+
Usage: `tries verify mode: "local: a normal full node", "full: state verification by verify node which has diffLayer of blocks", "insecure: state verification by verify node which has no diffLayer of blocks", "none: no state verification"`,
279279
Value: &defaultVerifyMode,
280280
}
281281
OverrideBerlinFlag = cli.Uint64Flag{
@@ -1688,7 +1688,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16881688
}
16891689
if ctx.GlobalIsSet(TriesVerifyModeFlag.Name) {
16901690
cfg.TriesVerifyMode = *GlobalTextMarshaler(ctx, TriesVerifyModeFlag.Name).(*core.VerifyMode)
1691-
// If a node sets verify mode to full or light, it's a fast node and need
1691+
// If a node sets verify mode to full or insecure, it's a fast node and need
16921692
// to verify blocks from verify nodes, then it should enable trust protocol.
16931693
if cfg.TriesVerifyMode.NeedRemoteVerify() {
16941694
cfg.EnableTrustProtocol = true

core/block_validator.go

-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
110110
if v.remoteValidator != nil && !v.remoteValidator.AncestorVerified(block.Header()) {
111111
return fmt.Errorf("%w, number: %s, hash: %s", ErrAncestorHasNotBeenVerified, block.Number(), block.Hash())
112112
}
113-
114113
return nil
115114
},
116115
}

core/blockchain.go

+22-27
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ import (
2828
"sync/atomic"
2929
"time"
3030

31-
"golang.org/x/crypto/sha3"
32-
3331
lru "github.com/hashicorp/golang-lru"
32+
"golang.org/x/crypto/sha3"
3433

3534
"github.com/ethereum/go-ethereum/common"
3635
"github.com/ethereum/go-ethereum/common/mclock"
@@ -509,31 +508,27 @@ func (bc *BlockChain) cacheReceipts(hash common.Hash, receipts types.Receipts) {
509508
bc.receiptsCache.Add(hash, receipts)
510509
}
511510

512-
func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer, sorted bool) {
513-
if !sorted {
514-
sort.SliceStable(diffLayer.Codes, func(i, j int) bool {
515-
return diffLayer.Codes[i].Hash.Hex() < diffLayer.Codes[j].Hash.Hex()
516-
})
517-
sort.SliceStable(diffLayer.Destructs, func(i, j int) bool {
518-
return diffLayer.Destructs[i].Hex() < (diffLayer.Destructs[j].Hex())
519-
})
520-
sort.SliceStable(diffLayer.Accounts, func(i, j int) bool {
521-
return diffLayer.Accounts[i].Account.Hex() < diffLayer.Accounts[j].Account.Hex()
522-
})
523-
sort.SliceStable(diffLayer.Storages, func(i, j int) bool {
524-
return diffLayer.Storages[i].Account.Hex() < diffLayer.Storages[j].Account.Hex()
525-
})
526-
}
511+
func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer, diffLayerCh chan struct{}) {
512+
sort.SliceStable(diffLayer.Codes, func(i, j int) bool {
513+
return diffLayer.Codes[i].Hash.Hex() < diffLayer.Codes[j].Hash.Hex()
514+
})
515+
sort.SliceStable(diffLayer.Destructs, func(i, j int) bool {
516+
return diffLayer.Destructs[i].Hex() < (diffLayer.Destructs[j].Hex())
517+
})
518+
sort.SliceStable(diffLayer.Accounts, func(i, j int) bool {
519+
return diffLayer.Accounts[i].Account.Hex() < diffLayer.Accounts[j].Account.Hex()
520+
})
521+
sort.SliceStable(diffLayer.Storages, func(i, j int) bool {
522+
return diffLayer.Storages[i].Account.Hex() < diffLayer.Storages[j].Account.Hex()
523+
})
527524

528525
if bc.diffLayerCache.Len() >= diffLayerCacheLimit {
529526
bc.diffLayerCache.RemoveOldest()
530527
}
531528

532529
bc.diffLayerCache.Add(diffLayer.BlockHash, diffLayer)
533-
if cached, ok := bc.diffLayerChanCache.Get(diffLayer.BlockHash); ok {
534-
diffLayerCh := cached.(chan struct{})
535-
close(diffLayerCh)
536-
}
530+
close(diffLayerCh)
531+
537532
if bc.db.DiffStore() != nil {
538533
// push to priority queue before persisting
539534
bc.diffQueueBuffer <- diffLayer
@@ -1840,7 +1835,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
18401835
}
18411836
bc.diffLayerChanCache.Add(diffLayer.BlockHash, diffLayerCh)
18421837

1843-
go bc.cacheDiffLayer(diffLayer, false)
1838+
go bc.cacheDiffLayer(diffLayer, diffLayerCh)
18441839
}
18451840

18461841
wg.Wait()
@@ -2156,7 +2151,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
21562151
return it.index, err
21572152
}
21582153
if statedb.NoTrie() {
2159-
statedb.SetCurrentRoot(block.Root())
2154+
statedb.SetExpectedStateRoot(block.Root())
21602155
}
21612156
bc.updateHighestVerifiedHeader(block.Header())
21622157

@@ -2833,10 +2828,10 @@ func (bc *BlockChain) HandleDiffLayer(diffLayer *types.DiffLayer, pid string, fu
28332828
return nil
28342829
}
28352830

2836-
diffHash := common.Hash{}
2837-
if diffLayer.DiffHash.Load() != nil {
2838-
diffHash = diffLayer.DiffHash.Load().(common.Hash)
2831+
if diffLayer.DiffHash.Load() == nil {
2832+
return fmt.Errorf("unexpected difflayer which diffHash is nil from peeer %s", pid)
28392833
}
2834+
diffHash := diffLayer.DiffHash.Load().(common.Hash)
28402835

28412836
bc.diffMux.Lock()
28422837
defer bc.diffMux.Unlock()
@@ -3178,7 +3173,7 @@ func EnableBlockValidator(chainConfig *params.ChainConfig, engine consensus.Engi
31783173
}
31793174
}
31803175

3181-
func (bc *BlockChain) GetRootByDiffHash(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) *VerifyResult {
3176+
func (bc *BlockChain) GetVerifyResult(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) *VerifyResult {
31823177
var res VerifyResult
31833178
res.BlockNumber = blockNumber
31843179
res.BlockHash = blockHash

core/blockchain_diff_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ func testGetRootByDiffHash(t *testing.T, chain1, chain2 *BlockChain, blockNumber
613613
chain1.diffLayerCache.Remove(block1.Hash())
614614
}
615615

616-
result := chain1.GetRootByDiffHash(blockNumber, block2.Hash(), diffHash2)
616+
result := chain1.GetVerifyResult(blockNumber, block2.Hash(), diffHash2)
617617
if result.Status != expect.Status {
618618
t.Fatalf("failed to verify block, number: %v, expect status: %v, real status: %v", blockNumber, expect.Status, result.Status)
619619
}

core/blockchain_notries_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func makeTestBackendWithRemoteValidator(blocks int, mode VerifyMode, failed *ver
161161

162162
peer.setCallBack(func(req *requestRoot) {
163163
if fastnode.validator != nil && fastnode.validator.RemoteVerifyManager() != nil {
164-
resp := verifier.GetRootByDiffHash(req.blockNumber, req.blockHash, req.diffHash)
164+
resp := verifier.GetVerifyResult(req.blockNumber, req.blockHash, req.diffHash)
165165
if failed != nil && req.blockNumber == failed.blockNumber {
166166
resp.Status = failed.status
167167
} else {

core/remote_state_verifier.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const (
2929
resendInterval = 2 * time.Second
3030
// tryAllPeersTime is the time that a block has not been verified and then try all the valid verify peers.
3131
tryAllPeersTime = 15 * time.Second
32+
// maxWaitVerifyResultTime is the max time of waiting for ancestor's verify result.
33+
maxWaitVerifyResultTime = 30 * time.Second
3234
)
3335

3436
var (
@@ -111,22 +113,18 @@ func (vm *remoteVerifyManager) mainLoop() {
111113
vm.cacheBlockVerified(hash)
112114
vm.taskLock.Lock()
113115
if task, ok := vm.tasks[hash]; ok {
114-
delete(vm.tasks, hash)
115-
verifyTaskCounter.Dec(1)
116+
vm.CloseTask(task)
116117
verifyTaskSucceedMeter.Mark(1)
117118
verifyTaskExecutionTimer.Update(time.Since(task.startAt))
118-
task.Close()
119119
}
120120
vm.taskLock.Unlock()
121121
case <-pruneTicker.C:
122122
vm.taskLock.Lock()
123-
for hash, task := range vm.tasks {
123+
for _, task := range vm.tasks {
124124
if vm.bc.insertStopped() || (vm.bc.CurrentHeader().Number.Cmp(task.blockHeader.Number) == 1 &&
125125
vm.bc.CurrentHeader().Number.Uint64()-task.blockHeader.Number.Uint64() > pruneHeightDiff) {
126-
delete(vm.tasks, hash)
127-
verifyTaskCounter.Dec(1)
126+
vm.CloseTask(task)
128127
verifyTaskFailedMeter.Mark(1)
129-
task.Close()
130128
}
131129
}
132130
vm.taskLock.Unlock()
@@ -180,7 +178,6 @@ func (vm *remoteVerifyManager) NewBlockVerifyTask(header *types.Header) {
180178
if cached, ok := vm.bc.diffLayerChanCache.Get(hash); ok {
181179
diffLayerCh := cached.(chan struct{})
182180
<-diffLayerCh
183-
vm.bc.diffLayerChanCache.Remove(hash)
184181
diffLayer = vm.bc.GetTrustedDiffLayer(hash)
185182
}
186183
// if this block has no diff, there is no need to verify it.
@@ -225,8 +222,13 @@ func (vm *remoteVerifyManager) AncestorVerified(header *types.Header) bool {
225222
vm.taskLock.RLock()
226223
task, exist := vm.tasks[hash]
227224
vm.taskLock.RUnlock()
225+
timeout := time.After(maxWaitVerifyResultTime)
228226
if exist {
229-
<-task.terminalCh
227+
select {
228+
case <-task.terminalCh:
229+
case <-timeout:
230+
return false
231+
}
230232
}
231233

232234
_, exist = vm.verifiedCache.Get(hash)
@@ -238,6 +240,12 @@ func (vm *remoteVerifyManager) HandleRootResponse(vr *VerifyResult, pid string)
238240
return nil
239241
}
240242

243+
func (vm *remoteVerifyManager) CloseTask(task *verifyTask) {
244+
delete(vm.tasks, task.blockHeader.Hash())
245+
task.Close()
246+
verifyTaskCounter.Dec(1)
247+
}
248+
241249
type VerifyResult struct {
242250
Status types.VerifyStatus
243251
BlockNumber uint64
@@ -335,7 +343,7 @@ func (vt *verifyTask) sendVerifyRequest(n int) {
335343
// if has not valid peer, log warning.
336344
if len(validPeers) == 0 {
337345
log.Warn("there is no valid peer for block", "number", vt.blockHeader.Number)
338-
vt.Close()
346+
return
339347
}
340348

341349
if n < len(validPeers) && n > 0 {
@@ -352,9 +360,8 @@ func (vt *verifyTask) sendVerifyRequest(n int) {
352360

353361
func (vt *verifyTask) compareRootHashAndMark(msg verifyMessage, verifyCh chan common.Hash) {
354362
if msg.verifyResult.Root == vt.blockHeader.Root {
355-
blockhash := msg.verifyResult.BlockHash
356363
// write back to manager so that manager can cache the result and delete this task.
357-
verifyCh <- blockhash
364+
verifyCh <- msg.verifyResult.BlockHash
358365
} else {
359366
vt.badPeers[msg.peerId] = struct{}{}
360367
}

core/state/state_object.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
"github.com/ethereum/go-ethereum/rlp"
3333
)
3434

35+
const snapshotStaleRetryInterval = time.Millisecond * 10
36+
3537
var emptyCodeHash = crypto.Keccak256(nil)
3638

3739
type Code []byte
@@ -284,7 +286,7 @@ func (s *StateObject) GetCommittedState(db Database, key common.Hash) common.Has
284286
// there is a small chance that the difflayer of the stale will be read while reading,
285287
// resulting in an empty array being returned here.
286288
// Therefore, noTrie mode must retry here,
287-
// and add a time interval when retrying to avoid stacking too much and causing OOM.
289+
// and add a time interval when retrying to avoid stacking too much and causing stack overflow.
288290
time.Sleep(snapshotStaleRetryInterval)
289291
return s.GetCommittedState(db, key)
290292
}

core/state/statedb.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ import (
3939
"github.com/ethereum/go-ethereum/trie"
4040
)
4141

42-
const (
43-
defaultNumOfSlots = 100
44-
snapshotStaleRetryInterval = time.Millisecond * 100
45-
)
42+
const defaultNumOfSlots = 100
4643

4744
type revision struct {
4845
id int
@@ -81,7 +78,6 @@ type StateDB struct {
8178
prefetcherLock sync.Mutex
8279
prefetcher *triePrefetcher
8380
originalRoot common.Hash // The pre-state root, before any changes were made
84-
currentRoot common.Hash // only used when noTrie is true
8581
expectedRoot common.Hash // The state root in the block header
8682
stateRoot common.Hash // The calculation result of IntermediateRoot
8783

@@ -276,10 +272,6 @@ func (s *StateDB) NoTrie() bool {
276272
return s.noTrie
277273
}
278274

279-
func (s *StateDB) SetCurrentRoot(root common.Hash) {
280-
s.currentRoot = root
281-
}
282-
283275
func (s *StateDB) Error() error {
284276
return s.dbErr
285277
}
@@ -1184,7 +1176,7 @@ func (s *StateDB) StateIntermediateRoot() common.Hash {
11841176
defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
11851177
}
11861178
if s.noTrie {
1187-
return s.currentRoot
1179+
return s.expectedRoot
11881180
} else {
11891181
return s.trie.Hash()
11901182
}

eth/handler.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"sync/atomic"
2525
"time"
2626

27-
"github.com/ethereum/go-ethereum/eth/protocols/trust"
28-
2927
"github.com/ethereum/go-ethereum/common"
3028
"github.com/ethereum/go-ethereum/core"
3129
"github.com/ethereum/go-ethereum/core/forkid"
@@ -35,6 +33,7 @@ import (
3533
"github.com/ethereum/go-ethereum/eth/protocols/diff"
3634
"github.com/ethereum/go-ethereum/eth/protocols/eth"
3735
"github.com/ethereum/go-ethereum/eth/protocols/snap"
36+
"github.com/ethereum/go-ethereum/eth/protocols/trust"
3837
"github.com/ethereum/go-ethereum/ethdb"
3938
"github.com/ethereum/go-ethereum/event"
4039
"github.com/ethereum/go-ethereum/log"

eth/protocols/trust/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func handleRootRequest(backend Backend, msg Decoder, peer *Peer) error {
126126
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
127127
}
128128

129-
res := backend.Chain().GetRootByDiffHash(req.BlockNumber, req.BlockHash, req.DiffHash)
129+
res := backend.Chain().GetVerifyResult(req.BlockNumber, req.BlockHash, req.DiffHash)
130130
return p2p.Send(peer.rw, RespondRootMsg, RootResponsePacket{
131131
RequestId: req.RequestId,
132132
Status: res.Status,

internal/ethapi/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1287,8 +1287,8 @@ func (s *PublicBlockChainAPI) GetDiffAccountsWithScope(ctx context.Context, bloc
12871287
return result, err
12881288
}
12891289

1290-
func (s *PublicBlockChainAPI) GetRootByDiffHash(ctx context.Context, blockNr rpc.BlockNumber, blockHash common.Hash, diffHash common.Hash) *core.VerifyResult {
1291-
return s.b.Chain().GetRootByDiffHash(uint64(blockNr), blockHash, diffHash)
1290+
func (s *PublicBlockChainAPI) GetVerifyResult(ctx context.Context, blockNr rpc.BlockNumber, blockHash common.Hash, diffHash common.Hash) *core.VerifyResult {
1291+
return s.b.Chain().GetVerifyResult(uint64(blockNr), blockHash, diffHash)
12921292
}
12931293

12941294
// ExecutionResult groups all structured logs emitted by the EVM

0 commit comments

Comments
 (0)