test_protect.py (2725B)
1 # standard imports 2 import unittest 3 import logging 4 import os 5 from chainlib.eth.nonce import RPCNonceOracle 6 from chainlib.eth.tx import receipt 7 from chainlib.eth.block import block_latest 8 from chainlib.eth.address import to_checksum_address 9 from hexathon import same as same_hex 10 from eth_erc20 import ERC20 11 from giftable_erc20_token import GiftableToken 12 13 # local imports 14 from evm_tokenvote.unittest import TestEvmVoteAccounts 15 from evm_tokenvote.unittest.base import hash_of_foo 16 from evm_tokenvote import Voter 17 from evm_tokenvote import ProposalState 18 19 20 logging.basicConfig(level=logging.DEBUG) 21 logg = logging.getLogger() 22 23 class TestVoteProtect(TestEvmVoteAccounts): 24 25 def setUp(self): 26 super(TestVoteProtect, self).setUp() 27 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn) 28 c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 29 (tx_hash, o) = c.constructor(self.accounts[0], self.token_address, protect_supply=True) 30 self.rpc.do(o) 31 o = receipt(tx_hash) 32 r = self.rpc.do(o) 33 self.assertEqual(r['status'], 1) 34 self.voter_address = to_checksum_address(r['contract_address']) 35 logg.debug('published protected voter on address {} with hash {}'.format(self.voter_address, tx_hash)) 36 37 38 def test_propose(self): 39 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn) 40 c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 41 (tx_hash, o) = c.propose(self.voter_address, self.accounts[0], hash_of_foo, 100) 42 self.rpc.do(o) 43 44 c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 45 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.alice, 1) 46 self.rpc.do(o) 47 48 self.backend.mine_blocks(100) 49 50 nonce_oracle = RPCNonceOracle(self.trent, conn=self.conn) 51 c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 52 (tx_hash, o) = c.scan(self.voter_address, self.trent, 0, 0) 53 self.rpc.do(o) 54 55 (tx_hash, o) = c.finalize_vote(self.voter_address, self.trent) 56 self.rpc.do(o) 57 o = receipt(tx_hash) 58 r = self.rpc.do(o) 59 self.assertEqual(r['status'], 1) 60 61 o = c.get_proposal(self.voter_address, 0, sender_address=self.accounts[0]) 62 r = self.rpc.do(o) 63 proposal = c.parse_proposal(r) 64 self.assertEqual(proposal.state & ProposalState.FINAL, ProposalState.FINAL) 65 self.assertEqual(proposal.state & ProposalState.SUPPLYCHANGE, ProposalState.SUPPLYCHANGE) 66 self.assertEqual(proposal.state & ProposalState.CANCELLED, ProposalState.CANCELLED) 67 68 69 if __name__ == '__main__': 70 unittest.main()