-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathante_test.go
296 lines (251 loc) · 9.7 KB
/
ante_test.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
package auth
import (
"crypto/ecdsa"
"encoding/binary"
"fmt"
types "github.com/FourthState/plasma-mvp-sidechain/types"
utils "github.com/FourthState/plasma-mvp-sidechain/utils"
"github.com/FourthState/plasma-mvp-sidechain/x/kvstore"
"github.com/FourthState/plasma-mvp-sidechain/x/utxo"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/log"
"testing"
)
func setup() (sdk.Context, utxo.Mapper, kvstore.KVStore, utxo.FeeUpdater) {
ms, capKey, plasmaCapKey := utxo.SetupMultiStore()
ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewNopLogger())
cdc := utxo.MakeCodec()
types.RegisterAmino(cdc)
mapper := utxo.NewBaseMapper(capKey, cdc)
plasmaStore := kvstore.NewKVStore(plasmaCapKey)
return ctx, mapper, plasmaStore, feeUpdater
}
// should be modified when fees are implemented
func feeUpdater(outputs []utxo.Output) sdk.Error {
return nil
}
func GenSpendMsg() types.SpendMsg {
// Creates Basic Spend Msg with owners and recipients
var confirmSigs [][65]byte
privKeyA, _ := ethcrypto.GenerateKey()
privKeyB, _ := ethcrypto.GenerateKey()
return types.SpendMsg{
Blknum0: 1,
Txindex0: 0,
Oindex0: 0,
DepositNum0: 0,
Owner0: utils.PrivKeyToAddress(privKeyA),
Input0ConfirmSigs: confirmSigs,
Blknum1: 1,
Txindex1: 1,
Oindex1: 0,
DepositNum1: 0,
Owner1: utils.PrivKeyToAddress(privKeyA),
Input1ConfirmSigs: confirmSigs,
Newowner0: utils.PrivKeyToAddress(privKeyB),
Amount0: 150,
Newowner1: utils.PrivKeyToAddress(privKeyB),
Amount1: 50,
FeeAmount: 0,
}
}
// Returns a confirmsig array signed by privKey0 and privKey1
func CreateConfirmSig(hash []byte, privKey0, privKey1 *ecdsa.PrivateKey, two_inputs bool) (confirmSigs [][65]byte) {
var confirmSig0 [65]byte
signHash := utils.SignHash(hash)
confirmSig0Slice, _ := ethcrypto.Sign(signHash, privKey0)
copy(confirmSig0[:], confirmSig0Slice)
if !two_inputs {
return [][65]byte{confirmSig0}
}
var confirmSig1 [65]byte
confirmSig1Slice, _ := ethcrypto.Sign(signHash, privKey1)
copy(confirmSig1[:], confirmSig1Slice)
return [][65]byte{confirmSig0, confirmSig1}
}
// helper for constructing single or double input tx
func GetTx(msg types.SpendMsg, privKey0, privKey1 *ecdsa.PrivateKey, two_sigs bool) (tx types.BaseTx) {
hash := ethcrypto.Keccak256(msg.GetSignBytes())
signHash := utils.SignHash(hash)
sig0, _ := ethcrypto.Sign(signHash, privKey0)
var sigs [2][65]byte
copy(sigs[0][:], sig0)
if two_sigs {
sig1, _ := ethcrypto.Sign(signHash, privKey1)
copy(sigs[1][:], sig1)
}
tx = types.NewBaseTx(msg, sigs)
return tx
}
// helper for constructing input addresses
func getInputAddr(addr0, addr1 common.Address, two bool) [][]byte {
if two {
return [][]byte{addr0.Bytes(), addr1.Bytes()}
} else {
return [][]byte{addr0.Bytes()}
}
}
// No signatures are provided
func TestNoSigs(t *testing.T) {
ctx, mapper, plasmaStore, feeUpdater := setup()
var msg = GenSpendMsg()
var emptysigs [2][65]byte
tx := types.NewBaseTx(msg, emptysigs)
// Add input UTXOs to mapper
utxo1 := utxo.NewUTXO(msg.Owner0.Bytes(), 100, types.Denom, types.NewPlasmaPosition(1, 0, 0, 0))
utxo2 := utxo.NewUTXO(msg.Owner0.Bytes(), 100, types.Denom, types.NewPlasmaPosition(1, 1, 0, 0))
mapper.ReceiveUTXO(ctx, utxo1)
mapper.ReceiveUTXO(ctx, utxo2)
handler := NewAnteHandler(mapper, plasmaStore, feeUpdater, nil)
_, res, abort := handler(ctx, tx, false)
assert.Equal(t, true, abort, "did not abort with no signatures")
require.Equal(t, sdk.ToABCICode(sdk.CodespaceType(1), sdk.CodeType(4)), res.Code, fmt.Sprintf("tx had processed with no signatures: %s", res.Log))
}
// The wrong amount of signatures are provided
func TestNotEnoughSigs(t *testing.T) {
ctx, mapper, plasmaStore, feeUpdater := setup()
var msg = GenSpendMsg()
priv, _ := ethcrypto.GenerateKey()
hash := ethcrypto.Keccak256(msg.GetSignBytes())
sig, _ := ethcrypto.Sign(hash, priv)
var sigs [2][65]byte
copy(sigs[0][:], sig)
tx := types.NewBaseTx(msg, sigs)
// Add input UTXOs to mapper
utxo1 := utxo.NewUTXO(msg.Owner0.Bytes(), 100, types.Denom, types.NewPlasmaPosition(1, 0, 0, 0))
utxo2 := utxo.NewUTXO(msg.Owner0.Bytes(), 100, types.Denom, types.NewPlasmaPosition(1, 1, 0, 0))
mapper.ReceiveUTXO(ctx, utxo1)
mapper.ReceiveUTXO(ctx, utxo2)
handler := NewAnteHandler(mapper, plasmaStore, feeUpdater, nil)
_, res, abort := handler(ctx, tx, false)
assert.Equal(t, true, abort, "did not abort with incorrect number of signatures")
require.Equal(t, sdk.ToABCICode(sdk.CodespaceType(1), sdk.CodeType(4)), res.Code, fmt.Sprintf("tx had processed with incorrect number of signatures: %s", res.Log))
}
// helper struct for readability
type input struct {
owner_index int64
addr common.Address
position types.PlasmaPosition
input_index0 int64
input_index1 int64
}
// Tests a different cases.
func TestDifferentCases(t *testing.T) {
ctx, mapper, plasmaStore, feeUpdater := setup()
var keys [6]*ecdsa.PrivateKey
var addrs []common.Address
for i := 0; i < 6; i++ {
keys[i], _ = ethcrypto.GenerateKey()
addrs = append(addrs, utils.PrivKeyToAddress(keys[i]))
}
cases := []struct {
input0 input
input1 input
newowner0 common.Address
amount0 uint64
newowner1 common.Address
amount1 uint64
abort bool
}{
// Test Case 0: Tx signed by the wrong address
{
input{1, addrs[0], types.NewPlasmaPosition(2, 0, 0, 0), 1, -1}, // first input
input{-1, common.Address{}, types.PlasmaPosition{}, -1, -1}, // second input
addrs[1], 1000, // first output
addrs[2], 1000, // second output
true,
},
// Test Case 1: Inputs != Outputs + Fee
{
input{0, addrs[0], types.NewPlasmaPosition(3, 0, 0, 0), 1, -1},
input{-1, common.Address{}, types.PlasmaPosition{}, -1, -1},
addrs[1], 2000,
addrs[2], 1000,
true,
},
// Test Case 2: 1 input 2 output
{
input{0, addrs[0], types.NewPlasmaPosition(4, 0, 0, 0), 1, -1},
input{-1, common.Address{}, types.PlasmaPosition{}, -1, -1},
addrs[1], 1000,
addrs[2], 1000,
false,
},
// Test Case 3: 2 input 2 output
{
input{1, addrs[1], types.NewPlasmaPosition(5, 0, 0, 0), 0, -1},
input{2, addrs[2], types.NewPlasmaPosition(5, 0, 1, 0), 0, -1},
addrs[3], 2500,
addrs[4], 1500,
false,
},
}
for index, tc := range cases {
input0_index1 := utils.GetIndex(tc.input0.input_index1)
input1_index0 := utils.GetIndex(tc.input1.input_index0)
input1_index1 := utils.GetIndex(tc.input1.input_index1)
// for ease of testing, blockHash is hash of case number
blockHash := tmhash.Sum([]byte(string(index)))
var msg = types.SpendMsg{
Blknum0: tc.input0.position.Blknum,
Txindex0: tc.input0.position.TxIndex,
Oindex0: tc.input0.position.Oindex,
DepositNum0: tc.input0.position.DepositNum,
Owner0: tc.input0.addr,
Blknum1: tc.input1.position.Blknum,
Txindex1: tc.input1.position.TxIndex,
Oindex1: tc.input1.position.Oindex,
DepositNum1: tc.input1.position.DepositNum,
Owner1: tc.input1.addr,
Newowner0: tc.newowner0,
Amount0: tc.amount0,
Newowner1: tc.newowner1,
Amount1: tc.amount1 - 5,
FeeAmount: 5,
}
owner_index1 := utils.GetIndex(tc.input1.owner_index)
tx := GetTx(msg, keys[tc.input0.owner_index], keys[owner_index1], tc.input1.owner_index != -1)
handler := NewAnteHandler(mapper, plasmaStore, feeUpdater, nil)
_, res, abort := handler(ctx, tx, false)
assert.Equal(t, true, abort, fmt.Sprintf("did not abort on utxo that does not exist. Case: %d", index))
require.Equal(t, sdk.ToABCICode(sdk.CodespaceType(1), sdk.CodeType(6)), res.Code, res.Log)
inputAddr := getInputAddr(addrs[tc.input0.input_index0], addrs[input0_index1], tc.input0.input_index1 != -1)
txhash0 := tmhash.Sum([]byte("first utxo"))
utxo0 := utxo.NewUTXOwithInputs(tc.input0.addr.Bytes(), 2000, types.Denom, tc.input0.position, txhash0, inputAddr)
var utxo1 utxo.UTXO
var txhash1 []byte
if tc.input1.owner_index != -1 {
txhash1 = tmhash.Sum([]byte("second utxo"))
inputAddr = getInputAddr(addrs[input1_index0], addrs[input1_index1], tc.input0.input_index1 != -1)
utxo1 = utxo.NewUTXOwithInputs(tc.input1.addr.Bytes(), 2000, types.Denom, tc.input1.position, txhash1, inputAddr)
}
blknumKey := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(blknumKey, tc.input0.position.Get()[0].Uint64())
key := append(utils.RootHashPrefix, blknumKey...)
plasmaStore.Set(ctx, key, blockHash)
// for ease of testing, txhash is simplified
// app_test tests for correct functionality when setting tx_hash
mapper.ReceiveUTXO(ctx, utxo0)
hash := tmhash.Sum(append(txhash0, blockHash...))
msg.Input0ConfirmSigs = CreateConfirmSig(hash, keys[tc.input0.input_index0], keys[input0_index1], tc.input0.input_index1 != -1)
if tc.input1.owner_index != -1 {
hash = tmhash.Sum(append(txhash1, blockHash...))
mapper.ReceiveUTXO(ctx, utxo1)
msg.Input1ConfirmSigs = CreateConfirmSig(hash, keys[input1_index0], keys[input1_index1], tc.input1.input_index1 != -1)
}
tx = GetTx(msg, keys[tc.input0.owner_index], keys[owner_index1], tc.input1.owner_index != -1)
_, res, abort = handler(ctx, tx, false)
assert.Equal(t, tc.abort, abort, fmt.Sprintf("aborted on case: %d", index))
if tc.abort == false {
require.Equal(t, sdk.ToABCICode(sdk.CodespaceType(1), sdk.CodeType(0)), res.Code, res.Log)
} else {
require.NotEqual(t, sdk.ToABCICode(sdk.CodespaceType(1), sdk.CodeType(0)), res.Code, res.Log)
}
}
}