chainlib-eth

Ethereum implementation of the chainlib interface
Info | Log | Files | Refs | README | LICENSE

test_bloom.py (8184B)


      1 # standard imports
      2 import os
      3 import unittest
      4 import logging
      5 
      6 # local imports
      7 from chainlib.eth.unittest.ethtester import EthTesterCase
      8 from chainlib.eth.nonce import RPCNonceOracle
      9 from chainlib.eth.gas import OverrideGasOracle
     10 from chainlib.eth.tx import receipt
     11 from chainlib.eth.block import block_by_number
     12 from chainlib.eth.log import LogBloom
     13 from hexathon import (
     14         strip_0x,
     15         add_0x,
     16         )
     17 
     18 # test imports
     19 from tests.contract import TestContract
     20 
     21 script_dir = os.path.realpath(os.path.dirname(__file__))
     22 
     23 logging.basicConfig(level=logging.DEBUG)
     24 logg = logging.getLogger()
     25 
     26 #{'blockHash': '0xe657e31045be85cfff8c28af6b4fd6417cace7150c4ebbeb736e638313d8e66d', 'block_hash': '0xe657e31045be85cfff8c28af6b4fd6417cace7150c4ebbeb736e638313d8e66d', 'blockNumber': '0xc1ee5a', 'block_number': '0xc1ee5a', 'contractAddress': None, 'contract_address': None, 'cumulativeGasUsed': '0xbc659', 'cumulative_gas_used': '0xbc659', 'from': '0xf6025e63cee5e436a5f1486e040aeead7e97b745', 'gasUsed': '0x1dddb', 'gas_used': '0x1dddb', 'logs': [
     27 
     28 #{'address': '0x4e58ab12d2051ea2068e78e4fcee7ddee6785848', 'blockHash': '0xe657e31045be85cfff8c28af6b4fd6417cace7150c4ebbeb736e638313d8e66d', 'blockNumber': '0xc1ee5a', 'data': '0x', 'logIndex': '0xd', 'removed': False, 'topics': ['0x92e98423f8adac6e64d0608e519fd1cefb861498385c6dee70d58fc926ddc68c', '0x0000000000000000000000000000000000000000000000000000000005f6aa5a', '0x00000000000000000000000000000000000000000000000000000000000000d6', '0x000000000000000000000000f6025e63cee5e436a5f1486e040aeead7e97b745'], 'transactionHash': '0xd0f039591953d277d55f628694248cb442590fab95ac53fcfb69e9dbba7db97a', 'transactionIndex': '0xe'},
     29 
     30 #{'address': '0x4e58ab12d2051ea2068e78e4fcee7ddee6785848', 'blockHash': '0xe657e31045be85cfff8c28af6b4fd6417cace7150c4ebbeb736e638313d8e66d', 'blockNumber': '0xc1ee5a', 'data': '0x0000000000000000000000000000000000000000000000000000000060d7119f', 'logIndex': '0xe', 'removed': False, 'topics': ['0x0559884fd3a460db3073b7fc896cc77986f16e378210ded43186175bf646fc5f', '0x0000000000000000000000000000000000000000000000000000000005f6aa5a', '0x00000000000000000000000000000000000000000000000000000000000000d6'], 'transactionHash': '0xd0f039591953d277d55f628694248cb442590fab95ac53fcfb69e9dbba7db97a', 'transactionIndex': '0xe'},
     31 
     32 #{'address': '0x4e58ab12d2051ea2068e78e4fcee7ddee6785848', 'blockHash': '0xe657e31045be85cfff8c28af6b4fd6417cace7150c4ebbeb736e638313d8e66d', 'blockNumber': '0xc1ee5a', 'data': '0x', 'logIndex': '0xf', 'removed': False, 'topics': ['0xfe25c73e3b9089fac37d55c4c7efcba6f04af04cebd2fc4d6d7dbb07e1e5234f', '0x0000000000000000000000000000000000000000000000813b65aa80e5770000'], 'transactionHash': '0xd0f039591953d277d55f628694248cb442590fab95ac53fcfb69e9dbba7db97a', 'transactionIndex': '0xe'}]
     33 
     34 #, 'logsBloom': '0x0000000000000000000000000000000000000080000000000000000000c000000000000000408000000000000000000000000000000200080000000000000000100000000000000000000000000000000000000000000200000020000000000000000000000000800000400000000000400000000400000000000400100000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000008000000000080000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000002008000000000000000', 'logs_bloom': '0x0000000000000000000000000000000000000080000000000000000000c000000000000000408000000000000000000000000000000200080000000000000000100000000000000000000000000000000000000000000200000020000000000000000000000000800000400000000000400000000400000000000400100000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000008000000000080000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000002008000000000000000', 'status': '0x1', 'to': '0x4e58ab12d2051ea2068e78e4fcee7ddee6785848', 'transactionHash': '0xd0f039591953d277d55f628694248cb442590fab95ac53fcfb69e9dbba7db97a', 'transaction_hash': '0xd0f039591953d277d55f628694248cb442590fab95ac53fcfb69e9dbba7db97a', 'transactionIndex': '0xe', 'transaction_index': '0xe', 'type': '0x0'}
     35 
     36 
     37 class BloomTestCase(EthTesterCase):
     38 
     39     def setUp(self):
     40         super(BloomTestCase, self).setUp()
     41 
     42         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
     43         c = TestContract(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     44         (tx_hash, o) = c.constructor(self.accounts[0])
     45         r = self.rpc.do(o)
     46         o = receipt(tx_hash)
     47         r = self.rpc.do(o)
     48         self.assertEqual(r['status'], 1)
     49 
     50         self.address = r['contract_address']
     51         logg.info('deployed contract on {}'.format(self.address))
     52        
     53 
     54     def test_log_proof(self):
     55         bloom = LogBloom()
     56 
     57         address = bytes.fromhex(strip_0x('0x4e58ab12d2051ea2068e78e4fcee7ddee6785848'))
     58         logs = [
     59             ['0x92e98423f8adac6e64d0608e519fd1cefb861498385c6dee70d58fc926ddc68c', '0x0000000000000000000000000000000000000000000000000000000005f6aa5a', '0x00000000000000000000000000000000000000000000000000000000000000d6', '0x000000000000000000000000f6025e63cee5e436a5f1486e040aeead7e97b745'],
     60             ['0x0559884fd3a460db3073b7fc896cc77986f16e378210ded43186175bf646fc5f', '0x0000000000000000000000000000000000000000000000000000000005f6aa5a', '0x00000000000000000000000000000000000000000000000000000000000000d6'],
     61             ['0xfe25c73e3b9089fac37d55c4c7efcba6f04af04cebd2fc4d6d7dbb07e1e5234f', '0x0000000000000000000000000000000000000000000000813b65aa80e5770000'],
     62                 ]
     63    
     64         bloom.add(address)
     65         for topics in logs:
     66             topics_bytes = []
     67             for topic in topics:
     68                 topic_bytes = bytes.fromhex(strip_0x(topic))
     69                 bloom.add(topic_bytes)
     70 
     71         log_proof_hex = '0x0000000000000000000000000000000000000080000000000000000000c000000000000000408000000000000000000000000000000200080000000000000000100000000000000000000000000000000000000000000200000020000000000000000000000000800000400000000000400000000400000000000400100000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000008000000000080000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000002008000000000000000'
     72         log_proof = bytes.fromhex(strip_0x(log_proof_hex))
     73 
     74         log_proof_bitcount = 0
     75         for b in log_proof:
     76             for i in range(8):
     77                 if b & (1 << (7 - i)) > 0:
     78                     log_proof_bitcount += 1
     79         logg.debug('proof log has {} bits set'.format(log_proof_bitcount))
     80 
     81         log_created_bitcount = 0
     82         for b in bloom.content:
     83             for i in range(8):
     84                 if b & (1 << (7 - i)) > 0:
     85                     log_created_bitcount += 1
     86         logg.debug('created log has {} bits set'.format(log_created_bitcount))
     87 
     88         logg.debug('log_proof:\n{}'.format(log_proof_hex))
     89         logg.debug('log_created:\n{}'.format(add_0x(bloom.content.hex())))
     90         for i in range(len(bloom.content)):
     91             chk = bloom.content[i] & log_proof[i]
     92             if chk != bloom.content[i]:
     93                 self.fail('mismatch at {}: {} != {}'.format(i, chk, bloom.content[i]))
     94 
     95         
     96 
     97     @unittest.skip('pyevm tester produces bogus log blooms')
     98     def test_log(self):
     99         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
    100         gas_oracle = OverrideGasOracle(limit=50000, conn=self.rpc)
    101         c = TestContract(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    102         b = b'\xee' * 32
    103         (tx_hash, o) = c.foo(self.address, self.accounts[0], 42, b.hex())
    104         r = self.rpc.do(o)
    105         o = receipt(tx_hash)
    106         rcpt = self.rpc.do(o)
    107         self.assertEqual(rcpt['status'], 1)
    108 
    109         bloom = LogBloom()
    110         topic = rcpt['logs'][0]['topics'][0]
    111         topic = bytes.fromhex(strip_0x(topic))
    112         address = bytes.fromhex(strip_0x(self.address))
    113         bloom.add(topic, address)
    114 
    115         o = block_by_number(rcpt['block_number'])
    116         r = self.rpc.do(o)
    117 
    118 
    119 if __name__ == '__main__':
    120     unittest.main()