aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/chequebook/cheque_test.go
blob: b7555d0815ad8871f98c30d0113714967f5c4f25 (plain) (blame)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package chequebook

import (
    "crypto/ecdsa"
    "math/big"
    "os"
    "path/filepath"
    "testing"
    "time"

    "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/contracts/chequebook/contract"
    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/crypto"
)

var (
    key0, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    key1, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
    key2, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
    addr0   = crypto.PubkeyToAddress(key0.PublicKey)
    addr1   = crypto.PubkeyToAddress(key1.PublicKey)
    addr2   = crypto.PubkeyToAddress(key2.PublicKey)
)

func newTestBackend() *backends.SimulatedBackend {
    return backends.NewSimulatedBackend(core.GenesisAlloc{
        addr0: {Balance: big.NewInt(1000000000)},
        addr1: {Balance: big.NewInt(1000000000)},
        addr2: {Balance: big.NewInt(1000000000)},
    })
}

func deploy(prvKey *ecdsa.PrivateKey, amount *big.Int, backend *backends.SimulatedBackend) (common.Address, error) {
    deployTransactor := bind.NewKeyedTransactor(prvKey)
    deployTransactor.Value = amount
    addr, _, _, err := contract.DeployChequebook(deployTransactor, backend)
    if err != nil {
        return common.Address{}, err
    }
    backend.Commit()
    return addr, nil
}

func TestIssueAndReceive(t *testing.T) {
    path := filepath.Join(os.TempDir(), "chequebook-test.json")
    backend := newTestBackend()
    addr0, err := deploy(key0, big.NewInt(0), backend)
    if err != nil {
        t.Fatalf("deploy contract: expected no error, got %v", err)
    }
    chbook, err := NewChequebook(path, addr0, key0, backend)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    chbook.sent[addr1] = new(big.Int).SetUint64(42)
    amount := common.Big1

    if _, err = chbook.Issue(addr1, amount); err == nil {
        t.Fatalf("expected insufficient funds error, got none")
    }

    chbook.balance = new(big.Int).Set(common.Big1)
    if chbook.Balance().Cmp(common.Big1) != 0 {
        t.Fatalf("expected: %v, got %v", "0", chbook.Balance())
    }

    ch, err := chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    if chbook.Balance().Sign() != 0 {
        t.Errorf("expected: %v, got %v", "0", chbook.Balance())
    }

    chbox, err := NewInbox(key1, addr0, addr1, &key0.PublicKey, backend)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    received, err := chbox.Receive(ch)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    if received.Cmp(big.NewInt(43)) != 0 {
        t.Errorf("expected: %v, got %v", "43", received)
    }

}

func TestCheckbookFile(t *testing.T) {
    path := filepath.Join(os.TempDir(), "chequebook-test.json")
    backend := newTestBackend()
    chbook, err := NewChequebook(path, addr0, key0, backend)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    chbook.sent[addr1] = new(big.Int).SetUint64(42)
    chbook.balance = new(big.Int).Set(common.Big1)

    chbook.Save()

    chbook, err = LoadChequebook(path, key0, backend, false)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    if chbook.Balance().Cmp(common.Big1) != 0 {
        t.Errorf("expected: %v, got %v", "0", chbook.Balance())
    }

    var ch *Cheque
    if ch, err = chbook.Issue(addr1, common.Big1); err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    if ch.Amount.Cmp(new(big.Int).SetUint64(43)) != 0 {
        t.Errorf("expected: %v, got %v", "0", ch.Amount)
    }

    err = chbook.Save()
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
}

func TestVerifyErrors(t *testing.T) {
    path0 := filepath.Join(os.TempDir(), "chequebook-test-0.json")
    backend := newTestBackend()
    contr0, err := deploy(key0, common.Big2, backend)
    if err != nil {
        t.Errorf("expected no error, got %v", err)
    }
    chbook0, err := NewChequebook(path0, contr0, key0, backend)
    if err != nil {
        t.Errorf("expected no error, got %v", err)
    }

    path1 := filepath.Join(os.TempDir(), "chequebook-test-1.json")
    contr1, _ := deploy(key1, common.Big2, backend)
    chbook1, err := NewChequebook(path1, contr1, key1, backend)
    if err != nil {
        t.Errorf("expected no error, got %v", err)
    }

    chbook0.sent[addr1] = new(big.Int).SetUint64(42)
    chbook0.balance = new(big.Int).Set(common.Big2)
    chbook1.balance = new(big.Int).Set(common.Big1)
    amount := common.Big1
    ch0, err := chbook0.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    chbox, err := NewInbox(key1, contr0, addr1, &key0.PublicKey, backend)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    received, err := chbox.Receive(ch0)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    if received.Cmp(big.NewInt(43)) != 0 {
        t.Errorf("expected: %v, got %v", "43", received)
    }

    ch1, err := chbook0.Issue(addr2, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    received, err = chbox.Receive(ch1)
    t.Logf("correct error: %v", err)
    if err == nil {
        t.Fatalf("expected receiver error, got none and value %v", received)
    }

    ch2, err := chbook1.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    received, err = chbox.Receive(ch2)
    t.Logf("correct error: %v", err)
    if err == nil {
        t.Fatalf("expected sender error, got none and value %v", received)
    }

    _, err = chbook1.Issue(addr1, new(big.Int).SetInt64(-1))
    t.Logf("correct error: %v", err)
    if err == nil {
        t.Fatalf("expected incorrect amount error, got none")
    }

    received, err = chbox.Receive(ch0)
    t.Logf("correct error: %v", err)
    if err == nil {
        t.Fatalf("expected incorrect amount error, got none and value %v", received)
    }

}

func TestDeposit(t *testing.T) {
    path0 := filepath.Join(os.TempDir(), "chequebook-test-0.json")
    backend := newTestBackend()
    contr0, _ := deploy(key0, new(big.Int), backend)

    chbook, err := NewChequebook(path0, contr0, key0, backend)
    if err != nil {
        t.Errorf("expected no error, got %v", err)
    }

    balance := new(big.Int).SetUint64(42)
    chbook.Deposit(balance)
    backend.Commit()
    if chbook.Balance().Cmp(balance) != 0 {
        t.Fatalf("expected balance %v, got %v", balance, chbook.Balance())
    }

    amount := common.Big1
    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    exp := new(big.Int).SetUint64(41)
    if chbook.Balance().Cmp(exp) != 0 {
        t.Fatalf("expected balance %v, got %v", exp, chbook.Balance())
    }

    // autodeposit on each issue
    chbook.AutoDeposit(0, balance, balance)
    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    if chbook.Balance().Cmp(balance) != 0 {
        t.Fatalf("expected balance %v, got %v", balance, chbook.Balance())
    }

    // autodeposit off
    chbook.AutoDeposit(0, common.Big0, balance)
    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

    exp = new(big.Int).SetUint64(40)
    if chbook.Balance().Cmp(exp) != 0 {
        t.Fatalf("expected balance %v, got %v", exp, chbook.Balance())
    }

    // autodeposit every 30ms if new cheque issued
    interval := 30 * time.Millisecond
    chbook.AutoDeposit(interval, common.Big1, balance)
    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

    exp = new(big.Int).SetUint64(38)
    if chbook.Balance().Cmp(exp) != 0 {
        t.Fatalf("expected balance %v, got %v", exp, chbook.Balance())
    }

    time.Sleep(3 * interval)
    backend.Commit()
    if chbook.Balance().Cmp(balance) != 0 {
        t.Fatalf("expected balance %v, got %v", balance, chbook.Balance())
    }

    exp = new(big.Int).SetUint64(40)
    chbook.AutoDeposit(4*interval, exp, balance)
    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    time.Sleep(3 * interval)
    backend.Commit()
    if chbook.Balance().Cmp(exp) != 0 {
        t.Fatalf("expected balance %v, got %v", exp, chbook.Balance())
    }

    _, err = chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    time.Sleep(1 * interval)
    backend.Commit()

    if chbook.Balance().Cmp(balance) != 0 {
        t.Fatalf("expected balance %v, got %v", balance, chbook.Balance())
    }

    chbook.AutoDeposit(1*interval, common.Big0, balance)
    chbook.Stop()

    _, err = chbook.Issue(addr1, common.Big1)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

    _, err = chbook.Issue(addr1, common.Big2)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    time.Sleep(1 * interval)
    backend.Commit()

    exp = new(big.Int).SetUint64(39)
    if chbook.Balance().Cmp(exp) != 0 {
        t.Fatalf("expected balance %v, got %v", exp, chbook.Balance())
    }

}

func TestCash(t *testing.T) {
    path := filepath.Join(os.TempDir(), "chequebook-test.json")
    backend := newTestBackend()
    contr0, _ := deploy(key0, common.Big2, backend)

    chbook, err := NewChequebook(path, contr0, key0, backend)
    if err != nil {
        t.Errorf("expected no error, got %v", err)
    }
    chbook.sent[addr1] = new(big.Int).SetUint64(42)
    amount := common.Big1
    chbook.balance = new(big.Int).Set(common.Big1)
    ch, err := chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    chbox, err := NewInbox(key1, contr0, addr1, &key0.PublicKey, backend)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    // cashing latest cheque
    if _, err = chbox.Receive(ch); err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    if _, err = ch.Cash(chbook.session); err != nil {
        t.Fatal("Cash failed:", err)
    }
    backend.Commit()

    chbook.balance = new(big.Int).Set(common.Big3)
    ch0, err := chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    ch1, err := chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

    interval := 10 * time.Millisecond
    // setting autocash with interval of 10ms
    chbox.AutoCash(interval, nil)
    _, err = chbox.Receive(ch0)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    _, err = chbox.Receive(ch1)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    // after 3x interval time and 2 cheques received, exactly one cashing tx is sent
    time.Sleep(4 * interval)
    backend.Commit()

    // after stopping autocash no more tx are sent
    ch2, err := chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    chbox.Stop()
    _, err = chbox.Receive(ch2)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    time.Sleep(2 * interval)
    backend.Commit()

    // autocash below 1
    chbook.balance = big.NewInt(2)
    chbox.AutoCash(0, common.Big1)

    ch3, err := chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

    ch4, err := chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

    _, err = chbox.Receive(ch3)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()
    _, err = chbox.Receive(ch4)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

    // autochash on receipt when maxUncashed is 0
    chbook.balance = new(big.Int).Set(common.Big2)
    chbox.AutoCash(0, common.Big0)

    ch5, err := chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

    ch6, err := chbook.Issue(addr1, amount)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }

    _, err = chbox.Receive(ch5)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

    _, err = chbox.Receive(ch6)
    if err != nil {
        t.Fatalf("expected no error, got %v", err)
    }
    backend.Commit()

}