evm-tokenvote

Voting machine using ERC20 tokens as votes.
Info | Log | Files | Refs | README

test_events.py (6581B)


      1 # standard imports
      2 import unittest
      3 import logging
      4 import os
      5 from chainlib.eth.nonce import RPCNonceOracle
      6 from chainlib.eth.contract import ABIContractLogDecoder
      7 from chainlib.eth.contract import ABIContractType
      8 from chainlib.eth.tx import receipt
      9 from chainlib.eth.block import block_latest
     10 from hexathon import same as same_hex
     11 from eth_erc20 import ERC20
     12 from giftable_erc20_token import GiftableToken
     13 
     14 # local imports
     15 from evm_tokenvote.unittest import TestEvmVote
     16 from evm_tokenvote.unittest.base import hash_of_foo
     17 from evm_tokenvote import Voter
     18 from evm_tokenvote import ProposalState
     19 
     20 
     21 logging.basicConfig(level=logging.DEBUG)
     22 logg = logging.getLogger()
     23 
     24 class TestVoteEvents(TestEvmVote):
     25 
     26     def test_event_propose(self):
     27         description = hash_of_foo
     28         nonce_oracle = RPCNonceOracle(self.ivan, conn=self.conn)
     29         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     30         (tx_hash, o) = c.propose(self.voter_address, self.ivan, description, 100)
     31         self.rpc.do(o)
     32         o = receipt(tx_hash)
     33         r = self.rpc.do(o)
     34 
     35         rlog = r['logs'][0]
     36 
     37         o = block_latest()
     38         now_block_height = self.rpc.do(o)
     39 
     40         topic = ABIContractLogDecoder()
     41         topic.topic('ProposalAdded')
     42         topic.typ(ABIContractType.UINT256)
     43         topic.typ(ABIContractType.UINT256)
     44         topic.typ(ABIContractType.UINT256)
     45         topic.apply(rlog['topics'], rlog['data'])
     46         self.assertEqual(int(topic.contents[0], 16), now_block_height + 100)
     47         self.assertEqual(int(topic.contents[1], 16), 500000)
     48         self.assertEqual(int(topic.contents[2], 16), 0)
     49 
     50 
     51     def test_event_complete(self):
     52         half_supply = int(self.initial_supply / 2)
     53         description = hash_of_foo
     54         nonce_oracle = RPCNonceOracle(self.ivan, conn=self.conn)
     55         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     56         (tx_hash, o) = c.propose(self.voter_address, self.ivan, description, 100)
     57         self.rpc.do(o)
     58        
     59         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
     60         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     61         (tx_hash, o) = c.transfer(self.address, self.accounts[0], self.alice, half_supply)
     62         self.rpc.do(o)
     63 
     64         nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn)
     65         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     66         (tx_hash, o) = c.approve(self.address, self.alice, self.voter_address, half_supply)
     67         self.rpc.do(o)
     68 
     69         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     70         (tx_hash, o) = c.vote(self.voter_address, self.alice, half_supply)
     71         self.rpc.do(o)
     72         
     73         (tx_hash, o) = c.finalize_vote(self.voter_address, self.alice)
     74         self.rpc.do(o)
     75         o = receipt(tx_hash)
     76         r = self.rpc.do(o)
     77         self.assertEqual(r['status'], 1)
     78 
     79         rlog = r['logs'][0]
     80 
     81         topic = ABIContractLogDecoder()
     82         topic.topic('ProposalCompleted')
     83         topic.typ(ABIContractType.UINT256)
     84         topic.typ(ABIContractType.BOOLEAN)
     85         topic.typ(ABIContractType.BOOLEAN)
     86         topic.typ(ABIContractType.UINT256)
     87         topic.apply(rlog['topics'], [rlog['data']])
     88         self.assertEqual(int(topic.contents[0], 16), 0)
     89         self.assertEqual(int(topic.contents[1], 16), 0)
     90         self.assertEqual(int(topic.contents[2], 16), 0)
     91         self.assertEqual(int(topic.contents[3], 16), half_supply)
     92    
     93 
     94     def test_event_insufficient(self):
     95         half_supply = int(self.initial_supply / 2)
     96         description = hash_of_foo
     97         nonce_oracle = RPCNonceOracle(self.ivan, conn=self.conn)
     98         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     99         (tx_hash, o) = c.propose(self.voter_address, self.ivan, description, 100)
    100         self.rpc.do(o)
    101 
    102         self.backend.mine_blocks(100)
    103 
    104         (tx_hash, o) = c.finalize_vote(self.voter_address, self.ivan)
    105         self.rpc.do(o)
    106         o = receipt(tx_hash)
    107         r = self.rpc.do(o)
    108         self.assertEqual(r['status'], 1)
    109 
    110         rlog = r['logs'][0]
    111 
    112         topic = ABIContractLogDecoder()
    113         topic.topic('ProposalCompleted')
    114         topic.typ(ABIContractType.UINT256)
    115         topic.typ(ABIContractType.BOOLEAN)
    116         topic.typ(ABIContractType.BOOLEAN)
    117         topic.typ(ABIContractType.UINT256)
    118         topic.apply(rlog['topics'], [rlog['data']])
    119         self.assertEqual(int(topic.contents[0], 16), 0)
    120         self.assertEqual(int(topic.contents[1], 16), 0)
    121         self.assertEqual(int(topic.contents[2], 16), 1)
    122         self.assertEqual(int(topic.contents[3], 16), 0)
    123 
    124 
    125     def test_event_cancelled(self):
    126         half_supply = int(self.initial_supply / 2)
    127         description = hash_of_foo
    128         nonce_oracle = RPCNonceOracle(self.ivan, conn=self.conn)
    129         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    130         (tx_hash, o) = c.propose(self.voter_address, self.ivan, description, 100)
    131         self.rpc.do(o)
    132        
    133         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
    134         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    135         (tx_hash, o) = c.transfer(self.address, self.accounts[0], self.alice, half_supply)
    136         self.rpc.do(o)
    137 
    138         nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn)
    139         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    140         (tx_hash, o) = c.approve(self.address, self.alice, self.voter_address, half_supply)
    141         self.rpc.do(o)
    142 
    143         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    144         (tx_hash, o) = c.vote_cancel(self.voter_address, self.alice, half_supply)
    145         self.rpc.do(o)
    146         
    147         (tx_hash, o) = c.finalize_vote(self.voter_address, self.alice)
    148         self.rpc.do(o)
    149         o = receipt(tx_hash)
    150         r = self.rpc.do(o)
    151         self.assertEqual(r['status'], 1)
    152 
    153         rlog = r['logs'][0]
    154 
    155         topic = ABIContractLogDecoder()
    156         topic.topic('ProposalCompleted')
    157         topic.typ(ABIContractType.UINT256)
    158         topic.typ(ABIContractType.BOOLEAN)
    159         topic.typ(ABIContractType.BOOLEAN)
    160         topic.typ(ABIContractType.UINT256)
    161         topic.apply(rlog['topics'], [rlog['data']])
    162         self.assertEqual(int(topic.contents[0], 16), 0)
    163         self.assertEqual(int(topic.contents[1], 16), 1)
    164         self.assertEqual(int(topic.contents[2], 16), 0)
    165         self.assertEqual(int(topic.contents[3], 16), half_supply)
    166 
    167 
    168 if __name__ == '__main__':
    169     unittest.main()