Skip to content

Commit 4250bfc

Browse files
authored
Merge pull request #2406 from CortexFoundation/dev
open trie when first invoke
2 parents 75502f6 + 1cbc540 commit 4250bfc

32 files changed

Lines changed: 18304 additions & 319 deletions

core/state/database.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,13 @@ func (db *CachingDB) Snapshot() *snapshot.Tree {
248248
func (db *CachingDB) SetSnapshot(snap *snapshot.Tree) {
249249
db.snap = snap
250250
}
251+
252+
// mustCopyTrie returns a deep-copied trie.
253+
func mustCopyTrie(t Trie) Trie {
254+
switch t := t.(type) {
255+
case *trie.StateTrie:
256+
return t.Copy()
257+
default:
258+
panic(fmt.Errorf("unknown trie type %T", t))
259+
}
260+
}

core/state/iterator.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
type NodeIterator struct {
3333
state *StateDB // State being iterated
3434

35+
tr Trie // Primary account trie for traversal
36+
3537
stateIt trie.NodeIterator // Primary iterator for the global state trie
3638
dataIt trie.NodeIterator // Secondary iterator for the data trie of a contract
3739

@@ -74,9 +76,20 @@ func (it *NodeIterator) step() error {
7476
if it.state == nil {
7577
return nil
7678
}
79+
if it.tr == nil {
80+
tr, err := it.state.db.OpenTrie(it.state.originalRoot)
81+
if err != nil {
82+
return err
83+
}
84+
it.tr = tr
85+
}
7786
// Initialize the iterator if we've just started
7887
if it.stateIt == nil {
79-
it.stateIt = it.state.trie.NodeIterator(nil)
88+
stateIt := it.tr.NodeIterator(nil)
89+
if stateIt == nil {
90+
return errors.New("state iter is nil")
91+
}
92+
it.stateIt = stateIt
8093
}
8194
// If we had data nodes previously, we surely have at least state nodes
8295
if it.dataIt != nil {
@@ -111,15 +124,14 @@ func (it *NodeIterator) step() error {
111124
return err
112125
}
113126
// Lookup the preimage of account hash
114-
preimage := it.state.trie.GetKey(it.stateIt.LeafKey())
127+
preimage := it.tr.GetKey(it.stateIt.LeafKey())
115128
if preimage == nil {
116129
return errors.New("account address is not available")
117130
}
118131
address := common.BytesToAddress(preimage)
119132

120133
// Traverse the storage slots belong to the account
121-
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, address, account.Root, it.state.trie)
122-
//dataTrie, err := it.state.db.OpenStorageTrie(common.BytesToAddress(it.stateIt.LeafKey()), account.Root)
134+
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, address, account.Root, it.tr)
123135
if err != nil {
124136
return err
125137
}

core/state/statedb.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,8 @@ type StateDB struct {
166166

167167
// Create a new state from a given trie.
168168
func New(root common.Hash, db Database) (*StateDB, error) {
169-
tr, err := db.OpenTrie(root)
170-
if err != nil {
171-
return nil, err
172-
}
173169
sdb := &StateDB{
174170
db: db,
175-
trie: tr,
176171
originalRoot: root,
177172
accounts: make(map[common.Hash][]byte),
178173
storages: make(map[common.Hash]map[common.Hash][]byte),
@@ -671,6 +666,14 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject {
671666
// If snapshot unavailable or reading from it failed, load from the database
672667
if data == nil {
673668
start := time.Now()
669+
if s.trie == nil {
670+
tr, err := s.db.OpenTrie(s.originalRoot)
671+
if err != nil {
672+
return nil
673+
}
674+
s.trie = tr
675+
676+
}
674677
enc, err := s.trie.TryGet(addr.Bytes())
675678
s.AccountReads += time.Since(start)
676679
if err != nil {
@@ -782,8 +785,7 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common
782785
func (s *StateDB) Copy() *StateDB {
783786
// Copy all the basic fields, initialize the memory ones
784787
state := &StateDB{
785-
db: s.db,
786-
trie: s.db.CopyTrie(s.trie),
788+
db: s.db,
787789
//hasher: crypto.NewKeccakState(),
788790
originalRoot: s.originalRoot,
789791
accounts: copySet(s.accounts),
@@ -808,6 +810,9 @@ func (s *StateDB) Copy() *StateDB {
808810
// miner to operate trie-backed only.
809811
snap: s.snap,
810812
}
813+
if s.trie != nil {
814+
state.trie = mustCopyTrie(s.trie)
815+
}
811816
// Deep copy cached state objects.
812817
for addr, obj := range s.stateObjects {
813818
state.stateObjects[addr] = obj.deepCopy(state)
@@ -913,6 +918,20 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
913918
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
914919
// Finalise all the dirty storage states and write them into the tries
915920
s.Finalise(deleteEmptyObjects)
921+
// Initialize the trie if it's not constructed yet. If the prefetch
922+
// is enabled, the trie constructed below will be replaced by the
923+
// prefetched one.
924+
//
925+
// This operation must be done before state object storage hashing,
926+
// as it assumes the main trie is already loaded.
927+
if s.trie == nil {
928+
tr, err := s.db.OpenTrie(s.originalRoot)
929+
if err != nil {
930+
s.setError(err)
931+
return common.Hash{}
932+
}
933+
s.trie = tr
934+
}
916935

917936
// If there was a trie prefetcher operating, terminate it async so that the
918937
// individual storage tries can be updated as soon as the disk load finishes.

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
2323
github.com/deckarep/golang-set/v2 v2.8.0
2424
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0
25-
github.com/dop251/goja v0.0.0-20250531102226-cb187b08699c
25+
github.com/dop251/goja v0.0.0-20250624190929-4d26883d182a
2626
github.com/ethereum/c-kzg-4844 v1.0.3
2727
github.com/ethereum/go-ethereum v1.15.11
2828
github.com/ethereum/go-verkle v0.2.2
@@ -168,7 +168,7 @@ require (
168168
github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf // indirect
169169
github.com/jedib0t/go-pretty/v6 v6.6.7 // indirect
170170
github.com/klauspost/compress v1.18.0 // indirect
171-
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
171+
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
172172
github.com/kr/pretty v0.3.1 // indirect
173173
github.com/kr/text v0.2.0 // indirect
174174
github.com/mattn/go-localereader v0.0.1 // indirect
@@ -241,17 +241,17 @@ require (
241241
github.com/zeebo/xxh3 v1.0.3-0.20230502181907-3808c706a06a // indirect
242242
go.etcd.io/bbolt v1.4.1 // indirect
243243
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
244-
go.opentelemetry.io/otel v1.36.0 // indirect
245-
go.opentelemetry.io/otel/metric v1.36.0 // indirect
246-
go.opentelemetry.io/otel/trace v1.36.0 // indirect
244+
go.opentelemetry.io/otel v1.37.0 // indirect
245+
go.opentelemetry.io/otel/metric v1.37.0 // indirect
246+
go.opentelemetry.io/otel/trace v1.37.0 // indirect
247247
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
248248
golang.org/x/mod v0.25.0 // indirect
249249
golang.org/x/net v0.41.0 // indirect
250250
golang.org/x/term v0.32.0 // indirect
251251
gopkg.in/yaml.v2 v2.4.0 // indirect
252252
gopkg.in/yaml.v3 v3.0.1 // indirect
253253
lukechampine.com/blake3 v1.4.1 // indirect
254-
modernc.org/libc v1.66.0 // indirect
254+
modernc.org/libc v1.66.1 // indirect
255255
modernc.org/mathutil v1.7.1 // indirect
256256
modernc.org/memory v1.11.0 // indirect
257257
modernc.org/sqlite v1.38.0 // indirect

go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cn
424424
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
425425
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
426426
github.com/dop251/goja v0.0.0-20200721192441-a695b0cdd498/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA=
427-
github.com/dop251/goja v0.0.0-20250531102226-cb187b08699c h1:In87uFQZsuGfjDDNfWnzMVY6JVTwc8XYMl6W2DAmNjk=
428-
github.com/dop251/goja v0.0.0-20250531102226-cb187b08699c/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
427+
github.com/dop251/goja v0.0.0-20250624190929-4d26883d182a h1:QIWJoaD2+zxUjN28l8zixmbuvtYqqcxj49Iwzw7mDpk=
428+
github.com/dop251/goja v0.0.0-20250624190929-4d26883d182a/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
429429
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
430430
github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
431431
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
@@ -757,8 +757,8 @@ github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zt
757757
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
758758
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
759759
github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
760-
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
761-
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
760+
github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU=
761+
github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
762762
github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg=
763763
github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
764764
github.com/klauspost/reedsolomon v1.9.3/go.mod h1:CwCi+NUr9pqSVktrkN+Ondf06rkhYZ/pcNv7fu+8Un4=
@@ -1305,12 +1305,12 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
13051305
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
13061306
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
13071307
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
1308-
go.opentelemetry.io/otel v1.36.0 h1:UumtzIklRBY6cI/lllNZlALOF5nNIzJVb16APdvgTXg=
1309-
go.opentelemetry.io/otel v1.36.0/go.mod h1:/TcFMXYjyRNh8khOAO9ybYkqaDBb/70aVwkNML4pP8E=
1310-
go.opentelemetry.io/otel/metric v1.36.0 h1:MoWPKVhQvJ+eeXWHFBOPoBOi20jh6Iq2CcCREuTYufE=
1311-
go.opentelemetry.io/otel/metric v1.36.0/go.mod h1:zC7Ks+yeyJt4xig9DEw9kuUFe5C3zLbVjV2PzT6qzbs=
1312-
go.opentelemetry.io/otel/trace v1.36.0 h1:ahxWNuqZjpdiFAyrIoQ4GIiAIhxAunQR6MUoKrsNd4w=
1313-
go.opentelemetry.io/otel/trace v1.36.0/go.mod h1:gQ+OnDZzrybY4k4seLzPAWNwVBBVlF2szhehOBB/tGA=
1308+
go.opentelemetry.io/otel v1.37.0 h1:9zhNfelUvx0KBfu/gb+ZgeAfAgtWrfHJZcAqFC228wQ=
1309+
go.opentelemetry.io/otel v1.37.0/go.mod h1:ehE/umFRLnuLa/vSccNq9oS1ErUlkkK71gMcN34UG8I=
1310+
go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/WgbsdpcPoZE=
1311+
go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E=
1312+
go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4=
1313+
go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0=
13141314
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
13151315
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
13161316
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
@@ -1770,8 +1770,8 @@ modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
17701770
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
17711771
modernc.org/goabi0 v0.0.3 h1:y81b9r3asCh6Xtse6Nz85aYGB0cG3M3U6222yap1KWI=
17721772
modernc.org/goabi0 v0.0.3/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
1773-
modernc.org/libc v1.66.0 h1:eoFuDb1ozurUY5WSWlgvxHp0FuL+AncMwNjFqGYMJPQ=
1774-
modernc.org/libc v1.66.0/go.mod h1:AiZxInURfEJx516LqEaFcrC+X38rt9G7+8ojIXQKHbo=
1773+
modernc.org/libc v1.66.1 h1:4uQsntXbVyAgrV+j6NhKvDiUypoJL48BWQx6sy9y8ok=
1774+
modernc.org/libc v1.66.1/go.mod h1:AiZxInURfEJx516LqEaFcrC+X38rt9G7+8ojIXQKHbo=
17751775
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
17761776
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
17771777
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=

vendor/github.com/dop251/goja/compiler_expr.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/klauspost/cpuid/v2/README.md

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/klauspost/cpuid/v2/cpuid.go

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)