@@ -99,6 +99,7 @@ func (n valueNode) fstring(ind string) string {
99
99
return fmt .Sprintf ("%x " , []byte (n ))
100
100
}
101
101
102
+ // mustDecodeNode is a wrapper of decodeNode and panic if any error is encountered.
102
103
func mustDecodeNode (hash , buf []byte ) node {
103
104
n , err := decodeNode (hash , buf )
104
105
if err != nil {
@@ -107,8 +108,29 @@ func mustDecodeNode(hash, buf []byte) node {
107
108
return n
108
109
}
109
110
110
- // decodeNode parses the RLP encoding of a trie node.
111
+ // mustDecodeNodeUnsafe is a wrapper of decodeNodeUnsafe and panic if any error is
112
+ // encountered.
113
+ func mustDecodeNodeUnsafe (hash , buf []byte ) node {
114
+ n , err := decodeNodeUnsafe (hash , buf )
115
+ if err != nil {
116
+ panic (fmt .Sprintf ("node %x: %v" , hash , err ))
117
+ }
118
+ return n
119
+ }
120
+
121
+ // decodeNode parses the RLP encoding of a trie node. It will deep-copy the passed
122
+ // byte slice for decoding, so it's safe to modify the byte slice afterwards. The-
123
+ // decode performance of this function is not optimal, but it is suitable for most
124
+ // scenarios with low performance requirements and hard to determine whether the
125
+ // byte slice be modified or not.
111
126
func decodeNode (hash , buf []byte ) (node , error ) {
127
+ return decodeNodeUnsafe (hash , common .CopyBytes (buf ))
128
+ }
129
+
130
+ // decodeNodeUnsafe parses the RLP encoding of a trie node. The passed byte slice
131
+ // will be directly referenced by node without bytes deep copy, so the input MUST
132
+ // not be changed after.
133
+ func decodeNodeUnsafe (hash , buf []byte ) (node , error ) {
112
134
if len (buf ) == 0 {
113
135
return nil , io .ErrUnexpectedEOF
114
136
}
@@ -141,7 +163,7 @@ func decodeShort(hash, elems []byte) (node, error) {
141
163
if err != nil {
142
164
return nil , fmt .Errorf ("invalid value node: %v" , err )
143
165
}
144
- return & shortNode {key , append ( valueNode {}, val ... ), flag }, nil
166
+ return & shortNode {key , valueNode ( val ), flag }, nil
145
167
}
146
168
r , _ , err := decodeRef (rest )
147
169
if err != nil {
@@ -164,7 +186,7 @@ func decodeFull(hash, elems []byte) (*fullNode, error) {
164
186
return n , err
165
187
}
166
188
if len (val ) > 0 {
167
- n .Children [16 ] = append ( valueNode {}, val ... )
189
+ n .Children [16 ] = valueNode ( val )
168
190
}
169
191
return n , nil
170
192
}
@@ -190,7 +212,7 @@ func decodeRef(buf []byte) (node, []byte, error) {
190
212
// empty node
191
213
return nil , rest , nil
192
214
case kind == rlp .String && len (val ) == 32 :
193
- return append ( hashNode {}, val ... ), rest , nil
215
+ return hashNode ( val ), rest , nil
194
216
default :
195
217
return nil , nil , fmt .Errorf ("invalid RLP string size %d (want 0 or 32)" , len (val ))
196
218
}
0 commit comments