-
Notifications
You must be signed in to change notification settings - Fork 199
/
transaction.go
228 lines (197 loc) · 5.93 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
package transaction
import (
"encoding/json"
"io"
"math/big"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/transaction/capnp"
capn "github.com/glycerine/go-capnproto"
)
// Transaction holds all the data needed for a value transfer or SC call
type Transaction struct {
Nonce uint64 `capid:"0" json:"nonce"`
Value *big.Int `capid:"1" json:"value"`
RcvAddr []byte `capid:"2" json:"receiver"`
SndAddr []byte `capid:"3" json:"sender"`
GasPrice uint64 `capid:"4" json:"gasPrice,omitempty"`
GasLimit uint64 `capid:"5" json:"gasLimit,omitempty"`
Data string `capid:"6" json:"data,omitempty"`
Signature []byte `capid:"7" json:"signature,omitempty"`
Challenge []byte `capid:"8" json:"challenge,omitempty"`
}
// Save saves the serialized data of a Transaction into a stream through Capnp protocol
func (tx *Transaction) Save(w io.Writer) error {
seg := capn.NewBuffer(nil)
TransactionGoToCapn(seg, tx)
_, err := seg.WriteTo(w)
return err
}
// Load loads the data from the stream into a Transaction object through Capnp protocol
func (tx *Transaction) Load(r io.Reader) error {
capMsg, err := capn.ReadFromStream(r, nil)
if err != nil {
return err
}
z := capnp.ReadRootTransactionCapn(capMsg)
TransactionCapnToGo(z, tx)
return nil
}
// TransactionCapnToGo is a helper function to copy fields from a TransactionCapn object to a Transaction object
func TransactionCapnToGo(src capnp.TransactionCapn, dest *Transaction) *Transaction {
if dest == nil {
dest = &Transaction{}
}
if dest.Value == nil {
dest.Value = big.NewInt(0)
}
// Nonce
dest.Nonce = src.Nonce()
// Value
err := dest.Value.GobDecode(src.Value())
if err != nil {
return nil
}
// RcvAddr
dest.RcvAddr = src.RcvAddr()
// SndAddr
dest.SndAddr = src.SndAddr()
// GasPrice
dest.GasPrice = src.GasPrice()
// GasLimit
dest.GasLimit = src.GasLimit()
// Data
dest.Data = src.Data()
// Signature
dest.Signature = src.Signature()
// Challenge
dest.Challenge = src.Challenge()
return dest
}
// TransactionGoToCapn is a helper function to copy fields from a Transaction object to a TransactionCapn object
func TransactionGoToCapn(seg *capn.Segment, src *Transaction) capnp.TransactionCapn {
dest := capnp.AutoNewTransactionCapn(seg)
value, _ := src.Value.GobEncode()
dest.SetNonce(src.Nonce)
dest.SetValue(value)
dest.SetRcvAddr(src.RcvAddr)
dest.SetSndAddr(src.SndAddr)
dest.SetGasPrice(src.GasPrice)
dest.SetGasLimit(src.GasLimit)
dest.SetData(src.Data)
dest.SetSignature(src.Signature)
dest.SetChallenge(src.Challenge)
return dest
}
// IsInterfaceNil verifies if underlying object is nil
func (tx *Transaction) IsInterfaceNil() bool {
return tx == nil
}
// GetValue returns the value of the transaction
func (tx *Transaction) GetValue() *big.Int {
return tx.Value
}
// GetNonce returns the transaction nonce
func (tx *Transaction) GetNonce() uint64 {
return tx.Nonce
}
// GetData returns the data of the transaction
func (tx *Transaction) GetData() string {
return tx.Data
}
// GetRecvAddress returns the receiver address from the transaction
func (tx *Transaction) GetRecvAddress() []byte {
return tx.RcvAddr
}
// GetSndAddress returns the sender address from the transaction
func (tx *Transaction) GetSndAddress() []byte {
return tx.SndAddr
}
// GetGasLimit returns the gas limit of the transaction
func (tx *Transaction) GetGasLimit() uint64 {
return tx.GasLimit
}
// GetGasPrice returns the gas price of the transaction
func (tx *Transaction) GetGasPrice() uint64 {
return tx.GasPrice
}
// SetValue sets the value of the transaction
func (tx *Transaction) SetValue(value *big.Int) {
tx.Value = value
}
// SetData sets the data of the transaction
func (tx *Transaction) SetData(data string) {
tx.Data = data
}
// SetRecvAddress sets the receiver address of the transaction
func (tx *Transaction) SetRecvAddress(addr []byte) {
tx.RcvAddr = addr
}
// SetSndAddress sets the sender address of the transaction
func (tx *Transaction) SetSndAddress(addr []byte) {
tx.SndAddr = addr
}
// MarshalJSON converts the Transaction data type into its corresponding equivalent in byte slice.
// Note that Value data type is converted in a string
func (tx *Transaction) MarshalJSON() ([]byte, error) {
valAsString := "nil"
if tx.Value != nil {
valAsString = tx.Value.String()
}
return json.Marshal(&struct {
Nonce uint64 `json:"nonce"`
Value string `json:"value"`
RcvAddr []byte `json:"receiver"`
SndAddr []byte `json:"sender"`
GasPrice uint64 `json:"gasPrice,omitempty"`
GasLimit uint64 `json:"gasLimit,omitempty"`
Data string `json:"data,omitempty"`
Signature []byte `json:"signature,omitempty"`
}{
Nonce: tx.Nonce,
Value: valAsString,
RcvAddr: tx.RcvAddr,
SndAddr: tx.SndAddr,
GasPrice: tx.GasPrice,
GasLimit: tx.GasLimit,
Data: tx.Data,
Signature: tx.Signature,
})
}
// UnmarshalJSON converts the provided bytes into a Transaction data type.
func (tx *Transaction) UnmarshalJSON(dataBuff []byte) error {
aux := &struct {
Nonce uint64 `json:"nonce"`
Value string `json:"value"`
RcvAddr []byte `json:"receiver"`
SndAddr []byte `json:"sender"`
GasPrice uint64 `json:"gasPrice,omitempty"`
GasLimit uint64 `json:"gasLimit,omitempty"`
Data string `json:"data,omitempty"`
Signature []byte `json:"signature,omitempty"`
}{}
if err := json.Unmarshal(dataBuff, &aux); err != nil {
return err
}
tx.Nonce = aux.Nonce
tx.RcvAddr = aux.RcvAddr
tx.SndAddr = aux.SndAddr
tx.GasPrice = aux.GasPrice
tx.GasLimit = aux.GasLimit
tx.Data = aux.Data
tx.Signature = aux.Signature
var ok bool
tx.Value, ok = big.NewInt(0).SetString(aux.Value, 10)
if !ok {
return data.ErrInvalidValue
}
return nil
}
// TrimSlicePtr creates a copy of the provided slice without the excess capacity
func TrimSlicePtr(in []*Transaction) []*Transaction {
if len(in) == 0 {
return []*Transaction{}
}
ret := make([]*Transaction, len(in))
copy(ret, in)
return ret
}