Add test for addresses index, rcpt
This commit is contained in:
parent
4a8b162cde
commit
50804f4d70
@ -29,30 +29,37 @@ class FileStore:
|
|||||||
tx_hash_dirnormal = strip_0x(tx.hash).upper()
|
tx_hash_dirnormal = strip_0x(tx.hash).upper()
|
||||||
tx_hash_bytes = bytes.fromhex(tx_hash_dirnormal)
|
tx_hash_bytes = bytes.fromhex(tx_hash_dirnormal)
|
||||||
self.tx_raw_dir.add(tx_hash_bytes, raw)
|
self.tx_raw_dir.add(tx_hash_bytes, raw)
|
||||||
|
addresses = []
|
||||||
if self.address_rules != None:
|
if self.address_rules != None:
|
||||||
for a in tx.outputs + tx.inputs:
|
for a in tx.outputs + tx.inputs:
|
||||||
if self.address_rules.apply_rules_addresses(a, a, tx.hash):
|
if a not in addresses:
|
||||||
a_hex = strip_0x(a).upper()
|
addresses.append(a)
|
||||||
a = bytes.fromhex(a_hex)
|
else:
|
||||||
self.address_dir.add_dir(tx_hash_dirnormal, a, b'')
|
for a in tx.outputs + tx.inputs:
|
||||||
dirpath = self.address_dir.to_filepath(a_hex)
|
addresses.append(a)
|
||||||
fp = os.path.join(dirpath, '.start')
|
|
||||||
num = tx.block.number
|
|
||||||
num_compare = 0
|
|
||||||
try:
|
|
||||||
f = open(fp, 'rb')
|
|
||||||
r = f.read(8)
|
|
||||||
f.close()
|
|
||||||
num_compare = int.from_bytes(r, 'big')
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if num_compare == 0 or num < num_compare:
|
for a in addresses:
|
||||||
logg.debug('recoding new start block {} for {}'.format(num, a))
|
a_hex = strip_0x(a).upper()
|
||||||
num_bytes = num.to_bytes(8, 'big')
|
a = bytes.fromhex(a_hex)
|
||||||
f = open(fp, 'wb')
|
self.address_dir.add_dir(tx_hash_dirnormal, a, b'')
|
||||||
f.write(num_bytes)
|
dirpath = self.address_dir.to_filepath(a_hex)
|
||||||
f.close()
|
fp = os.path.join(dirpath, '.start')
|
||||||
|
num = tx.block.number
|
||||||
|
num_compare = 0
|
||||||
|
try:
|
||||||
|
f = open(fp, 'rb')
|
||||||
|
r = f.read(8)
|
||||||
|
f.close()
|
||||||
|
num_compare = int.from_bytes(r, 'big')
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if num_compare == 0 or num < num_compare:
|
||||||
|
logg.debug('recoding new start block {} for {}'.format(num, a))
|
||||||
|
num_bytes = num.to_bytes(8, 'big')
|
||||||
|
f = open(fp, 'wb')
|
||||||
|
f.write(num_bytes)
|
||||||
|
f.close()
|
||||||
|
|
||||||
if include_data:
|
if include_data:
|
||||||
src = json.dumps(tx.src()).encode('utf-8')
|
src = json.dumps(tx.src()).encode('utf-8')
|
||||||
@ -119,15 +126,6 @@ class FileStore:
|
|||||||
|
|
||||||
|
|
||||||
def __init__(self, chain_spec, cache_root=default_base_dir, address_rules=None):
|
def __init__(self, chain_spec, cache_root=default_base_dir, address_rules=None):
|
||||||
# self.cache_root = os.path.join(
|
|
||||||
# cache_root,
|
|
||||||
# 'eth_cache',
|
|
||||||
# chain_spec.engine(),
|
|
||||||
# chain_spec.fork(),
|
|
||||||
# str(chain_spec.chain_id()),
|
|
||||||
# )
|
|
||||||
#self.cache_root = os.path.realpath(self.cache_root)
|
|
||||||
#self.chain_dir = chain_dir_for(chain_spec, self.cache_root)
|
|
||||||
self.chain_dir = chain_dir_for(chain_spec, cache_root)
|
self.chain_dir = chain_dir_for(chain_spec, cache_root)
|
||||||
self.cache_dir = self.chain_dir
|
self.cache_dir = self.chain_dir
|
||||||
self.block_src_path = os.path.join(self.cache_dir, 'block', 'src')
|
self.block_src_path = os.path.join(self.cache_dir, 'block', 'src')
|
||||||
|
@ -39,7 +39,12 @@ class TestCache(EthTesterCase):
|
|||||||
super(TestCache, self).setUp()
|
super(TestCache, self).setUp()
|
||||||
fp = tempfile.mkdtemp()
|
fp = tempfile.mkdtemp()
|
||||||
self.cache_dir = fp
|
self.cache_dir = fp
|
||||||
self.store = FileStore(self.chain_spec, cache_root=self.cache_dir)
|
|
||||||
|
class Applier:
|
||||||
|
def apply_rules_addresses(self, sender, recipient, address):
|
||||||
|
return True
|
||||||
|
|
||||||
|
self.store = FileStore(self.chain_spec, cache_root=self.cache_dir, address_rules=Applier())
|
||||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||||
gas_oracle = OverrideGasOracle(price=100000000000, limit=30000)
|
gas_oracle = OverrideGasOracle(price=100000000000, limit=30000)
|
||||||
c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
|
c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
|
||||||
@ -64,7 +69,6 @@ class TestCache(EthTesterCase):
|
|||||||
|
|
||||||
|
|
||||||
def test_tx(self):
|
def test_tx(self):
|
||||||
logg.debug('tx {}'.format(self.tx))
|
|
||||||
self.store.put_tx(self.tx, include_data=True)
|
self.store.put_tx(self.tx, include_data=True)
|
||||||
j = self.store.get_tx(self.tx.hash)
|
j = self.store.get_tx(self.tx.hash)
|
||||||
tx = json.loads(j)
|
tx = json.loads(j)
|
||||||
@ -72,7 +76,6 @@ class TestCache(EthTesterCase):
|
|||||||
|
|
||||||
|
|
||||||
def test_block(self):
|
def test_block(self):
|
||||||
logg.debug('tx {}'.format(self.tx))
|
|
||||||
self.store.put_block(self.block, include_data=True)
|
self.store.put_block(self.block, include_data=True)
|
||||||
block_hash = strip_0x(self.block.hash)
|
block_hash = strip_0x(self.block.hash)
|
||||||
j = self.store.get_block(block_hash)
|
j = self.store.get_block(block_hash)
|
||||||
@ -81,6 +84,57 @@ class TestCache(EthTesterCase):
|
|||||||
self.assertEqual(retrieved_block_hash, block_hash)
|
self.assertEqual(retrieved_block_hash, block_hash)
|
||||||
|
|
||||||
|
|
||||||
|
def test_block_number(self):
|
||||||
|
self.store.put_block(self.block, include_data=True)
|
||||||
|
block_hash = strip_0x(self.block.hash)
|
||||||
|
block_number = int(self.block.number)
|
||||||
|
j = self.store.get_block_number(block_number)
|
||||||
|
block = json.loads(j)
|
||||||
|
retrieved_block_hash = strip_0x(block['hash'])
|
||||||
|
self.assertEqual(retrieved_block_hash, block_hash)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rcpt(self):
|
||||||
|
self.store.put_tx(self.tx, include_data=True)
|
||||||
|
j = self.store.get_rcpt(self.tx.hash)
|
||||||
|
rcpt = json.loads(j)
|
||||||
|
self.assertTrue(is_same_address(rcpt['transaction_hash'], self.tx.hash))
|
||||||
|
|
||||||
|
|
||||||
|
def test_address(self):
|
||||||
|
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
|
||||||
|
gas_oracle = OverrideGasOracle(price=100000000000, limit=30000)
|
||||||
|
c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
|
||||||
|
(tx_hash, o) = c.create(self.accounts[2], self.accounts[1], 1024)
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
o = transaction(tx_hash)
|
||||||
|
tx_src = self.rpc.do(o)
|
||||||
|
|
||||||
|
o = block_by_hash(tx_src['block_hash'])
|
||||||
|
block_src = self.rpc.do(o)
|
||||||
|
block = Block(block_src)
|
||||||
|
|
||||||
|
tx = Tx(tx_src, block=block)
|
||||||
|
self.store.put_tx(tx, include_data=True)
|
||||||
|
|
||||||
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
||||||
|
c = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
|
||||||
|
(tx_hash, o) = c.create(self.accounts[1], self.accounts[0], 1024)
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
o = transaction(tx_hash)
|
||||||
|
tx_src = self.rpc.do(o)
|
||||||
|
|
||||||
|
o = block_by_hash(tx_src['block_hash'])
|
||||||
|
block_src = self.rpc.do(o)
|
||||||
|
block = Block(block_src)
|
||||||
|
|
||||||
|
tx = Tx(tx_src, block=block)
|
||||||
|
self.store.put_tx(tx, include_data=True)
|
||||||
|
|
||||||
|
address = strip_0x(self.accounts[1])
|
||||||
|
txs = self.store.get_address_tx(address)
|
||||||
|
self.assertEqual(len(txs), 2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user