test_block.py (2244B)
1 # standard imports 2 import unittest 3 import os 4 import datetime 5 import logging 6 7 # local imports 8 from chainlib.eth.jsonrpc import to_blockheight_param 9 from chainlib.eth.block import Block 10 from chainlib.eth.dialect import DialectFilter 11 12 logging.basicConfig(level=logging.DEBUG) 13 14 15 class TestBlock(unittest.TestCase): 16 17 18 def test_block(self): 19 tx_one_src = { 20 'hash': os.urandom(32).hex(), 21 'from': os.urandom(20).hex(), 22 'to': os.urandom(20).hex(), 23 'value': 13, 24 'data': '0xdeadbeef', 25 'nonce': 666, 26 'gasPrice': 100, 27 'gas': 21000, 28 } 29 30 tx_two_src_hash = os.urandom(32).hex() 31 32 block_hash = os.urandom(32).hex() 33 parent_hash = os.urandom(32).hex() 34 block_author = os.urandom(20).hex() 35 block_time = datetime.datetime.utcnow().timestamp() 36 block_src = { 37 'number': 42, 38 'hash': block_hash, 39 'author': block_author, 40 'transactions': [ 41 tx_one_src, 42 tx_two_src_hash, 43 ], 44 'timestamp': block_time, 45 'gas_used': '0x1234', 46 'gas_limit': '0x2345', 47 'parent_hash': parent_hash 48 } 49 block = Block(block_src) 50 51 self.assertEqual(block.number, 42) 52 self.assertEqual(block.hash, block_hash) 53 self.assertEqual(block.author, block_author) 54 self.assertEqual(block.timestamp, int(block_time)) 55 56 tx_index = block.tx_index_by_hash(tx_one_src['hash']) 57 self.assertEqual(tx_index, 0) 58 59 tx_retrieved = block.tx_by_index(tx_index) 60 self.assertEqual(tx_retrieved.hash, tx_one_src['hash']) 61 62 tx_index = block.tx_index_by_hash(tx_two_src_hash) 63 self.assertEqual(tx_index, 1) 64 65 66 def test_blockheight_param(self): 67 self.assertEqual(to_blockheight_param('latest'), 'latest') 68 self.assertEqual(to_blockheight_param(0), 'latest') 69 self.assertEqual(to_blockheight_param('pending'), 'pending') 70 self.assertEqual(to_blockheight_param(-1), 'pending') 71 self.assertEqual(to_blockheight_param(1), '0x0000000000000001') 72 73 74 if __name__ == '__main__': 75 unittest.main()