Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
6c149fd4
Unverified
Commit
6c149fd4
authored
Jan 03, 2023
by
rjl493456442
Committed by
GitHub
Jan 03, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core, eth, trie, light: clean up trie interface (#26388)
* all: cleanup trie interface * eth, trie: address comments
parent
41fe9d64
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
38 additions
and
35 deletions
+38
-35
database.go
core/state/database.go
+12
-5
statedb.go
core/state/statedb.go
+3
-3
trie_prefetcher.go
core/state/trie_prefetcher.go
+1
-5
handler.go
eth/protocols/snap/handler.go
+2
-2
sync.go
eth/protocols/snap/sync.go
+1
-1
trie.go
light/trie.go
+6
-6
secure_trie.go
trie/secure_trie.go
+13
-13
No files found.
core/state/database.go
View file @
6c149fd4
...
@@ -73,8 +73,13 @@ type Trie interface {
...
@@ -73,8 +73,13 @@ type Trie interface {
// trie.MissingNodeError is returned.
// trie.MissingNodeError is returned.
TryGet
(
key
[]
byte
)
([]
byte
,
error
)
TryGet
(
key
[]
byte
)
([]
byte
,
error
)
// TryGetAccount abstract an account read from the trie.
// TryGetAccount abstracts an account read from the trie. It retrieves the
TryGetAccount
(
key
[]
byte
)
(
*
types
.
StateAccount
,
error
)
// account blob from the trie with provided account address and decodes it
// with associated decoding algorithm. If the specified account is not in
// the trie, nil will be returned. If the trie is corrupted(e.g. some nodes
// are missing or the account blob is incorrect for decoding), an error will
// be returned.
TryGetAccount
(
address
common
.
Address
)
(
*
types
.
StateAccount
,
error
)
// TryUpdate associates key with value in the trie. If value has length zero, any
// TryUpdate associates key with value in the trie. If value has length zero, any
// existing value is deleted from the trie. The value bytes must not be modified
// existing value is deleted from the trie. The value bytes must not be modified
...
@@ -82,15 +87,17 @@ type Trie interface {
...
@@ -82,15 +87,17 @@ type Trie interface {
// database, a trie.MissingNodeError is returned.
// database, a trie.MissingNodeError is returned.
TryUpdate
(
key
,
value
[]
byte
)
error
TryUpdate
(
key
,
value
[]
byte
)
error
// TryUpdateAccount abstract an account write to the trie.
// TryUpdateAccount abstracts an account write to the trie. It encodes the
TryUpdateAccount
(
key
[]
byte
,
account
*
types
.
StateAccount
)
error
// provided account object with associated algorithm and then updates it
// in the trie with provided address.
TryUpdateAccount
(
address
common
.
Address
,
account
*
types
.
StateAccount
)
error
// TryDelete removes any existing value for key from the trie. If a node was not
// TryDelete removes any existing value for key from the trie. If a node was not
// found in the database, a trie.MissingNodeError is returned.
// found in the database, a trie.MissingNodeError is returned.
TryDelete
(
key
[]
byte
)
error
TryDelete
(
key
[]
byte
)
error
// TryDeleteAccount abstracts an account deletion from the trie.
// TryDeleteAccount abstracts an account deletion from the trie.
TryDeleteAccount
(
key
[]
byte
)
error
TryDeleteAccount
(
address
common
.
Address
)
error
// Hash returns the root hash of the trie. It does not write to the database and
// Hash returns the root hash of the trie. It does not write to the database and
// can be used even if the trie doesn't have one.
// can be used even if the trie doesn't have one.
...
...
core/state/statedb.go
View file @
6c149fd4
...
@@ -510,7 +510,7 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
...
@@ -510,7 +510,7 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
}
}
// Encode the account and update the account trie
// Encode the account and update the account trie
addr
:=
obj
.
Address
()
addr
:=
obj
.
Address
()
if
err
:=
s
.
trie
.
TryUpdateAccount
(
addr
[
:
]
,
&
obj
.
data
);
err
!=
nil
{
if
err
:=
s
.
trie
.
TryUpdateAccount
(
addr
,
&
obj
.
data
);
err
!=
nil
{
s
.
setError
(
fmt
.
Errorf
(
"updateStateObject (%x) error: %v"
,
addr
[
:
],
err
))
s
.
setError
(
fmt
.
Errorf
(
"updateStateObject (%x) error: %v"
,
addr
[
:
],
err
))
}
}
...
@@ -531,7 +531,7 @@ func (s *StateDB) deleteStateObject(obj *stateObject) {
...
@@ -531,7 +531,7 @@ func (s *StateDB) deleteStateObject(obj *stateObject) {
}
}
// Delete the account from the trie
// Delete the account from the trie
addr
:=
obj
.
Address
()
addr
:=
obj
.
Address
()
if
err
:=
s
.
trie
.
TryDeleteAccount
(
addr
[
:
]
);
err
!=
nil
{
if
err
:=
s
.
trie
.
TryDeleteAccount
(
addr
);
err
!=
nil
{
s
.
setError
(
fmt
.
Errorf
(
"deleteStateObject (%x) error: %v"
,
addr
[
:
],
err
))
s
.
setError
(
fmt
.
Errorf
(
"deleteStateObject (%x) error: %v"
,
addr
[
:
],
err
))
}
}
}
}
...
@@ -585,7 +585,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
...
@@ -585,7 +585,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
if
data
==
nil
{
if
data
==
nil
{
start
:=
time
.
Now
()
start
:=
time
.
Now
()
var
err
error
var
err
error
data
,
err
=
s
.
trie
.
TryGetAccount
(
addr
.
Bytes
()
)
data
,
err
=
s
.
trie
.
TryGetAccount
(
addr
)
if
metrics
.
EnabledExpensive
{
if
metrics
.
EnabledExpensive
{
s
.
AccountReads
+=
time
.
Since
(
start
)
s
.
AccountReads
+=
time
.
Since
(
start
)
}
}
...
...
core/state/trie_prefetcher.go
View file @
6c149fd4
...
@@ -335,12 +335,8 @@ func (sf *subfetcher) loop() {
...
@@ -335,12 +335,8 @@ func (sf *subfetcher) loop() {
// No termination request yet, prefetch the next entry
// No termination request yet, prefetch the next entry
if
_
,
ok
:=
sf
.
seen
[
string
(
task
)];
ok
{
if
_
,
ok
:=
sf
.
seen
[
string
(
task
)];
ok
{
sf
.
dups
++
sf
.
dups
++
}
else
{
if
len
(
task
)
==
len
(
common
.
Address
{})
{
sf
.
trie
.
TryGetAccount
(
task
)
}
else
{
}
else
{
sf
.
trie
.
TryGet
(
task
)
sf
.
trie
.
TryGet
(
task
)
}
sf
.
seen
[
string
(
task
)]
=
struct
{}{}
sf
.
seen
[
string
(
task
)]
=
struct
{}{}
}
}
}
}
...
...
eth/protocols/snap/handler.go
View file @
6c149fd4
...
@@ -417,7 +417,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
...
@@ -417,7 +417,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
nil
return
nil
,
nil
}
}
acc
,
err
:=
accTrie
.
TryGetAccount
WithPreHashedKey
(
account
[
:
]
)
acc
,
err
:=
accTrie
.
TryGetAccount
ByHash
(
account
)
if
err
!=
nil
||
acc
==
nil
{
if
err
!=
nil
||
acc
==
nil
{
return
nil
,
nil
return
nil
,
nil
}
}
...
@@ -523,7 +523,7 @@ func ServiceGetTrieNodesQuery(chain *core.BlockChain, req *GetTrieNodesPacket, s
...
@@ -523,7 +523,7 @@ func ServiceGetTrieNodesQuery(chain *core.BlockChain, req *GetTrieNodesPacket, s
if
snap
==
nil
{
if
snap
==
nil
{
// We don't have the requested state snapshotted yet (or it is stale),
// We don't have the requested state snapshotted yet (or it is stale),
// but can look up the account via the trie instead.
// but can look up the account via the trie instead.
account
,
err
:=
accTrie
.
TryGetAccount
WithPreHashedKey
(
pathset
[
0
]
)
account
,
err
:=
accTrie
.
TryGetAccount
ByHash
(
common
.
BytesToHash
(
pathset
[
0
])
)
loads
+=
8
// We don't know the exact cost of lookup, this is an estimate
loads
+=
8
// We don't know the exact cost of lookup, this is an estimate
if
err
!=
nil
||
account
==
nil
{
if
err
!=
nil
||
account
==
nil
{
break
break
...
...
eth/protocols/snap/sync.go
View file @
6c149fd4
...
@@ -3083,7 +3083,7 @@ func (t *healRequestSort) Swap(i, j int) {
...
@@ -3083,7 +3083,7 @@ func (t *healRequestSort) Swap(i, j int) {
func
(
t
*
healRequestSort
)
Merge
()
[]
TrieNodePathSet
{
func
(
t
*
healRequestSort
)
Merge
()
[]
TrieNodePathSet
{
var
result
[]
TrieNodePathSet
var
result
[]
TrieNodePathSet
for
_
,
path
:=
range
t
.
syncPaths
{
for
_
,
path
:=
range
t
.
syncPaths
{
pathset
:=
TrieNodePathSet
(
[][]
byte
(
path
)
)
pathset
:=
TrieNodePathSet
(
path
)
if
len
(
path
)
==
1
{
if
len
(
path
)
==
1
{
// It's an account reference.
// It's an account reference.
result
=
append
(
result
,
pathset
)
result
=
append
(
result
,
pathset
)
...
...
light/trie.go
View file @
6c149fd4
...
@@ -115,9 +115,9 @@ func (t *odrTrie) TryGet(key []byte) ([]byte, error) {
...
@@ -115,9 +115,9 @@ func (t *odrTrie) TryGet(key []byte) ([]byte, error) {
return
res
,
err
return
res
,
err
}
}
func
(
t
*
odrTrie
)
TryGetAccount
(
key
[]
byte
)
(
*
types
.
StateAccount
,
error
)
{
func
(
t
*
odrTrie
)
TryGetAccount
(
address
common
.
Address
)
(
*
types
.
StateAccount
,
error
)
{
key
=
crypto
.
Keccak256
(
key
)
var
res
types
.
StateAccount
var
res
types
.
StateAccount
key
:=
crypto
.
Keccak256
(
address
.
Bytes
())
err
:=
t
.
do
(
key
,
func
()
(
err
error
)
{
err
:=
t
.
do
(
key
,
func
()
(
err
error
)
{
value
,
err
:=
t
.
trie
.
TryGet
(
key
)
value
,
err
:=
t
.
trie
.
TryGet
(
key
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -131,8 +131,8 @@ func (t *odrTrie) TryGetAccount(key []byte) (*types.StateAccount, error) {
...
@@ -131,8 +131,8 @@ func (t *odrTrie) TryGetAccount(key []byte) (*types.StateAccount, error) {
return
&
res
,
err
return
&
res
,
err
}
}
func
(
t
*
odrTrie
)
TryUpdateAccount
(
key
[]
byte
,
acc
*
types
.
StateAccount
)
error
{
func
(
t
*
odrTrie
)
TryUpdateAccount
(
address
common
.
Address
,
acc
*
types
.
StateAccount
)
error
{
key
=
crypto
.
Keccak256
(
key
)
key
:=
crypto
.
Keccak256
(
address
.
Bytes
()
)
value
,
err
:=
rlp
.
EncodeToBytes
(
acc
)
value
,
err
:=
rlp
.
EncodeToBytes
(
acc
)
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"decoding error in account update: %w"
,
err
)
return
fmt
.
Errorf
(
"decoding error in account update: %w"
,
err
)
...
@@ -157,8 +157,8 @@ func (t *odrTrie) TryDelete(key []byte) error {
...
@@ -157,8 +157,8 @@ func (t *odrTrie) TryDelete(key []byte) error {
}
}
// TryDeleteAccount abstracts an account deletion from the trie.
// TryDeleteAccount abstracts an account deletion from the trie.
func
(
t
*
odrTrie
)
TryDeleteAccount
(
key
[]
byte
)
error
{
func
(
t
*
odrTrie
)
TryDeleteAccount
(
address
common
.
Address
)
error
{
key
=
crypto
.
Keccak256
(
key
)
key
:=
crypto
.
Keccak256
(
address
.
Bytes
()
)
return
t
.
do
(
key
,
func
()
error
{
return
t
.
do
(
key
,
func
()
error
{
return
t
.
trie
.
TryDelete
(
key
)
return
t
.
trie
.
TryDelete
(
key
)
})
})
...
...
trie/secure_trie.go
View file @
6c149fd4
...
@@ -90,11 +90,11 @@ func (t *StateTrie) TryGet(key []byte) ([]byte, error) {
...
@@ -90,11 +90,11 @@ func (t *StateTrie) TryGet(key []byte) ([]byte, error) {
return
t
.
trie
.
TryGet
(
t
.
hashKey
(
key
))
return
t
.
trie
.
TryGet
(
t
.
hashKey
(
key
))
}
}
// TryGetAccount attempts to retrieve an account with provided
trie path
.
// TryGetAccount attempts to retrieve an account with provided
account address
.
// If the specified account is not in the trie, nil will be returned.
// If the specified account is not in the trie, nil will be returned.
// If a trie node is not found in the database, a MissingNodeError is returned.
// If a trie node is not found in the database, a MissingNodeError is returned.
func
(
t
*
StateTrie
)
TryGetAccount
(
key
[]
byte
)
(
*
types
.
StateAccount
,
error
)
{
func
(
t
*
StateTrie
)
TryGetAccount
(
address
common
.
Address
)
(
*
types
.
StateAccount
,
error
)
{
res
,
err
:=
t
.
trie
.
TryGet
(
t
.
hashKey
(
key
))
res
,
err
:=
t
.
trie
.
TryGet
(
t
.
hashKey
(
address
.
Bytes
()
))
if
res
==
nil
||
err
!=
nil
{
if
res
==
nil
||
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -103,11 +103,11 @@ func (t *StateTrie) TryGetAccount(key []byte) (*types.StateAccount, error) {
...
@@ -103,11 +103,11 @@ func (t *StateTrie) TryGetAccount(key []byte) (*types.StateAccount, error) {
return
ret
,
err
return
ret
,
err
}
}
// TryGetAccount
WithPreHashedKey
does the same thing as TryGetAccount, however
// TryGetAccount
ByHash
does the same thing as TryGetAccount, however
// it expects a
key that is already hashed. This constitutes an abstraction leak,
// it expects a
n account hash that is the hash of address. This constitutes an
// since the client code needs to know the key format.
//
abstraction leak,
since the client code needs to know the key format.
func
(
t
*
StateTrie
)
TryGetAccount
WithPreHashedKey
(
key
[]
byte
)
(
*
types
.
StateAccount
,
error
)
{
func
(
t
*
StateTrie
)
TryGetAccount
ByHash
(
addrHash
common
.
Hash
)
(
*
types
.
StateAccount
,
error
)
{
res
,
err
:=
t
.
trie
.
TryGet
(
key
)
res
,
err
:=
t
.
trie
.
TryGet
(
addrHash
.
Bytes
()
)
if
res
==
nil
||
err
!=
nil
{
if
res
==
nil
||
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -156,8 +156,8 @@ func (t *StateTrie) TryUpdate(key, value []byte) error {
...
@@ -156,8 +156,8 @@ func (t *StateTrie) TryUpdate(key, value []byte) error {
// TryUpdateAccount account will abstract the write of an account to the
// TryUpdateAccount account will abstract the write of an account to the
// secure trie.
// secure trie.
func
(
t
*
StateTrie
)
TryUpdateAccount
(
key
[]
byte
,
acc
*
types
.
StateAccount
)
error
{
func
(
t
*
StateTrie
)
TryUpdateAccount
(
address
common
.
Address
,
acc
*
types
.
StateAccount
)
error
{
hk
:=
t
.
hashKey
(
key
)
hk
:=
t
.
hashKey
(
address
.
Bytes
()
)
data
,
err
:=
rlp
.
EncodeToBytes
(
acc
)
data
,
err
:=
rlp
.
EncodeToBytes
(
acc
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
@@ -165,7 +165,7 @@ func (t *StateTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error
...
@@ -165,7 +165,7 @@ func (t *StateTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error
if
err
:=
t
.
trie
.
TryUpdate
(
hk
,
data
);
err
!=
nil
{
if
err
:=
t
.
trie
.
TryUpdate
(
hk
,
data
);
err
!=
nil
{
return
err
return
err
}
}
t
.
getSecKeyCache
()[
string
(
hk
)]
=
common
.
CopyBytes
(
key
)
t
.
getSecKeyCache
()[
string
(
hk
)]
=
address
.
Bytes
(
)
return
nil
return
nil
}
}
...
@@ -186,8 +186,8 @@ func (t *StateTrie) TryDelete(key []byte) error {
...
@@ -186,8 +186,8 @@ func (t *StateTrie) TryDelete(key []byte) error {
}
}
// TryDeleteAccount abstracts an account deletion from the trie.
// TryDeleteAccount abstracts an account deletion from the trie.
func
(
t
*
StateTrie
)
TryDeleteAccount
(
key
[]
byte
)
error
{
func
(
t
*
StateTrie
)
TryDeleteAccount
(
address
common
.
Address
)
error
{
hk
:=
t
.
hashKey
(
key
)
hk
:=
t
.
hashKey
(
address
.
Bytes
()
)
delete
(
t
.
getSecKeyCache
(),
string
(
hk
))
delete
(
t
.
getSecKeyCache
(),
string
(
hk
))
return
t
.
trie
.
TryDelete
(
hk
)
return
t
.
trie
.
TryDelete
(
hk
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment