-
Notifications
You must be signed in to change notification settings - Fork 2
/
transaction.go
245 lines (200 loc) · 6.16 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
package chain
import (
"github.com/uuosio/chain/eosio"
)
// void send_deferred(const uint128_t* sender_id, capi_name payer, const char *serialized_transaction, size_t size, uint32_t replace_existing);
func SendDeferred(senderID Uint128, payer Name, transaction []byte, replaceExisting bool) {
eosio.SendDeferred(senderID, payer.N, transaction, replaceExisting)
}
// int cancel_deferred(const uint128_t* sender_id);
func CancelDeferred(senderID Uint128) int32 {
return eosio.CancelDeferred(senderID)
}
// size_t read_transaction(char *buffer, size_t size);
func ReadTransaction() []byte {
return eosio.ReadTransaction()
}
// __attribute__((eosio_wasm_import))
// size_t transaction_size( void );
func TransactionSize() int32 {
return eosio.TransactionSize()
}
// int tapos_block_num( void );
func TaposBlockNum() int32 {
return eosio.TaposBlockNum()
}
// int tapos_block_prefix( void );
func TaposBlockPrefix() int32 {
return eosio.TaposBlockPrefix()
}
// uint32_t expiration( void );
func Expiration() uint32 {
return eosio.Expiration()
}
// int get_action( uint32_t type, uint32_t index, char* buff, size_t size );
func GetAction(_type uint32, index uint32) []byte {
return eosio.GetAction(_type, index)
}
// int get_context_free_data( uint32_t index, char* buff, size_t size );
func GetContextFreeData(index uint32) []byte {
return eosio.GetContextFreeData(index)
}
type TransactionExtension struct {
Type uint16
Data []byte
}
func (a *TransactionExtension) Size() int {
return 2 + 5 + len(a.Data)
}
func (t *TransactionExtension) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.PackUint16(t.Type)
enc.PackBytes(t.Data)
return enc.GetSize() - oldSize
}
func (t *TransactionExtension) Unpack(data []byte) int {
dec := NewDecoder(data)
t.Type = dec.UnpackUint16()
t.Data = dec.UnpackBytes()
return dec.Pos()
}
func (t *TransactionExtension) Print() {
Print("{", t.Type, ",", string(t.Data), "}")
}
type Transaction struct {
// time_point_sec expiration;
// uint16_t ref_block_num;
// uint32_t ref_block_prefix;
// unsigned_int max_net_usage_words = 0UL; /// number of 8 byte words this transaction can serialize into after compressions
// uint8_t max_cpu_usage_ms = 0UL; /// number of CPU usage units to bill transaction for
// unsigned_int delay_sec = 0UL; /// number of seconds to delay transaction, default: 0
Expiration uint32
RefBlockNum uint16
RefBlockPrefix uint32
//[VLQ or Base-128 encoding](https://en.wikipedia.org/wiki/Variable-length_quantity)
//unsigned_int vaint (eosio.cdt/libraries/eosiolib/core/eosio/varint.hpp)
MaxNetUsageWords VarUint32
MaxCpuUsageMs uint8
DelaySec VarUint32 //unsigned_int
ContextFreeActions []*Action
Actions []*Action
Extention []*TransactionExtension
}
func NewTransaction(delaySec int) *Transaction {
t := &Transaction{}
t.Expiration = CurrentTimeSeconds() + uint32(60*60)
t.RefBlockNum = uint16(TaposBlockNum())
t.RefBlockPrefix = uint32(TaposBlockPrefix())
t.MaxNetUsageWords = VarUint32(0)
t.MaxCpuUsageMs = uint8(0)
t.DelaySec = VarUint32(delaySec)
return t
}
type TransactionCache struct {
sendId *Uint128
payer Name
transaction []byte
replace bool
}
var gTransactionCache []*TransactionCache
func AddTransactionCache(sendId *Uint128, payer Name, transaction []byte, replace bool) {
if gTransactionCache == nil {
gTransactionCache = make([]*TransactionCache, 0, 2)
}
gTransactionCache = append(gTransactionCache, &TransactionCache{sendId, payer, transaction, replace})
}
func (t *Transaction) Send(senderId Uint128, replaceExisting bool, payer Name) {
SendDeferred(senderId, payer, EncoderPack(t), replaceExisting)
}
func (t *Transaction) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
//4+2+4
enc.PackUint32(t.Expiration)
enc.PackUint16(t.RefBlockNum)
enc.PackUint32(t.RefBlockPrefix)
t.MaxNetUsageWords.Pack(enc)
enc.PackUint8(t.MaxCpuUsageMs)
t.DelaySec.Pack(enc)
enc.PackLength(len(t.ContextFreeActions))
for _, action := range t.ContextFreeActions {
action.Pack(enc)
}
enc.PackLength(len(t.Actions))
for _, action := range t.Actions {
action.Pack(enc)
}
enc.PackLength(len(t.Extention))
for _, extention := range t.Extention {
extention.Pack(enc)
}
return enc.GetSize() - oldSize
}
func (t *Transaction) Size() int {
initSize := 4 + 2 + 4 + 5 + 1 + 5
initSize += PackedVarUint32Length(uint32(len(t.ContextFreeActions))) // Max varint size
for _, action := range t.ContextFreeActions {
initSize += action.Size()
}
initSize += PackedVarUint32Length(uint32(len(t.Actions)))
for _, action := range t.Actions {
initSize += action.Size()
}
initSize += PackedVarUint32Length(uint32(len(t.Extention)))
for _, extention := range t.Extention {
initSize += extention.Size()
}
return initSize
}
func (t *Transaction) Unpack(data []byte) int {
dec := NewDecoder(data)
t.Expiration = dec.UnpackUint32()
t.RefBlockNum = dec.UnpackUint16()
t.RefBlockPrefix = dec.UnpackUint32()
dec.Unpack(&t.MaxNetUsageWords)
t.MaxCpuUsageMs = dec.UnpackUint8()
dec.Unpack(&t.DelaySec)
contextFreeActionLength := dec.UnpackVarUint32()
t.ContextFreeActions = make([]*Action, contextFreeActionLength)
for i := 0; i < int(contextFreeActionLength); i++ {
action := &Action{}
dec.Unpack(action)
t.ContextFreeActions[i] = action
}
actionLength := dec.UnpackVarUint32()
t.Actions = make([]*Action, actionLength)
for i := 0; i < int(actionLength); i++ {
action := &Action{}
dec.Unpack(action)
t.Actions[i] = action
}
extentionLength := dec.UnpackVarUint32()
t.Extention = make([]*TransactionExtension, extentionLength)
for i := 0; i < int(extentionLength); i++ {
extention := &TransactionExtension{}
extention.Type = dec.UnpackUint16()
extention.Data = dec.UnpackBytes()
t.Extention[i] = extention
}
return dec.Pos()
}
func (t *Transaction) Print() {
Print("{")
Print(t.Expiration, t.RefBlockNum, t.RefBlockPrefix, t.MaxNetUsageWords, t.MaxCpuUsageMs, t.DelaySec)
Print("[")
for _, a := range t.ContextFreeActions {
a.Print()
}
Print("]")
Print("[")
for _, a := range t.Actions {
a.Print()
}
Print("]")
Print("[")
for _, e := range t.Extention {
e.Print()
}
Print("]")
Print("}")
Println()
}