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