test_basic.py (3524B)
1 # standard imports 2 import tempfile 3 import shutil 4 import unittest 5 import logging 6 import json 7 8 # external imports 9 from chainlib.chain import ChainSpec 10 from chainlib.eth.gas import ( 11 Gas, 12 OverrideGasOracle, 13 ) 14 from chainlib.eth.nonce import RPCNonceOracle 15 from chainlib.eth.unittest.ethtester import EthTesterCase 16 from chainlib.eth.tx import ( 17 transaction, 18 receipt, 19 TxFormat, 20 Tx, 21 ) 22 from chainlib.eth.block import ( 23 block_by_hash, 24 Block, 25 ) 26 from chainlib.eth.address import is_same_address 27 from hexathon import strip_0x 28 from eth_monitor.index import AddressIndex 29 30 # local imports 31 from eth_cache.store.file import FileStore 32 from eth_cache.rpc import CacheRPC 33 34 logging.basicConfig(level=logging.DEBUG) 35 logg = logging.getLogger() 36 37 38 class TestMonitor(EthTesterCase): 39 40 def setUp(self): 41 super(TestMonitor, self).setUp() 42 fp = tempfile.mkdtemp() 43 self.cache_dir = fp 44 45 class Applier: 46 def apply_rules_addresses(self, sender, recipient, address): 47 return True 48 49 self.store = FileStore(self.chain_spec, cache_root=self.cache_dir, address_rules=Applier()) 50 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 51 gas_oracle = OverrideGasOracle(price=100000000000, limit=30000) 52 c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) 53 (tx_hash, o) = c.create(self.accounts[0], self.accounts[1], 1024) 54 r = self.rpc.do(o) 55 56 o = transaction(tx_hash) 57 tx_src = self.rpc.do(o) 58 59 o = receipt(tx_hash) 60 rcpt_src = self.rpc.do(o) 61 62 o = block_by_hash(tx_src['block_hash']) 63 block_src = self.rpc.do(o) 64 65 self.block = Block(block_src) 66 self.tx = Tx(tx_src, block=self.block, rcpt=rcpt_src) 67 68 self.cache_rpc = CacheRPC(None, self.store) 69 70 71 def tearDown(self): 72 shutil.rmtree(self.cache_dir) 73 74 75 def test_address(self): 76 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 77 gas_oracle = OverrideGasOracle(price=100000000000, limit=30000) 78 c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) 79 (tx_hash, o) = c.create(self.accounts[2], self.accounts[1], 1024) 80 r = self.rpc.do(o) 81 o = transaction(tx_hash) 82 tx_src = self.rpc.do(o) 83 84 o = block_by_hash(tx_src['block_hash']) 85 block_src = self.rpc.do(o) 86 block = Block(block_src) 87 88 tx = Tx(tx_src, block=block) 89 self.store.put_tx(tx, include_data=True) 90 91 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 92 c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) 93 (tx_hash, o) = c.create(self.accounts[1], self.accounts[0], 1024) 94 r = self.rpc.do(o) 95 o = transaction(tx_hash) 96 tx_src = self.rpc.do(o) 97 98 o = block_by_hash(tx_src['block_hash']) 99 block_src = self.rpc.do(o) 100 block = Block(block_src) 101 102 tx = Tx(tx_src, block=block) 103 self.store.put_tx(tx, include_data=True) 104 105 address = strip_0x(self.accounts[1]) 106 txs = self.store.get_address_tx(address) 107 self.assertEqual(len(txs), 2) 108 109 idx = AddressIndex(self.cache_rpc, self.store) 110 idx.load_address_tx(self.accounts[0]) 111 addrs = idx.get_address(self.accounts[0]) 112 self.assertEqual(len(addrs), 1) 113 114 115 if __name__ == '__main__': 116 unittest.main()