def test_ether_rises(self): block = Block(timestamp=self.ts_zero + 30 * 86400 + 1) block.contract_storage(self.contract.D)[self.contract.I] = 4000 tx = Tx(sender='bob', value=200) self.run(tx, self.contract, block) assert len(self.contract.txs) == 2 assert self.contract.txs == [('bob', 623 * 10 ** 18, 0, 0), ('alice', 4377 * 10 ** 18, 0, 0)]
def test_recipient_has_balance(self): tx = Tx(sender='alice', value=2000, data=['bob']) block = Block() block.set_account_balance('bob', 1000) self.run(tx, self.contract, block) assert len(self.contract.txs) == 1 assert self.contract.txs == [('alice', 1000, 0, 0)]
def test_ether_drops(self): block = Block(timestamp=self.ts_zero + 30 * 86400 + 1) block.contract_storage(self.contract.D)[self.contract.I] = 400 tx = Tx(sender='bob', value=200) self.run(tx, self.contract, block) assert len(self.contract.txs) == 1 assert self.contract.txs == [('bob', 5000 * 10 ** 18, 0, 0)]
def test_creation(self): block = Block(timestamp=self.ts_zero) block.contract_storage(self.contract.D)[self.contract.I] = 2500 tx = Tx(sender='bob', value=1000 * 10 ** 18) self.run(tx, self.contract, block) assert self.contract.storage[1000] == 1 assert self.contract.storage[1001] == 2495000 assert self.contract.storage[1002] == self.ts_zero + 30 * 86400 assert self.contract.storage[1003] == tx.sender assert len(self.contract.txs) == 0
def test_divorce_approval(self): tx = Tx(sender=PARTNER_2, value=100, data=[TX_DIVORCE]) block = Block() block.set_account_balance('myaddress', 1000) self.run(tx, self.contract, block) assert self.contract.storage[I_STATE] == S_DIVORCED assert len(self.contract.txs) == 2 assert self.contract.txs == [(PARTNER_1, 500, 0, 0), (PARTNER_2, 500, 0, 0)] assert self.stopped == "Divorced"
def test_confirmation_timeout(self): contract = Escrow() tx = Tx(sender=CUSTOMER, value=PRICE_ETHER) block = Block(timestamp=TS) self.run(tx, contract, block) tx = Tx(sender=CUSTOMER, value=MIN_FEE) block = Block(timestamp=TS + CONFIRMATION_TIMEOUT + 1) self.run(tx, contract, block) assert contract.storage[I_STATUS] == S_REFUNDED assert len(contract.txs) == 1 assert contract.txs == [(CUSTOMER, PRICE_ETHER, 0, 0)]
def test_shipped(self): contract = Escrow() tx = Tx(sender=CUSTOMER, value=PRICE_ETHER) block = Block(timestamp=TS) self.run(tx, contract, block) tx = Tx(sender=SHIPPER, value=MIN_FEE) block = Block(timestamp=TS + 1) self.run(tx, contract, block) assert contract.storage[I_STATUS] == S_SHIPPED assert len(contract.txs) == 1 assert contract.txs == [(MERCHANT, PRICE_ETHER, 0, 0)]
def test_cancel_proposal(self, early=0): tx = Tx(sender=PARTNER_1, value=100, data=[PARTNER_1]) self.run(tx, self.contract, Block(timestamp=(0 if early else 2000))) if early: assert self.stopped == "Cant cancel early" else: assert self.stopped == "Cancelled"
def test_customer_paid(self): contract = Escrow() tx = Tx(sender=CUSTOMER, value=PRICE_ETHER) block = Block(timestamp=TS) self.run(tx, contract, block) assert contract.storage[I_STATUS] == S_CUSTOMER_PAID assert contract.storage[I_CUSTOMER_ADDRESS] == CUSTOMER assert contract.storage[I_CUSTOMER_PAID_AMOUNT] == PRICE_ETHER assert contract.storage[I_CUSTOMER_PAID_TS] == TS
class LockinEscrowRun(Simulation): contract = LockinEscrow() block = Block() total = 0 incentive = 0 paid = 0 def reset(self): self.contract = LockinEscrow() self.total = 0 self.incentive = 0 self.paid = 0 def run_tx(self, value=0, sender="", data=[]): self.run(Tx(value=value, sender=sender, data=data), self.contract, self.block, method_name=inspect.stack()[1][3]) def test_donate(self, value=max(MIN_FEE, random() * TOTAL)): self.run_tx(sender="anyone", value=value) assert self.stopped == "Donation" def test_merchant_under_balance(self): self.contact = LockinEscrow() self.run_tx(sender=MERCHANT, value=random() * MIN_BALANCE * 0.9) self.stopped == "Below funds of operation" def test_merchant_allow(self): #Test intended when contract not busy. assert self.contract.storage[I_CUSTOMER] == 0 self.block.set_account_balance(self.contract.address, MIN_BALANCE) self.incentive = random_incentive() self.total = PRICE + self.incentive self.run_tx(sender=MERCHANT, value=MIN_BALANCE + MIN_FEE, data=[C_ALLOW, CUSTOMER, self.total, self.incentive]) assert self.stopped == "Customer allowed" assert self.contract.storage[I_CUSTOMER] == CUSTOMER assert self.contract.storage[I_TOTAL] == self.total assert self.contract.storage[I_INCENTIVE] == self.incentive assert self.contract.storage[I_PAID] == 0 def test_customer_change_blocked(self): r_incentive = random_incentive() self.run_tx(sender=MERCHANT, value=MIN_BALANCE + MIN_FEE, data=[C_ALLOW, CUSTOMER, 2 * TOTAL, r_incentive]) assert self.stopped == "Customer change blocked" assert self.contract.storage[I_TOTAL] == self.total assert self.contract.storage[I_INCENTIVE] == self.incentive def test_customer_pay(self): self.paid = random() * PRICE self.run_tx(sender=CUSTOMER, value=self.paid + MIN_FEE) assert self.stopped == "Customer paid(part)" def test_customer_pay_too_little(self): self.reset() self.test_merchant_allow() self.paid = random() * 0.9 * self.total self.run_tx(sender=CUSTOMER, value=self.paid + MIN_FEE, data=[C_SATISFIED]) assert self.stopped == "Customer didnt pay enough" assert len(self.contract.txs) == 0 def assert_reset(self): assert self.contract.storage[I_CUSTOMER] == 0 assert self.contract.storage[I_TOTAL] == 0 assert self.contract.storage[I_INCENTIVE] == 0 assert self.contract.storage[I_PAID] == 0 def assert_happy(self): assert self.stopped == "Customer paid and happy" self.assert_reset() def test_customer_pay_and_happy(self): self.reset() self.test_merchant_allow() self.paid = self.total + 1 self.run_tx(sender=CUSTOMER, value=self.paid + MIN_FEE, data=[C_SATISFIED]) assert self.contract.txs[0][0] == MERCHANT assert self.contract.txs[0][1] == self.paid - self.incentive assert self.contract.txs[1][0] == CUSTOMER assert self.contract.txs[1][1] == self.incentive self.assert_happy() def test_customer_pay_part(self): self.assert_reset() self.test_merchant_allow() self.paid = self.total + 1 self.run_tx(sender=CUSTOMER, value=self.paid + MIN_FEE) assert self.stopped == "Customer paid(part)" # (all, actually) assert self.contract.storage[I_PAID] == self.total + 1 def test_customer_happy(self): # depends on the pay one being run first. self.paid += 1 self.run_tx(sender=CUSTOMER, value=MIN_FEE + 1, data=[C_SATISFIED]) assert self.contract.txs[0][0] == MERCHANT assert self.contract.txs[0][1] == self.paid - self.incentive assert self.contract.txs[1][0] == CUSTOMER assert self.contract.txs[1][1] == self.incentive self.assert_happy() def test_refund(self): self.test_customer_pay_part() self.run_tx(sender=MERCHANT, value=MIN_FEE, data=[C_REFUND]) assert self.stopped == "Customer refunded" assert self.contract.txs[0][0] == CUSTOMER assert self.contract.txs[0][1] == min( self.paid * (1 + MIN_BALANCE / self.total), self.total + MIN_BALANCE) self.assert_reset()