github.com/etherite/go-etherite@v0.0.0-20171015192807-5f4dd87b2f6e/swarm/services/swap/swap/swap_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package swap 18 19 import ( 20 "math/big" 21 "testing" 22 "time" 23 24 "github.com/etherite/go-etherite/common" 25 ) 26 27 type testInPayment struct { 28 received []*testPromise 29 autocashInterval time.Duration 30 autocashLimit *big.Int 31 } 32 33 type testPromise struct { 34 amount *big.Int 35 } 36 37 func (self *testInPayment) Receive(promise Promise) (*big.Int, error) { 38 p := promise.(*testPromise) 39 self.received = append(self.received, p) 40 return p.amount, nil 41 } 42 43 func (self *testInPayment) AutoCash(interval time.Duration, limit *big.Int) { 44 self.autocashInterval = interval 45 self.autocashLimit = limit 46 } 47 48 func (self *testInPayment) Cash() (string, error) { return "", nil } 49 50 func (self *testInPayment) Stop() {} 51 52 type testOutPayment struct { 53 deposits []*big.Int 54 autodepositInterval time.Duration 55 autodepositThreshold *big.Int 56 autodepositBuffer *big.Int 57 } 58 59 func (self *testOutPayment) Issue(amount *big.Int) (promise Promise, err error) { 60 return &testPromise{amount}, nil 61 } 62 63 func (self *testOutPayment) Deposit(amount *big.Int) (string, error) { 64 self.deposits = append(self.deposits, amount) 65 return "", nil 66 } 67 68 func (self *testOutPayment) AutoDeposit(interval time.Duration, threshold, buffer *big.Int) { 69 self.autodepositInterval = interval 70 self.autodepositThreshold = threshold 71 self.autodepositBuffer = buffer 72 } 73 74 func (self *testOutPayment) Stop() {} 75 76 type testProtocol struct { 77 drop bool 78 amounts []int 79 promises []*testPromise 80 } 81 82 func (self *testProtocol) Drop() { 83 self.drop = true 84 } 85 86 func (self *testProtocol) String() string { 87 return "" 88 } 89 90 func (self *testProtocol) Pay(amount int, promise Promise) { 91 p := promise.(*testPromise) 92 self.promises = append(self.promises, p) 93 self.amounts = append(self.amounts, amount) 94 } 95 96 func TestSwap(t *testing.T) { 97 98 strategy := &Strategy{ 99 AutoCashInterval: 1 * time.Second, 100 AutoCashThreshold: big.NewInt(20), 101 AutoDepositInterval: 1 * time.Second, 102 AutoDepositThreshold: big.NewInt(20), 103 AutoDepositBuffer: big.NewInt(40), 104 } 105 106 local := &Params{ 107 Profile: &Profile{ 108 PayAt: 5, 109 DropAt: 10, 110 BuyAt: common.Big3, 111 SellAt: common.Big2, 112 }, 113 Strategy: strategy, 114 } 115 116 in := &testInPayment{} 117 out := &testOutPayment{} 118 proto := &testProtocol{} 119 120 swap, _ := New(local, Payment{In: in, Out: out, Buys: true, Sells: true}, proto) 121 122 if in.autocashInterval != strategy.AutoCashInterval { 123 t.Fatalf("autocash interval not properly set, expect %v, got %v", strategy.AutoCashInterval, in.autocashInterval) 124 } 125 if out.autodepositInterval != strategy.AutoDepositInterval { 126 t.Fatalf("autodeposit interval not properly set, expect %v, got %v", strategy.AutoDepositInterval, out.autodepositInterval) 127 } 128 129 remote := &Profile{ 130 PayAt: 3, 131 DropAt: 10, 132 BuyAt: common.Big2, 133 SellAt: common.Big3, 134 } 135 swap.SetRemote(remote) 136 137 swap.Add(9) 138 if proto.drop { 139 t.Fatalf("not expected peer to be dropped") 140 } 141 swap.Add(1) 142 if !proto.drop { 143 t.Fatalf("expected peer to be dropped") 144 } 145 if !proto.drop { 146 t.Fatalf("expected peer to be dropped") 147 } 148 proto.drop = false 149 150 swap.Receive(10, &testPromise{big.NewInt(20)}) 151 if swap.balance != 0 { 152 t.Fatalf("expected zero balance, got %v", swap.balance) 153 } 154 155 if len(proto.amounts) != 0 { 156 t.Fatalf("expected zero balance, got %v", swap.balance) 157 } 158 159 swap.Add(-2) 160 if len(proto.amounts) > 0 { 161 t.Fatalf("expected no payments yet, got %v", proto.amounts) 162 } 163 164 swap.Add(-1) 165 if len(proto.amounts) != 1 { 166 t.Fatalf("expected one payment, got %v", len(proto.amounts)) 167 } 168 169 if proto.amounts[0] != 3 { 170 t.Fatalf("expected payment for %v units, got %v", proto.amounts[0], 3) 171 } 172 173 exp := new(big.Int).Mul(big.NewInt(int64(proto.amounts[0])), remote.SellAt) 174 if proto.promises[0].amount.Cmp(exp) != 0 { 175 t.Fatalf("expected payment amount %v, got %v", exp, proto.promises[0].amount) 176 } 177 178 swap.SetParams(&Params{ 179 Profile: &Profile{ 180 PayAt: 5, 181 DropAt: 10, 182 BuyAt: common.Big3, 183 SellAt: common.Big2, 184 }, 185 Strategy: &Strategy{ 186 AutoCashInterval: 2 * time.Second, 187 AutoCashThreshold: big.NewInt(40), 188 AutoDepositInterval: 2 * time.Second, 189 AutoDepositThreshold: big.NewInt(40), 190 AutoDepositBuffer: big.NewInt(60), 191 }, 192 }) 193 194 }