/
utils.go
183 lines (160 loc) · 3.98 KB
/
utils.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package codec
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzgo/tezos"
)
// TODO: fetch dynamic from /chains/main/mempool/filter
const (
minFeeFixedNanoTez int64 = 100_000
minFeeByteNanoTez int64 = 1_000
minFeeGasNanoTez int64 = 100
)
// CalculateMinFee returns the minimum fee at/above which bakers will accept
// this operation under default config settings. Lower fee operations may not
// pass the fee filter and may time out in the mempool.
func CalculateMinFee(o Operation, gas int64, withHeader bool, p *tezos.Params) int64 {
buf := bytes.NewBuffer(nil)
_ = o.EncodeBuffer(buf, p)
sz := int64(buf.Len())
if withHeader {
sz += 32 + 64 // branch + signature
}
fee := minFeeFixedNanoTez + sz*minFeeByteNanoTez + gas*minFeeGasNanoTez
return int64(math.Ceil(float64(fee) / 1000)) // nano -> micro, round up
}
// ensureTagAndSize reads the binary operation's tag and matches it against the expected
// type tag and minimum size for the operation under the current protocol. It returns
// an error when tag does not match or when the buffer is too short for reading the
// mandatory operation contents.
func ensureTagAndSize(buf *bytes.Buffer, typ tezos.OpType, ver int) error {
if buf == nil {
return io.ErrShortBuffer
}
tag, err := buf.ReadByte()
if err != nil {
// unread so the caller is able to repair
buf.UnreadByte()
return err
}
if tag != typ.TagVersion(ver) {
// unread so the caller is able to repair
buf.UnreadByte()
return fmt.Errorf("invalid tag %d for op type %s", tag, typ)
}
// don't fail size checks for undefined ops
sz := typ.MinSizeVersion(ver)
if buf.Len() < sz-1 {
fmt.Printf("short buffer for tag %d for op type %s: exp=%d got=%d\n", tag, typ,
sz-1, buf.Len())
buf.UnreadByte()
return io.ErrShortBuffer
}
return nil
}
func readInt64(buf []byte) (int64, error) {
if len(buf) != 8 {
return 0, io.ErrShortBuffer
}
return int64(enc.Uint64(buf)), nil
}
func readInt32(buf []byte) (int32, error) {
if len(buf) != 4 {
return 0, io.ErrShortBuffer
}
return int32(enc.Uint32(buf)), nil
}
func readInt16(buf []byte) (int16, error) {
if len(buf) != 2 {
return 0, io.ErrShortBuffer
}
return int16(enc.Uint16(buf)), nil
}
func readUint32(buf []byte) (uint32, error) {
if len(buf) != 4 {
return 0, io.ErrShortBuffer
}
return enc.Uint32(buf), nil
}
func readBool(buf []byte) (bool, error) {
if len(buf) != 1 {
return false, io.ErrShortBuffer
}
return buf[0] == 255, nil
}
func readByte(buf []byte) (byte, error) {
if len(buf) != 1 {
return 0, io.ErrShortBuffer
}
return buf[0], nil
}
func max64(x, y int64) int64 {
if x > y {
return x
}
return y
}
func writeBytesWithLen(buf *bytes.Buffer, b tezos.HexBytes) (err error) {
err = binary.Write(buf, enc, uint32(len(b)))
if err != nil {
return
}
_, err = buf.Write(b)
return err
}
func readBytesWithLen(buf *bytes.Buffer) (b tezos.HexBytes, err error) {
var l uint32
l, err = readUint32(buf.Next(4))
if err != nil {
return
}
err = b.ReadBytes(buf, int(l))
return
}
func readPrimWithLen(buf *bytes.Buffer) (p micheline.Prim, err error) {
var l uint32
l, err = readUint32(buf.Next(4))
if err != nil {
return
}
err = p.UnmarshalBinary(buf.Next(int(l)))
return
}
func writePrimWithLen(buf *bytes.Buffer, p micheline.Prim) error {
v, err := p.MarshalBinary()
if err != nil {
return err
}
if err := binary.Write(buf, enc, uint32(len(v))); err != nil {
return err
}
_, err = buf.Write(v)
return err
}
func readStringWithLen(buf *bytes.Buffer) (s string, err error) {
var l uint32
l, err = readUint32(buf.Next(4))
if err != nil {
return
}
if b := buf.Next(int(l)); len(b) != int(l) {
err = io.ErrShortBuffer
return
} else {
s = string(b)
}
return
}
func writeStringWithLen(buf *bytes.Buffer, s string) error {
if err := binary.Write(buf, enc, uint32(len(s))); err != nil {
return err
}
_, err := buf.WriteString(s)
return err
}