mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
trie: rename pure_committer/hasher to committer/hasher
This commit is contained in:
parent
a2085bf678
commit
075e7552a5
6 changed files with 27 additions and 27 deletions
|
|
@ -43,19 +43,19 @@ func (b *sliceBuffer) Reset() {
|
||||||
*b = (*b)[:0]
|
*b = (*b)[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// pureHasher is a type used for the trie Hash operation. A pureHasher has some
|
// hasher is a type used for the trie Hash operation. A hasher has some
|
||||||
// internal preallocated temp space
|
// internal preallocated temp space
|
||||||
type pureHasher struct {
|
type hasher struct {
|
||||||
sha keccakState
|
sha keccakState
|
||||||
|
|
||||||
tmp sliceBuffer
|
tmp sliceBuffer
|
||||||
tmpKey []byte
|
tmpKey []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// pureHasherPool holds pureHashers
|
// hasherPool holds pureHashers
|
||||||
var pureHasherPool = sync.Pool{
|
var hasherPool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return &pureHasher{
|
return &hasher{
|
||||||
tmp: make(sliceBuffer, 0, 550), // cap is as large as a full fullNode.
|
tmp: make(sliceBuffer, 0, 550), // cap is as large as a full fullNode.
|
||||||
tmpKey: make([]byte, 64), // space for an packed key
|
tmpKey: make([]byte, 64), // space for an packed key
|
||||||
sha: sha3.NewLegacyKeccak256().(keccakState),
|
sha: sha3.NewLegacyKeccak256().(keccakState),
|
||||||
|
|
@ -63,18 +63,18 @@ var pureHasherPool = sync.Pool{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPureHasher() *pureHasher {
|
func newHasher() *hasher {
|
||||||
h := pureHasherPool.Get().(*pureHasher)
|
h := hasherPool.Get().(*hasher)
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
func returnPureHasherToPool(h *pureHasher) {
|
func returnHasherToPool(h *hasher) {
|
||||||
pureHasherPool.Put(h)
|
hasherPool.Put(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
// original node initialized with the computed hash to replace the original one.
|
// original node initialized with the computed hash to replace the original one.
|
||||||
func (h *pureHasher) hash(n node, force bool) (hashed node, cached node) {
|
func (h *hasher) hash(n node, force bool) (hashed node, cached node) {
|
||||||
// We're not storing the node, just hashing, use available cached data
|
// We're not storing the node, just hashing, use available cached data
|
||||||
if hash, _ := n.cache(); hash != nil {
|
if hash, _ := n.cache(); hash != nil {
|
||||||
return hash, n
|
return hash, n
|
||||||
|
|
@ -110,7 +110,7 @@ func (h *pureHasher) hash(n node, force bool) (hashed node, cached node) {
|
||||||
// hashShortNodeChildren collapses the short node. The returned collapsed node
|
// hashShortNodeChildren collapses the short node. The returned collapsed node
|
||||||
// holds a live reference to the Key, and must not be modified.
|
// holds a live reference to the Key, and must not be modified.
|
||||||
// The cached
|
// The cached
|
||||||
func (h *pureHasher) hashShortNodeChildren(n *shortNode) (collapsed, cached *shortNode) {
|
func (h *hasher) hashShortNodeChildren(n *shortNode) (collapsed, cached *shortNode) {
|
||||||
// Hash the short node's child, caching the newly hashed subtree
|
// Hash the short node's child, caching the newly hashed subtree
|
||||||
collapsed, cached = n.copy(), n.copy()
|
collapsed, cached = n.copy(), n.copy()
|
||||||
// Previously, we did copy this one. We don't seem to need to actually
|
// Previously, we did copy this one. We don't seem to need to actually
|
||||||
|
|
@ -125,7 +125,7 @@ func (h *pureHasher) hashShortNodeChildren(n *shortNode) (collapsed, cached *sho
|
||||||
return collapsed, cached
|
return collapsed, cached
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *pureHasher) hashFullNodeChildren(n *fullNode) (collapsed *fullNode, cached *fullNode) {
|
func (h *hasher) hashFullNodeChildren(n *fullNode) (collapsed *fullNode, cached *fullNode) {
|
||||||
// Hash the full node's children, caching the newly hashed subtrees
|
// Hash the full node's children, caching the newly hashed subtrees
|
||||||
cached = n.copy()
|
cached = n.copy()
|
||||||
collapsed = n.copy()
|
collapsed = n.copy()
|
||||||
|
|
@ -144,7 +144,7 @@ func (h *pureHasher) hashFullNodeChildren(n *fullNode) (collapsed *fullNode, cac
|
||||||
// should have hex-type Key, which will be converted (without modification)
|
// should have hex-type Key, which will be converted (without modification)
|
||||||
// into compact form for RLP encoding.
|
// into compact form for RLP encoding.
|
||||||
// If the rlp data is smaller than 32 bytes, `nil` is returned.
|
// If the rlp data is smaller than 32 bytes, `nil` is returned.
|
||||||
func (h *pureHasher) shortnodeToHash(n *shortNode, force bool) node {
|
func (h *hasher) shortnodeToHash(n *shortNode, force bool) node {
|
||||||
h.tmp.Reset()
|
h.tmp.Reset()
|
||||||
if err := rlp.Encode(&h.tmp, n); err != nil {
|
if err := rlp.Encode(&h.tmp, n); err != nil {
|
||||||
panic("encode error: " + err.Error())
|
panic("encode error: " + err.Error())
|
||||||
|
|
@ -158,7 +158,7 @@ func (h *pureHasher) shortnodeToHash(n *shortNode, force bool) node {
|
||||||
|
|
||||||
// shortnodeToHash is used to creates a hashNode from a set of hashNodes, (which
|
// shortnodeToHash is used to creates a hashNode from a set of hashNodes, (which
|
||||||
// may contain nil values)
|
// may contain nil values)
|
||||||
func (h *pureHasher) fullnodeToHash(n *fullNode, force bool) node {
|
func (h *hasher) fullnodeToHash(n *fullNode, force bool) node {
|
||||||
h.tmp.Reset()
|
h.tmp.Reset()
|
||||||
// Generate the RLP encoding of the node
|
// Generate the RLP encoding of the node
|
||||||
if err := n.EncodeRLP(&h.tmp); err != nil {
|
if err := n.EncodeRLP(&h.tmp); err != nil {
|
||||||
|
|
@ -172,7 +172,7 @@ func (h *pureHasher) fullnodeToHash(n *fullNode, force bool) node {
|
||||||
}
|
}
|
||||||
|
|
||||||
// hashData hashes the provided data
|
// hashData hashes the provided data
|
||||||
func (h *pureHasher) hashData(data []byte) hashNode {
|
func (h *hasher) hashData(data []byte) hashNode {
|
||||||
n := make(hashNode, 32)
|
n := make(hashNode, 32)
|
||||||
h.sha.Reset()
|
h.sha.Reset()
|
||||||
h.sha.Write(data)
|
h.sha.Write(data)
|
||||||
|
|
@ -184,7 +184,7 @@ func (h *pureHasher) hashData(data []byte) hashNode {
|
||||||
// node (for later RLP encoding) aswell as the hashed node -- unless the
|
// node (for later RLP encoding) aswell as the hashed node -- unless the
|
||||||
// node is smaller than 32 bytes, in which case it will be returned as is.
|
// node is smaller than 32 bytes, in which case it will be returned as is.
|
||||||
// This method does not do anything on value- or hash-nodes.
|
// This method does not do anything on value- or hash-nodes.
|
||||||
func (h *pureHasher) proofHash(original node) (collapsed, hashed node){
|
func (h *hasher) proofHash(original node) (collapsed, hashed node) {
|
||||||
switch n := original.(type) {
|
switch n := original.(type) {
|
||||||
case *shortNode:
|
case *shortNode:
|
||||||
sn, _ := h.hashShortNodeChildren(n)
|
sn, _ := h.hashShortNodeChildren(n)
|
||||||
|
|
@ -182,8 +182,8 @@ func (it *nodeIterator) LeafBlob() []byte {
|
||||||
func (it *nodeIterator) LeafProof() [][]byte {
|
func (it *nodeIterator) LeafProof() [][]byte {
|
||||||
if len(it.stack) > 0 {
|
if len(it.stack) > 0 {
|
||||||
if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
|
if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
|
||||||
hasher := newPureHasher()
|
hasher := newHasher()
|
||||||
defer returnPureHasherToPool(hasher)
|
defer returnHasherToPool(hasher)
|
||||||
proofs := make([][]byte, 0, len(it.stack))
|
proofs := make([][]byte, 0, len(it.stack))
|
||||||
|
|
||||||
for i, item := range it.stack[:len(it.stack)-1] {
|
for i, item := range it.stack[:len(it.stack)-1] {
|
||||||
|
|
|
||||||
|
|
@ -64,8 +64,8 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) e
|
||||||
panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
|
panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hasher := newPureHasher()
|
hasher := newHasher()
|
||||||
defer returnPureHasherToPool(hasher)
|
defer returnHasherToPool(hasher)
|
||||||
|
|
||||||
for i, n := range nodes {
|
for i, n := range nodes {
|
||||||
if fromLevel > 0 {
|
if fromLevel > 0 {
|
||||||
|
|
|
||||||
|
|
@ -176,11 +176,11 @@ func (t *SecureTrie) NodeIterator(start []byte) NodeIterator {
|
||||||
// The caller must not hold onto the return value because it will become
|
// The caller must not hold onto the return value because it will become
|
||||||
// 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 := newPureHasher()
|
h := newHasher()
|
||||||
h.sha.Reset()
|
h.sha.Reset()
|
||||||
h.sha.Write(key)
|
h.sha.Write(key)
|
||||||
buf := h.sha.Sum(t.hashKeyBuf[:0])
|
buf := h.sha.Sum(t.hashKeyBuf[:0])
|
||||||
returnPureHasherToPool(h)
|
returnHasherToPool(h)
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -456,8 +456,8 @@ func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (node, node, error) {
|
||||||
if t.root == nil {
|
if t.root == nil {
|
||||||
return hashNode(emptyRoot.Bytes()), nil, nil
|
return hashNode(emptyRoot.Bytes()), nil, nil
|
||||||
}
|
}
|
||||||
h := newPureHasher()
|
h := newHasher()
|
||||||
defer returnPureHasherToPool(h)
|
defer returnHasherToPool(h)
|
||||||
hashed, cached := h.hash(t.root, true)
|
hashed, cached := h.hash(t.root, true)
|
||||||
return hashed, cached, nil
|
return hashed, cached, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue