@ -28,7 +28,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
)
)
var ErrCommitDisabled = errors . New ( "no database for committing" )
var ErrCommitDisabled = errors . New ( "no database for committing" )
@ -224,6 +223,7 @@ func (st *StackTrie) insert(key, value []byte) {
switch st . nodeType {
switch st . nodeType {
case branchNode : /* Branch */
case branchNode : /* Branch */
idx := int ( key [ 0 ] )
idx := int ( key [ 0 ] )
// Unresolve elder siblings
// Unresolve elder siblings
for i := idx - 1 ; i >= 0 ; i -- {
for i := idx - 1 ; i >= 0 ; i -- {
if st . children [ i ] != nil {
if st . children [ i ] != nil {
@ -233,12 +233,14 @@ func (st *StackTrie) insert(key, value []byte) {
break
break
}
}
}
}
// Add new child
// Add new child
if st . children [ idx ] == nil {
if st . children [ idx ] == nil {
st . children [ idx ] = newLeaf ( key [ 1 : ] , value , st . db )
st . children [ idx ] = newLeaf ( key [ 1 : ] , value , st . db )
} else {
} else {
st . children [ idx ] . insert ( key [ 1 : ] , value )
st . children [ idx ] . insert ( key [ 1 : ] , value )
}
}
case extNode : /* Ext */
case extNode : /* Ext */
// Compare both key chunks and see where they differ
// Compare both key chunks and see where they differ
diffidx := st . getDiffIndex ( key )
diffidx := st . getDiffIndex ( key )
@ -326,10 +328,9 @@ func (st *StackTrie) insert(key, value []byte) {
p = st . children [ 0 ]
p = st . children [ 0 ]
}
}
// Create the two child leaves: the one containing the
// Create the two child leaves: one containing the original
// original value and the one containing the new value
// value and another containing the new value. The child leaf
// The child leave will be hashed directly in order to
// is hashed directly in order to free up some memory.
// free up some memory.
origIdx := st . key [ diffidx ]
origIdx := st . key [ diffidx ]
p . children [ origIdx ] = newLeaf ( st . key [ diffidx + 1 : ] , st . val , st . db )
p . children [ origIdx ] = newLeaf ( st . key [ diffidx + 1 : ] , st . val , st . db )
p . children [ origIdx ] . hash ( )
p . children [ origIdx ] . hash ( )
@ -341,19 +342,22 @@ func (st *StackTrie) insert(key, value []byte) {
// over to the children.
// over to the children.
st . key = st . key [ : diffidx ]
st . key = st . key [ : diffidx ]
st . val = nil
st . val = nil
case emptyNode : /* Empty */
case emptyNode : /* Empty */
st . nodeType = leafNode
st . nodeType = leafNode
st . key = key
st . key = key
st . val = value
st . val = value
case hashedNode :
case hashedNode :
panic ( "trying to insert into hash" )
panic ( "trying to insert into hash" )
default :
default :
panic ( "invalid type" )
panic ( "invalid type" )
}
}
}
}
// hash() hashes the node 'st' and converts it into 'hashedNode', if possible.
// hash converts st into a 'hashedNode', if possible. Possible outcomes:
// Possible outcomes:
//
// 1. The rlp-encoded value was >= 32 bytes:
// 1. The rlp-encoded value was >= 32 bytes:
// - Then the 32-byte `hash` will be accessible in `st.val`.
// - Then the 32-byte `hash` will be accessible in `st.val`.
// - And the 'st.type' will be 'hashedNode'
// - And the 'st.type' will be 'hashedNode'
@ -361,119 +365,116 @@ func (st *StackTrie) insert(key, value []byte) {
// - Then the <32 byte rlp-encoded value will be accessible in 'st.val'.
// - Then the <32 byte rlp-encoded value will be accessible in 'st.val'.
// - And the 'st.type' will be 'hashedNode' AGAIN
// - And the 'st.type' will be 'hashedNode' AGAIN
//
//
// This method will also:
// This method also sets 'st.type' to hashedNode, and clears 'st.key'.
// set 'st.type' to hashedNode
// clear 'st.key'
func ( st * StackTrie ) hash ( ) {
func ( st * StackTrie ) hash ( ) {
/* Shortcut if node is already hashed */
h := newHasher ( false )
if st . nodeType == hashedNode {
defer returnHasherToPool ( h )
return
}
st . hashRec ( h )
// The 'hasher' is taken from a pool, but we don't actually
}
// claim an instance until all children are done with their hashing,
// and we actually need one
func ( st * StackTrie ) hashRec ( hasher * hasher ) {
var h * hasher
// The switch below sets this to the RLP-encoding of this node.
var encodedNode [ ] byte
switch st . nodeType {
switch st . nodeType {
case hashedNode :
return
case emptyNode :
st . val = emptyRoot . Bytes ( )
st . key = st . key [ : 0 ]
st . nodeType = hashedNode
return
case branchNode :
case branchNode :
var nodes [ 17 ] node
var nodes rawFullN ode
for i , child := range st . children {
for i , child := range st . children {
if child == nil {
if child == nil {
nodes [ i ] = nilValueNode
nodes [ i ] = nilValueNode
continue
continue
}
}
child . hash ( )
child . hashRec ( hasher )
if len ( child . val ) < 32 {
if len ( child . val ) < 32 {
nodes [ i ] = rawNode ( child . val )
nodes [ i ] = rawNode ( child . val )
} else {
} else {
nodes [ i ] = hashNode ( child . val )
nodes [ i ] = hashNode ( child . val )
}
}
st . children [ i ] = nil // Reclaim mem from subtree
// Release child back to pool.
st . children [ i ] = nil
returnToPool ( child )
returnToPool ( child )
}
}
nodes [ 16 ] = nilValueNode
h = newHasher ( false )
nodes . encode ( hasher . encbuf )
defer returnHasherToPool ( h )
encodedNode = hasher . encodedBytes ( )
h . tmp . Reset ( )
if err := rlp . Encode ( & h . tmp , nodes ) ; err != nil {
panic ( err )
}
case extNode :
case extNode :
st . children [ 0 ] . hash ( )
st . children [ 0 ] . hashRec ( hasher )
h = newHasher ( false )
defer returnHasherToPool ( h )
sz := hexToCompactInPlace ( st . key )
h . tmp . Reset ( )
n := rawShortNode { Key : st . key [ : sz ] }
var valuenode node
if len ( st . children [ 0 ] . val ) < 32 {
if len ( st . children [ 0 ] . val ) < 32 {
valuenode = rawNode ( st . children [ 0 ] . val )
n . Val = rawNode ( st . children [ 0 ] . val )
} else {
} else {
valuenode = hashNode ( st . children [ 0 ] . val )
n . Val = hashNode ( st . children [ 0 ] . val )
}
n := struct {
Key [ ] byte
Val node
} {
Key : hexToCompact ( st . key ) ,
Val : valuenode ,
}
if err := rlp . Encode ( & h . tmp , n ) ; err != nil {
panic ( err )
}
}
n . encode ( hasher . encbuf )
encodedNode = hasher . encodedBytes ( )
// Release child back to pool.
returnToPool ( st . children [ 0 ] )
returnToPool ( st . children [ 0 ] )
st . children [ 0 ] = nil // Reclaim mem from subtree
st . children [ 0 ] = nil
case leafNode :
case leafNode :
h = newHasher ( false )
defer returnHasherToPool ( h )
h . tmp . Reset ( )
st . key = append ( st . key , byte ( 16 ) )
st . key = append ( st . key , byte ( 16 ) )
sz := hexToCompactInPlace ( st . key )
sz := hexToCompactInPlace ( st . key )
n := [ ] [ ] byte { st . key [ : sz ] , st . val }
n := rawShortNode { Key : st . key [ : sz ] , Val : valueNode ( st . val ) }
if err := rlp . Encode ( & h . tmp , n ) ; err != nil {
panic ( err )
n . encode ( hasher . encbuf )
}
encodedNode = hasher . encodedBytes ( )
case emptyNode :
st . val = emptyRoot . Bytes ( )
st . key = st . key [ : 0 ]
st . nodeType = hashedNode
return
default :
default :
panic ( "I nvalid node type" )
panic ( "invalid node type" )
}
}
st . key = st . key [ : 0 ]
st . nodeType = hashedNode
st . nodeType = hashedNode
if len ( h . tmp ) < 32 {
st . key = st . key [ : 0 ]
st . val = common . CopyBytes ( h . tmp )
if len ( encodedNode ) < 32 {
st . val = common . CopyBytes ( encodedNode )
return
return
}
}
// Write the hash to the 'val'. We allocate a new val here to not mutate
// Write the hash to the 'val'. We allocate a new val here to not mutate
// input values
// input values
st . val = make ( [ ] byte , 32 )
st . val = hasher . hashData ( encodedNode )
h . sha . Reset ( )
h . sha . Write ( h . tmp )
h . sha . Read ( st . val )
if st . db != nil {
if st . db != nil {
// TODO! Is it safe to Put the slice here?
// TODO! Is it safe to Put the slice here?
// Do all db implementations copy the value provided?
// Do all db implementations copy the value provided?
st . db . Put ( st . val , h . tmp )
st . db . Put ( st . val , encodedNode )
}
}
}
}
// Hash returns the hash of the current node
// Hash returns the hash of the current node.
func ( st * StackTrie ) Hash ( ) ( h common . Hash ) {
func ( st * StackTrie ) Hash ( ) ( h common . Hash ) {
st . hash ( )
hasher := newHasher ( false )
if len ( st . val ) != 32 {
defer returnHasherToPool ( hasher )
// If the node's RLP isn't 32 bytes long, the node will not
// be hashed, and instead contain the rlp-encoding of the
st . hashRec ( hasher )
// node. For the top level node, we need to force the hashing.
if len ( st . val ) == 32 {
ret := make ( [ ] byte , 32 )
copy ( h [ : ] , st . val )
h := newHasher ( false )
return h
defer returnHasherToPool ( h )
h . sha . Reset ( )
h . sha . Write ( st . val )
h . sha . Read ( ret )
return common . BytesToHash ( ret )
}
}
return common . BytesToHash ( st . val )
// If the node's RLP isn't 32 bytes long, the node will not
// be hashed, and instead contain the rlp-encoding of the
// node. For the top level node, we need to force the hashing.
hasher . sha . Reset ( )
hasher . sha . Write ( st . val )
hasher . sha . Read ( h [ : ] )
return h
}
}
// Commit will firstly hash the entrie trie if it's still not hashed
// Commit will firstly hash the entrie trie if it's still not hashed
@ -483,23 +484,26 @@ func (st *StackTrie) Hash() (h common.Hash) {
//
//
// The associated database is expected, otherwise the whole commit
// The associated database is expected, otherwise the whole commit
// functionality should be disabled.
// functionality should be disabled.
func ( st * StackTrie ) Commit ( ) ( common . Hash , error ) {
func ( st * StackTrie ) Commit ( ) ( h common . Hash , err error ) {
if st . db == nil {
if st . db == nil {
return common . Hash { } , ErrCommitDisabled
return common . Hash { } , ErrCommitDisabled
}
}
st . hash ( )
if len ( st . val ) != 32 {
hasher := newHasher ( false )
// If the node's RLP isn't 32 bytes long, the node will not
defer returnHasherToPool ( hasher )
// be hashed (and committed), and instead contain the rlp-encoding of the
// node. For the top level node, we need to force the hashing+commit.
st . hashRec ( hasher )
ret := make ( [ ] byte , 32 )
if len ( st . val ) == 32 {
h := newHasher ( false )
copy ( h [ : ] , st . val )
defer returnHasherToPool ( h )
return h , nil
h . sha . Reset ( )
h . sha . Write ( st . val )
h . sha . Read ( ret )
st . db . Put ( ret , st . val )
return common . BytesToHash ( ret ) , nil
}
}
return common . BytesToHash ( st . val ) , nil
// If the node's RLP isn't 32 bytes long, the node will not
// be hashed (and committed), and instead contain the rlp-encoding of the
// node. For the top level node, we need to force the hashing+commit.
hasher . sha . Reset ( )
hasher . sha . Write ( st . val )
hasher . sha . Read ( h [ : ] )
st . db . Put ( h [ : ] , st . val )
return h , nil
}
}