good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
af4080b4
Unverified
Commit
af4080b4
authored
Apr 15, 2020
by
Martin Holst Swende
Browse files
Options
Downloads
Patches
Plain Diff
trie: fix concurrent usage of secKeyBuf, ref #20920
parent
00064ddc
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
trie/database.go
+25
-11
25 additions, 11 deletions
trie/database.go
with
25 additions
and
11 deletions
trie/database.go
+
25
−
11
View file @
af4080b4
...
...
@@ -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 a
n 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
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment