-
Notifications
You must be signed in to change notification settings - Fork 5
/
encode.go
338 lines (291 loc) · 6.26 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package fastrlp
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
"sync"
)
// bufPool to convert int to bytes
var bufPool = sync.Pool{
New: func() interface{} {
buf := make([]byte, 8)
return &buf
},
}
type cache struct {
buf [8]byte
vs []Value
size uint64
indx uint64
}
func (c *cache) reset() {
c.vs = c.vs[:0]
c.size = 0
c.indx = 0
}
func (c *cache) getValue() *Value {
if cap(c.vs) > len(c.vs) {
c.vs = c.vs[:len(c.vs)+1]
} else {
c.vs = append(c.vs, Value{})
}
return &c.vs[len(c.vs)-1]
}
// Type represents an RLP type.
type Type int
const (
// TypeArray is an RLP array value.
TypeArray Type = iota
// TypeBytes is an RLP bytes value.
TypeBytes
// TypeNull is an RLP bytes null (0x80)
TypeNull
// TypeArrayNull is an RLP array null (0xC0)
TypeArrayNull
)
// String returns the string representation of the type.
func (t Type) String() string {
switch t {
case TypeArray:
return "array"
case TypeBytes:
return "bytes"
case TypeNull:
return "null"
case TypeArrayNull:
return "null-array"
default:
panic(fmt.Errorf("BUG: unknown Value type: %d", t))
}
}
// Value is an RLP value
type Value struct {
// t is the type of the value, either Bytes or Array
t Type
// a are the list of objects for the type array
a []*Value
// b is the bytes content of the bytes type
b []byte
// l is the length of the value
l uint64
// i is the starting index in the bytes input buffer
i uint64
}
// GetString returns string value.
func (v *Value) GetString() (string, error) {
if v.t != TypeBytes {
return "", errNoBytes()
}
return string(v.b), nil
}
// GetElems returns the elements of an array.
func (v *Value) GetElems() ([]*Value, error) {
if v.t != TypeArray {
return nil, errNoArray()
}
return v.a, nil
}
// GetBigInt returns big.int value.
func (v *Value) GetBigInt(b *big.Int) error {
if v.t != TypeBytes {
return errNoBytes()
}
b.SetBytes(v.b)
return nil
}
// GetBool returns bool value.
func (v *Value) GetBool() (bool, error) {
if v.t != TypeBytes {
return false, errNoBytes()
}
if bytes.Equal(v.b, valueTrue.b) {
return true, nil
}
if bytes.Equal(v.b, valueFalse.b) {
return false, nil
}
return false, fmt.Errorf("not a valid bool")
}
// Raw returns the raw bytes
func (v *Value) Raw() []byte {
return v.b
}
// Bytes returns the raw bytes.
func (v *Value) Bytes() ([]byte, error) {
if v.t != TypeBytes {
return nil, errNoBytes()
}
return v.b, nil
}
// GetBytes returns bytes to dst.
func (v *Value) GetBytes(dst []byte, bits ...int) ([]byte, error) {
if v.t != TypeBytes {
return nil, errNoBytes()
}
if len(bits) > 0 {
if len(v.b) != bits[0] {
return nil, fmt.Errorf("bad length, expected %d but found %d", bits[0], len(v.b))
}
}
dst = append(dst[:0], v.b...)
return dst, nil
}
// GetAddr returns bytes of size 20.
func (v *Value) GetAddr(buf []byte) error {
_, err := v.GetBytes(buf, 20)
return err
}
// GetHash returns bytes of size 32.
func (v *Value) GetHash(buf []byte) error {
_, err := v.GetBytes(buf, 32)
return err
}
// GetByte returns a byte
func (v *Value) GetByte() (byte, error) {
if v.t != TypeBytes {
return 0, errNoBytes()
}
if len(v.b) != 1 {
return 0, fmt.Errorf("bad length, expected 1 but found %d", len(v.b))
}
return byte(v.b[0]), nil
}
// GetUint64 returns uint64.
func (v *Value) GetUint64() (uint64, error) {
if v.t != TypeBytes {
return 0, errNoBytes()
}
if len(v.b) > 8 {
return 0, fmt.Errorf("bytes %d too long for uint64", len(v.b))
}
buf := bufPool.Get().(*[]byte)
num := readUint(v.b, *buf)
bufPool.Put(buf)
return num, nil
}
// Type returns the type of the value
func (v *Value) Type() Type {
return v.t
}
// Get returns the item at index i in the array
func (v *Value) Get(i int) *Value {
if i > len(v.a) {
return nil
}
return v.a[i]
}
// Elems returns the number of elements if its an array
func (v *Value) Elems() int {
return len(v.a)
}
// Len returns the raw size of the value
func (v *Value) Len() uint64 {
if v.t == TypeArray {
return v.l + intsize(v.l)
}
return v.l
}
func (v *Value) fullLen() uint64 {
// null
if v.t == TypeNull || v.t == TypeArrayNull {
return 1
}
// bytes
size := v.l
if v.t == TypeBytes {
if size == 1 && v.b[0] <= 0x7F {
return 1
} else if size < 56 {
return 1 + size
} else {
return 1 + intsize(size) + size
}
}
// array
if size < 56 {
return 1 + size
}
return 1 + intsize(size) + size
}
// Set sets a value in the array
func (v *Value) Set(vv *Value) {
if v == nil || v.t != TypeArray {
return
}
v.l += vv.fullLen()
v.a = append(v.a, vv)
}
func (v *Value) marshalLongSize(dst []byte) []byte {
return v.marshalSize(dst, 0xC0, 0xF7)
}
func (v *Value) marshalShortSize(dst []byte) []byte {
return v.marshalSize(dst, 0x80, 0xB7)
}
func (v *Value) marshalSize(dst []byte, short, long byte) []byte {
if v.l < 56 {
return append(dst, short+byte(v.l))
}
intSize := intsize(v.l)
buf := bufPool.Get().(*[]byte)
binary.BigEndian.PutUint64((*buf)[:], uint64(v.l))
dst = append(dst, long+byte(intSize))
dst = append(dst, (*buf)[8-intSize:]...)
bufPool.Put(buf)
return dst
}
// MarshalTo appends marshaled v to dst and returns the result.
func (v *Value) MarshalTo(dst []byte) []byte {
switch v.t {
case TypeBytes:
if len(v.b) == 1 && v.b[0] <= 0x7F {
// single element
return append(dst, v.b...)
}
dst = v.marshalShortSize(dst)
return append(dst, v.b...)
case TypeArray:
dst = v.marshalLongSize(dst)
for _, vv := range v.a {
dst = vv.MarshalTo(dst)
}
return dst
case TypeNull:
return append(dst, []byte{0x80}...)
case TypeArrayNull:
return append(dst, []byte{0xC0}...)
default:
panic(fmt.Errorf("BUG: unexpected Value type: %d", v.t))
}
}
var (
valueArrayNull = &Value{t: TypeArrayNull, l: 1}
valueNull = &Value{t: TypeNull, l: 1}
valueFalse = valueNull
valueTrue = &Value{t: TypeBytes, b: []byte{0x1}, l: 1}
)
func intsize(val uint64) uint64 {
switch {
case val < (1 << 8):
return 1
case val < (1 << 16):
return 2
case val < (1 << 24):
return 3
case val < (1 << 32):
return 4
case val < (1 << 40):
return 5
case val < (1 << 48):
return 6
case val < (1 << 56):
return 7
}
return 8
}
func errNoBytes() error {
return fmt.Errorf("value is not of type bytes")
}
func errNoArray() error {
return fmt.Errorf("value is not of type array")
}