test_addressdeclarator.py (9816B)
1 # standard imports 2 import os 3 import unittest 4 import json 5 import logging 6 import hashlib 7 8 # external imports 9 from chainlib.eth.unittest.ethtester import EthTesterCase 10 from chainlib.eth.contract import ( 11 ABIContractEncoder, 12 ABIContractType, 13 ) 14 from chainlib.eth.nonce import RPCNonceOracle 15 from chainlib.eth.tx import receipt 16 from giftable_erc20_token import GiftableToken 17 from hexathon import ( 18 add_0x, 19 strip_0x, 20 ) 21 22 # local imports 23 from eth_address_declarator.declarator import AddressDeclarator 24 from eth_address_declarator import Declarator 25 from eth_address_declarator.unittest import TestAddressDeclaratorBase 26 27 logging.basicConfig(level=logging.DEBUG) 28 logg = logging.getLogger() 29 30 testdir = os.path.dirname(__file__) 31 32 description = '0x{:<064s}'.format(b'foo'.hex()) 33 34 35 class TestAddressDeclarator(TestAddressDeclaratorBase): 36 37 def setUp(self): 38 super(TestAddressDeclarator, self).setUp() 39 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 40 41 #c = GiftableToken(signer=self.signer, nonce_oracle=nonce_oracle, chain_id=self.chain_spec.chain_id()) 42 c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 43 (tx_hash_hex, o) = c.constructor(self.accounts[0], 'BarToken', 'BAR', 6) 44 self.rpc.do(o) 45 46 o = receipt(tx_hash_hex) 47 r = self.rpc.do(o) 48 self.assertEqual(r['status'], 1) 49 50 self.bar_token_address = r['contract_address'] 51 52 53 def test_basic(self): 54 d = add_0x(os.urandom(32).hex()) 55 56 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 57 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 58 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], self.foo_token_address, d) 59 self.rpc.do(o) 60 61 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 62 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 63 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d) 64 self.rpc.do(o) 65 66 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 67 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 68 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], self.bar_token_address, d) 69 self.rpc.do(o) 70 71 o = c.declarator_count(self.address, self.foo_token_address, sender_address=self.accounts[0]) 72 r = self.rpc.do(o) 73 self.assertEqual(c.parse_declarator_count(r), 2) 74 75 o = c.declarator_count(self.address, self.bar_token_address, sender_address=self.accounts[0]) 76 r = self.rpc.do(o) 77 self.assertEqual(c.parse_declarator_count(r), 1) 78 79 80 def test_get_single_declaration(self): 81 d = add_0x(os.urandom(32).hex()) 82 83 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 84 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 85 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d) 86 self.rpc.do(o) 87 o = receipt(tx_hash_hex) 88 r = self.rpc.do(o) 89 self.assertEqual(r['status'], 1) 90 91 o = c.declaration(self.address, self.accounts[1], self.foo_token_address, sender_address=self.accounts[0]) 92 r = self.rpc.do(o) 93 proofs = c.parse_declaration(r) 94 self.assertEqual(proofs[0], strip_0x(d)) 95 96 97 def test_declaration(self): 98 99 d = add_0x(os.urandom(32).hex()) 100 d_two = add_0x(os.urandom(32).hex()) 101 102 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 103 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 104 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d) 105 self.rpc.do(o) 106 107 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 108 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d_two) 109 self.rpc.do(o) 110 111 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 112 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 113 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d) 114 self.rpc.do(o) 115 116 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 117 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.bar_token_address, d) 118 self.rpc.do(o) 119 120 o = c.declaration(self.address, self.accounts[1], self.foo_token_address, sender_address=self.accounts[0]) 121 r = self.rpc.do(o) 122 proofs = c.parse_declaration(r) 123 self.assertEqual(proofs[0], strip_0x(d)) 124 self.assertEqual(proofs[1], strip_0x(d_two)) 125 126 127 def test_declarator_to_subject(self): 128 d = add_0x(os.urandom(32).hex()) 129 130 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 131 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 132 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d) 133 self.rpc.do(o) 134 135 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 136 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 137 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d) 138 self.rpc.do(o) 139 140 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 141 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 142 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.bar_token_address, d) 143 self.rpc.do(o) 144 145 o = c.declaration_address_at(self.address, self.accounts[1], 0, sender_address=self.accounts[0]) 146 r = self.rpc.do(o) 147 self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.foo_token_address)) 148 149 o = c.declaration_address_at(self.address, self.accounts[2], 0, sender_address=self.accounts[0]) 150 r = self.rpc.do(o) 151 self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.foo_token_address)) 152 153 o = c.declaration_address_at(self.address, self.accounts[1], 1, sender_address=self.accounts[0]) 154 r = self.rpc.do(o) 155 self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.bar_token_address)) 156 157 158 def test_subject_to_declarator(self): 159 d = '0x' + os.urandom(32).hex() 160 161 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 162 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 163 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d) 164 self.rpc.do(o) 165 166 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 167 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 168 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d) 169 self.rpc.do(o) 170 171 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 172 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 173 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.bar_token_address, d) 174 self.rpc.do(o) 175 176 o = c.declarator_address_at(self.address, self.foo_token_address, 0, sender_address=self.accounts[0]) 177 r = self.rpc.do(o) 178 self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.accounts[1])) 179 180 o = c.declarator_address_at(self.address, self.foo_token_address, 1, sender_address=self.accounts[0]) 181 r = self.rpc.do(o) 182 self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.accounts[2])) 183 184 185 def test_three_first(self): 186 d = [] 187 for i in range(3): 188 d.append(add_0x(os.urandom(32).hex())) 189 190 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 191 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 192 193 for proof in d: 194 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], self.foo_token_address, proof) 195 self.rpc.do(o) 196 197 o = c.declarator_count(self.address, self.foo_token_address, sender_address=self.accounts[0]) 198 r = self.rpc.do(o) 199 self.assertEqual(c.parse_declarator_count(r), 1) 200 201 o = c.declaration(self.address, self.accounts[0], self.foo_token_address, sender_address=self.accounts[0]) 202 r = self.rpc.do(o) 203 proofs = c.parse_declaration(r) 204 self.assertEqual(len(proofs), 3) 205 206 for i in range(3): 207 self.assertEqual(proofs[i], strip_0x(d[i])) 208 209 210 def test_three_first_different(self): 211 d = [] 212 a = [] 213 for i in range(3): 214 d.append(add_0x(os.urandom(32).hex())) 215 a.append(add_0x(os.urandom(20).hex())) 216 217 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 218 c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 219 220 for i in range(3): 221 (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], a[i], d[i]) 222 self.rpc.do(o) 223 224 for i in range(3): 225 o = c.declarator_count(self.address, a[i], sender_address=self.accounts[0]) 226 r = self.rpc.do(o) 227 self.assertEqual(c.parse_declarator_count(r), 1) 228 229 o = c.declaration(self.address, self.accounts[0], a[i], sender_address=self.accounts[0]) 230 r = self.rpc.do(o) 231 proofs = c.parse_declaration(r) 232 self.assertEqual(len(proofs), 1) 233 self.assertEqual(proofs[0], strip_0x(d[i])) 234 235 236 if __name__ == '__main__': 237 unittest.main()