-
Notifications
You must be signed in to change notification settings - Fork 57
/
outgoing_tx.go
220 lines (187 loc) · 7 KB
/
outgoing_tx.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
package types
import (
"math/big"
"strings"
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"
)
var (
_ OutgoingTx = &SignerSetTx{}
_ OutgoingTx = &BatchTx{}
_ OutgoingTx = &ContractCallTx{}
)
const (
_ = iota
SignerSetTxPrefixByte
BatchTxPrefixByte
ContractCallTxPrefixByte
)
type ABIEncodedValsetArgs struct {
Validators []gethcommon.Address `abi:"validators"`
Powers []*big.Int `abi:"powers"`
Nonce *big.Int `abi:"valsetNonce"`
RewardAmount *big.Int `abi:"rewardAmount"`
RewardToken gethcommon.Address `abi:"rewardToken"`
}
///////////////////
// GetStoreIndex //
///////////////////
// TODO: do we need a prefix byte for the different types?
func (sstx *SignerSetTx) GetStoreIndex() []byte {
return MakeSignerSetTxKey(sstx.Nonce)
}
func (btx *BatchTx) GetStoreIndex() []byte {
return MakeBatchTxKey(gethcommon.HexToAddress(btx.TokenContract), btx.BatchNonce)
}
func (cctx *ContractCallTx) GetStoreIndex() []byte {
return MakeContractCallTxKey(cctx.InvalidationScope, cctx.InvalidationNonce)
}
///////////////////
// GetCheckpoint //
///////////////////
func (sstx *SignerSetTx) GetCosmosHeight() uint64 {
return sstx.Height
}
func (btx *BatchTx) GetCosmosHeight() uint64 {
return btx.Height
}
func (cctx *ContractCallTx) GetCosmosHeight() uint64 {
return cctx.Height
}
///////////////////
// GetCheckpoint //
///////////////////
// GetCheckpoint returns the checkpoint
func (u SignerSetTx) GetCheckpoint(gravityID []byte) []byte {
// 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
gravityIDFixed, err := byteArrayToFixByteArray(gravityID)
if err != nil {
panic(err)
}
checkpointBytes := []uint8("checkpoint")
var checkpoint [32]uint8
copy(checkpoint[:], checkpointBytes[:])
u.Signers.Sort()
memberAddresses := make([]gethcommon.Address, len(u.Signers))
convertedPowers := make([]*big.Int, len(u.Signers))
for i, m := range u.Signers {
memberAddresses[i] = gethcommon.HexToAddress(m.EthereumAddress)
convertedPowers[i] = big.NewInt(int64(m.Power))
}
// the word 'checkpoint' 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.
args := []interface{}{
gravityIDFixed,
checkpoint,
big.NewInt(int64(u.Nonce)),
memberAddresses,
convertedPowers,
big.NewInt(0),
gethcommon.HexToAddress("0x0000000000000000000000000000000000000000"),
}
return packCall(ValsetCheckpointABIJSON, "checkpoint", args)
}
// GetCheckpoint gets the checkpoint signature from the given outgoing tx batch
func (b BatchTx) GetCheckpoint(gravityID []byte) []byte {
// 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
gravityIDFixed, err := byteArrayToFixByteArray(gravityID)
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(b.Transactions))
txDestinations := make([]gethcommon.Address, len(b.Transactions))
txFees := make([]*big.Int, len(b.Transactions))
for i, tx := range b.Transactions {
txAmounts[i] = tx.Erc20Token.Amount.BigInt()
txDestinations[i] = gethcommon.HexToAddress(tx.EthereumRecipient)
txFees[i] = 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.
args := []interface{}{
gravityIDFixed,
batchMethodName,
txAmounts,
txDestinations,
txFees,
big.NewInt(int64(b.BatchNonce)),
gethcommon.HexToAddress(b.TokenContract),
big.NewInt(int64(b.Timeout)),
}
return packCall(OutgoingBatchTxCheckpointABIJSON, "submitBatch", args)
}
// GetCheckpoint gets the checkpoint signature from the given outgoing tx batch
func (c ContractCallTx) GetCheckpoint(gravityID []byte) []byte {
// Create the methodName argument which salts the signature
methodNameBytes := []uint8("logicCall")
var logicCallMethodName [32]uint8
copy(logicCallMethodName[:], methodNameBytes[:])
// the contract argument is not an 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
gravityIDFixed, err := byteArrayToFixByteArray(gravityID)
if err != nil {
panic(err)
}
// Run through the elements of the logic call and serialize them
transferAmounts := make([]*big.Int, len(c.Tokens))
transferTokenContracts := make([]gethcommon.Address, len(c.Tokens))
feeAmounts := make([]*big.Int, len(c.Fees))
feeTokenContracts := make([]gethcommon.Address, len(c.Fees))
for i, coin := range c.Tokens {
transferAmounts[i] = coin.Amount.BigInt()
transferTokenContracts[i] = gethcommon.HexToAddress(coin.Contract)
}
for i, coin := range c.Fees {
feeAmounts[i] = coin.Amount.BigInt()
feeTokenContracts[i] = gethcommon.HexToAddress(coin.Contract)
}
payload := make([]byte, len(c.Payload))
copy(payload, c.Payload)
var invalidationId [32]byte
copy(invalidationId[:], c.InvalidationScope[:])
// 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.
args := []interface{}{
gravityIDFixed,
logicCallMethodName,
transferAmounts,
transferTokenContracts,
feeAmounts,
feeTokenContracts,
gethcommon.HexToAddress(c.Address),
payload,
big.NewInt(int64(c.Timeout)),
invalidationId,
big.NewInt(int64(c.InvalidationNonce)),
}
return packCall(OutgoingLogicCallABIJSON, "checkpoint", args)
}
func packCall(abiString, method string, args []interface{}) []byte {
encodedCall, err := abi.JSON(strings.NewReader(abiString))
if err != nil {
panic(sdkerrors.Wrap(err, "bad ABI definition in code"))
}
abiEncodedCall, err := encodedCall.Pack(method, args...)
if err != nil {
panic(sdkerrors.Wrap(err, "packing checkpoint"))
}
return crypto.Keccak256Hash(abiEncodedCall[4:]).Bytes()
}