forked from eoscanada/eos-go
/
transaction.go
246 lines (197 loc) · 6.3 KB
/
transaction.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
package eos
import (
"bytes"
"compress/flate"
"compress/zlib"
"encoding/binary"
"encoding/hex"
"fmt"
"time"
"io"
"encoding/json"
"io/ioutil"
"github.com/eoscanada/eos-go/ecc"
)
type Transaction struct { // WARN: is a `variant` in C++, can be a SignedTransaction or a Transaction.
Expiration JSONTime `json:"expiration"`
Region uint16 `json:"region"`
RefBlockNum uint16 `json:"ref_block_num"`
RefBlockPrefix uint32 `json:"ref_block_prefix"`
MaxNetUsageWords Varuint32 `json:"max_net_usage_words"`
MaxKCPUUsage Varuint32 `json:"max_kcpu_usage"`
DelaySec Varuint32 `json:"delay_sec"` // number of secs to delay, making it cancellable for that duration
// TODO: implement the estimators and write that in `.Fill()`.. for the transaction.
ContextFreeActions []*Action `json:"context_free_actions,omitempty"`
Actions []*Action `json:"actions,omitempty"`
}
// 69c9c15a 0000 1400 62f95d45 b31d 904e 00 00 020000000000ea305500000040258ab2c2010000000000ea305500000000a8ed
func (tx *Transaction) Fill(api *API) ([]byte, error) {
var info *InfoResp
var err error
api.lastGetInfoLock.Lock()
if !api.lastGetInfoStamp.IsZero() && time.Now().Add(-1*time.Second).Before(api.lastGetInfoStamp) {
info = api.lastGetInfo
} else {
info, err = api.GetInfo()
if err != nil {
return nil, err
}
api.lastGetInfoStamp = time.Now()
api.lastGetInfo = info
}
api.lastGetInfoLock.Unlock()
if err != nil {
return nil, err
}
if tx.ContextFreeActions == nil {
tx.ContextFreeActions = make([]*Action, 0, 0)
}
blockID, err := hex.DecodeString(info.HeadBlockID)
if err != nil {
return nil, fmt.Errorf("decode hex: %s", err)
}
tx.setRefBlock(blockID)
/// TODO: configure somewhere the default time for transactions,
/// etc.. add a `.Timeout` with that duration, default to 30
/// seconds ?
tx.Expiration = JSONTime{info.HeadBlockTime.Add(30 * time.Second)}
return blockID, nil
}
func (tx *Transaction) setRefBlock(blockID []byte) {
tx.RefBlockNum = uint16(binary.BigEndian.Uint32(blockID[:4]))
tx.RefBlockPrefix = binary.LittleEndian.Uint32(blockID[8:16])
}
type SignedTransaction struct {
*Transaction
Signatures []ecc.Signature `json:"signatures"`
ContextFreeData []HexBytes `json:"context_free_data"`
packed *PackedTransaction
}
func NewSignedTransaction(tx *Transaction) *SignedTransaction {
return &SignedTransaction{
Transaction: tx,
Signatures: make([]ecc.Signature, 0),
ContextFreeData: make([]HexBytes, 0),
}
}
func (s *SignedTransaction) String() string {
data, err := json.Marshal(s)
if err != nil {
return err.Error()
}
return string(data)
}
func (tx *Transaction) ID() string {
return "ID here" //todo
}
func (s *SignedTransaction) Pack(opts TxOptions) (*PackedTransaction, error) {
rawtrx, err := MarshalBinary(s.Transaction)
if err != nil {
return nil, err
}
rawcfd, err := MarshalBinary(s.ContextFreeData)
if err != nil {
return nil, err
}
// Is it so ?
if len(s.ContextFreeData) == 0 {
rawcfd = []byte{}
}
switch opts.Compress {
case CompressionZlib:
var trx bytes.Buffer
var cfd bytes.Buffer
// Compress Trx
writer, _ := zlib.NewWriterLevel(&trx, flate.BestCompression) // can only fail if invalid `level`..
writer.Write(rawtrx) // ignore error, could only bust memory
rawtrx = trx.Bytes()
// Compress ContextFreeData
writer, _ = zlib.NewWriterLevel(&cfd, flate.BestCompression) // can only fail if invalid `level`..
writer.Write(rawcfd) // ignore errors, memory errors only
rawcfd = cfd.Bytes()
}
packed := &PackedTransaction{
Signatures: s.Signatures,
Compression: opts.Compress,
PackedContextFreeData: rawcfd,
PackedTransaction: rawtrx,
}
return packed, nil
}
func (tx *SignedTransaction) estimateResources(opts TxOptions, maxcpu, maxnet uint32) {
// see programs/cleos/main.cpp for an estimation algo..
if opts.MaxNetUsageWords != 0 {
tx.MaxNetUsageWords = Varuint32(opts.MaxNetUsageWords)
} else {
tx.MaxNetUsageWords = Varuint32(maxnet)
}
if opts.MaxKCPUUsage != 0 {
tx.MaxKCPUUsage = Varuint32(opts.MaxKCPUUsage)
} else {
tx.MaxKCPUUsage = Varuint32(maxcpu)
}
}
// PackedTransaction represents a fully packed transaction, with
// signatures, and all. They circulate like that on the P2P net, and
// that's how they are stored.
type PackedTransaction struct {
Signatures []ecc.Signature `json:"signatures"`
Compression CompressionType `json:"compression"` // in C++, it's an enum, not sure how it Binary-marshals..
PackedContextFreeData HexBytes `json:"packed_context_free_data"`
PackedTransaction HexBytes `json:"packed_trx"`
}
func (p *PackedTransaction) Unpack() (signedTx *SignedTransaction, err error) {
var txReader io.Reader
txReader = bytes.NewBuffer(p.PackedTransaction)
var freeDataReader io.Reader
freeDataReader = bytes.NewBuffer(p.PackedContextFreeData)
switch p.Compression {
case CompressionZlib:
txReader, err = zlib.NewReader(txReader)
if err != nil {
return
}
freeDataReader, err = zlib.NewReader(freeDataReader)
if err != nil {
return
}
}
data, err := ioutil.ReadAll(txReader)
if err != nil {
return
}
decoder := NewDecoder(data)
var tx Transaction
err = decoder.Decode(&tx)
if err != nil {
return nil, fmt.Errorf("unpacking Transaction, %s", err)
}
// TODO: wire that in
//decoder = NewDecoder(freeDataReader)
//var contextFreeData []HexBytes
//err = decoder.Decode(&contextFreeData)
//if err != nil {
// fmt.Println("PackedTransaction@freedata err: ", err)
// return
//}
signedTx = NewSignedTransaction(&tx)
//signedTx.ContextFreeData = contextFreeData
signedTx.Signatures = p.Signatures
signedTx.packed = p
return
}
type DeferredTransaction struct {
*Transaction
SenderID uint32 `json:"sender_id"`
Sender AccountName `json:"sender"`
DelayUntil JSONTime `json:"delay_until"`
}
// TxOptions represents options you want to pass to the transaction
// you're sending.
type TxOptions struct {
MaxNetUsageWords uint32
Delay time.Duration
MaxKCPUUsage uint32 // If you want to override the CPU usage (in counts of 1024)
//ExtraKCPUUsage uint32 // If you want to *add* some CPU usage to the estimated amount (in counts of 1024)
Compress CompressionType
}