github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/swarm/swap/swap_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package swap 18 19 import ( 20 "flag" 21 "fmt" 22 "io/ioutil" 23 mrand "math/rand" 24 "os" 25 "testing" 26 "time" 27 28 "github.com/ShyftNetwork/go-empyrean/log" 29 "github.com/ShyftNetwork/go-empyrean/p2p" 30 "github.com/ShyftNetwork/go-empyrean/p2p/protocols" 31 "github.com/ShyftNetwork/go-empyrean/p2p/simulations/adapters" 32 "github.com/ShyftNetwork/go-empyrean/swarm/state" 33 colorable "github.com/mattn/go-colorable" 34 ) 35 36 var ( 37 loglevel = flag.Int("loglevel", 2, "verbosity of logs") 38 ) 39 40 func init() { 41 flag.Parse() 42 mrand.Seed(time.Now().UnixNano()) 43 44 log.PrintOrigins(true) 45 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) 46 } 47 48 //Test getting a peer's balance 49 func TestGetPeerBalance(t *testing.T) { 50 //create a test swap account 51 swap, testDir := createTestSwap(t) 52 defer os.RemoveAll(testDir) 53 54 //test for correct value 55 testPeer := newDummyPeer() 56 swap.balances[testPeer.ID()] = 888 57 b, err := swap.GetPeerBalance(testPeer.ID()) 58 if err != nil { 59 t.Fatal(err) 60 } 61 if b != 888 { 62 t.Fatalf("Expected peer's balance to be %d, but is %d", 888, b) 63 } 64 65 //test for inexistent node 66 id := adapters.RandomNodeConfig().ID 67 _, err = swap.GetPeerBalance(id) 68 if err == nil { 69 t.Fatal("Expected call to fail, but it didn't!") 70 } 71 if err.Error() != "Peer not found" { 72 t.Fatalf("Expected test to fail with %s, but is %s", "Peer not found", err.Error()) 73 } 74 } 75 76 //Test that repeated bookings do correct accounting 77 func TestRepeatedBookings(t *testing.T) { 78 //create a test swap account 79 swap, testDir := createTestSwap(t) 80 defer os.RemoveAll(testDir) 81 82 testPeer := newDummyPeer() 83 amount := mrand.Intn(100) 84 cnt := 1 + mrand.Intn(10) 85 for i := 0; i < cnt; i++ { 86 swap.Add(int64(amount), testPeer.Peer) 87 } 88 expectedBalance := int64(cnt * amount) 89 realBalance := swap.balances[testPeer.ID()] 90 if expectedBalance != realBalance { 91 t.Fatal(fmt.Sprintf("After %d credits of %d, expected balance to be: %d, but is: %d", cnt, amount, expectedBalance, realBalance)) 92 } 93 94 testPeer2 := newDummyPeer() 95 amount = mrand.Intn(100) 96 cnt = 1 + mrand.Intn(10) 97 for i := 0; i < cnt; i++ { 98 swap.Add(0-int64(amount), testPeer2.Peer) 99 } 100 expectedBalance = int64(0 - (cnt * amount)) 101 realBalance = swap.balances[testPeer2.ID()] 102 if expectedBalance != realBalance { 103 t.Fatal(fmt.Sprintf("After %d debits of %d, expected balance to be: %d, but is: %d", cnt, amount, expectedBalance, realBalance)) 104 } 105 106 //mixed debits and credits 107 amount1 := mrand.Intn(100) 108 amount2 := mrand.Intn(55) 109 amount3 := mrand.Intn(999) 110 swap.Add(int64(amount1), testPeer2.Peer) 111 swap.Add(int64(0-amount2), testPeer2.Peer) 112 swap.Add(int64(0-amount3), testPeer2.Peer) 113 114 expectedBalance = expectedBalance + int64(amount1-amount2-amount3) 115 realBalance = swap.balances[testPeer2.ID()] 116 117 if expectedBalance != realBalance { 118 t.Fatal(fmt.Sprintf("After mixed debits and credits, expected balance to be: %d, but is: %d", expectedBalance, realBalance)) 119 } 120 } 121 122 //try restoring a balance from state store 123 //this is simulated by creating a node, 124 //assigning it an arbitrary balance, 125 //then closing the state store. 126 //Then we re-open the state store and check that 127 //the balance is still the same 128 func TestRestoreBalanceFromStateStore(t *testing.T) { 129 //create a test swap account 130 swap, testDir := createTestSwap(t) 131 defer os.RemoveAll(testDir) 132 133 testPeer := newDummyPeer() 134 swap.balances[testPeer.ID()] = -8888 135 136 tmpBalance := swap.balances[testPeer.ID()] 137 swap.stateStore.Put(testPeer.ID().String(), &tmpBalance) 138 139 swap.stateStore.Close() 140 swap.stateStore = nil 141 142 stateStore, err := state.NewDBStore(testDir) 143 if err != nil { 144 t.Fatal(err) 145 } 146 147 var newBalance int64 148 stateStore.Get(testPeer.ID().String(), &newBalance) 149 150 //compare the balances 151 if tmpBalance != newBalance { 152 t.Fatal(fmt.Sprintf("Unexpected balance value after sending cheap message test. Expected balance: %d, balance is: %d", 153 tmpBalance, newBalance)) 154 } 155 } 156 157 //create a test swap account 158 //creates a stateStore for persistence and a Swap account 159 func createTestSwap(t *testing.T) (*Swap, string) { 160 dir, err := ioutil.TempDir("", "swap_test_store") 161 if err != nil { 162 t.Fatal(err) 163 } 164 stateStore, err2 := state.NewDBStore(dir) 165 if err2 != nil { 166 t.Fatal(err2) 167 } 168 swap := New(stateStore) 169 return swap, dir 170 } 171 172 type dummyPeer struct { 173 *protocols.Peer 174 } 175 176 //creates a dummy protocols.Peer with dummy MsgReadWriter 177 func newDummyPeer() *dummyPeer { 178 id := adapters.RandomNodeConfig().ID 179 protoPeer := protocols.NewPeer(p2p.NewPeer(id, "testPeer", nil), nil, nil) 180 dummy := &dummyPeer{ 181 Peer: protoPeer, 182 } 183 return dummy 184 }