-
Notifications
You must be signed in to change notification settings - Fork 31
/
parserV2.go
73 lines (66 loc) · 1.46 KB
/
parserV2.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
package serialization
import (
"bytes"
"encoding/binary"
"math"
"github.com/pkg/errors"
"github.com/wavesplatform/gowaves/pkg/ride/meta"
g "github.com/wavesplatform/gowaves/pkg/ride/meta/generated"
protobuf "google.golang.org/protobuf/proto"
)
func newParserV2(r *bytes.Reader, id [32]byte, header scriptHeader) *parser {
p := &parser{
r: r,
id: id,
header: header,
}
p.readShort = readShortV2
p.readInt = readIntV2
p.readLong = readLongV2
p.readMeta = readMetaV2
return p
}
func readShortV2(r *bytes.Reader) (int16, error) {
v, err := binary.ReadUvarint(r)
if err != nil {
return 0, err
}
vv := int64(v)
if vv < math.MinInt16 || vv > math.MaxInt16 {
return 0, errors.New("value out of int16 range")
}
return int16(v), nil
}
func readIntV2(r *bytes.Reader) (int32, error) {
v, err := binary.ReadUvarint(r)
if err != nil {
return 0, err
}
vv := int64(v)
if vv < math.MinInt32 || vv > math.MaxInt32 {
return 0, errors.New("value out of int32 range")
}
return int32(v), nil
}
func readLongV2(r *bytes.Reader) (int64, error) {
v, err := binary.ReadUvarint(r)
if err != nil {
return 0, err
}
return int64(v), nil
}
func readMetaV2(p *parser) (meta.DApp, error) {
b, err := p.readBytes()
if err != nil {
return meta.DApp{}, err
}
pbMeta := new(g.DAppMeta)
if err := protobuf.Unmarshal(b, pbMeta); err != nil {
return meta.DApp{}, err
}
m, err := meta.Convert(pbMeta)
if err != nil {
return meta.DApp{}, err
}
return m, nil
}