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
8401e427
Unverified
Commit
8401e427
authored
Mar 08, 2022
by
Ceyhun Onur
Committed by
GitHub
Mar 08, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core/rawdb: add HasCode, HashTrieNode and use them where possible (#24454)
parent
ec64358a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
41 deletions
+50
-41
snapshot.go
cmd/geth/snapshot.go
+5
-8
accessors_state.go
core/rawdb/accessors_state.go
+43
-31
pruner.go
core/state/pruner/pruner.go
+2
-2
No files found.
cmd/geth/snapshot.go
View file @
8401e427
...
@@ -314,8 +314,7 @@ func traverseState(ctx *cli.Context) error {
...
@@ -314,8 +314,7 @@ func traverseState(ctx *cli.Context) error {
}
}
}
}
if
!
bytes
.
Equal
(
acc
.
CodeHash
,
emptyCode
)
{
if
!
bytes
.
Equal
(
acc
.
CodeHash
,
emptyCode
)
{
code
:=
rawdb
.
ReadCode
(
chaindb
,
common
.
BytesToHash
(
acc
.
CodeHash
))
if
!
rawdb
.
HasCode
(
chaindb
,
common
.
BytesToHash
(
acc
.
CodeHash
))
{
if
len
(
code
)
==
0
{
log
.
Error
(
"Code is missing"
,
"hash"
,
common
.
BytesToHash
(
acc
.
CodeHash
))
log
.
Error
(
"Code is missing"
,
"hash"
,
common
.
BytesToHash
(
acc
.
CodeHash
))
return
errors
.
New
(
"missing code"
)
return
errors
.
New
(
"missing code"
)
}
}
...
@@ -386,11 +385,10 @@ func traverseRawState(ctx *cli.Context) error {
...
@@ -386,11 +385,10 @@ func traverseRawState(ctx *cli.Context) error {
nodes
+=
1
nodes
+=
1
node
:=
accIter
.
Hash
()
node
:=
accIter
.
Hash
()
if
node
!=
(
common
.
Hash
{})
{
// Check the present for non-empty hash node(embedded node doesn't
// Check the present for non-empty hash node(embedded node doesn't
// have their own hash).
// have their own hash).
blob
:=
rawdb
.
ReadTrieNode
(
chaindb
,
node
)
if
node
!=
(
common
.
Hash
{})
{
if
len
(
blob
)
==
0
{
if
!
rawdb
.
HasTrieNode
(
chaindb
,
node
)
{
log
.
Error
(
"Missing trie node(account)"
,
"hash"
,
node
)
log
.
Error
(
"Missing trie node(account)"
,
"hash"
,
node
)
return
errors
.
New
(
"missing account"
)
return
errors
.
New
(
"missing account"
)
}
}
...
@@ -434,8 +432,7 @@ func traverseRawState(ctx *cli.Context) error {
...
@@ -434,8 +432,7 @@ func traverseRawState(ctx *cli.Context) error {
}
}
}
}
if
!
bytes
.
Equal
(
acc
.
CodeHash
,
emptyCode
)
{
if
!
bytes
.
Equal
(
acc
.
CodeHash
,
emptyCode
)
{
code
:=
rawdb
.
ReadCode
(
chaindb
,
common
.
BytesToHash
(
acc
.
CodeHash
))
if
!
rawdb
.
HasCode
(
chaindb
,
common
.
BytesToHash
(
acc
.
CodeHash
))
{
if
len
(
code
)
==
0
{
log
.
Error
(
"Code is missing"
,
"account"
,
common
.
BytesToHash
(
accIter
.
LeafKey
()))
log
.
Error
(
"Code is missing"
,
"account"
,
common
.
BytesToHash
(
accIter
.
LeafKey
()))
return
errors
.
New
(
"missing code"
)
return
errors
.
New
(
"missing code"
)
}
}
...
...
core/rawdb/accessors_state.go
View file @
8401e427
...
@@ -28,17 +28,6 @@ func ReadPreimage(db ethdb.KeyValueReader, hash common.Hash) []byte {
...
@@ -28,17 +28,6 @@ func ReadPreimage(db ethdb.KeyValueReader, hash common.Hash) []byte {
return
data
return
data
}
}
// WritePreimages writes the provided set of preimages to the database.
func
WritePreimages
(
db
ethdb
.
KeyValueWriter
,
preimages
map
[
common
.
Hash
][]
byte
)
{
for
hash
,
preimage
:=
range
preimages
{
if
err
:=
db
.
Put
(
preimageKey
(
hash
),
preimage
);
err
!=
nil
{
log
.
Crit
(
"Failed to store trie preimage"
,
"err"
,
err
)
}
}
preimageCounter
.
Inc
(
int64
(
len
(
preimages
)))
preimageHitCounter
.
Inc
(
int64
(
len
(
preimages
)))
}
// ReadCode retrieves the contract code of the provided code hash.
// ReadCode retrieves the contract code of the provided code hash.
func
ReadCode
(
db
ethdb
.
KeyValueReader
,
hash
common
.
Hash
)
[]
byte
{
func
ReadCode
(
db
ethdb
.
KeyValueReader
,
hash
common
.
Hash
)
[]
byte
{
// Try with the prefixed code scheme first, if not then try with legacy
// Try with the prefixed code scheme first, if not then try with legacy
...
@@ -47,7 +36,7 @@ func ReadCode(db ethdb.KeyValueReader, hash common.Hash) []byte {
...
@@ -47,7 +36,7 @@ func ReadCode(db ethdb.KeyValueReader, hash common.Hash) []byte {
if
len
(
data
)
!=
0
{
if
len
(
data
)
!=
0
{
return
data
return
data
}
}
data
,
_
=
db
.
Get
(
hash
[
:
]
)
data
,
_
=
db
.
Get
(
hash
.
Bytes
()
)
return
data
return
data
}
}
...
@@ -59,6 +48,24 @@ func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte {
...
@@ -59,6 +48,24 @@ func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte {
return
data
return
data
}
}
// ReadTrieNode retrieves the trie node of the provided hash.
func
ReadTrieNode
(
db
ethdb
.
KeyValueReader
,
hash
common
.
Hash
)
[]
byte
{
data
,
_
:=
db
.
Get
(
hash
.
Bytes
())
return
data
}
// HasCode checks if the contract code corresponding to the
// provided code hash is present in the db.
func
HasCode
(
db
ethdb
.
KeyValueReader
,
hash
common
.
Hash
)
bool
{
// Try with the prefixed code scheme first, if not then try with legacy
// scheme.
if
ok
:=
HasCodeWithPrefix
(
db
,
hash
);
ok
{
return
true
}
ok
,
_
:=
db
.
Has
(
hash
.
Bytes
())
return
ok
}
// HasCodeWithPrefix checks if the contract code corresponding to the
// HasCodeWithPrefix checks if the contract code corresponding to the
// provided code hash is present in the db. This function will only check
// provided code hash is present in the db. This function will only check
// presence using the prefix-scheme.
// presence using the prefix-scheme.
...
@@ -67,30 +74,28 @@ func HasCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) bool {
...
@@ -67,30 +74,28 @@ func HasCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) bool {
return
ok
return
ok
}
}
// WriteCode writes the provided contract code database.
// HasTrieNode checks if the trie node with the provided hash is present in db.
func
WriteCode
(
db
ethdb
.
KeyValueWriter
,
hash
common
.
Hash
,
code
[]
byte
)
{
func
HasTrieNode
(
db
ethdb
.
KeyValueReader
,
hash
common
.
Hash
)
bool
{
if
err
:=
db
.
Put
(
codeKey
(
hash
),
code
);
err
!=
nil
{
ok
,
_
:=
db
.
Has
(
hash
.
Bytes
())
log
.
Crit
(
"Failed to store contract code"
,
"err"
,
err
)
return
ok
}
}
}
// DeleteCode deletes the specified contract code from the database.
// WritePreimages writes the provided set of preimages to the database.
func
DeleteCode
(
db
ethdb
.
KeyValueWriter
,
hash
common
.
Hash
)
{
func
WritePreimages
(
db
ethdb
.
KeyValueWriter
,
preimages
map
[
common
.
Hash
][]
byte
)
{
if
err
:=
db
.
Delete
(
codeKey
(
hash
));
err
!=
nil
{
for
hash
,
preimage
:=
range
preimages
{
log
.
Crit
(
"Failed to delete contract code"
,
"err"
,
err
)
if
err
:=
db
.
Put
(
preimageKey
(
hash
),
preimage
);
err
!=
nil
{
log
.
Crit
(
"Failed to store trie preimage"
,
"err"
,
err
)
}
}
}
preimageCounter
.
Inc
(
int64
(
len
(
preimages
)))
preimageHitCounter
.
Inc
(
int64
(
len
(
preimages
)))
}
}
// ReadTrieNode retrieves the trie node of the provided hash.
// WriteCode writes the provided contract code database.
func
ReadTrieNode
(
db
ethdb
.
KeyValueReader
,
hash
common
.
Hash
)
[]
byte
{
func
WriteCode
(
db
ethdb
.
KeyValueWriter
,
hash
common
.
Hash
,
code
[]
byte
)
{
data
,
_
:=
db
.
Get
(
hash
.
Bytes
())
if
err
:=
db
.
Put
(
codeKey
(
hash
),
code
);
err
!=
nil
{
return
data
log
.
Crit
(
"Failed to store contract code"
,
"err"
,
err
)
}
}
// HasTrieNode checks if the trie node with the provided hash is present in db.
func
HasTrieNode
(
db
ethdb
.
KeyValueReader
,
hash
common
.
Hash
)
bool
{
ok
,
_
:=
db
.
Has
(
hash
.
Bytes
())
return
ok
}
}
// WriteTrieNode writes the provided trie node database.
// WriteTrieNode writes the provided trie node database.
...
@@ -100,6 +105,13 @@ func WriteTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) {
...
@@ -100,6 +105,13 @@ func WriteTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) {
}
}
}
}
// DeleteCode deletes the specified contract code from the database.
func
DeleteCode
(
db
ethdb
.
KeyValueWriter
,
hash
common
.
Hash
)
{
if
err
:=
db
.
Delete
(
codeKey
(
hash
));
err
!=
nil
{
log
.
Crit
(
"Failed to delete contract code"
,
"err"
,
err
)
}
}
// DeleteTrieNode deletes the specified trie node from the database.
// DeleteTrieNode deletes the specified trie node from the database.
func
DeleteTrieNode
(
db
ethdb
.
KeyValueWriter
,
hash
common
.
Hash
)
{
func
DeleteTrieNode
(
db
ethdb
.
KeyValueWriter
,
hash
common
.
Hash
)
{
if
err
:=
db
.
Delete
(
hash
.
Bytes
());
err
!=
nil
{
if
err
:=
db
.
Delete
(
hash
.
Bytes
());
err
!=
nil
{
...
...
core/state/pruner/pruner.go
View file @
8401e427
...
@@ -265,7 +265,7 @@ func (p *Pruner) Prune(root common.Hash) error {
...
@@ -265,7 +265,7 @@ func (p *Pruner) Prune(root common.Hash) error {
// Ensure the root is really present. The weak assumption
// Ensure the root is really present. The weak assumption
// is the presence of root can indicate the presence of the
// is the presence of root can indicate the presence of the
// entire trie.
// entire trie.
if
blob
:=
rawdb
.
ReadTrieNode
(
p
.
db
,
root
);
len
(
blob
)
==
0
{
if
!
rawdb
.
HasTrieNode
(
p
.
db
,
root
)
{
// The special case is for clique based networks(rinkeby, goerli
// The special case is for clique based networks(rinkeby, goerli
// and some other private networks), it's possible that two
// and some other private networks), it's possible that two
// consecutive blocks will have same root. In this case snapshot
// consecutive blocks will have same root. In this case snapshot
...
@@ -279,7 +279,7 @@ func (p *Pruner) Prune(root common.Hash) error {
...
@@ -279,7 +279,7 @@ func (p *Pruner) Prune(root common.Hash) error {
// as the pruning target.
// as the pruning target.
var
found
bool
var
found
bool
for
i
:=
len
(
layers
)
-
2
;
i
>=
2
;
i
--
{
for
i
:=
len
(
layers
)
-
2
;
i
>=
2
;
i
--
{
if
blob
:=
rawdb
.
ReadTrieNode
(
p
.
db
,
layers
[
i
]
.
Root
());
len
(
blob
)
!=
0
{
if
rawdb
.
HasTrieNode
(
p
.
db
,
layers
[
i
]
.
Root
())
{
root
=
layers
[
i
]
.
Root
()
root
=
layers
[
i
]
.
Root
()
found
=
true
found
=
true
log
.
Info
(
"Selecting middle-layer as the pruning target"
,
"root"
,
root
,
"depth"
,
i
)
log
.
Info
(
"Selecting middle-layer as the pruning target"
,
"root"
,
root
,
"depth"
,
i
)
...
...
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