@ -99,6 +99,7 @@ func (n valueNode) fstring(ind string) string {
return fmt . Sprintf ( "%x " , [ ] byte ( n ) )
return fmt . Sprintf ( "%x " , [ ] byte ( n ) )
}
}
// mustDecodeNode is a wrapper of decodeNode and panic if any error is encountered.
func mustDecodeNode ( hash , buf [ ] byte ) node {
func mustDecodeNode ( hash , buf [ ] byte ) node {
n , err := decodeNode ( hash , buf )
n , err := decodeNode ( hash , buf )
if err != nil {
if err != nil {
@ -107,8 +108,29 @@ func mustDecodeNode(hash, buf []byte) node {
return n
return n
}
}
// decodeNode parses the RLP encoding of a trie node.
// mustDecodeNodeUnsafe is a wrapper of decodeNodeUnsafe and panic if any error is
// encountered.
func mustDecodeNodeUnsafe ( hash , buf [ ] byte ) node {
n , err := decodeNodeUnsafe ( hash , buf )
if err != nil {
panic ( fmt . Sprintf ( "node %x: %v" , hash , err ) )
}
return n
}
// decodeNode parses the RLP encoding of a trie node. It will deep-copy the passed
// byte slice for decoding, so it's safe to modify the byte slice afterwards. The-
// decode performance of this function is not optimal, but it is suitable for most
// scenarios with low performance requirements and hard to determine whether the
// byte slice be modified or not.
func decodeNode ( hash , buf [ ] byte ) ( node , error ) {
func decodeNode ( hash , buf [ ] byte ) ( node , error ) {
return decodeNodeUnsafe ( hash , common . CopyBytes ( buf ) )
}
// decodeNodeUnsafe parses the RLP encoding of a trie node. The passed byte slice
// will be directly referenced by node without bytes deep copy, so the input MUST
// not be changed after.
func decodeNodeUnsafe ( hash , buf [ ] byte ) ( node , error ) {
if len ( buf ) == 0 {
if len ( buf ) == 0 {
return nil , io . ErrUnexpectedEOF
return nil , io . ErrUnexpectedEOF
}
}
@ -141,7 +163,7 @@ func decodeShort(hash, elems []byte) (node, error) {
if err != nil {
if err != nil {
return nil , fmt . Errorf ( "invalid value node: %v" , err )
return nil , fmt . Errorf ( "invalid value node: %v" , err )
}
}
return & shortNode { key , append ( valueNode { } , val ... ) , flag } , nil
return & shortNode { key , valueNode ( val ) , flag } , nil
}
}
r , _ , err := decodeRef ( rest )
r , _ , err := decodeRef ( rest )
if err != nil {
if err != nil {
@ -164,7 +186,7 @@ func decodeFull(hash, elems []byte) (*fullNode, error) {
return n , err
return n , err
}
}
if len ( val ) > 0 {
if len ( val ) > 0 {
n . Children [ 16 ] = append ( valueNode { } , val ... )
n . Children [ 16 ] = valueNode ( val )
}
}
return n , nil
return n , nil
}
}
@ -190,7 +212,7 @@ func decodeRef(buf []byte) (node, []byte, error) {
// empty node
// empty node
return nil , rest , nil
return nil , rest , nil
case kind == rlp . String && len ( val ) == 32 :
case kind == rlp . String && len ( val ) == 32 :
return append ( hashNode { } , val ... ) , rest , nil
return hashNode ( val ) , rest , nil
default :
default :
return nil , nil , fmt . Errorf ( "invalid RLP string size %d (want 0 or 32)" , len ( val ) )
return nil , nil , fmt . Errorf ( "invalid RLP string size %d (want 0 or 32)" , len ( val ) )
}
}