From fe7ffe3eb12299a0d7488b3304391047b6789787 Mon Sep 17 00:00:00 2001 From: Fabio Barone Date: Mon, 8 Oct 2018 13:26:30 -0500 Subject: [PATCH] swarm/swap: introduced sleep after retrieval for network test --- p2p/protocols/protocol.go | 3 +++ swarm/swap/swap.go | 5 ++--- swarm/swap/swap_test.go | 47 +++++++++++++++++++++++++++++++++++++++ swarm/swap_test.go | 22 ++++++++++-------- 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/p2p/protocols/protocol.go b/p2p/protocols/protocol.go index b0e1c3a865..5cbc254bca 100644 --- a/p2p/protocols/protocol.go +++ b/p2p/protocols/protocol.go @@ -353,6 +353,9 @@ func (p *Peer) handleIncoming(handle func(ctx context.Context, msg interface{}) } if p.spec.Hook != nil { + if wmsg.Size != uint32(len(wmsg.Payload)) { + log.Warn("Advertised message size and payload length don't match") + } err := p.spec.Hook.Receive(p, wmsg.Size, val) if err != nil { return err diff --git a/swarm/swap/swap.go b/swarm/swap/swap.go index fd5a334fb6..5e96514b0c 100644 --- a/swarm/swap/swap.go +++ b/swarm/swap/swap.go @@ -32,14 +32,13 @@ import ( const ( defaultMaxMsgSize = 1024 * 1024 - OracleID = "swap" swapProtocolName = "swap" swapVersion = 1 ) var ( - payAt = int64(-4096 * 10000) // threshold that triggers payment {request} (bytes) - dropAt = int64(-4096 * 12000) // threshold that triggers disconnect (bytes) + payAt = int64(-4096 * 10000000) // threshold that triggers payment {request} (bytes) + dropAt = int64(-4096 * 12000000) // threshold that triggers disconnect (bytes) ErrNotAccountedMsg = errors.New("Message does not need accounting") ErrInsufficientFunds = errors.New("Insufficient funds") diff --git a/swarm/swap/swap_test.go b/swarm/swap/swap_test.go index f2b460d92d..297fbf798d 100644 --- a/swarm/swap/swap_test.go +++ b/swarm/swap/swap_test.go @@ -23,6 +23,7 @@ import ( "fmt" "io/ioutil" "math" + mrand "math/rand" "os" "path/filepath" "testing" @@ -148,6 +149,52 @@ func TestLimits(t *testing.T) { } } +//check that the disconnect threshold is below the payment threshold +func TestRepeatedBookings(t *testing.T) { + //create a test swap account + swap, testDir := createTestSwap(t) + defer os.RemoveAll(testDir) + + testPeer := newDummyPeer() + amount := mrand.Intn(100) + cnt := 1 + mrand.Intn(10) + for i := 0; i < cnt; i++ { + swap.Credit(testPeer.Peer.Peer, uint64(amount)) + } + expectedBalance := int64(cnt * amount) + realBalance := swap.balances[testPeer.ID()] + if expectedBalance != realBalance { + t.Fatal(fmt.Sprintf("After %d credits of %d, expected balance to be: %d, but is: %d", cnt, amount, expectedBalance, realBalance)) + } + + testPeer2 := newDummyPeer() + amount = mrand.Intn(100) + cnt = 1 + mrand.Intn(10) + for i := 0; i < cnt; i++ { + swap.Debit(testPeer2.Peer.Peer, uint64(amount)) + } + expectedBalance = int64(0 - (cnt * amount)) + realBalance = swap.balances[testPeer2.ID()] + if expectedBalance != realBalance { + t.Fatal(fmt.Sprintf("After %d debits of %d, expected balance to be: %d, but is: %d", cnt, amount, expectedBalance, realBalance)) + } + + //mixed debits and credits + amount1 := mrand.Intn(100) + amount2 := mrand.Intn(100) + amount3 := mrand.Intn(100) + swap.Credit(testPeer2.Peer.Peer, uint64(amount1)) + swap.Credit(testPeer2.Peer.Peer, uint64(amount2)) + swap.Debit(testPeer2.Peer.Peer, uint64(amount3)) + + expectedBalance = expectedBalance + int64(amount1+amount2-amount3) + realBalance = swap.balances[testPeer2.ID()] + + if expectedBalance != realBalance { + t.Fatal(fmt.Sprintf("After mixed debits and credits, expected balance to be: %d, but is: %d", expectedBalance, realBalance)) + } +} + //unit test for exceeds pay limit //when the payment threshold is reached, a cheque will be issued //this test checks that a cheque is present if a message is sent diff --git a/swarm/swap_test.go b/swarm/swap_test.go index 755a698c1c..1a34626dcc 100644 --- a/swarm/swap_test.go +++ b/swarm/swap_test.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "io/ioutil" - "math" "math/rand" "os" "strconv" @@ -140,6 +139,8 @@ func TestSwapNetworkSymmetricFileUpload(t *testing.T) { break } } + + time.Sleep(5 * time.Second) //every node has a map to all nodes it had interactions //each entry in the map is a map of the other node with all the balances balancesMap := make(map[enode.ID]map[enode.ID]int64) @@ -165,6 +166,7 @@ func TestSwapNetworkSymmetricFileUpload(t *testing.T) { //get the peer's balance with this node balance, err := swarm.swap.GetPeerBalance(n) + fmt.Println(balance) if err == nil { subBalances[n] = balance log.Debug(fmt.Sprintf("Balance of node %s to node %s: %d", node.TerminalString(), n.TerminalString(), balance)) @@ -203,8 +205,8 @@ func TestSwapNetworkSymmetricFileUpload(t *testing.T) { log.Trace(fmt.Sprintf("balance of %s with %s: %d", k.TerminalString(), n.TerminalString(), balanceKwithN)) log.Trace(fmt.Sprintf("balance of %s with %s: %d", n.TerminalString(), k.TerminalString(), mapForSubK[k])) //...check that they have the same balance in Abs terms and that it is not 0 - if math.Abs(float64(balanceKwithN)) != math.Abs(float64(mapForSubK[k])) && balanceKwithN != 0 { - log.Error(fmt.Sprintf("Expected balances to be |abs| = 0 AND balance1 != 0, but they are not: %d, %d", balanceKwithN, mapForSubK[k])) + if balanceKwithN+mapForSubK[k] != 0 && balanceKwithN != 0 { + log.Error(fmt.Sprintf("Expected balances to be a+b = 0 AND balance(a) != 0, but they are not, balance k with n: %d, balance n with k: %d", balanceKwithN, mapForSubK[k])) success = false } } @@ -278,10 +280,10 @@ func TestSwapNetworkAsymmetricFileUpload(t *testing.T) { t.Fatal(err) } - //this is actually quite a big maxFileSize, which results - //in the test running for nearly 2 minutes - //maybe for the test, we could reduce it - const maxFileSize = 1024 * 1024 * 4 //1024 bytes * 1024 * 4 = 4MB + //NOTE: maxFileSize is 4 kB, this in order to provide faster tests + //it would be interesting to run these tests with bigger files + //(to see how drop limits are affected etc.) + const maxFileSize = 1024 * 4 //1024 bytes * 4 = 4kB const minfileSize = 1024 //pseudo random algo to define if a node will upload or not @@ -323,6 +325,8 @@ func TestSwapNetworkAsymmetricFileUpload(t *testing.T) { } } + time.Sleep(5 * time.Second) + balancesMap := make(map[enode.ID]map[enode.ID]int64) for _, node := range sim.NodeIDs() { @@ -370,8 +374,8 @@ func TestSwapNetworkAsymmetricFileUpload(t *testing.T) { if n == subK { log.Trace(fmt.Sprintf("balance of %s with %s: %d", k.TerminalString(), n.TerminalString(), balanceKwithN)) log.Trace(fmt.Sprintf("balance of %s with %s: %d", n.TerminalString(), k.TerminalString(), mapForSubK[k])) - if math.Abs(float64(balanceKwithN)) != math.Abs(float64(mapForSubK[k])) && balanceKwithN != 0 { - log.Error("Expected balances to be |abs| = 0 AND balance1 != 0, but they are not") + if balanceKwithN+mapForSubK[k] != 0 && balanceKwithN != 0 { + log.Error(fmt.Sprintf("Expected balances to be a+b = 0 AND balance(a) != 0, but they are not, balance k with n: %d, balance n with k: %d", balanceKwithN, mapForSubK[k])) success = false } }