def test_callback(): R = [] def the_callback(blockchain, ops): R.extend(ops) parent_for_0 = b'\0' * 32 # same as test_fork, above BC = BlockChain(parent_for_0) BC.add_change_callback(the_callback) ITEMS = dict((i, FakeBlock(i)) for i in range(7)) ITEMS[0] = FakeBlock(0, parent_for_0) ITEMS.update(dict((i, FakeBlock(i)) for i in range(301, 306))) ITEMS[301] = FakeBlock(301, 3) # send them all except 302 BC.add_headers((ITEMS[i] for i in ITEMS.keys() if i != 302)) # now send 302 BC.add_headers([ITEMS[302]]) expected = [("add", ITEMS[i], i) for i in range(7)] expected += [("remove", ITEMS[i], i) for i in range(6, 3, -1)] expected += [("add", ITEMS[i], i + 4 - 301) for i in range(301, 306)] assert R == expected
def test_callback(): R = [] def the_callback(blockchain, ops): R.extend(ops) parent_for_0 = b'\0' * 32 # same as test_fork, above BC = BlockChain(parent_for_0) BC.add_change_callback(the_callback) ITEMS = dict((i, FakeBlock(i)) for i in range(7)) ITEMS[0] = FakeBlock(0, parent_for_0) ITEMS.update(dict((i, FakeBlock(i)) for i in range(301, 306))) ITEMS[301] = FakeBlock(301, 3) # send them all except 302 BC.add_headers((ITEMS[i] for i in ITEMS.keys() if i != 302)) # now send 302 BC.add_headers([ITEMS[302]]) expected = [("add", ITEMS[i], i) for i in range(7)] expected += [("remove", ITEMS[i], i) for i in range(6, 3, -1)] expected += [("add", ITEMS[i], i+4-301) for i in range(301, 306)] assert R == expected
def test_fork(): parent_for_0 = b'\0' * 32 # 0 <= 1 <= ... <= 5 <= 6 # 3 <= 301 <= 302 <= 303 <= 304 <= 305 # parent_for_0 = "motherless" BC = BlockChain(parent_for_0) ITEMS = dict((i, FakeBlock(i)) for i in range(7)) ITEMS[0] = FakeBlock(0, parent_for_0) ITEMS.update(dict((i, FakeBlock(i)) for i in range(301, 306))) ITEMS[301] = FakeBlock(301, 3) assert longest_block_chain(BC) == [] assert BC.locked_length() == 0 assert BC.length() == 0 assert set(BC.chain_finder.missing_parents()) == set() # send them all except 302 ops = BC.add_headers((ITEMS[i] for i in ITEMS.keys() if i != 302)) assert ops == [("add", ITEMS[i], i) for i in range(7)] assert set(BC.chain_finder.missing_parents()) == set([parent_for_0, 302]) # now send 302 ops = BC.add_headers([ITEMS[302]]) # we should see a change expected = [("remove", ITEMS[i], i) for i in range(6, 3, -1)] expected += [("add", ITEMS[i], i + 4 - 301) for i in range(301, 306)] assert ops == expected assert set(BC.chain_finder.missing_parents()) == set([parent_for_0])
def test_fork(): parent_for_0 = b'\0' * 32 # 0 <= 1 <= ... <= 5 <= 6 # 3 <= 301 <= 302 <= 303 <= 304 <= 305 # parent_for_0 = "motherless" BC = BlockChain(parent_for_0) ITEMS = dict((i, FakeBlock(i)) for i in range(7)) ITEMS[0] = FakeBlock(0, parent_for_0) ITEMS.update(dict((i, FakeBlock(i)) for i in range(301, 306))) ITEMS[301] = FakeBlock(301, 3) assert longest_block_chain(BC) == [] assert BC.locked_length() == 0 assert BC.length() == 0 assert set(BC.chain_finder.missing_parents()) == set() # send them all except 302 ops = BC.add_headers((ITEMS[i] for i in ITEMS.keys() if i != 302)) assert ops == [("add", ITEMS[i], i) for i in range(7)] assert set(BC.chain_finder.missing_parents()) == set([parent_for_0, 302]) # now send 302 ops = BC.add_headers([ITEMS[302]]) # we should see a change expected = [("remove", ITEMS[i], i) for i in range(6, 3, -1)] expected += [("add", ITEMS[i], i+4-301) for i in range(301, 306)] assert ops == expected assert set(BC.chain_finder.missing_parents()) == set([parent_for_0])
def test_chain_locking(): SIZE = 2000 COUNT = 200 ITEMS = [FakeBlock(i, i - 1) for i in range(SIZE * COUNT)] ITEMS[0] = FakeBlock(0, parent_for_0) BC = BlockChain(parent_for_0) assert longest_block_chain(BC) == [] assert BC.locked_length() == 0 assert BC.length() == 0 assert set(BC.chain_finder.missing_parents()) == set() for i in range(COUNT): start, end = i * SIZE, (i + 1) * SIZE lock_start = max(0, start - 10) expected_parent = lock_start - 1 if lock_start else parent_for_0 assert BC.length() == start assert BC.locked_length() == lock_start ops = BC.add_headers(ITEMS[start:end]) assert ops == [("add", ITEMS[i], i) for i in range(start, end)] assert longest_locked_block_chain(BC) == list(range(lock_start, end)) assert set(BC.chain_finder.missing_parents()) == {expected_parent} assert BC.parent_hash == expected_parent assert BC.locked_length() == lock_start assert BC.length() == end for i in range(start, end): v = BC.tuple_for_index(i) assert v[0] == i assert v[1] == parent_for_0 if i == 0 else i assert BC.index_for_hash(-1) is None assert BC.locked_length() == max(0, lock_start) BC.lock_to_index(end - 10) assert BC.locked_length() == end - 10
def test_chain_locking(): SIZE = 2000 COUNT = 200 ITEMS = [FakeBlock(i, i-1) for i in range(SIZE*COUNT)] ITEMS[0] = FakeBlock(0, parent_for_0) BC = BlockChain(parent_for_0) assert longest_block_chain(BC) == [] assert BC.locked_length() == 0 assert BC.length() == 0 assert set(BC.chain_finder.missing_parents()) == set() for i in range(COUNT): start, end = i*SIZE, (i+1)*SIZE lock_start = max(0, start-10) expected_parent = lock_start-1 if lock_start else parent_for_0 assert BC.length() == start assert BC.locked_length() == lock_start ops = BC.add_headers(ITEMS[start:end]) assert ops == [("add", ITEMS[i], i) for i in range(start, end)] assert longest_locked_block_chain(BC) == list(range(lock_start, end)) assert set(BC.chain_finder.missing_parents()) == {expected_parent} assert BC.parent_hash == expected_parent assert BC.locked_length() == lock_start assert BC.length() == end for i in range(start, end): v = BC.tuple_for_index(i) assert v[0] == i assert v[1] == parent_for_0 if i == 0 else i assert BC.index_for_hash(-1) is None assert BC.locked_length() == max(0, lock_start) BC.lock_to_index(end-10) assert BC.locked_length() == end-10
def items_for_client(initial_blocks=[]): block_store = {} block_chain = BlockChain() blockfetcher = Blockfetcher() inv_collector = InvCollector() block_handler = BlockHandler(inv_collector, block_chain, block_store) fast_forward_add_peer = fast_forwarder_add_peer_f(block_chain) for block in initial_blocks: inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK, block.hash())) block_store[block.hash()] = block block_chain.add_headers(initial_blocks) inv_q = inv_collector.new_inv_item_queue() ap = make_add_peer(fast_forward_add_peer, blockfetcher, block_handler, inv_collector, block_chain, block_store) ap.block_getter_task = asyncio.Task(block_getter(inv_q, inv_collector, block_handler, block_chain, block_store)) return block_handler, block_chain, block_store, ap
def locked_blocks_iterator(blockfile, start_info=(0, 0), cached_headers=50, batch_size=50): """ This method loads blocks from disk, skipping any orphan blocks. """ f = blockfile current_state = [] def change_state(bc, ops): for op, bh, work in ops: if op == 'add': current_state.append(bh) pass else: current_state.pop() bc = BlockChain() bc.add_change_callback(change_state) bhs = [] index = 0 info_offset = start_info while 1: v = blockfile.next_offset(info_offset) if v is None: break block_offset, info_offset = v f.jump_to(block_offset) bh = Block.parse_as_header(f) bh.info = block_offset bhs.append(bh) if len(bhs) > batch_size: bc.add_headers(bhs) bhs = [] if len(current_state) > cached_headers: for bh in current_state[:cached_headers]: bh.index = index yield bh index += 1 bc.lock_to_index(index) current_state = current_state[cached_headers:]
def locked_blocks_iterator(start_info=(0, 0), cached_headers=50, batch_size=50, base_dir=None, headers_only=False): """ This method loads blocks from disk, skipping any orphan blocks. """ block_class = BlockHeader if headers_only else Block f = Blockfiles(base_dir, start_info) for initial_location in block_info_iterator(start_info, base_dir): f.jump_to(initial_location) initial_header = BlockHeader.parse(f) break current_state = [] def change_state(bc, ops): for op, bh, work in ops: if op == 'add': current_state.append(bh) pass else: current_state.pop() bc = BlockChain() bc.add_change_callback(change_state) bhs = [] index = 0 for info in block_info_iterator(start_info, base_dir): bh = blockheader_for_offset_info(info, base_dir) bh.info = info bhs.append(bh) if len(bhs) > batch_size: bc.add_headers(bhs) bhs = [] if len(current_state) > cached_headers: for bh in current_state[:cached_headers]: f.jump_to(bh.info) block = block_class.parse(f) yield block index += 1 bc.lock_to_index(index) current_state = current_state[cached_headers:]
def items_for_client(initial_blocks=[]): block_store = {} block_chain = BlockChain() blockfetcher = Blockfetcher() inv_collector = InvCollector() block_handler = BlockHandler(inv_collector, block_chain, block_store) fast_forward_add_peer = fast_forwarder_add_peer_f(block_chain) for block in initial_blocks: inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK, block.hash())) block_store[block.hash()] = block block_chain.add_headers(initial_blocks) inv_q = inv_collector.new_inv_item_queue() ap = make_add_peer(fast_forward_add_peer, blockfetcher, block_handler, inv_collector, block_chain, block_store) ap.block_getter_task = asyncio.Task( block_getter(inv_q, inv_collector, block_handler, block_chain, block_store)) return block_handler, block_chain, block_store, ap
def locked_blocks_iterator(start_info=(0, 0), cached_headers=50, batch_size=50, base_dir=None, headers_only=False): """ This method loads blocks from disk, skipping any orphan blocks. """ parse_method = Block.parse_as_header if headers_only else Block.parse f = Blockfiles(base_dir, start_info) for initial_location in block_info_iterator(start_info, base_dir): f.jump_to(initial_location) parse_method(f) break current_state = [] def change_state(bc, ops): for op, bh, work in ops: if op == 'add': current_state.append(bh) pass else: current_state.pop() bc = BlockChain() bc.add_change_callback(change_state) bhs = [] index = 0 for info in block_info_iterator(start_info, base_dir): bh = blockheader_for_offset_info(info, base_dir) bh.info = info bhs.append(bh) if len(bhs) > batch_size: bc.add_headers(bhs) bhs = [] if len(current_state) > cached_headers: for bh in current_state[:cached_headers]: f.jump_to(bh.info) block = parse_method(f) yield block index += 1 bc.lock_to_index(index) current_state = current_state[cached_headers:]
def test_simple_getheader(): BLOCKS = make_blocks(20) blockchain1 = BlockChain() blockchain1.add_headers(BLOCKS) block_store = dict((b.hash(), b) for b in BLOCKS) peer1, peer2 = create_handshaked_peers() block_store = {} block_chain = BlockChain() inv_collector = InvCollector() block_handler = BlockHandler(inv_collector, block_chain, block_store) for block in BLOCKS: inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK, block.hash())) block_store[block.hash()] = block block_chain.add_headers(BLOCKS) inv_collector.add_peer(peer1) block_handler.add_peer(peer1) @asyncio.coroutine def run_peer2(): r = [] headers = yield from standards.get_headers_hashes( peer2, until_block_hash=b'\0' * 32) r.append(headers) return r f2 = asyncio.Task(run_peer2()) asyncio.get_event_loop().run_until_complete(asyncio.wait([f2])) r = f2.result() assert len(r) == 1 assert [b.hash() for b in r[0]] == [b.hash() for b in BLOCKS]
def test_simple_getheader(): BLOCKS = make_blocks(20) blockchain1 = BlockChain() blockchain1.add_headers(BLOCKS) block_store = dict((b.hash(), b) for b in BLOCKS) peer1, peer2 = create_handshaked_peers() block_store = {} block_chain = BlockChain() inv_collector = InvCollector() block_handler = BlockHandler(inv_collector, block_chain, block_store) for block in BLOCKS: inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK, block.hash())) block_store[block.hash()] = block block_chain.add_headers(BLOCKS) inv_collector.add_peer(peer1) block_handler.add_peer(peer1) @asyncio.coroutine def run_peer2(): r = [] headers = yield from standards.get_headers_hashes(peer2, until_block_hash=b"\0" * 32) r.append(headers) return r f2 = asyncio.Task(run_peer2()) asyncio.get_event_loop().run_until_complete(asyncio.wait([f2])) r = f2.result() assert len(r) == 1 assert [b.hash() for b in r[0]] == [b.hash() for b in BLOCKS]
def test_large(): SIZE = 3000 ITEMS = [FakeBlock(i) for i in range(SIZE)] ITEMS[0] = FakeBlock(0, parent_for_0) BC = BlockChain(parent_for_0) assert longest_block_chain(BC) == [] assert BC.locked_length() == 0 assert BC.length() == 0 assert set(BC.chain_finder.missing_parents()) == set() ops = BC.add_headers(ITEMS) assert ops == [("add", ITEMS[i], i) for i in range(SIZE)] assert longest_block_chain(BC) == list(range(SIZE)) assert set(BC.chain_finder.missing_parents()) == {parent_for_0} assert BC.parent_hash == parent_for_0 assert BC.locked_length() == 0 assert BC.length() == SIZE for i in range(SIZE): v = BC.tuple_for_index(i) assert v[0] == i assert v[1] == parent_for_0 if i == 0 else i assert BC.index_for_hash(-1) is None
def test_basic(): BC = BlockChain(parent_for_0) ITEMS = [FakeBlock(i) for i in range(100)] ITEMS[0] = FakeBlock(0, parent_for_0) assert longest_block_chain(BC) == [] assert BC.length() == 0 assert BC.locked_length() == 0 assert set(BC.chain_finder.missing_parents()) == set() assert BC.parent_hash == parent_for_0 assert BC.index_for_hash(0) is None assert BC.index_for_hash(-1) is None ops = BC.add_headers(ITEMS[:5]) assert ops == [("add", ITEMS[i], i) for i in range(5)] assert BC.parent_hash == parent_for_0 assert longest_block_chain(BC) == list(range(5)) assert BC.length() == 5 assert BC.locked_length() == 0 assert set(BC.chain_finder.missing_parents()) == {parent_for_0} for i in range(5): v = BC.tuple_for_index(i) assert v[0] == i assert v[1] == parent_for_0 if i == 0 else i assert BC.index_for_hash(-1) is None ops = BC.add_headers(ITEMS[:7]) assert ops == [("add", ITEMS[i], i) for i in range(5, 7)] assert BC.parent_hash == parent_for_0 assert longest_block_chain(BC) == list(range(7)) assert BC.length() == 7 assert BC.locked_length() == 0 assert set(BC.chain_finder.missing_parents()) == {parent_for_0} for i in range(7): v = BC.tuple_for_index(i) assert v[0] == i assert v[1] == parent_for_0 if i == 0 else i assert BC.index_for_hash(-1) is None ops = BC.add_headers(ITEMS[10:14]) assert ops == [] assert BC.parent_hash == parent_for_0 assert longest_block_chain(BC) == [0, 1, 2, 3, 4, 5, 6] assert BC.locked_length() == 0 assert BC.locked_length() == 0 assert BC.length() == 7 assert set(BC.chain_finder.missing_parents()) == {parent_for_0, 9} for i in range(7): v = BC.tuple_for_index(i) assert v[0] == i assert v[1] == parent_for_0 if i == 0 else i assert BC.index_for_hash(-1) is None ops = BC.add_headers(ITEMS[7:10]) assert ops == [("add", ITEMS[i], i) for i in range(7, 14)] assert longest_block_chain(BC) == list(range(14)) assert set(BC.chain_finder.missing_parents()) == {parent_for_0} assert BC.parent_hash == parent_for_0 assert BC.locked_length() == 0 assert BC.length() == 14 for i in range(14): v = BC.tuple_for_index(i) assert v[0] == i assert v[1] == parent_for_0 if i == 0 else i assert BC.index_for_hash(-1) is None ops = BC.add_headers(ITEMS[90:]) assert ops == [] assert longest_block_chain(BC) == list(range(14)) assert set(BC.chain_finder.missing_parents()) == {parent_for_0, 89} assert BC.parent_hash == parent_for_0 assert BC.locked_length() == 0 assert BC.length() == 14 for i in range(14): v = BC.tuple_for_index(i) assert v[0] == i assert v[1] == parent_for_0 if i == 0 else i assert BC.index_for_hash(-1) is None ops = BC.add_headers(ITEMS[14:90]) assert ops == [("add", ITEMS[i], i) for i in range(14, 100)] assert longest_block_chain(BC) == list(range(100)) assert set(BC.chain_finder.missing_parents()) == {parent_for_0} assert BC.parent_hash == parent_for_0 assert BC.locked_length() == 0 assert BC.length() == 100 for i in range(100): v = BC.tuple_for_index(i) assert v[0] == i assert v[1] == parent_for_0 if i == 0 else i assert BC.index_for_hash(-1) is None