@@ -44,7 +44,10 @@ type (
4444 flags nodeFlag
4545 }
4646 hashNode []byte
47- valueNode func () []byte
47+ valueNode struct {
48+ resolver func () []byte
49+ val []byte
50+ }
4851
4952 // fullnodeEncoder is a type used exclusively for encoding fullNode.
5053 // Briefly instantiating a fullnodeEncoder and initializing with
@@ -68,6 +71,19 @@ type (
6871 }
6972)
7073
74+ func newValueNode (resolver func () []byte ) * valueNode {
75+ return & valueNode {
76+ resolver : resolver ,
77+ }
78+ }
79+
80+ func (v * valueNode ) resolve () []byte {
81+ if v .val == nil {
82+ v .val = v .resolver ()
83+ }
84+ return v .val
85+ }
86+
7187// EncodeRLP encodes a full node into the consensus RLP format.
7288func (n * fullNode ) EncodeRLP (w io.Writer ) error {
7389 eb := rlp .NewEncoderBuffer (w )
@@ -118,7 +134,7 @@ func (n hashNode) fstring(ind string) string {
118134 return fmt .Sprintf ("<%x> " , []byte (n ))
119135}
120136func (n valueNode ) fstring (ind string ) string {
121- return fmt .Sprintf ("%x " , n ())
137+ return fmt .Sprintf ("%x " , n . resolve ())
122138}
123139
124140// mustDecodeNode is a wrapper of decodeNode and panic if any error is encountered.
@@ -185,7 +201,7 @@ func decodeShort(hash, elems []byte) (node, error) {
185201 if err != nil {
186202 return nil , fmt .Errorf ("invalid value node: %v" , err )
187203 }
188- return & shortNode {key , valueNode (func () []byte { return val }), flag }, nil
204+ return & shortNode {key , newValueNode (func () []byte { return val }), flag }, nil
189205 }
190206 r , _ , err := decodeRef (rest )
191207 if err != nil {
@@ -208,7 +224,7 @@ func decodeFull(hash, elems []byte) (*fullNode, error) {
208224 return n , err
209225 }
210226 if len (val ) > 0 {
211- n .Children [16 ] = valueNode (func () []byte { return val })
227+ n .Children [16 ] = newValueNode (func () []byte { return val })
212228 }
213229 return n , nil
214230}
0 commit comments