-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
344 lines (306 loc) · 11.3 KB
/
main_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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
Copyright (C) 2018 WeTrustPlatform
This file is part of poa-interchain-node.
poa-interchain-node is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
poa-interchain-node is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with poa-interchain-node. If not, see <http://www.gnu.org/licenses/>.
*/
package icn
import (
"context"
"math/big"
"os"
"reflect"
"sync"
"testing"
"github.com/WeTrustPlatform/poa-interchain-node/bind/mainchain"
"github.com/WeTrustPlatform/poa-interchain-node/bind/sidechain"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
)
func TestMsgHash(t *testing.T) {
type args struct {
contractAddress common.Address
txHash common.Hash
toAddress common.Address
value *big.Int
data []byte
version uint8
}
tests := []struct {
name string
args args
want common.Hash
}{
{
name: "Computes solidity compatible hash",
args: args{
common.HexToAddress("75076e4fbba61f65efb41d64e45cff340b1e518a"),
common.HexToHash("03c85f1da84d9c6313e0c34bcb5ace945a9b12105988895252b88ce5b769f82b"),
common.HexToAddress("f17f52151ebef6c7334fad080c5704d77216b732"),
big.NewInt(100000000),
[]byte{},
1,
},
want: common.HexToHash("6b0673bcb3726c0f7956ef57a9542ed225bfe74f1d2a75414d198d55e8956da5"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MsgHash(tt.args.contractAddress, tt.args.txHash, tt.args.toAddress, tt.args.value, tt.args.data, tt.args.version); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MsgHash() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseSignature(t *testing.T) {
type args struct {
sig []byte
}
tests := []struct {
name string
args args
wantV uint8
wantR common.Hash
wantS common.Hash
}{
{
name: "Parses signature correctly",
args: args{
sig: common.Hex2Bytes("a27a17b20a8dcc6fedb6196b84624ce3f3961a2423642fe13003a816c383f93205adf64e0805449d18b866991ce19e5439567cd3613ae1775e90fb4a8b0cbc6800"),
},
wantV: 27,
wantR: common.HexToHash("a27a17b20a8dcc6fedb6196b84624ce3f3961a2423642fe13003a816c383f932"),
wantS: common.HexToHash("05adf64e0805449d18b866991ce19e5439567cd3613ae1775e90fb4a8b0cbc68"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotV, gotR, gotS := ParseSignature(tt.args.sig)
if gotV != tt.wantV {
t.Errorf("ParseSignature() gotV = %v, want %v", gotV, tt.wantV)
}
if !reflect.DeepEqual(gotR, tt.wantR) {
t.Errorf("ParseSignature() gotR = %v, want %v", gotR, tt.wantR)
}
if !reflect.DeepEqual(gotS, tt.wantS) {
t.Errorf("ParseSignature() gotS = %v, want %v", gotS, tt.wantS)
}
})
}
}
func TestSign(t *testing.T) {
key2, _ := crypto.HexToECDSA("148435bc1bc5ee5ab6f57745625d6c3e15e99b335f29ba75a8542546fd2e2dc4")
gotV, gotR, gotS, _ := Sign(common.HexToHash("0x6b0673bcb3726c0f7956ef57a9542ed225bfe74f1d2a75414d198d55e8956da5"), key2)
wantV := uint8(28)
wantR := common.HexToHash("1ad083863dffca2c52befbe45984236f58721430c613da5653a6c0990619b892")
wantS := common.HexToHash("27d9b8b7094cbd42d6c30a3c7ed840531b744abbf7f95ab294efe399eb394797")
if gotV != wantV {
t.Errorf("Sign() gotV = %v, want %v", gotV, wantV)
}
if gotR != wantR {
t.Errorf("Sign() gotR = %v, want %v", gotR, wantR)
}
if gotS != wantS {
t.Errorf("Sign() gotS = %v, want %v", gotS, wantS)
}
}
func TestEndBlock(t *testing.T) {
var sum uint64 = 130
type args struct {
start uint64
nblocks uint64
}
tests := []struct {
name string
args args
want *uint64
}{
{
name: "Returns nil if nblock is 0",
args: args{start: 0, nblocks: 0},
want: nil,
},
{
name: "Returns a pointer to the sum if nblocks is positive",
args: args{start: 30, nblocks: 100},
want: &sum,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := EndBlock(tt.args.start, tt.args.nblocks); !reflect.DeepEqual(got, tt.want) {
t.Errorf("EndBlock() = %v, want %v", got, tt.want)
}
})
}
}
func TestMainChainToSideChain(t *testing.T) {
ctx := context.Background()
key0, _ := crypto.GenerateKey()
miner := bind.NewKeyedTransactor(key0)
sealer1Key, _ := crypto.GenerateKey()
sealer1 := bind.NewKeyedTransactor(sealer1Key)
sealer1Auth := bind.NewKeyedTransactor(sealer1Key)
sealer2Key, _ := crypto.GenerateKey()
sealer2 := bind.NewKeyedTransactor(sealer2Key)
sealer2Auth := bind.NewKeyedTransactor(sealer2Key)
tester1Key, _ := crypto.GenerateKey()
tester1 := bind.NewKeyedTransactor(tester1Key)
tester2Key, _ := crypto.GenerateKey()
tester2 := bind.NewKeyedTransactor(tester2Key)
scAddr := crypto.CreateAddress(sealer1.From, 0)
mcAddr := crypto.CreateAddress(sealer2.From, 0)
scClient := backends.NewSimulatedBackend(core.GenesisAlloc{
scAddr: core.GenesisAccount{Balance: big.NewInt(50000000000)},
sealer1.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
sealer2.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
tester1.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
})
mcClient := backends.NewSimulatedBackend(core.GenesisAlloc{
mcAddr: core.GenesisAccount{Balance: big.NewInt(50000000000)},
miner.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
sealer1.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
sealer2.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
tester2.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
})
_, _, sc, _ := sidechain.DeploySideChain(sealer1, scClient, []common.Address{sealer1.From, sealer2.From}, 2)
_, _, mc, _ := mainchain.DeployMainChain(sealer2, mcClient, []common.Address{sealer1.From, sealer2.From}, 2)
tester2.Value = big.NewInt(200000000)
tx, _ := mc.Deposit(tester2, tester1.From)
mcClient.Commit()
var wg sync.WaitGroup
wg.Add(2)
go ProcessMCDeposits(ctx, sealer1Auth, mc, sc, os.TempDir(), 0, nil, &wg)
go ProcessMCDeposits(ctx, sealer2Auth, mc, sc, os.TempDir(), 0, nil, &wg)
wg.Wait()
scClient.Commit()
t.Run("Transaction is confirmed after the number of required votes on the SC is reached", func(t *testing.T) {
confirmed, _ := sc.IsConfirmed(&bind.CallOpts{
Context: ctx,
Pending: false,
From: sealer1.From,
}, tx.Hash())
if !confirmed {
t.Errorf("confirmed = %v, want %v", confirmed, true)
}
})
t.Run("Sender has been debited on the mainchain", func(t *testing.T) {
have, _ := mcClient.BalanceAt(ctx, tester2.From, nil)
want := big.NewInt(10000000000 - 200000000 - int64(tx.Gas()))
if !reflect.DeepEqual(have, want) {
t.Errorf("have = %v, want %v", have, want)
}
})
t.Run("Recipient has been credited on the sidechain", func(t *testing.T) {
have, _ := scClient.BalanceAt(ctx, tester1.From, nil)
want := big.NewInt(10000000000 + 200000000)
if !reflect.DeepEqual(have, want) {
t.Errorf("have = %v, want %v", have, want)
}
})
t.Run("Main chain smart contract has been credited", func(t *testing.T) {
have, _ := mcClient.BalanceAt(ctx, mcAddr, nil)
want := big.NewInt(50000000000 + 200000000)
if !reflect.DeepEqual(have, want) {
t.Errorf("have = %v, want %v", have, want)
}
})
t.Run("Side chain smart contract has been debited", func(t *testing.T) {
have, _ := scClient.BalanceAt(ctx, scAddr, nil)
want := big.NewInt(50000000000 - 200000000)
if !reflect.DeepEqual(have, want) {
t.Errorf("have = %v, want %v", have, want)
}
})
}
func TestSideChainToMainChain(t *testing.T) {
ctx := context.Background()
minerKey, _ := crypto.GenerateKey()
miner := bind.NewKeyedTransactor(minerKey)
sealer1Key, _ := crypto.GenerateKey()
sealer1 := bind.NewKeyedTransactor(sealer1Key)
sealer1Auth := bind.NewKeyedTransactor(sealer1Key)
sealer2Key, _ := crypto.GenerateKey()
sealer2 := bind.NewKeyedTransactor(sealer2Key)
sealer2Auth := bind.NewKeyedTransactor(sealer2Key)
tester1Key, _ := crypto.GenerateKey()
tester1 := bind.NewKeyedTransactor(tester1Key)
tester2Key, _ := crypto.GenerateKey()
tester2 := bind.NewKeyedTransactor(tester2Key)
scAddr := crypto.CreateAddress(sealer1.From, 0)
mcAddr := crypto.CreateAddress(sealer2.From, 0)
scClient := backends.NewSimulatedBackend(core.GenesisAlloc{
scAddr: core.GenesisAccount{Balance: big.NewInt(50000000000)},
sealer1.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
sealer2.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
tester1.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
})
mcClient := backends.NewSimulatedBackend(core.GenesisAlloc{
mcAddr: core.GenesisAccount{Balance: big.NewInt(50000000000)},
miner.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
sealer1.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
sealer2.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
tester2.From: core.GenesisAccount{Balance: big.NewInt(10000000000)},
})
_, _, sc, _ := sidechain.DeploySideChain(sealer1, scClient, []common.Address{sealer1.From, sealer2.From}, 2)
_, _, mc, _ := mainchain.DeployMainChain(sealer2, mcClient, []common.Address{sealer1.From, sealer2.From}, 2)
tester1.Value = big.NewInt(200000000)
tx, _ := sc.Deposit(tester1, tester2.From)
scClient.Commit()
var wg sync.WaitGroup
wg.Add(2)
go ProcessSCDeposits(ctx, sealer1Auth, mc, sc, scAddr, sealer1Key, os.TempDir(), 0, nil, &wg)
go ProcessSCDeposits(ctx, sealer2Auth, mc, sc, scAddr, sealer2Key, os.TempDir(), 0, nil, &wg)
wg.Wait()
scClient.Commit()
t.Run("HasEnoughSignaturesMC", func(t *testing.T) {
have, _ := HasEnoughSignaturesMC(ctx, sc, sealer1.From, tx.Hash())
want := true
if have != want {
t.Errorf("have = %v, want %v", have, want)
}
})
wg.Add(1)
go ProcessSCSignatureAdded(ctx, sealer1Auth, mc, sc, os.TempDir(), 0, nil, &wg)
wg.Wait()
mcClient.Commit()
t.Run("Sender has been debited on the sidechain", func(t *testing.T) {
have, _ := scClient.BalanceAt(ctx, tester1.From, nil)
want := big.NewInt(10000000000 - 200000000 - int64(tx.Gas()))
if !reflect.DeepEqual(have, want) {
t.Errorf("have = %v, want %v", have, want)
}
})
t.Run("Recipient has been credited on the mainchain", func(t *testing.T) {
have, _ := mcClient.BalanceAt(ctx, tester2.From, nil)
want := big.NewInt(10000000000 + 200000000)
if !reflect.DeepEqual(have, want) {
t.Errorf("have = %v, want %v", have, want)
}
})
t.Run("Main chain smart contract has been debited", func(t *testing.T) {
have, _ := mcClient.BalanceAt(ctx, mcAddr, nil)
want := big.NewInt(50000000000 - 200000000)
if !reflect.DeepEqual(have, want) {
t.Errorf("have = %v, want %v", have, want)
}
})
t.Run("Side chain smart contract has been credited", func(t *testing.T) {
have, _ := scClient.BalanceAt(ctx, scAddr, nil)
want := big.NewInt(50000000000 + 200000000)
if !reflect.DeepEqual(have, want) {
t.Errorf("have = %v, want %v", have, want)
}
})
}