Skip to content

Commit 3805ac3

Browse files
authored
core: rework fast node (bnb-chain#3340)
1 parent da459f4 commit 3805ac3

14 files changed

Lines changed: 30 additions & 25 deletions

File tree

consensus/parlia/parlia.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2361,7 +2361,7 @@ func (p *Parlia) detectNewVersionWithFork(chain consensus.ChainHeaderReader, hea
23612361
forkHashHex := hex.EncodeToString(nextForkHash[:])
23622362
if !snap.isMajorityFork(forkHashHex) {
23632363
logFn := log.Debug
2364-
if state.NoTrie() {
2364+
if state.NoTries() {
23652365
logFn = log.Warn
23662366
}
23672367
logFn("possible fork detected: client is not in majority", "nextForkHash", forkHashHex)

core/blockchain_reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
484484
// any state will by default return nil.
485485
// Instead of that, it will be more useful to return an error to indicate
486486
// the state is not available.
487-
if stateDb.NoTrie() && stateDb.GetSnap() == nil {
487+
if stateDb.NoTries() && stateDb.GetSnap() == nil {
488488
return nil, errors.New("state is not available")
489489
}
490490

core/filtermaps/filtermaps.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ func (f *FilterMaps) Start() {
287287
log.Error("Could not load head filter map snapshot", "error", err)
288288
}
289289
}
290+
if f.indexedView == nil {
291+
log.Error("FilterMaps fail to Start, restart maybe ok!")
292+
return
293+
}
290294
f.closeWg.Add(2)
291295
go f.removeBloomBits()
292296
go f.indexerLoop()

core/genesis.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) {
168168
func flushAlloc(ga *types.GenesisAlloc, triedb *triedb.Database) (common.Hash, error) {
169169
triedbConfig := triedb.Config()
170170
if triedbConfig != nil {
171+
origin := triedbConfig.NoTries
171172
triedbConfig.NoTries = false
173+
defer func() {
174+
triedbConfig.NoTries = origin
175+
}()
172176
}
173177

174178
emptyRoot := types.EmptyRootHash

core/state/database.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ type Trie interface {
150150
type CachingDB struct {
151151
disk ethdb.KeyValueStore
152152
triedb *triedb.Database
153-
noTries bool
154153
snap *snapshot.Tree
155154
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
156155
codeSizeCache *lru.Cache[common.Hash, int]
@@ -159,12 +158,9 @@ type CachingDB struct {
159158

160159
// NewDatabase creates a state database with the provided data sources.
161160
func NewDatabase(triedb *triedb.Database, snap *snapshot.Tree) *CachingDB {
162-
noTries := triedb != nil && triedb.Config() != nil && triedb.Config().NoTries
163-
164161
return &CachingDB{
165162
disk: triedb.Disk(),
166163
triedb: triedb,
167-
noTries: noTries,
168164
snap: snap,
169165
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
170166
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
@@ -232,7 +228,7 @@ func (db *CachingDB) ReadersWithCacheStats(stateRoot common.Hash) (ReaderWithSta
232228

233229
// OpenTrie opens the main account trie at a specific root hash.
234230
func (db *CachingDB) OpenTrie(root common.Hash) (Trie, error) {
235-
if db.noTries {
231+
if db.NoTries() {
236232
return trie.NewEmptyTrie(), nil
237233
}
238234
if db.triedb.IsVerkle() {
@@ -247,7 +243,7 @@ func (db *CachingDB) OpenTrie(root common.Hash) (Trie, error) {
247243

248244
// OpenStorageTrie opens the storage trie of an account.
249245
func (db *CachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, self Trie) (Trie, error) {
250-
if db.noTries {
246+
if db.NoTries() {
251247
return trie.NewEmptyTrie(), nil
252248
}
253249

@@ -265,7 +261,7 @@ func (db *CachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Addre
265261
}
266262

267263
func (db *CachingDB) NoTries() bool {
268-
return db.noTries
264+
return db.triedb != nil && db.triedb.Config() != nil && db.triedb.Config().NoTries
269265
}
270266

271267
// ContractCodeWithPrefix retrieves a particular contract's code. If the

core/state/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (d iterativeDump) OnRoot(root common.Hash) {
117117
// The state iterator is still trie-based and can be converted to snapshot-based
118118
// once the state snapshot is fully integrated into database. TODO(rjl493456442).
119119
func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte) {
120-
if s.NoTrie() { // TODO(Nathan): remove after converted to snapshot-based
120+
if s.NoTries() { // TODO(Nathan): remove after converted to snapshot-based
121121
return nil
122122
}
123123

core/state/statedb.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func (s *StateDB) setError(err error) {
276276
}
277277
}
278278

279-
func (s *StateDB) NoTrie() bool {
279+
func (s *StateDB) NoTries() bool {
280280
return s.db.NoTries()
281281
}
282282

@@ -1277,6 +1277,9 @@ func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool) (*stateU
12771277
storageTrieNodesUpdated += updates
12781278
storageTrieNodesDeleted += deletes
12791279
}
1280+
if s.NoTries() {
1281+
return nil
1282+
}
12801283
return nodes.Merge(set)
12811284
}
12821285
)

core/state/statedb_hooked.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ func (s *hookedStateDB) SelfDestruct6780(address common.Address) (uint256.Int, b
269269
return prev, changed
270270
}
271271

272-
func (s *hookedStateDB) NoTrie() bool {
273-
return s.inner.NoTrie()
272+
func (s *hookedStateDB) NoTries() bool {
273+
return s.inner.NoTries()
274274
}
275275

276276
func (s *hookedStateDB) AddLog(log *types.Log) {

core/vm/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type StateDB interface {
9696
RevertToSnapshot(int)
9797
Snapshot() int
9898

99-
NoTrie() bool
99+
NoTries() bool
100100

101101
AddLog(*types.Log)
102102
GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash, blockTime uint64) []*types.Log

eth/backend.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
184184
if err != nil {
185185
return nil, err
186186
}
187+
noTries := config.TriesVerifyMode != core.LocalVerify
188+
if noTries && config.StateScheme != rawdb.HashScheme {
189+
config.StateScheme = rawdb.HashScheme
190+
log.Info("Using hash-based state scheme since tries are disabled")
191+
}
187192

188193
if config.StateScheme == rawdb.HashScheme && config.NoPruning && config.TrieDirtyCache > 0 {
189194
if config.SnapshotCache > 0 {
@@ -322,7 +327,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
322327
TrieDirtyLimit: config.TrieDirtyCache,
323328
ArchiveMode: config.NoPruning,
324329
TrieTimeLimit: config.TrieTimeout,
325-
NoTries: config.TriesVerifyMode != core.LocalVerify,
330+
NoTries: noTries,
326331
SnapshotLimit: config.SnapshotCache,
327332
TriesInMemory: config.TriesInMemory,
328333
Preimages: config.Preimages,

0 commit comments

Comments
 (0)