github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/contracts/chequebook/cheque_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package chequebook 18 19 import ( 20 "crypto/ecdsa" 21 "math/big" 22 "os" 23 "path/filepath" 24 "testing" 25 "time" 26 27 "github.com/atheioschain/go-atheios/accounts/abi/bind" 28 "github.com/atheioschain/go-atheios/accounts/abi/bind/backends" 29 "github.com/atheioschain/go-atheios/common" 30 "github.com/atheioschain/go-atheios/contracts/chequebook/contract" 31 "github.com/atheioschain/go-atheios/core" 32 "github.com/atheioschain/go-atheios/crypto" 33 ) 34 35 var ( 36 key0, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 37 key1, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") 38 key2, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") 39 addr0 = crypto.PubkeyToAddress(key0.PublicKey) 40 addr1 = crypto.PubkeyToAddress(key1.PublicKey) 41 addr2 = crypto.PubkeyToAddress(key2.PublicKey) 42 ) 43 44 func newTestBackend() *backends.SimulatedBackend { 45 return backends.NewSimulatedBackend( 46 core.GenesisAccount{Address: addr0, Balance: big.NewInt(1000000000)}, 47 core.GenesisAccount{Address: addr1, Balance: big.NewInt(1000000000)}, 48 core.GenesisAccount{Address: addr2, Balance: big.NewInt(1000000000)}, 49 ) 50 } 51 52 func deploy(prvKey *ecdsa.PrivateKey, amount *big.Int, backend *backends.SimulatedBackend) (common.Address, error) { 53 deployTransactor := bind.NewKeyedTransactor(prvKey) 54 deployTransactor.Value = amount 55 addr, _, _, err := contract.DeployChequebook(deployTransactor, backend) 56 if err != nil { 57 return common.Address{}, err 58 } 59 backend.Commit() 60 return addr, nil 61 } 62 63 func TestIssueAndReceive(t *testing.T) { 64 path := filepath.Join(os.TempDir(), "chequebook-test.json") 65 backend := newTestBackend() 66 addr0, err := deploy(key0, big.NewInt(0), backend) 67 if err != nil { 68 t.Fatalf("deploy contract: expected no error, got %v", err) 69 } 70 chbook, err := NewChequebook(path, addr0, key0, backend) 71 if err != nil { 72 t.Fatalf("expected no error, got %v", err) 73 } 74 chbook.sent[addr1] = new(big.Int).SetUint64(42) 75 amount := common.Big1 76 77 if _, err = chbook.Issue(addr1, amount); err == nil { 78 t.Fatalf("expected insufficient funds error, got none") 79 } 80 81 chbook.balance = new(big.Int).Set(common.Big1) 82 if chbook.Balance().Cmp(common.Big1) != 0 { 83 t.Fatalf("expected: %v, got %v", "0", chbook.Balance()) 84 } 85 86 ch, err := chbook.Issue(addr1, amount) 87 if err != nil { 88 t.Fatalf("expected no error, got %v", err) 89 } 90 91 if chbook.Balance().Cmp(common.Big0) != 0 { 92 t.Errorf("expected: %v, got %v", "0", chbook.Balance()) 93 } 94 95 chbox, err := NewInbox(key1, addr0, addr1, &key0.PublicKey, backend) 96 if err != nil { 97 t.Fatalf("expected no error, got %v", err) 98 } 99 100 received, err := chbox.Receive(ch) 101 if err != nil { 102 t.Fatalf("expected no error, got %v", err) 103 } 104 105 if received.Cmp(big.NewInt(43)) != 0 { 106 t.Errorf("expected: %v, got %v", "43", received) 107 } 108 109 } 110 111 func TestCheckbookFile(t *testing.T) { 112 path := filepath.Join(os.TempDir(), "chequebook-test.json") 113 backend := newTestBackend() 114 chbook, err := NewChequebook(path, addr0, key0, backend) 115 if err != nil { 116 t.Fatalf("expected no error, got %v", err) 117 } 118 chbook.sent[addr1] = new(big.Int).SetUint64(42) 119 chbook.balance = new(big.Int).Set(common.Big1) 120 121 chbook.Save() 122 123 chbook, err = LoadChequebook(path, key0, backend, false) 124 if err != nil { 125 t.Fatalf("expected no error, got %v", err) 126 } 127 if chbook.Balance().Cmp(common.Big1) != 0 { 128 t.Errorf("expected: %v, got %v", "0", chbook.Balance()) 129 } 130 131 var ch *Cheque 132 if ch, err = chbook.Issue(addr1, common.Big1); err != nil { 133 t.Fatalf("expected no error, got %v", err) 134 } 135 if ch.Amount.Cmp(new(big.Int).SetUint64(43)) != 0 { 136 t.Errorf("expected: %v, got %v", "0", ch.Amount) 137 } 138 139 err = chbook.Save() 140 if err != nil { 141 t.Fatalf("expected no error, got %v", err) 142 } 143 } 144 145 func TestVerifyErrors(t *testing.T) { 146 path0 := filepath.Join(os.TempDir(), "chequebook-test-0.json") 147 backend := newTestBackend() 148 contr0, err := deploy(key0, common.Big2, backend) 149 if err != nil { 150 t.Errorf("expected no error, got %v", err) 151 } 152 chbook0, err := NewChequebook(path0, contr0, key0, backend) 153 if err != nil { 154 t.Errorf("expected no error, got %v", err) 155 } 156 157 path1 := filepath.Join(os.TempDir(), "chequebook-test-1.json") 158 contr1, _ := deploy(key1, common.Big2, backend) 159 chbook1, err := NewChequebook(path1, contr1, key1, backend) 160 if err != nil { 161 t.Errorf("expected no error, got %v", err) 162 } 163 164 chbook0.sent[addr1] = new(big.Int).SetUint64(42) 165 chbook0.balance = new(big.Int).Set(common.Big2) 166 chbook1.balance = new(big.Int).Set(common.Big1) 167 amount := common.Big1 168 ch0, err := chbook0.Issue(addr1, amount) 169 if err != nil { 170 t.Fatalf("expected no error, got %v", err) 171 } 172 173 time.Sleep(5) 174 chbox, err := NewInbox(key1, contr0, addr1, &key0.PublicKey, backend) 175 if err != nil { 176 t.Fatalf("expected no error, got %v", err) 177 } 178 179 received, err := chbox.Receive(ch0) 180 if err != nil { 181 t.Fatalf("expected no error, got %v", err) 182 } 183 184 if received.Cmp(big.NewInt(43)) != 0 { 185 t.Errorf("expected: %v, got %v", "43", received) 186 } 187 188 ch1, err := chbook0.Issue(addr2, amount) 189 if err != nil { 190 t.Fatalf("expected no error, got %v", err) 191 } 192 193 received, err = chbox.Receive(ch1) 194 t.Logf("correct error: %v", err) 195 if err == nil { 196 t.Fatalf("expected receiver error, got none") 197 } 198 199 ch2, err := chbook1.Issue(addr1, amount) 200 if err != nil { 201 t.Fatalf("expected no error, got %v", err) 202 } 203 received, err = chbox.Receive(ch2) 204 t.Logf("correct error: %v", err) 205 if err == nil { 206 t.Fatalf("expected sender error, got none") 207 } 208 209 _, err = chbook1.Issue(addr1, new(big.Int).SetInt64(-1)) 210 t.Logf("correct error: %v", err) 211 if err == nil { 212 t.Fatalf("expected incorrect amount error, got none") 213 } 214 215 received, err = chbox.Receive(ch0) 216 t.Logf("correct error: %v", err) 217 if err == nil { 218 t.Fatalf("expected incorrect amount error, got none") 219 } 220 221 } 222 223 func TestDeposit(t *testing.T) { 224 path0 := filepath.Join(os.TempDir(), "chequebook-test-0.json") 225 backend := newTestBackend() 226 contr0, _ := deploy(key0, new(big.Int), backend) 227 228 chbook, err := NewChequebook(path0, contr0, key0, backend) 229 if err != nil { 230 t.Errorf("expected no error, got %v", err) 231 } 232 233 balance := new(big.Int).SetUint64(42) 234 chbook.Deposit(balance) 235 backend.Commit() 236 if chbook.balance.Cmp(balance) != 0 { 237 t.Fatalf("expected balance %v, got %v", balance, chbook.balance) 238 } 239 240 amount := common.Big1 241 _, err = chbook.Issue(addr1, amount) 242 if err != nil { 243 t.Fatalf("expected no error, got %v", err) 244 } 245 backend.Commit() 246 exp := new(big.Int).SetUint64(41) 247 if chbook.balance.Cmp(exp) != 0 { 248 t.Fatalf("expected balance %v, got %v", exp, chbook.balance) 249 } 250 251 // autodeposit on each issue 252 chbook.AutoDeposit(0, balance, balance) 253 _, err = chbook.Issue(addr1, amount) 254 if err != nil { 255 t.Fatalf("expected no error, got %v", err) 256 } 257 backend.Commit() 258 _, err = chbook.Issue(addr1, amount) 259 if err != nil { 260 t.Fatalf("expected no error, got %v", err) 261 } 262 backend.Commit() 263 if chbook.balance.Cmp(balance) != 0 { 264 t.Fatalf("expected balance %v, got %v", balance, chbook.balance) 265 } 266 267 // autodeposit off 268 chbook.AutoDeposit(0, common.Big0, balance) 269 _, err = chbook.Issue(addr1, amount) 270 if err != nil { 271 t.Fatalf("expected no error, got %v", err) 272 } 273 backend.Commit() 274 _, err = chbook.Issue(addr1, amount) 275 if err != nil { 276 t.Fatalf("expected no error, got %v", err) 277 } 278 backend.Commit() 279 280 exp = new(big.Int).SetUint64(40) 281 if chbook.balance.Cmp(exp) != 0 { 282 t.Fatalf("expected balance %v, got %v", exp, chbook.balance) 283 } 284 285 // autodeposit every 10ms if new cheque issued 286 interval := 30 * time.Millisecond 287 chbook.AutoDeposit(interval, common.Big1, balance) 288 _, err = chbook.Issue(addr1, amount) 289 if err != nil { 290 t.Fatalf("expected no error, got %v", err) 291 } 292 backend.Commit() 293 _, err = chbook.Issue(addr1, amount) 294 if err != nil { 295 t.Fatalf("expected no error, got %v", err) 296 } 297 backend.Commit() 298 299 exp = new(big.Int).SetUint64(38) 300 if chbook.balance.Cmp(exp) != 0 { 301 t.Fatalf("expected balance %v, got %v", exp, chbook.balance) 302 } 303 304 time.Sleep(3 * interval) 305 backend.Commit() 306 if chbook.balance.Cmp(balance) != 0 { 307 t.Fatalf("expected balance %v, got %v", balance, chbook.balance) 308 } 309 310 exp = new(big.Int).SetUint64(40) 311 chbook.AutoDeposit(4*interval, exp, balance) 312 _, err = chbook.Issue(addr1, amount) 313 if err != nil { 314 t.Fatalf("expected no error, got %v", err) 315 } 316 backend.Commit() 317 _, err = chbook.Issue(addr1, amount) 318 if err != nil { 319 t.Fatalf("expected no error, got %v", err) 320 } 321 time.Sleep(3 * interval) 322 backend.Commit() 323 if chbook.balance.Cmp(exp) != 0 { 324 t.Fatalf("expected balance %v, got %v", exp, chbook.balance) 325 } 326 327 _, err = chbook.Issue(addr1, amount) 328 if err != nil { 329 t.Fatalf("expected no error, got %v", err) 330 } 331 time.Sleep(1 * interval) 332 backend.Commit() 333 334 if chbook.balance.Cmp(balance) != 0 { 335 t.Fatalf("expected balance %v, got %v", balance, chbook.balance) 336 } 337 338 chbook.AutoDeposit(1*interval, common.Big0, balance) 339 chbook.Stop() 340 341 _, err = chbook.Issue(addr1, common.Big1) 342 if err != nil { 343 t.Fatalf("expected no error, got %v", err) 344 } 345 backend.Commit() 346 347 _, err = chbook.Issue(addr1, common.Big2) 348 if err != nil { 349 t.Fatalf("expected no error, got %v", err) 350 } 351 352 time.Sleep(1 * interval) 353 backend.Commit() 354 355 exp = new(big.Int).SetUint64(39) 356 if chbook.balance.Cmp(exp) != 0 { 357 t.Fatalf("expected balance %v, got %v", exp, chbook.balance) 358 } 359 360 } 361 362 func TestCash(t *testing.T) { 363 path := filepath.Join(os.TempDir(), "chequebook-test.json") 364 backend := newTestBackend() 365 contr0, _ := deploy(key0, common.Big2, backend) 366 367 chbook, err := NewChequebook(path, contr0, key0, backend) 368 if err != nil { 369 t.Errorf("expected no error, got %v", err) 370 } 371 chbook.sent[addr1] = new(big.Int).SetUint64(42) 372 amount := common.Big1 373 chbook.balance = new(big.Int).Set(common.Big1) 374 ch, err := chbook.Issue(addr1, amount) 375 if err != nil { 376 t.Fatalf("expected no error, got %v", err) 377 } 378 backend.Commit() 379 chbox, err := NewInbox(key1, contr0, addr1, &key0.PublicKey, backend) 380 if err != nil { 381 t.Fatalf("expected no error, got %v", err) 382 } 383 384 // cashing latest cheque 385 if _, err = chbox.Receive(ch); err != nil { 386 t.Fatalf("expected no error, got %v", err) 387 } 388 if _, err = ch.Cash(chbook.session); err != nil { 389 t.Fatal("Cash failed:", err) 390 } 391 backend.Commit() 392 393 chbook.balance = new(big.Int).Set(common.Big3) 394 ch0, err := chbook.Issue(addr1, amount) 395 if err != nil { 396 t.Fatalf("expected no error, got %v", err) 397 } 398 backend.Commit() 399 ch1, err := chbook.Issue(addr1, amount) 400 if err != nil { 401 t.Fatalf("expected no error, got %v", err) 402 } 403 backend.Commit() 404 405 interval := 10 * time.Millisecond 406 // setting autocash with interval of 10ms 407 chbox.AutoCash(interval, nil) 408 _, err = chbox.Receive(ch0) 409 if err != nil { 410 t.Fatalf("expected no error, got %v", err) 411 } 412 _, err = chbox.Receive(ch1) 413 if err != nil { 414 t.Fatalf("expected no error, got %v", err) 415 } 416 backend.Commit() 417 // expBalance := big.NewInt(2) 418 // gotBalance := backend.BalanceAt(addr1) 419 // if gotBalance.Cmp(expBalance) != 0 { 420 // t.Fatalf("expected beneficiary balance %v, got %v", expBalance, gotBalance) 421 // } 422 // after 3x interval time and 2 cheques received, exactly one cashing tx is sent 423 time.Sleep(4 * interval) 424 backend.Commit() 425 426 // expBalance = big.NewInt(4) 427 // gotBalance = backend.BalanceAt(addr1) 428 // if gotBalance.Cmp(expBalance) != 0 { 429 // t.Fatalf("expected beneficiary balance %v, got %v", expBalance, gotBalance) 430 // } 431 432 // after stopping autocash no more tx are sent 433 ch2, err := chbook.Issue(addr1, amount) 434 if err != nil { 435 t.Fatalf("expected no error, got %v", err) 436 } 437 chbox.Stop() 438 _, err = chbox.Receive(ch2) 439 if err != nil { 440 t.Fatalf("expected no error, got %v", err) 441 } 442 time.Sleep(2 * interval) 443 backend.Commit() 444 // expBalance = big.NewInt(4) 445 // gotBalance = backend.BalanceAt(addr1) 446 // if gotBalance.Cmp(expBalance) != 0 { 447 // t.Fatalf("expected beneficiary balance %v, got %v", expBalance, gotBalance) 448 // } 449 450 // autocash below 1 451 chbook.balance = big.NewInt(2) 452 chbox.AutoCash(0, common.Big1) 453 454 ch3, err := chbook.Issue(addr1, amount) 455 if err != nil { 456 t.Fatalf("expected no error, got %v", err) 457 } 458 backend.Commit() 459 // expBalance = big.NewInt(4) 460 // gotBalance = backend.BalanceAt(addr1) 461 // if gotBalance.Cmp(expBalance) != 0 { 462 // t.Fatalf("expected beneficiary balance %v, got %v", expBalance, gotBalance) 463 // } 464 465 ch4, err := chbook.Issue(addr1, amount) 466 if err != nil { 467 t.Fatalf("expected no error, got %v", err) 468 } 469 backend.Commit() 470 471 _, err = chbox.Receive(ch3) 472 if err != nil { 473 t.Fatalf("expected no error, got %v", err) 474 } 475 backend.Commit() 476 _, err = chbox.Receive(ch4) 477 if err != nil { 478 t.Fatalf("expected no error, got %v", err) 479 } 480 backend.Commit() 481 482 // 2 checks of amount 1 received, exactly 1 tx is sent 483 // expBalance = big.NewInt(6) 484 // gotBalance = backend.BalanceAt(addr1) 485 // if gotBalance.Cmp(expBalance) != 0 { 486 // t.Fatalf("expected beneficiary balance %v, got %v", expBalance, gotBalance) 487 // } 488 489 // autochash on receipt when maxUncashed is 0 490 chbook.balance = new(big.Int).Set(common.Big2) 491 chbox.AutoCash(0, common.Big0) 492 493 ch5, err := chbook.Issue(addr1, amount) 494 if err != nil { 495 t.Fatalf("expected no error, got %v", err) 496 } 497 backend.Commit() 498 // expBalance = big.NewInt(5) 499 // gotBalance = backend.BalanceAt(addr1) 500 // if gotBalance.Cmp(expBalance) != 0 { 501 // t.Fatalf("expected beneficiary balance %v, got %v", expBalance, gotBalance) 502 // } 503 504 ch6, err := chbook.Issue(addr1, amount) 505 if err != nil { 506 t.Fatalf("expected no error, got %v", err) 507 } 508 509 _, err = chbox.Receive(ch5) 510 if err != nil { 511 t.Fatalf("expected no error, got %v", err) 512 } 513 backend.Commit() 514 // expBalance = big.NewInt(4) 515 // gotBalance = backend.BalanceAt(addr1) 516 // if gotBalance.Cmp(expBalance) != 0 { 517 // t.Fatalf("expected beneficiary balance %v, got %v", expBalance, gotBalance) 518 // } 519 520 _, err = chbox.Receive(ch6) 521 if err != nil { 522 t.Fatalf("expected no error, got %v", err) 523 } 524 backend.Commit() 525 // expBalance = big.NewInt(6) 526 // gotBalance = backend.BalanceAt(addr1) 527 // if gotBalance.Cmp(expBalance) != 0 { 528 // t.Fatalf("expected beneficiary balance %v, got %v", expBalance, gotBalance) 529 // } 530 531 }