/
batch.go
328 lines (288 loc) · 9.96 KB
/
batch.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package types
import (
"fmt"
"math/big"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/accounts/abi"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
func (o OutgoingTransferTx) ToInternal() (*InternalOutgoingTransferTx, error) {
return NewInternalOutgoingTransferTx(o.Id, o.Sender, o.DestAddress, o.Erc20Token, o.Erc20Fee)
}
// InternalOutgoingTransferTx is an internal duplicate of OutgoingTransferTx with validation
type InternalOutgoingTransferTx struct {
Id uint64
Sender sdk.AccAddress
DestAddress *EthAddress
Erc20Token *InternalERC20Token
Erc20Fee *InternalERC20Token
}
func NewInternalOutgoingTransferTx(
id uint64,
sender string,
destAddress string,
erc20Token ERC20Token,
erc20Fee ERC20Token,
) (*InternalOutgoingTransferTx, error) {
send, err := sdk.AccAddressFromBech32(sender)
if err != nil {
return nil, sdkerrors.Wrap(err, "invalid sender")
}
dest, err := NewEthAddress(destAddress)
if err != nil {
return nil, sdkerrors.Wrap(err, "invalid eth destination")
}
token, err := erc20Token.ToInternal()
if err != nil {
return nil, sdkerrors.Wrap(err, "invalid Erc20Token")
}
fee, err := erc20Fee.ToInternal()
if err != nil {
return nil, sdkerrors.Wrap(err, "invalid Erc20Fee")
}
return &InternalOutgoingTransferTx{
Id: id,
Sender: send,
DestAddress: dest,
Erc20Token: token,
Erc20Fee: fee,
}, nil
}
func (i InternalOutgoingTransferTx) ToExternal() OutgoingTransferTx {
return OutgoingTransferTx{
Id: i.Id,
Sender: i.Sender.String(),
DestAddress: i.DestAddress.GetAddress().Hex(),
Erc20Token: i.Erc20Token.ToExternal(),
Erc20Fee: i.Erc20Fee.ToExternal(),
}
}
func (i InternalOutgoingTransferTx) ValidateBasic() error {
//TODO: Validate id?
//TODO: Validate cosmos sender?
err := i.DestAddress.ValidateBasic()
if err != nil {
return sdkerrors.Wrap(err, "invalid DestAddress")
}
err = i.Erc20Token.ValidateBasic()
if err != nil {
return sdkerrors.Wrap(err, "invalid Erc20Token")
}
err = i.Erc20Fee.ValidateBasic()
if err != nil {
return sdkerrors.Wrap(err, "invalid Erc20Fee")
}
return nil
}
// InternalOutgoingTxBatch is an internal duplicate array of OutgoingTxBatch with validation
type InternalOutgoingTxBatches []InternalOutgoingTxBatch
// InternalOutgoingTxBatch is an internal duplicate of OutgoingTxBatch with validation
type InternalOutgoingTxBatch struct {
BatchNonce uint64
BatchTimeout uint64
Transactions []*InternalOutgoingTransferTx
TokenContract EthAddress
EthBlock uint64
}
func NewInternalOutgingTxBatch(
nonce uint64,
timeout uint64,
transactions []*InternalOutgoingTransferTx,
contract EthAddress,
block uint64) (*InternalOutgoingTxBatch, error) {
ret := &InternalOutgoingTxBatch{
BatchNonce: nonce,
BatchTimeout: timeout,
Transactions: transactions,
TokenContract: contract,
EthBlock: block,
}
if err := ret.ValidateBasic(); err != nil {
return nil, err
}
return ret, nil
}
func NewInternalOutgingTxBatchFromExternalBatch(batch OutgoingTxBatch) (*InternalOutgoingTxBatch, error) {
contractAddr, err := NewEthAddress(batch.TokenContract)
if err != nil {
return nil, sdkerrors.Wrap(err, "invalid eth address")
}
txs := make([]*InternalOutgoingTransferTx, len(batch.Transactions))
for i, tx := range batch.Transactions {
intTx, err := tx.ToInternal()
if err != nil {
return nil, sdkerrors.Wrapf(err, "invalid transaction in batch: %v", tx)
}
txs[i] = intTx
}
return &InternalOutgoingTxBatch{
BatchNonce: batch.BatchNonce,
BatchTimeout: batch.BatchTimeout,
Transactions: txs,
TokenContract: *contractAddr,
EthBlock: batch.EthBlock,
}, nil
}
func (o *OutgoingTxBatch) ToInternal() (*InternalOutgoingTxBatch, error) {
return NewInternalOutgingTxBatchFromExternalBatch(*o)
}
func (i *InternalOutgoingTxBatch) ToExternal() OutgoingTxBatch {
txs := make([]OutgoingTransferTx, len(i.Transactions))
for i, tx := range i.Transactions {
txs[i] = tx.ToExternal()
}
return OutgoingTxBatch{
BatchNonce: i.BatchNonce,
BatchTimeout: i.BatchTimeout,
Transactions: txs,
TokenContract: i.TokenContract.GetAddress().Hex(),
EthBlock: i.EthBlock,
}
}
func (i *InternalOutgoingTxBatches) ToExternalArray() []OutgoingTxBatch {
var arr []OutgoingTxBatch
for index, val := range *i {
txs := make([]OutgoingTransferTx, len((*i)[index].Transactions))
for i, tx := range (*i)[index].Transactions {
txs[i] = tx.ToExternal()
}
arr = append(arr, OutgoingTxBatch{
BatchNonce: val.BatchNonce,
BatchTimeout: val.BatchTimeout,
Transactions: txs,
TokenContract: val.TokenContract.GetAddress().Hex(),
EthBlock: val.EthBlock,
})
}
return arr
}
func (i *InternalOutgoingTxBatch) ValidateBasic() error {
if err := i.TokenContract.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "invalid eth address")
}
for i, tx := range i.Transactions {
if err := tx.ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "transaction %d is invalid", i)
}
}
return nil
}
// Required for EthereumSigned interface
func (o OutgoingTxBatch) GetCheckpoint(gravityIDstring string) []byte {
i, err := o.ToInternal()
if err != nil {
panic(sdkerrors.Wrap(err, "invalid OutgoingTxBatch"))
}
return i.GetCheckpoint(gravityIDstring)
}
// GetCheckpoint gets the checkpoint signature from the given outgoing tx batch
func (i InternalOutgoingTxBatch) GetCheckpoint(gravityIDstring string) []byte {
abi, err := abi.JSON(strings.NewReader(OutgoingBatchTxCheckpointABIJSON))
if err != nil {
panic("Bad ABI constant!")
}
// the contract argument is not a arbitrary length array but a fixed length 32 byte
// array, therefore we have to utf8 encode the string (the default in this case) and
// then copy the variable length encoded data into a fixed length array. This function
// will panic if gravityId is too long to fit in 32 bytes
gravityID, err := strToFixByteArray(gravityIDstring)
if err != nil {
panic(err)
}
// Create the methodName argument which salts the signature
methodNameBytes := []uint8("transactionBatch")
var batchMethodName [32]uint8
copy(batchMethodName[:], methodNameBytes)
// Run through the elements of the batch and serialize them
txAmounts := make([]*big.Int, len(i.Transactions))
txDestinations := make([]gethcommon.Address, len(i.Transactions))
txFees := make([]*big.Int, len(i.Transactions))
for j, tx := range i.Transactions {
txAmounts[j] = tx.Erc20Token.Amount.BigInt()
txDestinations[j] = tx.DestAddress.GetAddress()
txFees[j] = tx.Erc20Fee.Amount.BigInt()
}
// the methodName needs to be the same as the 'name' above in the checkpointAbiJson
// but other than that it's a constant that has no impact on the output. This is because
// it gets encoded as a function name which we must then discard.
abiEncodedBatch, err := abi.Pack("submitBatch",
gravityID,
batchMethodName,
txAmounts,
txDestinations,
txFees,
big.NewInt(int64(i.BatchNonce)),
i.TokenContract.GetAddress(),
big.NewInt(int64(i.BatchTimeout)),
)
// this should never happen outside of test since any case that could crash on encoding
// should be filtered above.
if err != nil {
panic(fmt.Sprintf("Error packing checkpoint! %s/n", err))
}
// we hash the resulting encoded bytes discarding the first 4 bytes these 4 bytes are the constant
// method name 'checkpoint'. If you were to replace the checkpoint constant in this code you would
// then need to adjust how many bytes you truncate off the front to get the output of abi.encode()
return crypto.Keccak256Hash(abiEncodedBatch[4:]).Bytes()
}
// GetCheckpoint gets the checkpoint signature from the given outgoing tx batch
func (c OutgoingLogicCall) GetCheckpoint(gravityIDstring string) []byte {
abi, err := abi.JSON(strings.NewReader(OutgoingLogicCallABIJSON))
if err != nil {
panic("Bad ABI constant!")
}
// Create the methodName argument which salts the signature
methodNameBytes := []uint8("logicCall")
var logicCallMethodName [32]uint8
copy(logicCallMethodName[:], methodNameBytes)
// the contract argument is not a arbitrary length array but a fixed length 32 byte
// array, therefore we have to utf8 encode the string (the default in this case) and
// then copy the variable length encoded data into a fixed length array. This function
// will panic if gravityId is too long to fit in 32 bytes
gravityID, err := strToFixByteArray(gravityIDstring)
if err != nil {
panic(err)
}
// Run through the elements of the logic call and serialize them
transferAmounts := make([]*big.Int, len(c.Transfers))
transferTokenContracts := make([]gethcommon.Address, len(c.Transfers))
feeAmounts := make([]*big.Int, len(c.Fees))
feeTokenContracts := make([]gethcommon.Address, len(c.Fees))
for i, tx := range c.Transfers {
transferAmounts[i] = tx.Amount.BigInt()
transferTokenContracts[i] = gethcommon.HexToAddress(tx.Contract)
}
for i, tx := range c.Fees {
feeAmounts[i] = tx.Amount.BigInt()
feeTokenContracts[i] = gethcommon.HexToAddress(tx.Contract)
}
payload := make([]byte, len(c.Payload))
copy(payload, c.Payload)
var invalidationId [32]byte
copy(invalidationId[:], c.InvalidationId)
// the methodName needs to be the same as the 'name' above in the checkpointAbiJson
// but other than that it's a constant that has no impact on the output. This is because
// it gets encoded as a function name which we must then discard.
abiEncodedCall, err := abi.Pack("checkpoint",
gravityID,
logicCallMethodName,
transferAmounts,
transferTokenContracts,
feeAmounts,
feeTokenContracts,
gethcommon.HexToAddress(c.LogicContractAddress),
payload,
big.NewInt(int64(c.Timeout)),
invalidationId,
big.NewInt(int64(c.InvalidationNonce)),
)
// this should never happen outside of test since any case that could crash on encoding
// should be filtered above.
if err != nil {
panic(fmt.Sprintf("Error packing checkpoint! %s/n", err))
}
return crypto.Keccak256Hash(abiEncodedCall[4:]).Bytes()
}