-
Notifications
You must be signed in to change notification settings - Fork 3
/
blockchain.go
203 lines (181 loc) · 4.29 KB
/
blockchain.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
// Package blockchain provides the tools for encoding
// data primitives in blockchain structures
package blockchain
import (
"encoding/binary"
"errors"
"io"
"math"
"sync"
"github.com/bytom/encoding/bufpool"
)
var bufPool = sync.Pool{New: func() interface{} { return new([9]byte) }}
var ErrRange = errors.New("value out of range")
// Reader wraps a buffer and provides utilities for decoding
// data primitives in blockchain structures. Its various read
// calls may return a slice of the underlying buffer.
type Reader struct {
buf []byte
}
// NewReader constructs a new reader with the provided bytes. It
// does not create a copy of the bytes, so the caller is responsible
// for copying the bytes if necessary.
func NewReader(b []byte) *Reader {
return &Reader{buf: b}
}
// Len returns the number of unread bytes.
func (r *Reader) Len() int {
return len(r.buf)
}
// ReadByte reads and returns the next byte from the input.
//
// It implements the io.ByteReader interface.
func (r *Reader) ReadByte() (byte, error) {
if len(r.buf) == 0 {
return 0, io.EOF
}
b := r.buf[0]
r.buf = r.buf[1:]
return b, nil
}
// Read reads up to len(p) bytes into p. It implements
// the io.Reader interface.
func (r *Reader) Read(p []byte) (n int, err error) {
n = copy(p, r.buf)
r.buf = r.buf[n:]
if len(r.buf) == 0 {
err = io.EOF
}
return
}
func ReadVarint31(r *Reader) (uint32, error) {
val, err := binary.ReadUvarint(r)
if err != nil {
return 0, err
}
if val > math.MaxInt32 {
return 0, ErrRange
}
return uint32(val), nil
}
func ReadVarint63(r *Reader) (uint64, error) {
val, err := binary.ReadUvarint(r)
if err != nil {
return 0, err
}
if val > math.MaxInt64 {
return 0, ErrRange
}
return val, nil
}
func ReadVarstr31(r *Reader) ([]byte, error) {
l, err := ReadVarint31(r)
if err != nil {
return nil, err
}
if l == 0 {
return nil, nil
}
if int(l) > len(r.buf) {
return nil, io.ErrUnexpectedEOF
}
str := r.buf[:l]
r.buf = r.buf[l:]
return str, nil
}
// ReadVarstrList reads a varint31 length prefix followed by
// that many varstrs.
func ReadVarstrList(r *Reader) (result [][]byte, err error) {
nelts, err := ReadVarint31(r)
if err != nil {
return nil, err
}
if nelts == 0 {
return nil, nil
}
for ; nelts > 0 && err == nil; nelts-- {
var s []byte
s, err = ReadVarstr31(r)
result = append(result, s)
}
if len(result) < int(nelts) {
err = io.ErrUnexpectedEOF
}
return result, err
}
// ReadExtensibleString reads a varint31 length prefix and that many
// bytes from r. It then calls the given function to consume those
// bytes, returning any unconsumed suffix.
func ReadExtensibleString(r *Reader, f func(*Reader) error) (suffix []byte, err error) {
s, err := ReadVarstr31(r)
if err != nil {
return nil, err
}
sr := NewReader(s)
err = f(sr)
if err != nil {
return nil, err
}
return sr.buf, nil
}
func WriteVarint31(w io.Writer, val uint64) (int, error) {
if val > math.MaxInt32 {
return 0, ErrRange
}
buf := bufPool.Get().(*[9]byte)
n := binary.PutUvarint(buf[:], val)
b, err := w.Write(buf[:n])
bufPool.Put(buf)
return b, err
}
func WriteVarint63(w io.Writer, val uint64) (int, error) {
if val > math.MaxInt64 {
return 0, ErrRange
}
buf := bufPool.Get().(*[9]byte)
n := binary.PutUvarint(buf[:], val)
b, err := w.Write(buf[:n])
bufPool.Put(buf)
return b, err
}
func WriteVarstr31(w io.Writer, str []byte) (int, error) {
n, err := WriteVarint31(w, uint64(len(str)))
if err != nil {
return n, err
}
n2, err := w.Write(str)
return n + n2, err
}
// WriteVarstrList writes a varint31 length prefix followed by the
// elements of l as varstrs.
func WriteVarstrList(w io.Writer, l [][]byte) (int, error) {
n, err := WriteVarint31(w, uint64(len(l)))
if err != nil {
return n, err
}
for _, s := range l {
n2, err := WriteVarstr31(w, s)
n += n2
if err != nil {
return n, err
}
}
return n, err
}
// WriteExtensibleString sends the output of the given function, plus
// the given suffix, to w, together with a varint31 length prefix.
func WriteExtensibleString(w io.Writer, suffix []byte, f func(io.Writer) error) (int, error) {
buf := bufpool.Get()
defer bufpool.Put(buf)
err := f(buf)
if err != nil {
return 0, err
}
if len(suffix) > 0 {
_, err := buf.Write(suffix)
if err != nil {
return 0, err
}
}
return WriteVarstr31(w, buf.Bytes())
}