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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
216e5848
Unverified
Commit
216e5848
authored
7 years ago
by
Péter Szilágyi
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Revert "trie: make fullnode children hash calculation concurrently (#15131)" (#15889)
This reverts commit
0f7fbb85
.
parent
18a7d313
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
trie/hasher.go
+27
-84
27 additions, 84 deletions
trie/hasher.go
trie/secure_trie.go
+4
-4
4 additions, 4 deletions
trie/secure_trie.go
trie/trie.go
+1
-0
1 addition, 0 deletions
trie/trie.go
with
32 additions
and
88 deletions
trie/hasher.go
+
27
−
84
View file @
216e5848
...
@@ -26,46 +26,27 @@ import (
...
@@ -26,46 +26,27 @@ import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rlp"
)
)
// calculator is a utility used by the hasher to calculate the hash value of the tree node.
type
hasher
struct
{
type
calculator
struct
{
tmp
*
bytes
.
Buffer
sha
hash
.
Hash
sha
hash
.
Hash
buffer
*
bytes
.
Buffer
cachegen
,
cachelimit
uint16
}
}
//
calculatorPool is a set of temporary calculators that may be individually saved and retrieved
.
//
hashers live in a global pool
.
var
calculato
rPool
=
sync
.
Pool
{
var
hashe
rPool
=
sync
.
Pool
{
New
:
func
()
interface
{}
{
New
:
func
()
interface
{}
{
return
&
calculator
{
buffer
:
new
(
bytes
.
Buffer
),
sha
:
sha3
.
NewKeccak256
()}
return
&
hasher
{
tmp
:
new
(
bytes
.
Buffer
),
sha
:
sha3
.
NewKeccak256
()}
},
},
}
}
// hasher hasher is used to calculate the hash value of the whole tree.
type
hasher
struct
{
cachegen
uint16
cachelimit
uint16
threaded
bool
mu
sync
.
Mutex
}
func
newHasher
(
cachegen
,
cachelimit
uint16
)
*
hasher
{
func
newHasher
(
cachegen
,
cachelimit
uint16
)
*
hasher
{
h
:=
&
hasher
{
h
:=
hasherPool
.
Get
()
.
(
*
hasher
)
cachegen
:
cachegen
,
h
.
cachegen
,
h
.
cachelimit
=
cachegen
,
cachelimit
cachelimit
:
cachelimit
,
}
return
h
return
h
}
}
// newCalculator retrieves a cleaned calculator from calculator pool.
func
returnHasherToPool
(
h
*
hasher
)
{
func
(
h
*
hasher
)
newCalculator
()
*
calculator
{
hasherPool
.
Put
(
h
)
calculator
:=
calculatorPool
.
Get
()
.
(
*
calculator
)
calculator
.
buffer
.
Reset
()
calculator
.
sha
.
Reset
()
return
calculator
}
// returnCalculator returns a no longer used calculator to the pool.
func
(
h
*
hasher
)
returnCalculator
(
calculator
*
calculator
)
{
calculatorPool
.
Put
(
calculator
)
}
}
// hash collapses a node down into a hash node, also returning a copy of the
// hash collapses a node down into a hash node, also returning a copy of the
...
@@ -142,49 +123,16 @@ func (h *hasher) hashChildren(original node, db DatabaseWriter) (node, node, err
...
@@ -142,49 +123,16 @@ func (h *hasher) hashChildren(original node, db DatabaseWriter) (node, node, err
// Hash the full node's children, caching the newly hashed subtrees
// Hash the full node's children, caching the newly hashed subtrees
collapsed
,
cached
:=
n
.
copy
(),
n
.
copy
()
collapsed
,
cached
:=
n
.
copy
(),
n
.
copy
()
// hashChild is a helper to hash a single child, which is called either on the
for
i
:=
0
;
i
<
16
;
i
++
{
// same thread as the caller or in a goroutine for the toplevel branching.
if
n
.
Children
[
i
]
!=
nil
{
hashChild
:=
func
(
index
int
,
wg
*
sync
.
WaitGroup
)
{
collapsed
.
Children
[
i
],
cached
.
Children
[
i
],
err
=
h
.
hash
(
n
.
Children
[
i
],
db
,
false
)
if
wg
!=
nil
{
if
err
!=
nil
{
defer
wg
.
Done
()
return
original
,
original
,
err
}
}
// Ensure that nil children are encoded as empty strings.
}
else
{
if
collapsed
.
Children
[
index
]
==
nil
{
collapsed
.
Children
[
i
]
=
valueNode
(
nil
)
// Ensure that nil children are encoded as empty strings.
collapsed
.
Children
[
index
]
=
valueNode
(
nil
)
return
}
// Hash all other children properly
var
herr
error
collapsed
.
Children
[
index
],
cached
.
Children
[
index
],
herr
=
h
.
hash
(
n
.
Children
[
index
],
db
,
false
)
if
herr
!=
nil
{
h
.
mu
.
Lock
()
// rarely if ever locked, no congenstion
err
=
herr
h
.
mu
.
Unlock
()
}
}
}
}
// If we're not running in threaded mode yet, span a goroutine for each child
if
!
h
.
threaded
{
// Disable further threading
h
.
threaded
=
true
// Hash all the children concurrently
var
wg
sync
.
WaitGroup
for
i
:=
0
;
i
<
16
;
i
++
{
wg
.
Add
(
1
)
go
hashChild
(
i
,
&
wg
)
}
wg
.
Wait
()
// Reenable threading for subsequent hash calls
h
.
threaded
=
false
}
else
{
for
i
:=
0
;
i
<
16
;
i
++
{
hashChild
(
i
,
nil
)
}
}
if
err
!=
nil
{
return
original
,
original
,
err
}
cached
.
Children
[
16
]
=
n
.
Children
[
16
]
cached
.
Children
[
16
]
=
n
.
Children
[
16
]
if
collapsed
.
Children
[
16
]
==
nil
{
if
collapsed
.
Children
[
16
]
==
nil
{
collapsed
.
Children
[
16
]
=
valueNode
(
nil
)
collapsed
.
Children
[
16
]
=
valueNode
(
nil
)
...
@@ -202,29 +150,24 @@ func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
...
@@ -202,29 +150,24 @@ func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
if
_
,
isHash
:=
n
.
(
hashNode
);
n
==
nil
||
isHash
{
if
_
,
isHash
:=
n
.
(
hashNode
);
n
==
nil
||
isHash
{
return
n
,
nil
return
n
,
nil
}
}
calculator
:=
h
.
newCalculator
()
defer
h
.
returnCalculator
(
calculator
)
// Generate the RLP encoding of the node
// Generate the RLP encoding of the node
if
err
:=
rlp
.
Encode
(
calculator
.
buffer
,
n
);
err
!=
nil
{
h
.
tmp
.
Reset
()
if
err
:=
rlp
.
Encode
(
h
.
tmp
,
n
);
err
!=
nil
{
panic
(
"encode error: "
+
err
.
Error
())
panic
(
"encode error: "
+
err
.
Error
())
}
}
if
calculator
.
buffer
.
Len
()
<
32
&&
!
force
{
if
h
.
tmp
.
Len
()
<
32
&&
!
force
{
return
n
,
nil
// Nodes smaller than 32 bytes are stored inside their parent
return
n
,
nil
// Nodes smaller than 32 bytes are stored inside their parent
}
}
// Larger nodes are replaced by their hash and stored in the database.
// Larger nodes are replaced by their hash and stored in the database.
hash
,
_
:=
n
.
cache
()
hash
,
_
:=
n
.
cache
()
if
hash
==
nil
{
if
hash
==
nil
{
calculator
.
sha
.
Write
(
calculator
.
buffer
.
Bytes
())
h
.
sha
.
Reset
()
hash
=
hashNode
(
calculator
.
sha
.
Sum
(
nil
))
h
.
sha
.
Write
(
h
.
tmp
.
Bytes
())
hash
=
hashNode
(
h
.
sha
.
Sum
(
nil
))
}
}
if
db
!=
nil
{
if
db
!=
nil
{
// db might be a leveldb batch, which is not safe for concurrent writes
return
hash
,
db
.
Put
(
hash
,
h
.
tmp
.
Bytes
())
h
.
mu
.
Lock
()
err
:=
db
.
Put
(
hash
,
calculator
.
buffer
.
Bytes
())
h
.
mu
.
Unlock
()
return
hash
,
err
}
}
return
hash
,
nil
return
hash
,
nil
}
}
This diff is collapsed.
Click to expand it.
trie/secure_trie.go
+
4
−
4
View file @
216e5848
...
@@ -199,10 +199,10 @@ func (t *SecureTrie) secKey(key []byte) []byte {
...
@@ -199,10 +199,10 @@ func (t *SecureTrie) secKey(key []byte) []byte {
// invalid on the next call to hashKey or secKey.
// invalid on the next call to hashKey or secKey.
func
(
t
*
SecureTrie
)
hashKey
(
key
[]
byte
)
[]
byte
{
func
(
t
*
SecureTrie
)
hashKey
(
key
[]
byte
)
[]
byte
{
h
:=
newHasher
(
0
,
0
)
h
:=
newHasher
(
0
,
0
)
calculator
:=
h
.
newCalculator
()
h
.
sha
.
Reset
()
calculator
.
sha
.
Write
(
key
)
h
.
sha
.
Write
(
key
)
buf
:=
calculator
.
sha
.
Sum
(
t
.
hashKeyBuf
[
:
0
])
buf
:=
h
.
sha
.
Sum
(
t
.
hashKeyBuf
[
:
0
])
h
.
return
Calculator
(
calculator
)
return
HasherToPool
(
h
)
return
buf
return
buf
}
}
...
...
This diff is collapsed.
Click to expand it.
trie/trie.go
+
1
−
0
View file @
216e5848
...
@@ -501,5 +501,6 @@ func (t *Trie) hashRoot(db DatabaseWriter) (node, node, error) {
...
@@ -501,5 +501,6 @@ func (t *Trie) hashRoot(db DatabaseWriter) (node, node, error) {
return
hashNode
(
emptyRoot
.
Bytes
()),
nil
,
nil
return
hashNode
(
emptyRoot
.
Bytes
()),
nil
,
nil
}
}
h
:=
newHasher
(
t
.
cachegen
,
t
.
cachelimit
)
h
:=
newHasher
(
t
.
cachegen
,
t
.
cachelimit
)
defer
returnHasherToPool
(
h
)
return
h
.
hash
(
t
.
root
,
db
,
true
)
return
h
.
hash
(
t
.
root
,
db
,
true
)
}
}
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