From 671a30de1718442f65906cbcbc4007f388622c75 Mon Sep 17 00:00:00 2001 From: mortelli Date: Tue, 26 Nov 2019 12:26:58 -0300 Subject: [PATCH 01/49] swap: add DefaultChequeDebtTolerance const --- swap/config.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/swap/config.go b/swap/config.go index ab7aae6e09..e4461c6147 100644 --- a/swap/config.go +++ b/swap/config.go @@ -27,6 +27,8 @@ const ( // DefaultPaymentThreshold is set to be equivalent to requesting and serving 10mb of data (2441 chunks (4096 bytes) = 10 mb, 10^7 bytes = 10 mb) DefaultPaymentThreshold = 2441*RetrieveRequestPrice + (10^7)*ChunkDeliveryPrice // 4096 * 2441 = 10 mb, DefaultDisconnectThreshold = 20 * DefaultPaymentThreshold + // DefaultChequeDebtTolerance represents how low a resulting balance the node is willing to accept when receiving a cheque + DefaultChequeDebtTolerance = DefaultPaymentThreshold / 100 * 5 // 5% of the payment threshold // DefaultInitialDepositAmount is the default amount to send to the contract when initially deploying // NOTE: deliberate value for now; needs experimentation DefaultInitialDepositAmount = 0 From 2723804cdc6d80ed1f51fbdc3bbbbb4c9eb5ee9b Mon Sep 17 00:00:00 2001 From: mortelli Date: Tue, 26 Nov 2019 12:47:50 -0300 Subject: [PATCH 02/49] swap: add code to prevent accepting cheques which would put the node in debt --- swap/swap.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/swap/swap.go b/swap/swap.go index d89bc2cb23..7e5d46739f 100644 --- a/swap/swap.go +++ b/swap/swap.go @@ -477,6 +477,13 @@ func (s *Swap) processAndVerifyCheque(cheque *Cheque, p *Peer) (uint64, error) { return 0, err } + // calculate tentative new balance after cheque is processed + newBalance := p.getBalance() - int64(actualAmount) + // check if this new balance would put us into debt + if newBalance < -int64(DefaultChequeDebtTolerance) { + return 0, errors.New("received cheque exceeds tolerance and would cause debt") + } + if err := p.setLastReceivedCheque(cheque); err != nil { p.logger.Error("error while saving last received cheque", "err", err.Error()) // TODO: what do we do here? Related issue: https://github.com/ethersphere/swarm/issues/1515 From 75cfe2359fa1cf6b092fe74b6b63f0c29095e4f4 Mon Sep 17 00:00:00 2001 From: mortelli Date: Tue, 26 Nov 2019 12:51:09 -0300 Subject: [PATCH 03/49] swap: rename DefaultChequeDebtTolerance const to ChequeDebtTolerance --- swap/config.go | 4 ++-- swap/swap.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/swap/config.go b/swap/config.go index e4461c6147..406313c589 100644 --- a/swap/config.go +++ b/swap/config.go @@ -27,8 +27,8 @@ const ( // DefaultPaymentThreshold is set to be equivalent to requesting and serving 10mb of data (2441 chunks (4096 bytes) = 10 mb, 10^7 bytes = 10 mb) DefaultPaymentThreshold = 2441*RetrieveRequestPrice + (10^7)*ChunkDeliveryPrice // 4096 * 2441 = 10 mb, DefaultDisconnectThreshold = 20 * DefaultPaymentThreshold - // DefaultChequeDebtTolerance represents how low a resulting balance the node is willing to accept when receiving a cheque - DefaultChequeDebtTolerance = DefaultPaymentThreshold / 100 * 5 // 5% of the payment threshold + // ChequeDebtTolerance represents how low a resulting balance the node is willing to accept when receiving a cheque + ChequeDebtTolerance = DefaultPaymentThreshold / 100 * 5 // 5% of the payment threshold // DefaultInitialDepositAmount is the default amount to send to the contract when initially deploying // NOTE: deliberate value for now; needs experimentation DefaultInitialDepositAmount = 0 diff --git a/swap/swap.go b/swap/swap.go index 7e5d46739f..16a05ba75e 100644 --- a/swap/swap.go +++ b/swap/swap.go @@ -480,7 +480,7 @@ func (s *Swap) processAndVerifyCheque(cheque *Cheque, p *Peer) (uint64, error) { // calculate tentative new balance after cheque is processed newBalance := p.getBalance() - int64(actualAmount) // check if this new balance would put us into debt - if newBalance < -int64(DefaultChequeDebtTolerance) { + if newBalance < -int64(ChequeDebtTolerance) { return 0, errors.New("received cheque exceeds tolerance and would cause debt") } From 4b85c4db1890138e124b5ed9ca557e8ae9dbd85b Mon Sep 17 00:00:00 2001 From: mortelli Date: Tue, 26 Nov 2019 15:10:25 -0300 Subject: [PATCH 04/49] swap: improve bad cheques prevention comments --- swap/config.go | 5 +++-- swap/swap.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/swap/config.go b/swap/config.go index 406313c589..33f0ceb771 100644 --- a/swap/config.go +++ b/swap/config.go @@ -27,8 +27,9 @@ const ( // DefaultPaymentThreshold is set to be equivalent to requesting and serving 10mb of data (2441 chunks (4096 bytes) = 10 mb, 10^7 bytes = 10 mb) DefaultPaymentThreshold = 2441*RetrieveRequestPrice + (10^7)*ChunkDeliveryPrice // 4096 * 2441 = 10 mb, DefaultDisconnectThreshold = 20 * DefaultPaymentThreshold - // ChequeDebtTolerance represents how low a resulting balance the node is willing to accept when receiving a cheque - ChequeDebtTolerance = DefaultPaymentThreshold / 100 * 5 // 5% of the payment threshold + // ChequeDebtTolerance is the lowest resulting balance a node is willing to accept when receiving a cheque + // the value is meant to be used below 0, as positive resulting balances should always be accepted when receiving cheques + ChequeDebtTolerance = DefaultPaymentThreshold / 100 * 5 // roughly 5% of the payment threshold // DefaultInitialDepositAmount is the default amount to send to the contract when initially deploying // NOTE: deliberate value for now; needs experimentation DefaultInitialDepositAmount = 0 diff --git a/swap/swap.go b/swap/swap.go index 16a05ba75e..a0e7829855 100644 --- a/swap/swap.go +++ b/swap/swap.go @@ -479,9 +479,9 @@ func (s *Swap) processAndVerifyCheque(cheque *Cheque, p *Peer) (uint64, error) { // calculate tentative new balance after cheque is processed newBalance := p.getBalance() - int64(actualAmount) - // check if this new balance would put us into debt + // check if this new balance would put creditor into debt if newBalance < -int64(ChequeDebtTolerance) { - return 0, errors.New("received cheque exceeds tolerance and would cause debt") + return 0, fmt.Errorf("received cheque would result in balance %d which exceeds tolerance %d and would cause debt", newBalance, ChequeDebtTolerance) } if err := p.setLastReceivedCheque(cheque); err != nil { From 6bc0fe57b4f7e320dead8ae198312f83125dff9c Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 6 Dec 2019 17:02:57 -0300 Subject: [PATCH 05/49] swap: duplicate ChequeDebtTolerance --- swap/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/config.go b/swap/config.go index f2428ffb2c..33f11c6491 100644 --- a/swap/config.go +++ b/swap/config.go @@ -29,7 +29,7 @@ const ( DefaultDisconnectThreshold = 20 * DefaultPaymentThreshold // ChequeDebtTolerance is the lowest resulting balance a node is willing to accept when receiving a cheque // the value is meant to be used below 0, as positive resulting balances should always be accepted when receiving cheques - ChequeDebtTolerance = DefaultPaymentThreshold / 100 * 5 // roughly 5% of the payment threshold + ChequeDebtTolerance = DefaultPaymentThreshold / 100 * 10 // roughly 10% of the payment threshold // DefaultDepositAmount is the default amount to send to the contract when initially deploying // NOTE: deliberate value for now; needs experimentation DefaultDepositAmount = 0 From 8ee2e2c1579f2e45e11ebc7e78934736837c0bde Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 9 Jan 2020 16:00:17 -0300 Subject: [PATCH 06/49] swap: simplify ChequeDebtTolerance calculation --- swap/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/config.go b/swap/config.go index 33f11c6491..c3cf450f5b 100644 --- a/swap/config.go +++ b/swap/config.go @@ -29,7 +29,7 @@ const ( DefaultDisconnectThreshold = 20 * DefaultPaymentThreshold // ChequeDebtTolerance is the lowest resulting balance a node is willing to accept when receiving a cheque // the value is meant to be used below 0, as positive resulting balances should always be accepted when receiving cheques - ChequeDebtTolerance = DefaultPaymentThreshold / 100 * 10 // roughly 10% of the payment threshold + ChequeDebtTolerance = DefaultPaymentThreshold / 10 // roughly 10% of the payment threshold // DefaultDepositAmount is the default amount to send to the contract when initially deploying // NOTE: deliberate value for now; needs experimentation DefaultDepositAmount = 0 From 5f2b9f531941fff287368c47b5845d37ef274df3 Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 9 Jan 2020 16:57:35 -0300 Subject: [PATCH 07/49] swap: fix bug in processAndVerifyCheque --- swap/swap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/swap.go b/swap/swap.go index 731bd6ca57..6aafe94fa1 100644 --- a/swap/swap.go +++ b/swap/swap.go @@ -532,7 +532,7 @@ func (s *Swap) processAndVerifyCheque(cheque *Cheque, p *Peer) (uint64, error) { } // calculate tentative new balance after cheque is processed - newBalance := p.getBalance() - int64(actualAmount) + newBalance := p.getBalance() - int64(cheque.Honey) // check if this new balance would put creditor into debt if newBalance < -int64(ChequeDebtTolerance) { return 0, fmt.Errorf("received cheque would result in balance %d which exceeds tolerance %d and would cause debt", newBalance, ChequeDebtTolerance) From 0aa47da4d5c43b7817a56797ee72a5846cb81e41 Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 9 Jan 2020 17:57:57 -0300 Subject: [PATCH 08/49] swap: add first iteration for TestDebtChequeTolerance --- swap/protocol_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/swap/protocol_test.go b/swap/protocol_test.go index 13c05741db..c1fb71b979 100644 --- a/swap/protocol_test.go +++ b/swap/protocol_test.go @@ -325,6 +325,36 @@ func TestEmitCheque(t *testing.T) { } } +func TestDebtChequeTolerance(t *testing.T) { + testBackend := newTestBackend(t) + protocolTester, clean, err := newSwapTester(t, testBackend, big.NewInt(int64(DefaultPaymentThreshold)*2)) + defer clean() + if err != nil { + t.Fatal(err) + } + debitorSwap := protocolTester.swap + + _, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + // setup the wait for mined transaction function for testing + cleanup := setupContractTest() + defer cleanup() + + if err = protocolTester.testHandshake( + correctSwapHandshakeMsg(debitorSwap), + correctSwapHandshakeMsg(debitorSwap), + ); err != nil { + t.Fatal(err) + } + + creditorPeer := debitorSwap.getPeer(protocolTester.Nodes[0].ID()) + + // no cheques should be present by this point + if creditorPeer.getLastSentCheque() != nil { + t.Fatalf("Expected no cheques yet, but there is %v:", creditorPeer.getLastSentCheque()) + } +} + // TestTriggerPaymentThreshold is to test that the whole cheque protocol is triggered // when we reach the payment threshold // One protocol tester is created and then Add with a value above the payment threshold is called for another node From b271f2d062635de48d8c0b0f4a01da3ea826e1cb Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 11:35:17 -0300 Subject: [PATCH 09/49] swap: iterate TestDebtChequeTolerance test --- swap/protocol_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/swap/protocol_test.go b/swap/protocol_test.go index c1fb71b979..04ecdb18fd 100644 --- a/swap/protocol_test.go +++ b/swap/protocol_test.go @@ -327,6 +327,7 @@ func TestEmitCheque(t *testing.T) { func TestDebtChequeTolerance(t *testing.T) { testBackend := newTestBackend(t) + protocolTester, clean, err := newSwapTester(t, testBackend, big.NewInt(int64(DefaultPaymentThreshold)*2)) defer clean() if err != nil { @@ -353,6 +354,11 @@ func TestDebtChequeTolerance(t *testing.T) { if creditorPeer.getLastSentCheque() != nil { t.Fatalf("Expected no cheques yet, but there is %v:", creditorPeer.getLastSentCheque()) } + + // balance should be 0 for both peers + if creditorPeer.getBalance() != 0 { + t.Fatalf("Expected creditor Swap balance to be 0, but is %d", creditorPeer.getBalance()) + } } // TestTriggerPaymentThreshold is to test that the whole cheque protocol is triggered From ab755d9899bf93a2df84fdd6deea9acb2108964a Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 12:40:10 -0300 Subject: [PATCH 10/49] swap: TestDebtChequeTolerance --- swap/protocol_test.go | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/swap/protocol_test.go b/swap/protocol_test.go index 04ecdb18fd..13c05741db 100644 --- a/swap/protocol_test.go +++ b/swap/protocol_test.go @@ -325,42 +325,6 @@ func TestEmitCheque(t *testing.T) { } } -func TestDebtChequeTolerance(t *testing.T) { - testBackend := newTestBackend(t) - - protocolTester, clean, err := newSwapTester(t, testBackend, big.NewInt(int64(DefaultPaymentThreshold)*2)) - defer clean() - if err != nil { - t.Fatal(err) - } - debitorSwap := protocolTester.swap - - _, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - // setup the wait for mined transaction function for testing - cleanup := setupContractTest() - defer cleanup() - - if err = protocolTester.testHandshake( - correctSwapHandshakeMsg(debitorSwap), - correctSwapHandshakeMsg(debitorSwap), - ); err != nil { - t.Fatal(err) - } - - creditorPeer := debitorSwap.getPeer(protocolTester.Nodes[0].ID()) - - // no cheques should be present by this point - if creditorPeer.getLastSentCheque() != nil { - t.Fatalf("Expected no cheques yet, but there is %v:", creditorPeer.getLastSentCheque()) - } - - // balance should be 0 for both peers - if creditorPeer.getBalance() != 0 { - t.Fatalf("Expected creditor Swap balance to be 0, but is %d", creditorPeer.getBalance()) - } -} - // TestTriggerPaymentThreshold is to test that the whole cheque protocol is triggered // when we reach the payment threshold // One protocol tester is created and then Add with a value above the payment threshold is called for another node From 4a6c3a31103837233f6e852020d964cace2e8615 Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 14:16:02 -0300 Subject: [PATCH 11/49] swap: add TestDebtCheques func --- swap/swap_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/swap/swap_test.go b/swap/swap_test.go index 51a67eb25d..7f14ebedfe 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -739,6 +739,45 @@ func TestResetBalance(t *testing.T) { } } +func TestDebtCheques(t *testing.T) { + testBackend := newTestBackend(t) + defer testBackend.Close() + + creditorSwap, cClean := newTestSwap(t, beneficiaryKey, testBackend) + debitorSwap, dClean := newTestSwap(t, ownerKey, testBackend) + defer cClean() + defer dClean() + + ctx := context.Background() + err := testDeploy(ctx, creditorSwap, big.NewInt(0)) + if err != nil { + t.Fatal(err) + } + err = testDeploy(ctx, debitorSwap, big.NewInt(int64(DefaultPaymentThreshold*2))) + if err != nil { + t.Fatal(err) + } + + cDummyPeer := newDummyPeerWithSpec(Spec) + dDummyPeer := newDummyPeerWithSpec(Spec) + cPeer, err := debitorSwap.addPeer(cDummyPeer.Peer, creditorSwap.owner.address, debitorSwap.GetParams().ContractAddress) + if err != nil { + t.Fatal(err) + } + dPeer, err := creditorSwap.addPeer(dDummyPeer.Peer, debitorSwap.owner.address, debitorSwap.GetParams().ContractAddress) + if err != nil { + t.Fatal(err) + } + + // the creditor and debitor should start out with a balance of 0 + if cPeer.getBalance() != 0 { + t.Fatalf("unexpected balance to be 0 for peer %v, but it is %d", cPeer.ID(), cPeer.getBalance()) + } + if dPeer.getBalance() != 0 { + t.Fatalf("unexpected balance to be 0 for peer %v, but it is %d", dPeer.ID(), dPeer.getBalance()) + } +} + // generate bookings based on parameters, apply them to a Swap struct and verify the result // append generated bookings to slice pointer func testPeerBookings(t *testing.T, s *Swap, bookings *[]booking, bookingAmount int64, bookingQuantity int, peer *protocols.Peer) { From f02a9f20868415aa52de0eb3d744c77bfb28cb8f Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 14:27:08 -0300 Subject: [PATCH 12/49] swap: iterate TestDebtCheques --- swap/swap_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/swap/swap_test.go b/swap/swap_test.go index 7f14ebedfe..911a44be71 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -776,6 +776,14 @@ func TestDebtCheques(t *testing.T) { if dPeer.getBalance() != 0 { t.Fatalf("unexpected balance to be 0 for peer %v, but it is %d", dPeer.ID(), dPeer.getBalance()) } + + // no cheques should be present at this point + if cPeer.getLastSentCheque() != nil { + t.Fatalf("expected no cheque sent to peer %v, but it is %d", cPeer.ID(), cPeer.getLastSentCheque()) + } + if dPeer.getLastReceivedCheque() != nil { + t.Fatalf("expected no cheque sent to peer %v, but it is %d", dPeer.ID(), dPeer.getLastReceivedCheque()) + } } // generate bookings based on parameters, apply them to a Swap struct and verify the result From 03484adefadc0e338dabb3ff8857002853166f5c Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 14:32:38 -0300 Subject: [PATCH 13/49] swap: iterate TestDebtCheques --- swap/swap_test.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 911a44be71..05d14b890b 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -760,30 +760,39 @@ func TestDebtCheques(t *testing.T) { cDummyPeer := newDummyPeerWithSpec(Spec) dDummyPeer := newDummyPeerWithSpec(Spec) - cPeer, err := debitorSwap.addPeer(cDummyPeer.Peer, creditorSwap.owner.address, debitorSwap.GetParams().ContractAddress) + creditor, err := debitorSwap.addPeer(cDummyPeer.Peer, creditorSwap.owner.address, debitorSwap.GetParams().ContractAddress) if err != nil { t.Fatal(err) } - dPeer, err := creditorSwap.addPeer(dDummyPeer.Peer, debitorSwap.owner.address, debitorSwap.GetParams().ContractAddress) + debitor, err := creditorSwap.addPeer(dDummyPeer.Peer, debitorSwap.owner.address, debitorSwap.GetParams().ContractAddress) if err != nil { t.Fatal(err) } // the creditor and debitor should start out with a balance of 0 - if cPeer.getBalance() != 0 { - t.Fatalf("unexpected balance to be 0 for peer %v, but it is %d", cPeer.ID(), cPeer.getBalance()) + if creditor.getBalance() != 0 { + t.Fatalf("unexpected balance to be 0 for peer %v, but it is %d", creditor.ID(), creditor.getBalance()) } - if dPeer.getBalance() != 0 { - t.Fatalf("unexpected balance to be 0 for peer %v, but it is %d", dPeer.ID(), dPeer.getBalance()) + if debitor.getBalance() != 0 { + t.Fatalf("unexpected balance to be 0 for peer %v, but it is %d", debitor.ID(), debitor.getBalance()) } // no cheques should be present at this point - if cPeer.getLastSentCheque() != nil { - t.Fatalf("expected no cheque sent to peer %v, but it is %d", cPeer.ID(), cPeer.getLastSentCheque()) + if creditor.getLastSentCheque() != nil { + t.Fatalf("expected no cheque sent to peer %v, but it is %d", creditor.ID(), creditor.getLastSentCheque()) } - if dPeer.getLastReceivedCheque() != nil { - t.Fatalf("expected no cheque sent to peer %v, but it is %d", dPeer.ID(), dPeer.getLastReceivedCheque()) + if debitor.getLastReceivedCheque() != nil { + t.Fatalf("expected no cheque sent to peer %v, but it is %d", debitor.ID(), debitor.getLastReceivedCheque()) } + + // set asymmetric balance and attempt to send cheque + debitor.setBalance(int64(DefaultPaymentThreshold) + 10) + // now simulate sending the cheque to the creditor from the debitor + creditor.sendCheque() + + debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ + Cheque: creditor.getPendingCheque(), + }) } // generate bookings based on parameters, apply them to a Swap struct and verify the result From daa620aea9184f1938d0e6b2870e6227e85b9b74 Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 14:43:25 -0300 Subject: [PATCH 14/49] swap: iterate TestDebtCheques function --- swap/swap_test.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 05d14b890b..6104e0bb3d 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -699,7 +699,10 @@ func TestResetBalance(t *testing.T) { defer cleanup() // now simulate sending the cheque to the creditor from the debitor - creditor.sendCheque() + err = creditor.sendCheque() + if err != nil { + t.Fatal(err) + } debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ Cheque: creditor.getPendingCheque(), @@ -786,13 +789,24 @@ func TestDebtCheques(t *testing.T) { } // set asymmetric balance and attempt to send cheque - debitor.setBalance(int64(DefaultPaymentThreshold) + 10) + creditor.setBalance(-1 * (int64(DefaultPaymentThreshold) + 10)) // now simulate sending the cheque to the creditor from the debitor - creditor.sendCheque() + err = creditor.sendCheque() + if err != nil { + t.Fatal(err) + } - debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ + creditorSwap.handleEmitChequeMsg(ctx, creditor, &EmitChequeMsg{ Cheque: creditor.getPendingCheque(), }) + + // cheque should not have gone through as it would put the creditor in debt + if creditor.getLastSentCheque() != nil { + t.Fatalf("expected no cheque sent to peer %v, but it is %d", creditor.ID(), creditor.getLastSentCheque()) + } + if creditor.getPendingCheque() == nil { + t.Fatalf("expected pending cheque to be sent to peer %v, but found none", creditor.ID()) + } } // generate bookings based on parameters, apply them to a Swap struct and verify the result From eb7d34ae7c8843780f81f8023514f9fd1f6357e5 Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 16:05:28 -0300 Subject: [PATCH 15/49] swap: iterate TestDebtCheques function --- swap/swap_test.go | 63 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 6104e0bb3d..9ced9f88ab 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -774,10 +774,10 @@ func TestDebtCheques(t *testing.T) { // the creditor and debitor should start out with a balance of 0 if creditor.getBalance() != 0 { - t.Fatalf("unexpected balance to be 0 for peer %v, but it is %d", creditor.ID(), creditor.getBalance()) + t.Fatalf("expected balance to be 0 for peer %v, but it is %d", creditor.ID(), creditor.getBalance()) } if debitor.getBalance() != 0 { - t.Fatalf("unexpected balance to be 0 for peer %v, but it is %d", debitor.ID(), debitor.getBalance()) + t.Fatalf("expected balance to be 0 for peer %v, but it is %d", debitor.ID(), debitor.getBalance()) } // no cheques should be present at this point @@ -785,27 +785,72 @@ func TestDebtCheques(t *testing.T) { t.Fatalf("expected no cheque sent to peer %v, but it is %d", creditor.ID(), creditor.getLastSentCheque()) } if debitor.getLastReceivedCheque() != nil { - t.Fatalf("expected no cheque sent to peer %v, but it is %d", debitor.ID(), debitor.getLastReceivedCheque()) + t.Fatalf("expected no cheque received from peer %v, but it is %d", debitor.ID(), debitor.getLastReceivedCheque()) } // set asymmetric balance and attempt to send cheque - creditor.setBalance(-1 * (int64(DefaultPaymentThreshold) + 10)) + err = creditor.setBalance(-1 * int64(DefaultPaymentThreshold)) + if err != nil { + t.Fatal(err) + } // now simulate sending the cheque to the creditor from the debitor err = creditor.sendCheque() if err != nil { t.Fatal(err) } - creditorSwap.handleEmitChequeMsg(ctx, creditor, &EmitChequeMsg{ - Cheque: creditor.getPendingCheque(), - }) - // cheque should not have gone through as it would put the creditor in debt if creditor.getLastSentCheque() != nil { t.Fatalf("expected no cheque sent to peer %v, but it is %d", creditor.ID(), creditor.getLastSentCheque()) } if creditor.getPendingCheque() == nil { - t.Fatalf("expected pending cheque to be sent to peer %v, but found none", creditor.ID()) + t.Fatalf("expected pending cheque for peer %v, but found none", creditor.ID()) + } + + // clear pending cheque to start over + err = creditor.setPendingCheque(nil) + if err != nil { + t.Fatal(err) + } + + // now send an admissible cheque + err = debitor.setBalance(int64(ChequeDebtTolerance)) + if err != nil { + t.Fatal(err) + } + err = creditor.setBalance(-1 * int64(ChequeDebtTolerance)) + if err != nil { + t.Fatal(err) + } + + err = creditor.sendCheque() + if err != nil { + t.Fatal(err) + } + + debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ + Cheque: creditor.getPendingCheque(), + }) + + // the debitor should have already reset its balance + if creditor.getBalance() != 0 { + t.Fatalf("expected balance to be 0 for peer %v, but it is %d", creditor.ID(), creditor.getBalance()) + } + + cheque := creditor.getLastSentCheque() + if cheque == nil { + t.Fatalf("expected pending cheque for peer %v, but found none", creditor.ID()) + } + + err = creditorSwap.handleEmitChequeMsg(ctx, debitor, &EmitChequeMsg{ + Cheque: cheque, + }) + if err != nil { + t.Fatal(err) + } + + if creditor.getPendingCheque() != nil { + t.Fatalf("expected no pending cheque for peer %v, but found %d", creditor.ID(), creditor.getPendingCheque()) } } From 068b58c33fcc4c3ac79d8464c6acb9341a74db2f Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 16:22:18 -0300 Subject: [PATCH 16/49] swap: iterate TestDebtCheques function --- swap/swap_test.go | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 9ced9f88ab..3f7422b1ca 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -789,7 +789,7 @@ func TestDebtCheques(t *testing.T) { } // set asymmetric balance and attempt to send cheque - err = creditor.setBalance(-1 * int64(DefaultPaymentThreshold)) + err = creditor.setBalance(-int64(DefaultPaymentThreshold)) if err != nil { t.Fatal(err) } @@ -813,45 +813,47 @@ func TestDebtCheques(t *testing.T) { t.Fatal(err) } - // now send an admissible cheque - err = debitor.setBalance(int64(ChequeDebtTolerance)) + // set asymmetric balances to send a (barely) admissible cheque + err = creditor.setBalance(-int64(ChequeDebtTolerance)) if err != nil { t.Fatal(err) } - err = creditor.setBalance(-1 * int64(ChequeDebtTolerance)) + + err = creditor.sendCheque() if err != nil { t.Fatal(err) } + cheque := creditor.getPendingCheque() - err = creditor.sendCheque() + // simulate cheque handling + err = creditorSwap.handleEmitChequeMsg(ctx, debitor, &EmitChequeMsg{ + Cheque: cheque, + }) if err != nil { t.Fatal(err) } + // simulate cheque confirmation debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ Cheque: creditor.getPendingCheque(), }) - // the debitor should have already reset its balance + // balance should be 0 according to the debitor if creditor.getBalance() != 0 { t.Fatalf("expected balance to be 0 for peer %v, but it is %d", creditor.ID(), creditor.getBalance()) } - - cheque := creditor.getLastSentCheque() - if cheque == nil { - t.Fatalf("expected pending cheque for peer %v, but found none", creditor.ID()) - } - - err = creditorSwap.handleEmitChequeMsg(ctx, debitor, &EmitChequeMsg{ - Cheque: cheque, - }) - if err != nil { - t.Fatal(err) + // balance should be -ChequeDebtTolerance according to the debitor + if debitor.getBalance() != -int64(ChequeDebtTolerance) { + t.Fatalf("expected balance to be %d for peer %v, but it is %d", -int64(ChequeDebtTolerance), debitor.ID(), debitor.getBalance()) } - + // no cheque should be pending if creditor.getPendingCheque() != nil { t.Fatalf("expected no pending cheque for peer %v, but found %d", creditor.ID(), creditor.getPendingCheque()) } + // verify last received cheque + if debitor.getLastReceivedCheque() != cheque { + t.Fatalf("expected last received cheque from peer %v to be %v, but found %v", debitor.ID(), cheque, creditor.getLastReceivedCheque()) + } } // generate bookings based on parameters, apply them to a Swap struct and verify the result From 9ba613d936f0913dabaa8f56696a1ce75c4e4d18 Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 16:25:38 -0300 Subject: [PATCH 17/49] swap: add missing error handling in swap tests --- swap/swap_test.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 3f7422b1ca..683ac901c7 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -691,8 +691,14 @@ func TestResetBalance(t *testing.T) { } // set balances arbitrarily - debitor.setBalance(testAmount) - creditor.setBalance(-testAmount) + err = debitor.setBalance(testAmount) + if err != nil { + t.Fatal(err) + } + err = creditor.setBalance(-testAmount) + if err != nil { + t.Fatal(err) + } // setup the wait for mined transaction function for testing cleanup := setupContractTest() @@ -940,7 +946,10 @@ func TestRestoreBalanceFromStateStore(t *testing.T) { if err != nil { t.Fatal(err) } - testPeer.setBalance(-8888) + err = testPeer.setBalance(-8888) + if err != nil { + t.Fatal(err) + } tmpBalance := testPeer.getBalance() swap.store.Put(testPeer.ID().String(), &tmpBalance) @@ -1695,15 +1704,24 @@ func TestSwapLogToFile(t *testing.T) { } // set balances arbitrarily - debitor.setBalance(testAmount) - creditor.setBalance(-testAmount) + err = debitor.setBalance(testAmount) + if err != nil { + t.Fatal(err) + } + err = creditor.setBalance(-testAmount) + if err != nil { + t.Fatal(err) + } // setup the wait for mined transaction function for testing cleanup := setupContractTest() defer cleanup() // now simulate sending the cheque to the creditor from the debitor - creditor.sendCheque() + err = creditor.sendCheque() + if err != nil { + t.Fatal(err) + } if logDirDebitor == "" { t.Fatal("Swap Log Dir is not defined") @@ -1802,7 +1820,10 @@ func TestAvailableBalance(t *testing.T) { // send a cheque worth 42 chequeAmount := int64(42) // create a dummy peer. Note: the peer's contract address and the peers address are resp the swap contract and the swap owner - peer.setBalance(-chequeAmount) + err = peer.setBalance(-chequeAmount) + if err != nil { + t.Fatal(err) + } if err = peer.sendCheque(); err != nil { t.Fatal(err) } From ba65ef7fd3ce1198c4e550173233c6c220c2f5da Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 10 Jan 2020 16:33:45 -0300 Subject: [PATCH 18/49] swap: simplify error handling in swap tests --- swap/swap_test.go | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index b4d046330d..bb7db3a9b3 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -757,12 +757,10 @@ func TestDebtCheques(t *testing.T) { defer dClean() ctx := context.Background() - err := testDeploy(ctx, creditorSwap, big.NewInt(0)) - if err != nil { + if err := testDeploy(ctx, creditorSwap, big.NewInt(0)); err != nil { t.Fatal(err) } - err = testDeploy(ctx, debitorSwap, big.NewInt(int64(DefaultPaymentThreshold*2))) - if err != nil { + if err := testDeploy(ctx, debitorSwap, big.NewInt(int64(DefaultPaymentThreshold*2))); err != nil { t.Fatal(err) } @@ -794,13 +792,11 @@ func TestDebtCheques(t *testing.T) { } // set asymmetric balance and attempt to send cheque - err = creditor.setBalance(-int64(DefaultPaymentThreshold)) - if err != nil { + if err = creditor.setBalance(-int64(DefaultPaymentThreshold)); err != nil { t.Fatal(err) } // now simulate sending the cheque to the creditor from the debitor - err = creditor.sendCheque() - if err != nil { + if err = creditor.sendCheque(); err != nil { t.Fatal(err) } @@ -813,19 +809,16 @@ func TestDebtCheques(t *testing.T) { } // clear pending cheque to start over - err = creditor.setPendingCheque(nil) - if err != nil { + if err = creditor.setPendingCheque(nil); err != nil { t.Fatal(err) } // set asymmetric balances to send a (barely) admissible cheque - err = creditor.setBalance(-int64(ChequeDebtTolerance)) - if err != nil { + if err = creditor.setBalance(-int64(ChequeDebtTolerance)); err != nil { t.Fatal(err) } - err = creditor.sendCheque() - if err != nil { + if err = creditor.sendCheque(); err != nil { t.Fatal(err) } cheque := creditor.getPendingCheque() @@ -945,8 +938,7 @@ func TestRestoreBalanceFromStateStore(t *testing.T) { if err != nil { t.Fatal(err) } - err = testPeer.setBalance(-8888) - if err != nil { + if err = testPeer.setBalance(-8888); err != nil { t.Fatal(err) } @@ -1623,12 +1615,10 @@ func TestSwapLogToFile(t *testing.T) { } // set balances arbitrarily - err = debitor.setBalance(testAmount) - if err != nil { + if err = debitor.setBalance(testAmount); err != nil { t.Fatal(err) } - err = creditor.setBalance(-testAmount) - if err != nil { + if err = creditor.setBalance(-testAmount); err != nil { t.Fatal(err) } @@ -1637,8 +1627,7 @@ func TestSwapLogToFile(t *testing.T) { defer cleanup() // now simulate sending the cheque to the creditor from the debitor - err = creditor.sendCheque() - if err != nil { + if err = creditor.sendCheque(); err != nil { t.Fatal(err) } From d558c1609e4f02999406f34f105cb32bceabee61 Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 13 Jan 2020 11:34:38 -0300 Subject: [PATCH 19/49] swap: small refactor in tests --- swap/swap_test.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index bb7db3a9b3..63ce2d8d3d 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -690,12 +690,10 @@ func TestResetBalance(t *testing.T) { } // set balances arbitrarily - err = debitor.setBalance(testAmount) - if err != nil { + if err = debitor.setBalance(testAmount); err != nil { t.Fatal(err) } - err = creditor.setBalance(-testAmount) - if err != nil { + if err = creditor.setBalance(-testAmount); err != nil { t.Fatal(err) } @@ -704,8 +702,7 @@ func TestResetBalance(t *testing.T) { defer cleanup() // now simulate sending the cheque to the creditor from the debitor - err = creditor.sendCheque() - if err != nil { + if err = creditor.sendCheque(); err != nil { t.Fatal(err) } @@ -1728,8 +1725,7 @@ func TestAvailableBalance(t *testing.T) { // send a cheque worth 42 chequeAmount := int64(42) // create a dummy peer. Note: the peer's contract address and the peers address are resp the swap contract and the swap owner - err = peer.setBalance(-chequeAmount) - if err != nil { + if err = peer.setBalance(-chequeAmount); err != nil { t.Fatal(err) } if err = peer.sendCheque(); err != nil { From ba551bcf3fd2c5eb1aba8ea96d6c32e115f515e6 Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 13 Jan 2020 15:28:40 -0300 Subject: [PATCH 20/49] swap: refactor TestDebtCheques --- swap/swap_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 124d86c676..3237348b48 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -687,6 +687,8 @@ func TestResetBalance(t *testing.T) { } } +// TestDebtCheques verifies that cheques that would put a node in debt past the defined tolerance are rejected +// and that ones within the tolerance are accepted func TestDebtCheques(t *testing.T) { testBackend := newTestBackend(t) defer testBackend.Close() @@ -732,7 +734,7 @@ func TestDebtCheques(t *testing.T) { } // set asymmetric balance and attempt to send cheque - if err = creditor.setBalance(-int64(DefaultPaymentThreshold)); err != nil { + if err = creditor.setBalance(-int64(ChequeDebtTolerance + 1)); err != nil { t.Fatal(err) } // now simulate sending the cheque to the creditor from the debitor @@ -773,7 +775,7 @@ func TestDebtCheques(t *testing.T) { // simulate cheque confirmation debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ - Cheque: creditor.getPendingCheque(), + Cheque: cheque, }) // balance should be 0 according to the debitor From 03d601bc582aa745d3c2d306951087feaf2f5c0c Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 13 Jan 2020 15:49:51 -0300 Subject: [PATCH 21/49] swap: iterate TestDebtCheques function --- swap/swap_test.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 3237348b48..40f5513566 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -734,20 +734,30 @@ func TestDebtCheques(t *testing.T) { } // set asymmetric balance and attempt to send cheque - if err = creditor.setBalance(-int64(ChequeDebtTolerance + 1)); err != nil { + if err = creditor.setBalance(-int64(ChequeDebtTolerance * 2)); err != nil { t.Fatal(err) } // now simulate sending the cheque to the creditor from the debitor if err = creditor.sendCheque(); err != nil { t.Fatal(err) } + cheque := creditor.getPendingCheque() + // simulate cheque handling + err = creditorSwap.handleEmitChequeMsg(ctx, debitor, &EmitChequeMsg{ + Cheque: cheque, + }) // cheque should not have gone through as it would put the creditor in debt + if err == nil { + t.Fatal("expected invalid cheque reception to cause a failure, but got none") + } + // since the emission caused an error, the confirmation should not be simualted + // cheque should not be marked as sent, and should be pending if creditor.getLastSentCheque() != nil { t.Fatalf("expected no cheque sent to peer %v, but it is %d", creditor.ID(), creditor.getLastSentCheque()) } - if creditor.getPendingCheque() == nil { - t.Fatalf("expected pending cheque for peer %v, but found none", creditor.ID()) + if creditor.getPendingCheque() != cheque { + t.Fatalf("expected pending cheque for peer %v to be %v, but is %v", creditor.ID(), cheque, creditor.getPendingCheque()) } // clear pending cheque to start over @@ -763,7 +773,7 @@ func TestDebtCheques(t *testing.T) { if err = creditor.sendCheque(); err != nil { t.Fatal(err) } - cheque := creditor.getPendingCheque() + cheque = creditor.getPendingCheque() // simulate cheque handling err = creditorSwap.handleEmitChequeMsg(ctx, debitor, &EmitChequeMsg{ @@ -778,11 +788,11 @@ func TestDebtCheques(t *testing.T) { Cheque: cheque, }) - // balance should be 0 according to the debitor + // balance should now be 0 according to the debitor if creditor.getBalance() != 0 { t.Fatalf("expected balance to be 0 for peer %v, but it is %d", creditor.ID(), creditor.getBalance()) } - // balance should be -ChequeDebtTolerance according to the debitor + // balance should now be -ChequeDebtTolerance according to the debitor if debitor.getBalance() != -int64(ChequeDebtTolerance) { t.Fatalf("expected balance to be %d for peer %v, but it is %d", -int64(ChequeDebtTolerance), debitor.ID(), debitor.getBalance()) } From c7ceba364301c7203a1d32c2f6363ef649c59bc6 Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 13 Jan 2020 16:00:28 -0300 Subject: [PATCH 22/49] swap: add missing error catch for TestTriggerPaymentThreshold --- swap/protocol_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/swap/protocol_test.go b/swap/protocol_test.go index bc9c17a3ce..a7c669cf2a 100644 --- a/swap/protocol_test.go +++ b/swap/protocol_test.go @@ -357,7 +357,9 @@ func TestTriggerPaymentThreshold(t *testing.T) { // set the balance to manually be at PaymentThreshold overDraft := 42 expectedAmount := uint64(overDraft) + DefaultPaymentThreshold - creditor.setBalance(-int64(DefaultPaymentThreshold)) + if err = creditor.setBalance(-int64(DefaultPaymentThreshold)); err != nil { + t.Fatal(err) + } // we expect a cheque at the end of the test, but not yet if creditor.getLastSentCheque() != nil { @@ -492,7 +494,9 @@ func TestTriggerDisconnectThreshold(t *testing.T) { overDraft := 42 expectedBalance := int64(DefaultDisconnectThreshold) // we don't expect any change after the test - debitor.setBalance(expectedBalance) + if err = debitor.setBalance(expectedBalance); err != nil { + t.Fatal(err) + } // we also don't expect any cheques yet if debitor.getPendingCheque() != nil { t.Fatalf("Expected no cheques yet, but there is %v", debitor.getPendingCheque()) From f0445322b6246f5ca064048a5f7975cd309a86dd Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 13 Jan 2020 16:07:37 -0300 Subject: [PATCH 23/49] swap: fix typo in TestDebtCheques --- swap/swap_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 40f5513566..a19f5bc19f 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -751,7 +751,7 @@ func TestDebtCheques(t *testing.T) { if err == nil { t.Fatal("expected invalid cheque reception to cause a failure, but got none") } - // since the emission caused an error, the confirmation should not be simualted + // since the emission caused an error, the confirmation should not be simulated // cheque should not be marked as sent, and should be pending if creditor.getLastSentCheque() != nil { t.Fatalf("expected no cheque sent to peer %v, but it is %d", creditor.ID(), creditor.getLastSentCheque()) From e17b503aa89eb0d51bd396f20e776315935dcf5a Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 16 Jan 2020 09:51:27 -0300 Subject: [PATCH 24/49] swap: fix bug in processAndVerifyCheque return --- swap/swap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/swap.go b/swap/swap.go index fc13693fe8..fbd69a835e 100644 --- a/swap/swap.go +++ b/swap/swap.go @@ -518,7 +518,7 @@ func (s *Swap) processAndVerifyCheque(cheque *Cheque, p *Peer) (*uint256.Uint256 newBalance := p.getBalance() - int64(cheque.Honey) // check if this new balance would put creditor into debt if newBalance < -int64(ChequeDebtTolerance) { - return 0, fmt.Errorf("received cheque would result in balance %d which exceeds tolerance %d and would cause debt", newBalance, ChequeDebtTolerance) + return nil, fmt.Errorf("received cheque would result in balance %d which exceeds tolerance %d and would cause debt", newBalance, ChequeDebtTolerance) } if err := p.setLastReceivedCheque(cheque); err != nil { From 22b4dd38f7f0aab6843768af6ce47be277496c8b Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 16 Jan 2020 09:59:32 -0300 Subject: [PATCH 25/49] swap: fix formatting in TestDebtCheques --- swap/swap_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 1ad7e43cde..39be894f3b 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -728,10 +728,10 @@ func TestDebtCheques(t *testing.T) { // no cheques should be present at this point if creditor.getLastSentCheque() != nil { - t.Fatalf("expected no cheque sent to peer %v, but it is %d", creditor.ID(), creditor.getLastSentCheque()) + t.Fatalf("expected no cheque sent to peer %v, but it is %v", creditor.ID(), creditor.getLastSentCheque()) } if debitor.getLastReceivedCheque() != nil { - t.Fatalf("expected no cheque received from peer %v, but it is %d", debitor.ID(), debitor.getLastReceivedCheque()) + t.Fatalf("expected no cheque received from peer %v, but it is %v", debitor.ID(), debitor.getLastReceivedCheque()) } // set asymmetric balance and attempt to send cheque @@ -755,7 +755,7 @@ func TestDebtCheques(t *testing.T) { // since the emission caused an error, the confirmation should not be simulated // cheque should not be marked as sent, and should be pending if creditor.getLastSentCheque() != nil { - t.Fatalf("expected no cheque sent to peer %v, but it is %d", creditor.ID(), creditor.getLastSentCheque()) + t.Fatalf("expected no cheque sent to peer %v, but it is %v", creditor.ID(), creditor.getLastSentCheque()) } if creditor.getPendingCheque() != cheque { t.Fatalf("expected pending cheque for peer %v to be %v, but is %v", creditor.ID(), cheque, creditor.getPendingCheque()) @@ -799,7 +799,7 @@ func TestDebtCheques(t *testing.T) { } // no cheque should be pending if creditor.getPendingCheque() != nil { - t.Fatalf("expected no pending cheque for peer %v, but found %d", creditor.ID(), creditor.getPendingCheque()) + t.Fatalf("expected no pending cheque for peer %v, but found %v", creditor.ID(), creditor.getPendingCheque()) } // verify last received cheque if debitor.getLastReceivedCheque() != cheque { From 7706df9b4283ceaf592f333c6c206d841e7a09bd Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 16 Jan 2020 13:34:36 -0300 Subject: [PATCH 26/49] swap: add testMsgSmallPrice type --- swap/simulations_test.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index 53cbd863da..d32bcf2a5c 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -75,6 +75,7 @@ type swapSimulationParams struct { type testMsgBySender struct{} type testMsgByReceiver struct{} type testMsgBigPrice struct{} +type testMsgSmallPrice struct{} // create a test Spec; every node has its Spec and its accounting Hook func newTestSpec() *protocols.Spec { @@ -86,6 +87,7 @@ func newTestSpec() *protocols.Spec { testMsgBySender{}, testMsgByReceiver{}, testMsgBigPrice{}, + testMsgSmallPrice{}, }, } } @@ -114,6 +116,14 @@ func (m *testMsgBigPrice) Price() *protocols.Price { } } +func (m *testMsgSmallPrice) Price() *protocols.Price { + return &protocols.Price{ + Value: ChequeDebtTolerance / 10, // ensures that the message won't put nodes into debt + PerByte: false, + Payer: protocols.Sender, + } +} + // testService encapsulates objects needed for the simulation type testService struct { lock sync.Mutex @@ -277,6 +287,8 @@ func TestPingPongChequeSimulation(t *testing.T) { log.Info("Initializing") + smallMessagePrice := ChequeDebtTolerance / 10 + // we are going to use the metrics system to sync the test // we are only going to continue with the next iteration after the message // has been received on the other side @@ -287,7 +299,7 @@ func TestPingPongChequeSimulation(t *testing.T) { counter := cter.(metrics.Counter) counter.Clear() var lastCount int64 - expectedPayout1, expectedPayout2 := DefaultPaymentThreshold+1, DefaultPaymentThreshold+1 + expectedPayout1, expectedPayout2 := smallMessagePrice, smallMessagePrice _, err = sim.AddNodesAndConnectFull(nodeCount) if err != nil { @@ -339,23 +351,23 @@ func TestPingPongChequeSimulation(t *testing.T) { for i := 0; i < maxCheques; i++ { if i%2 == 0 { - if err := p2Peer.Send(context.Background(), &testMsgBigPrice{}); err != nil { + if err := p2Peer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { t.Fatal(err) } if err := waitForChequeProcessed(t, params.backend, counter, lastCount, ts1.swap.peers[p2], expectedPayout1); err != nil { t.Fatal(err) } lastCount += 1 - expectedPayout1 += DefaultPaymentThreshold + 1 + expectedPayout1 += smallMessagePrice } else { - if err := p1Peer.Send(context.Background(), &testMsgBigPrice{}); err != nil { + if err := p1Peer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { t.Fatal(err) } if err := waitForChequeProcessed(t, params.backend, counter, lastCount, ts2.swap.peers[p1], expectedPayout2); err != nil { t.Fatal(err) } lastCount += 1 - expectedPayout2 += DefaultPaymentThreshold + 1 + expectedPayout2 += smallMessagePrice } } From 4d33e9e7616558f47d14d159879dc50ef3cf03ec Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 16 Jan 2020 15:52:16 -0300 Subject: [PATCH 27/49] swap: remove TestPingPongChequeSimulation --- swap/simulations_test.go | 131 --------------------------------------- 1 file changed, 131 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index d32bcf2a5c..e2ac140088 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -260,137 +260,6 @@ func newSharedBackendSwaps(t *testing.T, nodeCount int) (*swapSimulationParams, return params, nil } -// TestPingPongChequeSimulation just launches two nodes and sends each other -// messages which immediately crosses the PaymentThreshold and triggers cheques -// to each other. Checks that accounting and cheque handling works across multiple -// cheques and also if cheques are mutually sent -func TestPingPongChequeSimulation(t *testing.T) { - nodeCount := 2 - // create the shared backend and params - params, err := newSharedBackendSwaps(t, nodeCount) - if err != nil { - t.Fatal(err) - } - // cleanup backend - defer params.backend.Close() - - // setup the wait for mined transaction function for testing - cleanup := setupContractTest() - defer cleanup() - - params.backend.cashDone = make(chan struct{}, 1) - defer close(params.backend.cashDone) - - // initialize the simulation - sim := simulation.NewBzzInProc(newSimServiceMap(params), false) - defer sim.Close() - - log.Info("Initializing") - - smallMessagePrice := ChequeDebtTolerance / 10 - - // we are going to use the metrics system to sync the test - // we are only going to continue with the next iteration after the message - // has been received on the other side - metricsReg := metrics.AccountingRegistry - // testMsgBigPrice is paid by the sender, so the credit counter will only be - // increased when receiving the message, which is what we want for this test - cter := metricsReg.Get("account.msg.credit") - counter := cter.(metrics.Counter) - counter.Clear() - var lastCount int64 - expectedPayout1, expectedPayout2 := smallMessagePrice, smallMessagePrice - - _, err = sim.AddNodesAndConnectFull(nodeCount) - if err != nil { - t.Fatal(err) - } - - p1 := sim.UpNodeIDs()[0] - p2 := sim.UpNodeIDs()[1] - ts1 := sim.Service("swap", p1).(*testService) - ts2 := sim.Service("swap", p2).(*testService) - - var ts1Len, ts2Len, ts1sLen, ts2sLen int - timeout := time.After(10 * time.Second) - - for { - // let's always be nice and allow a time out to be catched - select { - case <-timeout: - t.Fatal("Timed out waiting for all swap peer connections to be established") - default: - } - // the node has all other peers in its peer list - ts1.swap.peersLock.Lock() - ts1Len = len(ts1.swap.peers) - ts1sLen = len(ts1.peers) - ts1.swap.peersLock.Unlock() - - ts2.swap.peersLock.Lock() - ts2Len = len(ts2.swap.peers) - ts2sLen = len(ts2.peers) - ts2.swap.peersLock.Unlock() - - if ts1Len == 1 && ts2Len == 1 && ts1sLen == 1 && ts2sLen == 1 { - break - } - // don't overheat the CPU... - time.Sleep(5 * time.Millisecond) - } - - maxCheques := 42 - - ts1.lock.Lock() - p2Peer := ts1.peers[p2] - ts1.lock.Unlock() - - ts2.lock.Lock() - p1Peer := ts2.peers[p1] - ts2.lock.Unlock() - - for i := 0; i < maxCheques; i++ { - if i%2 == 0 { - if err := p2Peer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { - t.Fatal(err) - } - if err := waitForChequeProcessed(t, params.backend, counter, lastCount, ts1.swap.peers[p2], expectedPayout1); err != nil { - t.Fatal(err) - } - lastCount += 1 - expectedPayout1 += smallMessagePrice - } else { - if err := p1Peer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { - t.Fatal(err) - } - if err := waitForChequeProcessed(t, params.backend, counter, lastCount, ts2.swap.peers[p1], expectedPayout2); err != nil { - t.Fatal(err) - } - lastCount += 1 - expectedPayout2 += smallMessagePrice - } - } - - ch1, err := ts2.swap.loadLastReceivedCheque(p1) - if err != nil { - t.Fatal(err) - } - ch2, err := ts1.swap.loadLastReceivedCheque(p2) - if err != nil { - t.Fatal(err) - } - - expected := uint64(maxCheques) / 2 * (DefaultPaymentThreshold + 1) - if !ch1.CumulativePayout.Equals(uint256.FromUint64(expected)) { - t.Fatalf("expected cumulative payout to be %d, but is %v", expected, ch1.CumulativePayout) - } - if !ch2.CumulativePayout.Equals(uint256.FromUint64(expected)) { - t.Fatalf("expected cumulative payout to be %d, but is %v", expected, ch2.CumulativePayout) - } - - log.Info("Simulation ended") -} - // TestMultiChequeSimulation just launches two nodes, a creditor and a debitor. // The debitor is the one owing to the creditor, so the debitor is the one sending cheques // It sends multiple cheques in a sequence to the same node. From de01e2df2d629c0b51e617d1d3d79fb4065ce9ab Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 16 Jan 2020 16:16:22 -0300 Subject: [PATCH 28/49] swap: iterate TestMultiChequeSimulation --- swap/simulations_test.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index e2ac140088..0cfc27f1ca 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -286,7 +286,7 @@ func TestMultiChequeSimulation(t *testing.T) { defer sim.Close() log.Info("Initializing") - + msgPrice := (&testMsgSmallPrice{}).Price().Value // we are going to use the metrics system to sync the test // we are only going to continue with the next iteration after the message // has been received on the other side @@ -297,7 +297,7 @@ func TestMultiChequeSimulation(t *testing.T) { counter := cter.(metrics.Counter) counter.Clear() var lastCount int64 - expectedPayout := DefaultPaymentThreshold + 1 + expectedPayout := msgPrice _, err = sim.AddNodesAndConnectFull(nodeCount) if err != nil { @@ -339,27 +339,28 @@ func TestMultiChequeSimulation(t *testing.T) { time.Sleep(5 * time.Millisecond) } - // we will send just maxCheques number of cheques - maxCheques := 10 + // we will send just maxMsgs number of messages + maxMsgs := 10 // the peer object used for sending debitorSvc.lock.Lock() creditorPeer := debitorSvc.peers[creditor] debitorSvc.lock.Unlock() - // send maxCheques number of cheques - for i := 0; i < maxCheques; i++ { - // use a price which will trigger a cheque each time - if err := creditorPeer.Send(context.Background(), &testMsgBigPrice{}); err != nil { - t.Fatal(err) - } - // we need to wait a bit in order to give time for the cheque to be processed - if err := waitForChequeProcessed(t, params.backend, counter, lastCount, debitorSvc.swap.peers[creditor], expectedPayout); err != nil { + // send maxMsgs number of msgs + for i := 0; i < maxMsgs; i++ { + if err := creditorPeer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { t.Fatal(err) } + // // we need to wait a bit in order to give time for the cheque to be processed + // if err := waitForChequeProcessed(t, params.backend, counter, lastCount, debitorSvc.swap.peers[creditor], expectedPayout); err != nil { + // t.Fatal(err) + // } lastCount += 1 - expectedPayout += DefaultPaymentThreshold + 1 + expectedPayout += msgPrice } + // Give enough time for all messages to be processed + time.Sleep(5 * time.Millisecond) // check balances: b1, err := debitorSvc.swap.loadBalance(creditor) @@ -389,7 +390,7 @@ func TestMultiChequeSimulation(t *testing.T) { } // check also the actual expected amount - expectedPayout = uint64(maxCheques) * (DefaultPaymentThreshold + 1) + expectedPayout = uint64(maxMsgs) * (msgPrice) if !cheque2.CumulativePayout.Equals(uint256.FromUint64(expectedPayout)) { t.Fatalf("Expected %d in cumulative payout, got %v", expectedPayout, cheque1.CumulativePayout) From 586953317cebc9493aa1520d565ed7ec45a746cf Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 16 Jan 2020 16:26:51 -0300 Subject: [PATCH 29/49] swap: iterate TestMultiChequeSimulation --- swap/simulations_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index 0cfc27f1ca..03ed3531d4 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -352,10 +352,13 @@ func TestMultiChequeSimulation(t *testing.T) { if err := creditorPeer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { t.Fatal(err) } - // // we need to wait a bit in order to give time for the cheque to be processed - // if err := waitForChequeProcessed(t, params.backend, counter, lastCount, debitorSvc.swap.peers[creditor], expectedPayout); err != nil { - // t.Fatal(err) - // } + if uint64(i+1)*msgPrice >= uint64(debitorSvc.swap.params.PaymentThreshold) { + // we need to wait a bit in order to give time for the cheque to be processed + if err := waitForChequeProcessed(t, params.backend, counter, lastCount, debitorSvc.swap.peers[creditor], expectedPayout); err != nil { + t.Fatal(err) + } + } + lastCount += 1 expectedPayout += msgPrice } From 78561f2f574238de1152faae9006c7a8edc928ab Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 16 Jan 2020 16:42:05 -0300 Subject: [PATCH 30/49] swap: iterate TestMultiChequeSimulation --- swap/simulations_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index 03ed3531d4..ce26708189 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -340,7 +340,7 @@ func TestMultiChequeSimulation(t *testing.T) { } // we will send just maxMsgs number of messages - maxMsgs := 10 + maxMsgs := 150 // the peer object used for sending debitorSvc.lock.Lock() @@ -352,7 +352,12 @@ func TestMultiChequeSimulation(t *testing.T) { if err := creditorPeer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { t.Fatal(err) } - if uint64(i+1)*msgPrice >= uint64(debitorSvc.swap.params.PaymentThreshold) { + debitorBalance, err := debitorSvc.swap.loadBalance(creditor) + if err != nil { + t.Fatal(err) + } + // check if cheque should have been sent + if debitorBalance <= -debitorSvc.swap.params.PaymentThreshold { // we need to wait a bit in order to give time for the cheque to be processed if err := waitForChequeProcessed(t, params.backend, counter, lastCount, debitorSvc.swap.peers[creditor], expectedPayout); err != nil { t.Fatal(err) @@ -362,7 +367,7 @@ func TestMultiChequeSimulation(t *testing.T) { lastCount += 1 expectedPayout += msgPrice } - // Give enough time for all messages to be processed + // give enough time for all messages to be processed time.Sleep(5 * time.Millisecond) // check balances: From b4496070e3e7c73777c50a640ed1ad86841d1a30 Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 16 Jan 2020 17:00:34 -0300 Subject: [PATCH 31/49] swap: iterate TestMultiChequeSimulation --- swap/simulations_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index ce26708189..fc74d95f36 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -340,7 +340,7 @@ func TestMultiChequeSimulation(t *testing.T) { } // we will send just maxMsgs number of messages - maxMsgs := 150 + maxMsgs := 160 // the peer object used for sending debitorSvc.lock.Lock() @@ -349,15 +349,16 @@ func TestMultiChequeSimulation(t *testing.T) { // send maxMsgs number of msgs for i := 0; i < maxMsgs; i++ { - if err := creditorPeer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { - t.Fatal(err) - } debitorBalance, err := debitorSvc.swap.loadBalance(creditor) if err != nil { t.Fatal(err) } + + if err := creditorPeer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { + t.Fatal(err) + } // check if cheque should have been sent - if debitorBalance <= -debitorSvc.swap.params.PaymentThreshold { + if debitorBalance-int64(msgPrice) <= -debitorSvc.swap.params.PaymentThreshold { // we need to wait a bit in order to give time for the cheque to be processed if err := waitForChequeProcessed(t, params.backend, counter, lastCount, debitorSvc.swap.peers[creditor], expectedPayout); err != nil { t.Fatal(err) From 1e815e8147430f36b0f424d4c78813a411fef364 Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 16 Jan 2020 17:25:51 -0300 Subject: [PATCH 32/49] swap: iterate TestMultiChequeSimulation --- swap/simulations_test.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index fc74d95f36..3746837d75 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -297,7 +297,7 @@ func TestMultiChequeSimulation(t *testing.T) { counter := cter.(metrics.Counter) counter.Clear() var lastCount int64 - expectedPayout := msgPrice + var expectedPayout uint64 _, err = sim.AddNodesAndConnectFull(nodeCount) if err != nil { @@ -340,7 +340,7 @@ func TestMultiChequeSimulation(t *testing.T) { } // we will send just maxMsgs number of messages - maxMsgs := 160 + maxMsgs := 150 // the peer object used for sending debitorSvc.lock.Lock() @@ -358,15 +358,16 @@ func TestMultiChequeSimulation(t *testing.T) { t.Fatal(err) } // check if cheque should have been sent - if debitorBalance-int64(msgPrice) <= -debitorSvc.swap.params.PaymentThreshold { + balanceAfterMessage := debitorBalance - int64(msgPrice) + if balanceAfterMessage <= -debitorSvc.swap.params.PaymentThreshold { // we need to wait a bit in order to give time for the cheque to be processed if err := waitForChequeProcessed(t, params.backend, counter, lastCount, debitorSvc.swap.peers[creditor], expectedPayout); err != nil { t.Fatal(err) } + expectedPayout += uint64(-balanceAfterMessage) } lastCount += 1 - expectedPayout += msgPrice } // give enough time for all messages to be processed time.Sleep(5 * time.Millisecond) @@ -398,9 +399,6 @@ func TestMultiChequeSimulation(t *testing.T) { t.Fatalf("Expected symmetric cheques payout, but they are not: %v vs %v", cheque1.CumulativePayout, cheque2.CumulativePayout) } - // check also the actual expected amount - expectedPayout = uint64(maxMsgs) * (msgPrice) - if !cheque2.CumulativePayout.Equals(uint256.FromUint64(expectedPayout)) { t.Fatalf("Expected %d in cumulative payout, got %v", expectedPayout, cheque1.CumulativePayout) } From f34200aa15fee329e7f110d150046ff5c12b88dc Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 17 Jan 2020 09:21:55 -0300 Subject: [PATCH 33/49] swap: iterate TestMultiChequeSimulation --- swap/simulations_test.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index 3746837d75..42aef318c4 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -23,6 +23,7 @@ import ( "fmt" "io/ioutil" "math/big" + "math/rand" "os" "strings" "sync" @@ -339,16 +340,24 @@ func TestMultiChequeSimulation(t *testing.T) { time.Sleep(5 * time.Millisecond) } - // we will send just maxMsgs number of messages - maxMsgs := 150 + paymentThreshold := debitorSvc.swap.params.PaymentThreshold + + minCheques := 2 + maxCheques := 5 + + msgsPerCheque := (uint64(paymentThreshold) / msgPrice) + 1 // +1 to round up without casting to float + + minMsgs := msgsPerCheque * uint64(minCheques) + maxMsgs := msgsPerCheque * uint64(maxCheques) + + msgAmount := rand.Intn(int(maxMsgs-minMsgs)) + int(minMsgs) // the peer object used for sending debitorSvc.lock.Lock() creditorPeer := debitorSvc.peers[creditor] debitorSvc.lock.Unlock() - // send maxMsgs number of msgs - for i := 0; i < maxMsgs; i++ { + for i := 0; i < msgAmount; i++ { debitorBalance, err := debitorSvc.swap.loadBalance(creditor) if err != nil { t.Fatal(err) @@ -357,9 +366,10 @@ func TestMultiChequeSimulation(t *testing.T) { if err := creditorPeer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { t.Fatal(err) } + // check if cheque should have been sent balanceAfterMessage := debitorBalance - int64(msgPrice) - if balanceAfterMessage <= -debitorSvc.swap.params.PaymentThreshold { + if balanceAfterMessage <= -paymentThreshold { // we need to wait a bit in order to give time for the cheque to be processed if err := waitForChequeProcessed(t, params.backend, counter, lastCount, debitorSvc.swap.peers[creditor], expectedPayout); err != nil { t.Fatal(err) @@ -367,7 +377,7 @@ func TestMultiChequeSimulation(t *testing.T) { expectedPayout += uint64(-balanceAfterMessage) } - lastCount += 1 + lastCount++ } // give enough time for all messages to be processed time.Sleep(5 * time.Millisecond) From 585e77d7522e7e861526eb931b1148bfb5c0bb9c Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 17 Jan 2020 10:58:11 -0300 Subject: [PATCH 34/49] swap: iterate TestMultiChequeSimulation, remove testMsgBigPrice --- swap/config.go | 2 +- swap/simulations_test.go | 16 +++------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/swap/config.go b/swap/config.go index c1c8346e21..3fd96c7e7a 100644 --- a/swap/config.go +++ b/swap/config.go @@ -29,7 +29,7 @@ const ( DefaultDisconnectThreshold = 20 * DefaultPaymentThreshold // ChequeDebtTolerance is the lowest resulting balance a node is willing to accept when receiving a cheque // the value is meant to be used below 0, as positive resulting balances should always be accepted when receiving cheques - ChequeDebtTolerance = DefaultPaymentThreshold / 10 // roughly 10% of the payment threshold + ChequeDebtTolerance = DefaultPaymentThreshold / 20 // roughly 20% of the payment threshold // DefaultDepositAmount is the default amount to send to the contract when initially deploying // NOTE: deliberate value for now; needs experimentation DefaultDepositAmount = 0 diff --git a/swap/simulations_test.go b/swap/simulations_test.go index 42aef318c4..2ca57220c6 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -75,7 +75,6 @@ type swapSimulationParams struct { // define test message types type testMsgBySender struct{} type testMsgByReceiver struct{} -type testMsgBigPrice struct{} type testMsgSmallPrice struct{} // create a test Spec; every node has its Spec and its accounting Hook @@ -87,7 +86,6 @@ func newTestSpec() *protocols.Spec { Messages: []interface{}{ testMsgBySender{}, testMsgByReceiver{}, - testMsgBigPrice{}, testMsgSmallPrice{}, }, } @@ -109,17 +107,9 @@ func (m *testMsgByReceiver) Price() *protocols.Price { } } -func (m *testMsgBigPrice) Price() *protocols.Price { - return &protocols.Price{ - Value: DefaultPaymentThreshold + 1, - PerByte: false, - Payer: protocols.Sender, - } -} - func (m *testMsgSmallPrice) Price() *protocols.Price { return &protocols.Price{ - Value: ChequeDebtTolerance / 10, // ensures that the message won't put nodes into debt + Value: DefaultPaymentThreshold / 100, // ensures that the message won't put nodes into debt PerByte: false, Payer: protocols.Sender, } @@ -292,7 +282,7 @@ func TestMultiChequeSimulation(t *testing.T) { // we are only going to continue with the next iteration after the message // has been received on the other side metricsReg := metrics.AccountingRegistry - // testMsgBigPrice is paid by the sender, so the credit counter will only be + // testMsgSmallPrice is paid by the sender, so the credit counter will only be // increased when receiving the message, which is what we want for this test cter := metricsReg.Get("account.msg.credit") counter := cter.(metrics.Counter) @@ -380,7 +370,7 @@ func TestMultiChequeSimulation(t *testing.T) { lastCount++ } // give enough time for all messages to be processed - time.Sleep(5 * time.Millisecond) + time.Sleep(10 * time.Millisecond) // check balances: b1, err := debitorSvc.swap.loadBalance(creditor) From 5e53773e97d5262bf1b0d65f778ae4cd6ed5e42b Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 17 Jan 2020 14:17:02 -0300 Subject: [PATCH 35/49] swap: fix wrong ChequeDebtTolerance computation --- swap/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/config.go b/swap/config.go index 3fd96c7e7a..ee66e8ad25 100644 --- a/swap/config.go +++ b/swap/config.go @@ -29,7 +29,7 @@ const ( DefaultDisconnectThreshold = 20 * DefaultPaymentThreshold // ChequeDebtTolerance is the lowest resulting balance a node is willing to accept when receiving a cheque // the value is meant to be used below 0, as positive resulting balances should always be accepted when receiving cheques - ChequeDebtTolerance = DefaultPaymentThreshold / 20 // roughly 20% of the payment threshold + ChequeDebtTolerance = DefaultPaymentThreshold * 20 / 100 // roughly 20% of the payment threshold // DefaultDepositAmount is the default amount to send to the contract when initially deploying // NOTE: deliberate value for now; needs experimentation DefaultDepositAmount = 0 From fd99c9cb8ba94fdbdd9f5e1e0dc3ad7f06fc253d Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 17 Jan 2020 14:32:02 -0300 Subject: [PATCH 36/49] swap: call Equal for cheques comparison in TestDebtCheques --- swap/swap_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 39be894f3b..89dee19097 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -757,7 +757,7 @@ func TestDebtCheques(t *testing.T) { if creditor.getLastSentCheque() != nil { t.Fatalf("expected no cheque sent to peer %v, but it is %v", creditor.ID(), creditor.getLastSentCheque()) } - if creditor.getPendingCheque() != cheque { + if !creditor.getPendingCheque().Equal(cheque) { t.Fatalf("expected pending cheque for peer %v to be %v, but is %v", creditor.ID(), cheque, creditor.getPendingCheque()) } @@ -785,9 +785,9 @@ func TestDebtCheques(t *testing.T) { } // simulate cheque confirmation - debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ + /* debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ Cheque: cheque, - }) + }) */ // balance should now be 0 according to the debitor if creditor.getBalance() != 0 { @@ -802,7 +802,7 @@ func TestDebtCheques(t *testing.T) { t.Fatalf("expected no pending cheque for peer %v, but found %v", creditor.ID(), creditor.getPendingCheque()) } // verify last received cheque - if debitor.getLastReceivedCheque() != cheque { + if !debitor.getLastReceivedCheque().Equal(cheque) { t.Fatalf("expected last received cheque from peer %v to be %v, but found %v", debitor.ID(), cheque, creditor.getLastReceivedCheque()) } } From 9102171c31c4ad98789bcf55da4d376a1df5740b Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 17 Jan 2020 15:50:48 -0300 Subject: [PATCH 37/49] swap: iterate TestDebtCheques --- swap/swap_test.go | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 89dee19097..7fbaa6e3cf 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -718,22 +718,6 @@ func TestDebtCheques(t *testing.T) { t.Fatal(err) } - // the creditor and debitor should start out with a balance of 0 - if creditor.getBalance() != 0 { - t.Fatalf("expected balance to be 0 for peer %v, but it is %d", creditor.ID(), creditor.getBalance()) - } - if debitor.getBalance() != 0 { - t.Fatalf("expected balance to be 0 for peer %v, but it is %d", debitor.ID(), debitor.getBalance()) - } - - // no cheques should be present at this point - if creditor.getLastSentCheque() != nil { - t.Fatalf("expected no cheque sent to peer %v, but it is %v", creditor.ID(), creditor.getLastSentCheque()) - } - if debitor.getLastReceivedCheque() != nil { - t.Fatalf("expected no cheque received from peer %v, but it is %v", debitor.ID(), debitor.getLastReceivedCheque()) - } - // set asymmetric balance and attempt to send cheque if err = creditor.setBalance(-int64(ChequeDebtTolerance * 2)); err != nil { t.Fatal(err) @@ -749,8 +733,8 @@ func TestDebtCheques(t *testing.T) { Cheque: cheque, }) // cheque should not have gone through as it would put the creditor in debt - if err == nil { - t.Fatal("expected invalid cheque reception to cause a failure, but got none") + if err == nil || !strings.Contains(err.Error(), "cause debt") { + t.Fatalf("expected invalid cheque reception to trigger debt cheque error, but got: %v", err) } // since the emission caused an error, the confirmation should not be simulated // cheque should not be marked as sent, and should be pending @@ -785,9 +769,9 @@ func TestDebtCheques(t *testing.T) { } // simulate cheque confirmation - /* debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ + debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ Cheque: cheque, - }) */ + }) // balance should now be 0 according to the debitor if creditor.getBalance() != 0 { @@ -797,14 +781,6 @@ func TestDebtCheques(t *testing.T) { if debitor.getBalance() != -int64(ChequeDebtTolerance) { t.Fatalf("expected balance to be %d for peer %v, but it is %d", -int64(ChequeDebtTolerance), debitor.ID(), debitor.getBalance()) } - // no cheque should be pending - if creditor.getPendingCheque() != nil { - t.Fatalf("expected no pending cheque for peer %v, but found %v", creditor.ID(), creditor.getPendingCheque()) - } - // verify last received cheque - if !debitor.getLastReceivedCheque().Equal(cheque) { - t.Fatalf("expected last received cheque from peer %v to be %v, but found %v", debitor.ID(), cheque, creditor.getLastReceivedCheque()) - } } // generate bookings based on parameters, apply them to a Swap struct and verify the result From 1ff452ca0913dd189b1440bc26177b6d18319e88 Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 17 Jan 2020 15:52:07 -0300 Subject: [PATCH 38/49] swap: iterate TestDebtCheques --- swap/swap_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 7fbaa6e3cf..62c70f9028 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -736,7 +736,6 @@ func TestDebtCheques(t *testing.T) { if err == nil || !strings.Contains(err.Error(), "cause debt") { t.Fatalf("expected invalid cheque reception to trigger debt cheque error, but got: %v", err) } - // since the emission caused an error, the confirmation should not be simulated // cheque should not be marked as sent, and should be pending if creditor.getLastSentCheque() != nil { t.Fatalf("expected no cheque sent to peer %v, but it is %v", creditor.ID(), creditor.getLastSentCheque()) From f4757e2f7b66b90f2a20f606d5daaa5274935726 Mon Sep 17 00:00:00 2001 From: mortelli Date: Fri, 17 Jan 2020 16:04:13 -0300 Subject: [PATCH 39/49] swap: iterate TestDebtCheques --- swap/swap_test.go | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 62c70f9028..7ac67c2af5 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -734,14 +734,7 @@ func TestDebtCheques(t *testing.T) { }) // cheque should not have gone through as it would put the creditor in debt if err == nil || !strings.Contains(err.Error(), "cause debt") { - t.Fatalf("expected invalid cheque reception to trigger debt cheque error, but got: %v", err) - } - // cheque should not be marked as sent, and should be pending - if creditor.getLastSentCheque() != nil { - t.Fatalf("expected no cheque sent to peer %v, but it is %v", creditor.ID(), creditor.getLastSentCheque()) - } - if !creditor.getPendingCheque().Equal(cheque) { - t.Fatalf("expected pending cheque for peer %v to be %v, but is %v", creditor.ID(), cheque, creditor.getPendingCheque()) + t.Fatalf("expected invalid cheque to trigger debt cheque error, but got: %v", err) } // clear pending cheque to start over @@ -766,20 +759,6 @@ func TestDebtCheques(t *testing.T) { if err != nil { t.Fatal(err) } - - // simulate cheque confirmation - debitorSwap.handleConfirmChequeMsg(ctx, creditor, &ConfirmChequeMsg{ - Cheque: cheque, - }) - - // balance should now be 0 according to the debitor - if creditor.getBalance() != 0 { - t.Fatalf("expected balance to be 0 for peer %v, but it is %d", creditor.ID(), creditor.getBalance()) - } - // balance should now be -ChequeDebtTolerance according to the debitor - if debitor.getBalance() != -int64(ChequeDebtTolerance) { - t.Fatalf("expected balance to be %d for peer %v, but it is %d", -int64(ChequeDebtTolerance), debitor.ID(), debitor.getBalance()) - } } // generate bookings based on parameters, apply them to a Swap struct and verify the result From 5010458c635187d3de7b089fee48b49cc24cf105 Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 20 Jan 2020 09:39:59 -0300 Subject: [PATCH 40/49] swap: add debug log to TestMultiChequeSimulation --- swap/simulations_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index 2ca57220c6..a3e5b251c6 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -341,6 +341,7 @@ func TestMultiChequeSimulation(t *testing.T) { maxMsgs := msgsPerCheque * uint64(maxCheques) msgAmount := rand.Intn(int(maxMsgs-minMsgs)) + int(minMsgs) + log.Debug("sending %d messages", msgAmount) // the peer object used for sending debitorSvc.lock.Lock() @@ -383,6 +384,7 @@ func TestMultiChequeSimulation(t *testing.T) { } if b1 != -b2 { + fmt.Printf("balances") t.Fatalf("Expected symmetric balances, but they are not: %d vs %d", b1, b2) } // check cheques From 39f3aae31d334220914f7846f1f702101e359bbf Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 20 Jan 2020 12:27:50 -0300 Subject: [PATCH 41/49] swap: add check for all messages received in TestMultiChequeSimulation, fix typos --- swap/simulations_test.go | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index a3e5b251c6..004644a01c 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -306,7 +306,7 @@ func TestMultiChequeSimulation(t *testing.T) { var debLen, credLen, debSwapLen, credSwapLen int timeout := time.After(10 * time.Second) for { - // let's always be nice and allow a time out to be catched + // let's always be nice and allow a time out to be caught select { case <-timeout: t.Fatal("Timed out waiting for all swap peer connections to be established") @@ -348,6 +348,27 @@ func TestMultiChequeSimulation(t *testing.T) { creditorPeer := debitorSvc.peers[creditor] debitorSvc.lock.Unlock() + allMessagesArrived := make(chan struct{}) + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + go func() { + for { + select { + case <-ctx.Done(): + return + default: + } + // all messages have been received + if counter.Count() == int64(msgAmount) { + close(allMessagesArrived) + return + } + time.Sleep(10 * time.Millisecond) + } + }() + for i := 0; i < msgAmount; i++ { debitorBalance, err := debitorSvc.swap.loadBalance(creditor) if err != nil { @@ -371,7 +392,12 @@ func TestMultiChequeSimulation(t *testing.T) { lastCount++ } // give enough time for all messages to be processed - time.Sleep(10 * time.Millisecond) + select { + case <-ctx.Done(): + t.Fatal("timed out waiting for all messages to arrive, aborting") + case <-allMessagesArrived: + } + log.Debug("all messages arrived") // check balances: b1, err := debitorSvc.swap.loadBalance(creditor) @@ -613,7 +639,7 @@ func waitForChequeProcessed(t *testing.T, backend *swapTestBackend, counter metr select { case <-ctx.Done(): lock.Lock() - errs = append(errs, "Timed out waiting for cheque to have been cached") + errs = append(errs, "Timed out waiting for cheque to be cashed") lock.Unlock() wg.Done() return @@ -653,7 +679,7 @@ func waitForChequeProcessed(t *testing.T, backend *swapTestBackend, counter metr select { case <-ctx.Done(): lock.Lock() - errs = append(errs, "Timed out waiting for peer to have processed accounted message") + errs = append(errs, "Timed out waiting for peer to process accounted message") lock.Unlock() wg.Done() return From 5b6f493fa5836366a2501d8a804be84e89616505 Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 20 Jan 2020 14:35:56 -0300 Subject: [PATCH 42/49] swap: remove debug print in TestMultiChequeSimulation --- swap/simulations_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index 004644a01c..fbc161e76a 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -410,7 +410,6 @@ func TestMultiChequeSimulation(t *testing.T) { } if b1 != -b2 { - fmt.Printf("balances") t.Fatalf("Expected symmetric balances, but they are not: %d vs %d", b1, b2) } // check cheques From 6ca077b5c714dc944ea27ca80ac129b4dbe2c6ea Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 20 Jan 2020 14:37:21 -0300 Subject: [PATCH 43/49] swap: small refactor to TestMultiChequeSimulation --- swap/simulations_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index fbc161e76a..49771df7d6 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -287,8 +287,7 @@ func TestMultiChequeSimulation(t *testing.T) { cter := metricsReg.Get("account.msg.credit") counter := cter.(metrics.Counter) counter.Clear() - var lastCount int64 - var expectedPayout uint64 + var lastCount, expectedPayout uint64 _, err = sim.AddNodesAndConnectFull(nodeCount) if err != nil { @@ -324,7 +323,7 @@ func TestMultiChequeSimulation(t *testing.T) { creditorSvc.swap.peersLock.Unlock() if debLen == 1 && credLen == 1 && debSwapLen == 1 && credSwapLen == 1 { - break + breakallMessagesArrived } // don't overheat the CPU... time.Sleep(5 * time.Millisecond) From 8e8df8a41327eaad8ca89853fc5f750399f26c71 Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 20 Jan 2020 14:37:41 -0300 Subject: [PATCH 44/49] Revert "swap: small refactor to TestMultiChequeSimulation" This reverts commit 6ca077b5c714dc944ea27ca80ac129b4dbe2c6ea. --- swap/simulations_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index 49771df7d6..fbc161e76a 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -287,7 +287,8 @@ func TestMultiChequeSimulation(t *testing.T) { cter := metricsReg.Get("account.msg.credit") counter := cter.(metrics.Counter) counter.Clear() - var lastCount, expectedPayout uint64 + var lastCount int64 + var expectedPayout uint64 _, err = sim.AddNodesAndConnectFull(nodeCount) if err != nil { @@ -323,7 +324,7 @@ func TestMultiChequeSimulation(t *testing.T) { creditorSvc.swap.peersLock.Unlock() if debLen == 1 && credLen == 1 && debSwapLen == 1 && credSwapLen == 1 { - breakallMessagesArrived + break } // don't overheat the CPU... time.Sleep(5 * time.Millisecond) From 3faf60aaec6c8b95cf5392fb17a236dcae29472f Mon Sep 17 00:00:00 2001 From: mortelli Date: Tue, 21 Jan 2020 10:37:10 -0300 Subject: [PATCH 45/49] swap: simplify TestDebtCheques function --- swap/swap_test.go | 49 ++++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 7ac67c2af5..629257a2ce 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -694,42 +694,34 @@ func TestDebtCheques(t *testing.T) { testBackend := newTestBackend(t) defer testBackend.Close() - creditorSwap, cClean := newTestSwap(t, beneficiaryKey, testBackend) - debitorSwap, dClean := newTestSwap(t, ownerKey, testBackend) - defer cClean() - defer dClean() + creditorSwap, cleanup := newTestSwap(t, beneficiaryKey, testBackend) + defer cleanup() ctx := context.Background() if err := testDeploy(ctx, creditorSwap, big.NewInt(0)); err != nil { t.Fatal(err) } - if err := testDeploy(ctx, debitorSwap, big.NewInt(int64(DefaultPaymentThreshold*2))); err != nil { - t.Fatal(err) - } - cDummyPeer := newDummyPeerWithSpec(Spec) - dDummyPeer := newDummyPeerWithSpec(Spec) - creditor, err := debitorSwap.addPeer(cDummyPeer.Peer, creditorSwap.owner.address, debitorSwap.GetParams().ContractAddress) + debitorChequebook, err := testDeployWithPrivateKey(ctx, testBackend, ownerKey, ownerAddress, big.NewInt(int64(DefaultPaymentThreshold*2))) if err != nil { t.Fatal(err) } - debitor, err := creditorSwap.addPeer(dDummyPeer.Peer, debitorSwap.owner.address, debitorSwap.GetParams().ContractAddress) + + debitorDummyPeer := newDummyPeerWithSpec(Spec) + debitorPeer, err := creditorSwap.addPeer(debitorDummyPeer.Peer, ownerAddress, debitorChequebook.ContractParams().ContractAddress) if err != nil { t.Fatal(err) } - // set asymmetric balance and attempt to send cheque - if err = creditor.setBalance(-int64(ChequeDebtTolerance * 2)); err != nil { - t.Fatal(err) - } - // now simulate sending the cheque to the creditor from the debitor - if err = creditor.sendCheque(); err != nil { + // create debt cheque + chequeAmount := big.NewInt(int64(ChequeDebtTolerance * 2)) + cheque, err := newSignedTestCheque(debitorChequebook.ContractParams().ContractAddress, creditorSwap.owner.address, chequeAmount, ownerKey) + if err != nil { t.Fatal(err) } - cheque := creditor.getPendingCheque() // simulate cheque handling - err = creditorSwap.handleEmitChequeMsg(ctx, debitor, &EmitChequeMsg{ + err = creditorSwap.handleEmitChequeMsg(ctx, debitorPeer, &EmitChequeMsg{ Cheque: cheque, }) // cheque should not have gone through as it would put the creditor in debt @@ -737,25 +729,18 @@ func TestDebtCheques(t *testing.T) { t.Fatalf("expected invalid cheque to trigger debt cheque error, but got: %v", err) } - // clear pending cheque to start over - if err = creditor.setPendingCheque(nil); err != nil { - t.Fatal(err) - } - - // set asymmetric balances to send a (barely) admissible cheque - if err = creditor.setBalance(-int64(ChequeDebtTolerance)); err != nil { - t.Fatal(err) - } - - if err = creditor.sendCheque(); err != nil { + // now create a (barely) admissible cheque + chequeAmount = big.NewInt(int64(ChequeDebtTolerance)) + cheque, err = newSignedTestCheque(debitorChequebook.ContractParams().ContractAddress, creditorSwap.owner.address, chequeAmount, ownerKey) + if err != nil { t.Fatal(err) } - cheque = creditor.getPendingCheque() // simulate cheque handling - err = creditorSwap.handleEmitChequeMsg(ctx, debitor, &EmitChequeMsg{ + err = creditorSwap.handleEmitChequeMsg(ctx, debitorPeer, &EmitChequeMsg{ Cheque: cheque, }) + // cheque should have gone through if err != nil { t.Fatal(err) } From 7d1ce9c1f942c72ae3aa0e1cbb1ee11b431dd393 Mon Sep 17 00:00:00 2001 From: mortelli Date: Wed, 22 Jan 2020 08:46:41 -0300 Subject: [PATCH 46/49] swap: remove extra context in TestMultiChequeSimulation --- swap/simulations_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index fbc161e76a..34375812e6 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -375,7 +375,7 @@ func TestMultiChequeSimulation(t *testing.T) { t.Fatal(err) } - if err := creditorPeer.Send(context.Background(), &testMsgSmallPrice{}); err != nil { + if err := creditorPeer.Send(ctx, &testMsgSmallPrice{}); err != nil { t.Fatal(err) } From 1d885fc29af374e3033ee90ef304d4583a6eb69f Mon Sep 17 00:00:00 2001 From: mortelli Date: Wed, 22 Jan 2020 09:09:32 -0300 Subject: [PATCH 47/49] swap: remove randomness in TestMultiChequeSimulation --- swap/simulations_test.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/swap/simulations_test.go b/swap/simulations_test.go index 34375812e6..ff084bda09 100644 --- a/swap/simulations_test.go +++ b/swap/simulations_test.go @@ -23,7 +23,6 @@ import ( "fmt" "io/ioutil" "math/big" - "math/rand" "os" "strings" "sync" @@ -332,15 +331,9 @@ func TestMultiChequeSimulation(t *testing.T) { paymentThreshold := debitorSvc.swap.params.PaymentThreshold - minCheques := 2 - maxCheques := 5 - + chequesAmount := 4 msgsPerCheque := (uint64(paymentThreshold) / msgPrice) + 1 // +1 to round up without casting to float - - minMsgs := msgsPerCheque * uint64(minCheques) - maxMsgs := msgsPerCheque * uint64(maxCheques) - - msgAmount := rand.Intn(int(maxMsgs-minMsgs)) + int(minMsgs) + msgAmount := int(msgsPerCheque) * chequesAmount log.Debug("sending %d messages", msgAmount) // the peer object used for sending From 77a153b0c3db00ab283ec35ad37316191de35284 Mon Sep 17 00:00:00 2001 From: mortelli Date: Wed, 22 Jan 2020 12:24:41 -0300 Subject: [PATCH 48/49] swap: fix wrong error message in sendCheque function --- swap/peer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/peer.go b/swap/peer.go index 174d71c953..83b8ad6f9c 100644 --- a/swap/peer.go +++ b/swap/peer.go @@ -212,7 +212,7 @@ func (p *Peer) sendCheque() error { honeyAmount := int64(cheque.Honey) err = p.updateBalance(honeyAmount) if err != nil { - return fmt.Errorf("error while creating cheque: %v", err) + return fmt.Errorf("error while updating balance: %v", err) } metrics.GetOrRegisterCounter("swap.cheques.emitted.num", nil).Inc(1) From 415e7f6049cf2f0c2ad86d89b1c8fa9dc6d55cf4 Mon Sep 17 00:00:00 2001 From: mortelli Date: Thu, 30 Jan 2020 17:37:49 -0300 Subject: [PATCH 49/49] swap: fix wrong types in tests --- swap/swap_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/swap/swap_test.go b/swap/swap_test.go index 698c27e870..cc0656bdb1 100644 --- a/swap/swap_test.go +++ b/swap/swap_test.go @@ -698,11 +698,11 @@ func TestDebtCheques(t *testing.T) { defer cleanup() ctx := context.Background() - if err := testDeploy(ctx, creditorSwap, big.NewInt(0)); err != nil { + if err := testDeploy(ctx, creditorSwap, uint256.FromUint64(0)); err != nil { t.Fatal(err) } - debitorChequebook, err := testDeployWithPrivateKey(ctx, testBackend, ownerKey, ownerAddress, big.NewInt(int64(DefaultPaymentThreshold*2))) + debitorChequebook, err := testDeployWithPrivateKey(ctx, testBackend, ownerKey, ownerAddress, uint256.FromUint64((DefaultPaymentThreshold * 2))) if err != nil { t.Fatal(err) } @@ -714,7 +714,7 @@ func TestDebtCheques(t *testing.T) { } // create debt cheque - chequeAmount := big.NewInt(int64(ChequeDebtTolerance * 2)) + chequeAmount := uint256.FromUint64(ChequeDebtTolerance * 2) cheque, err := newSignedTestCheque(debitorChequebook.ContractParams().ContractAddress, creditorSwap.owner.address, chequeAmount, ownerKey) if err != nil { t.Fatal(err) @@ -730,7 +730,7 @@ func TestDebtCheques(t *testing.T) { } // now create a (barely) admissible cheque - chequeAmount = big.NewInt(int64(ChequeDebtTolerance)) + chequeAmount = uint256.FromUint64(ChequeDebtTolerance) cheque, err = newSignedTestCheque(debitorChequebook.ContractParams().ContractAddress, creditorSwap.owner.address, chequeAmount, ownerKey) if err != nil { t.Fatal(err)