def _recv_Blocks(self, data): print("RECEIVED BLOCKS", len(data)) if len(data) < MIN_BLOCKS: return assert blocks.TransientBlock(rlp.encode( data[0])).number >= blocks.TransientBlock(rlp.encode(data[-1])).number for x in data: enc = rlp.encode(x) tb = blocks.TransientBlock(enc) print tb self.blk_counter += 1 if self.lowest_block is None: self.lowest_block = tb.number else: if self.lowest_block - 1 == tb.number: self.lowest_block = tb.number else: # i.e. newly mined block sent return if tb not in collected_blocks: collected_blocks.append(tb) # exit if we are at the genesis if tb.number == 1: print 'done' for tb in sorted(collected_blocks, key=attrgetter('number')): print 'writing', tb fh.write(tb.rlpdata.encode('hex') + '\n') # LOG line sys.exit(0) # fetch more print("ASKING FOR MORE HASHES", tb.hash.encode('hex'), tb.number) self.send_GetBlockHashes(tb.hash, NUM_BLOCKS_PER_REQUEST)
def test_add_side_chain(): """" Local: L0, L1, L2 add Remote: R0, R1 """ k, v, k2, v2 = accounts() # Remote: mine one block set_db() R0 = mkquickgenesis({v: utils.denoms.ether * 1}) db_store(R0) tx0 = get_transaction(nonce=0) R1 = mine_next_block(R0, transactions=[tx0]) db_store(R1) assert tx0 in R1.get_transactions() # Local: mine two blocks set_db() L0 = mkquickgenesis({v: utils.denoms.ether * 1}) cm = get_chainmanager(genesis=L0) tx0 = get_transaction(nonce=0) L1 = mine_next_block(L0, transactions=[tx0]) cm.add_block(L1) tx1 = get_transaction(nonce=1) L2 = mine_next_block(L1, transactions=[tx1]) cm.add_block(L2) # receive serialized remote blocks, newest first transient_blocks = [ blocks.TransientBlock(R1.serialize()), blocks.TransientBlock(R0.serialize()) ] cm.receive_chain(transient_blocks=transient_blocks) assert L2.hash in cm
def test_add_longer_side_chain(): """" Local: L0, L1, L2 Remote: R0, R1, R2, R3 """ k, v, k2, v2 = accounts() # Remote: mine one block set_db() blk = mkquickgenesis({v: utils.denoms.ether * 1}) db_store(blk) remote_blocks = [blk] for i in range(3): tx = get_transaction(nonce=i) blk = mine_next_block(remote_blocks[-1], transactions=[tx]) db_store(blk) remote_blocks.append(blk) # Local: mine two blocks set_db() L0 = mkquickgenesis({v: utils.denoms.ether * 1}) cm = get_chainmanager(genesis=L0) tx0 = get_transaction(nonce=0) L1 = mine_next_block(L0, transactions=[tx0]) cm.add_block(L1) tx1 = get_transaction(nonce=1) L2 = mine_next_block(L1, transactions=[tx1]) cm.add_block(L2) # receive serialized remote blocks, newest first transient_blocks = [ blocks.TransientBlock(b.serialize()) for b in reversed(remote_blocks) ] cm.receive_chain(transient_blocks=transient_blocks) assert cm.head == remote_blocks[-1]
def do_test(hex_rlp_encoded_data): from test_chain import set_db, get_chainmanager set_db() chain_manager = get_chainmanager() data = rlp.decode(hex_rlp_encoded_data.decode('hex')) transient_blocks = [blocks.TransientBlock(rlp.encode(b)) for b in data] assert len(transient_blocks) == 128 chain_manager.receive_chain(transient_blocks) print chain_manager.head
def import_chain_data(raw_blocks_fn, test_db_path, skip=0): from pyethereum import chainmanager utils.data_dir.set(test_db_path) chain_manager = chainmanager.ChainManager() chain_manager.configure(config=get_default_config(), genesis=None) fh = open(raw_blocks_fn) for i in range(skip): fh.readline() for hex_rlp_encoded_data in fh: hexdata = hex_rlp_encoded_data.strip().decode('hex') data = rlp.decode(hexdata) blk = blocks.TransientBlock(hexdata) print blk.number, blk.hash.encode('hex'), '%d txs' % len( blk.transaction_list) chain_manager.receive_chain([blk]) assert blk.hash in chain_manager
def import_chain_data(raw_blocks_fn, test_db_path, skip=0): chain_manager = new_chainmanager() fh = open(raw_blocks_fn) for i in range(skip): fh.readline() for hex_rlp_encoded_data in fh: hexdata = hex_rlp_encoded_data.strip().decode('hex') data = rlp.decode(hexdata) blk = blocks.TransientBlock(hexdata) print blk.number, blk.hash.encode('hex'), '%d txs' % len(blk.transaction_list) head = chain_manager.head assert blocks.check_header_pow(blk.header_args) chain_manager.receive_chain([blk]) if not blk.hash in chain_manager: print 'block could not be added' assert head == chain_manager.head chain_manager.head.deserialize_child(blk.rlpdata) assert blk.hash in chain_manager
def dump_transactions(hex_rlp_encoded_data): "use py.test -s to get logs" blk = blocks.TransientBlock(hex_rlp_encoded_data.decode('hex')) for tx_lst_serialized, _state_root, _gas_used_encoded in blk.transaction_list: tx = transactions.Transaction.create(tx_lst_serialized) print tx.to_dict()
data can be created with blockfetcherpatch.py """ import sys raw_blocks_fn = sys.argv[1] test_db_path = sys.argv[2] skip = int(sys.argv[3]) if len(sys.argv) == 4 or sys.argv[4] != 'silent': logging.basicConfig(level=logging.DEBUG) global logger logger = logging.getLogger() print utils utils.data_dir.set(test_db_path) chain_manager = chainmanager.ChainManager() chain_manager.configure(config=eth.create_default_config(), genesis=None) fh = open(raw_blocks_fn) for i in range(skip): fh.readline() for hex_rlp_encoded_data in fh: hexdata = hex_rlp_encoded_data.strip().decode('hex') data = rlp.decode(hexdata) # print repr(data) blk = blocks.TransientBlock(hexdata) print blk.number, blk.hash.encode('hex') chain_manager.receive_chain([blk]) assert blk.hash in chain_manager
def test_transient_block(): db = new_db() blk = blocks.genesis(db) tb_blk = blocks.TransientBlock(blk.serialize()) assert blk.hash == tb_blk.hash assert blk.number == tb_blk.number
def dump_transactions(hex_rlp_encoded_data): "use py.test -s to get logs" blk = blocks.TransientBlock(hex_rlp_encoded_data.decode('hex')) for tx_lst_serialized in blk.transaction_list: tx = transactions.Transaction.create(tx_lst_serialized)