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
79b68dd7
Unverified
Commit
79b68dd7
authored
Apr 20, 2020
by
Péter Szilágyi
Committed by
GitHub
Apr 20, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #20923 from holiman/fix_seckeybuf
trie: fix concurrent usage of secKeyBuf, ref #20920
parents
648b0cb7
af4080b4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
11 deletions
+25
-11
database.go
trie/database.go
+25
-11
No files found.
trie/database.go
View file @
79b68dd7
...
...
@@ -59,8 +59,11 @@ var (
// secureKeyPrefix is the database key prefix used to store trie node preimages.
var
secureKeyPrefix
=
[]
byte
(
"secure-key-"
)
// secureKeyPrefixLength is the length of the above prefix
const
secureKeyPrefixLength
=
11
// secureKeyLength is the length of the above prefix + 32byte hash.
const
secureKeyLength
=
11
+
32
const
secureKeyLength
=
secureKeyPrefixLength
+
32
// Database is an intermediate write layer between the trie data structures and
// the disk database. The aim is to accumulate trie writes in-memory and only
...
...
@@ -79,7 +82,6 @@ type Database struct {
newest
common
.
Hash
// Newest tracked node, flush-list tail
preimages
map
[
common
.
Hash
][]
byte
// Preimages of nodes from the secure trie
seckeybuf
[
secureKeyLength
]
byte
// Ephemeral buffer for calculating preimage keys
gctime
time
.
Duration
// Time spent on garbage collection since last commit
gcnodes
uint64
// Nodes garbage collected since last commit
...
...
@@ -445,15 +447,15 @@ func (db *Database) preimage(hash common.Hash) ([]byte, error) {
return
preimage
,
nil
}
// Content unavailable in memory, attempt to retrieve from disk
return
db
.
diskdb
.
Get
(
db
.
secureKey
(
hash
[
:
]
))
return
db
.
diskdb
.
Get
(
secureKey
(
hash
))
}
// secureKey returns the database key for the preimage of key
, as an ephemeral
//
buffer. The caller must not hold onto the return value because it will become
// invalid on the next call.
func
(
db
*
Database
)
secureKey
(
key
[]
byte
)
[]
byte
{
buf
:=
append
(
db
.
seckeybuf
[
:
0
],
secureKeyPrefix
...
)
buf
=
append
(
buf
,
key
...
)
// secureKey returns the database key for the preimage of key
(as a newly
//
allocated byte-slice)
func
secureKey
(
hash
common
.
Hash
)
[]
byte
{
buf
:=
make
([]
byte
,
secureKeyLength
)
copy
(
buf
,
secureKeyPrefix
)
copy
(
buf
[
secureKeyPrefixLength
:
],
hash
[
:
]
)
return
buf
}
...
...
@@ -596,12 +598,18 @@ func (db *Database) Cap(limit common.StorageSize) error {
size
:=
db
.
dirtiesSize
+
common
.
StorageSize
((
len
(
db
.
dirties
)
-
1
)
*
cachedNodeSize
)
size
+=
db
.
childrenSize
-
common
.
StorageSize
(
len
(
db
.
dirties
[
common
.
Hash
{}]
.
children
)
*
(
common
.
HashLength
+
2
))
// We reuse an ephemeral buffer for the keys. The batch Put operation
// copies it internally, so we can reuse it.
var
keyBuf
[
secureKeyLength
]
byte
copy
(
keyBuf
[
:
],
secureKeyPrefix
)
// If the preimage cache got large enough, push to disk. If it's still small
// leave for later to deduplicate writes.
flushPreimages
:=
db
.
preimagesSize
>
4
*
1024
*
1024
if
flushPreimages
{
for
hash
,
preimage
:=
range
db
.
preimages
{
if
err
:=
batch
.
Put
(
db
.
secureKey
(
hash
[
:
]),
preimage
);
err
!=
nil
{
copy
(
keyBuf
[
secureKeyPrefixLength
:
],
hash
[
:
])
if
err
:=
batch
.
Put
(
keyBuf
[
:
],
preimage
);
err
!=
nil
{
log
.
Error
(
"Failed to commit preimage from trie database"
,
"err"
,
err
)
return
err
}
...
...
@@ -692,9 +700,15 @@ func (db *Database) Commit(node common.Hash, report bool) error {
start
:=
time
.
Now
()
batch
:=
db
.
diskdb
.
NewBatch
()
// We reuse an ephemeral buffer for the keys. The batch Put operation
// copies it internally, so we can reuse it.
var
keyBuf
[
secureKeyLength
]
byte
copy
(
keyBuf
[
:
],
secureKeyPrefix
)
// Move all of the accumulated preimages into a write batch
for
hash
,
preimage
:=
range
db
.
preimages
{
if
err
:=
batch
.
Put
(
db
.
secureKey
(
hash
[
:
]),
preimage
);
err
!=
nil
{
copy
(
keyBuf
[
secureKeyPrefixLength
:
],
hash
[
:
])
if
err
:=
batch
.
Put
(
keyBuf
[
:
],
preimage
);
err
!=
nil
{
log
.
Error
(
"Failed to commit preimage from trie database"
,
"err"
,
err
)
return
err
}
...
...
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