eth-owned

EIP-173 interface and tools for chainlib-eth
git clone git://holbrook.no/eth-owned.git
Log | Files | Refs

test_void.py (4077B)


      1 # standard imports
      2 import os
      3 import unittest
      4 import json
      5 import logging
      6 
      7 # external imports
      8 from chainlib.eth.unittest.ethtester import EthTesterCase
      9 from chainlib.connection import RPCConnection
     10 from chainlib.eth.address import to_checksum_address
     11 from chainlib.eth.nonce import RPCNonceOracle
     12 from chainlib.eth.tx import (
     13         receipt,
     14         transaction,
     15         TxFormat,
     16         TxFactory,
     17         )
     18 from chainlib.eth.contract import (
     19         abi_decode_single,
     20         ABIContractType,
     21         )
     22 from chainlib.eth.contract import (
     23         ABIContractEncoder,
     24         )
     25 from hexathon import (
     26         add_0x,
     27         strip_0x,
     28         )
     29 
     30 # local imports
     31 from eth_owned.void import VoidOwner
     32 from eth_owned.owned import Owned
     33 from eth_owned import data_dir
     34 
     35 logging.basicConfig(level=logging.DEBUG)
     36 logg = logging.getLogger()
     37 
     38 testdir = os.path.dirname(__file__)
     39 
     40 
     41 class Test(EthTesterCase):
     42 
     43 
     44     def setUp(self):
     45         super(Test, self).setUp()
     46         self.conn = RPCConnection.connect(self.chain_spec, 'default')
     47         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
     48         self.o = VoidOwner(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     49         (tx_hash_hex, o) = self.o.constructor(self.accounts[0])
     50         r = self.conn.do(o)
     51         logg.debug('deployed with hash {}'.format(r))
     52 
     53         o = receipt(tx_hash_hex)
     54         r = self.conn.do(o)
     55         self.address = r['contract_address']
     56 
     57         f = open(os.path.join(data_dir, 'Owned.bin'), 'r')
     58         b = f.read()
     59         f.close()
     60        
     61         txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     62         tx = txf.template(self.accounts[0], None, use_nonce=True)
     63         tx = txf.set_code(tx, b)
     64         (tx_hash_hex, o) = txf.build(tx)
     65         r = self.conn.do(o)
     66 
     67         o = receipt(tx_hash_hex)
     68         r = self.conn.do(o)
     69         self.owned_demo_address = r['contract_address']
     70 
     71 
     72     def test_accept(self):
     73         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
     74         txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     75 
     76         c = Owned(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     77         (tx_hash_hex, o) = c.transfer_ownership(self.owned_demo_address, self.accounts[0], self.accounts[1])
     78         r = self.conn.do(o)
     79 
     80         o = receipt(tx_hash_hex)
     81         r = self.conn.do(o)
     82         self.assertEqual(r['status'], 1)
     83 
     84         nonce_oracle = RPCNonceOracle(self.accounts[1], self.conn)
     85         c = Owned(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     86         (tx_hash_hex, o) = c.accept_ownership(self.owned_demo_address, self.accounts[1])
     87        
     88         r = self.conn.do(o)
     89 
     90         o = receipt(tx_hash_hex)
     91         r = self.conn.do(o)
     92         self.assertEqual(r['status'], 1)
     93 
     94         o = c.owner(self.owned_demo_address, sender_address=self.accounts[0])
     95         r = self.conn.do(o)
     96         owner_address = abi_decode_single(ABIContractType.ADDRESS, r)
     97         self.assertEqual(owner_address, strip_0x(self.accounts[1]))
     98 
     99 
    100     def test_void(self):
    101         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
    102         txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    103 
    104         c = Owned(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    105         (tx_hash_hex, o) = c.transfer_ownership(self.owned_demo_address, self.accounts[0], self.address)
    106         r = self.conn.do(o)
    107 
    108         o = receipt(tx_hash_hex)
    109         r = self.conn.do(o)
    110         self.assertEqual(r['status'], 1)
    111 
    112         (tx_hash_hex, o) = c.take_ownership(self.address, self.accounts[0], self.owned_demo_address)
    113         r = self.conn.do(o)
    114 
    115         o = receipt(tx_hash_hex)
    116         r = self.conn.do(o)
    117         self.assertEqual(r['status'], 1)
    118 
    119         o = c.owner(self.owned_demo_address, sender_address=self.accounts[0])
    120         r = self.conn.do(o)
    121 
    122         owner_address = abi_decode_single(ABIContractType.ADDRESS, r)
    123         self.assertEqual(owner_address, strip_0x(self.address))
    124 
    125 
    126 if __name__ == '__main__':
    127     unittest.main()