@@ -44,7 +44,10 @@ type (
4444 flags nodeFlag
4545 }
4646 hashNode []byte
47- valueNode []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 )
@@ -91,13 +107,13 @@ func (n nodeFlag) copy() nodeFlag {
91107func (n * fullNode ) cache () (hashNode , bool ) { return n .flags .hash , n .flags .dirty }
92108func (n * shortNode ) cache () (hashNode , bool ) { return n .flags .hash , n .flags .dirty }
93109func (n hashNode ) cache () (hashNode , bool ) { return nil , true }
94- func (n valueNode ) cache () (hashNode , bool ) { return nil , true }
110+ func (n * valueNode ) cache () (hashNode , bool ) { return nil , true }
95111
96112// Pretty printing.
97113func (n * fullNode ) String () string { return n .fstring ("" ) }
98114func (n * shortNode ) String () string { return n .fstring ("" ) }
99115func (n hashNode ) String () string { return n .fstring ("" ) }
100- func (n valueNode ) String () string { return n .fstring ("" ) }
116+ func (n * valueNode ) String () string { return n .fstring ("" ) }
101117
102118func (n * fullNode ) fstring (ind string ) string {
103119 resp := fmt .Sprintf ("[\n %s " , ind )
@@ -117,8 +133,8 @@ func (n *shortNode) fstring(ind string) string {
117133func (n hashNode ) fstring (ind string ) string {
118134 return fmt .Sprintf ("<%x> " , []byte (n ))
119135}
120- func (n valueNode ) fstring (ind string ) string {
121- return fmt .Sprintf ("%x " , [] byte ( n ))
136+ func (n * valueNode ) fstring (ind string ) string {
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 ( 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 ( val )
227+ n .Children [16 ] = newValueNode ( func () [] byte { return val } )
212228 }
213229 return n , nil
214230}
0 commit comments