def handshake_peers(peer1, peer2, peer_info_1={}, peer_info_2={}): msg1 = version_data_for_peer(peer1, **peer_info_1) msg2 = version_data_for_peer(peer2, **peer_info_2) asyncio.get_event_loop().run_until_complete( asyncio.wait( [initial_handshake(peer1, msg1), initial_handshake(peer2, msg2)])) return peer1, peer2
def create_handshaked_peers(ip1="127.0.0.1", ip2="127.0.0.2"): peer1, peer2 = create_peers(ip1=ip1, ip2=ip2) watch_messages(peer1) watch_messages(peer2) asyncio.get_event_loop().run_until_complete( asyncio.wait([ initial_handshake(peer1, VERSION_MSG), initial_handshake(peer2, VERSION_MSG_2) ])) return peer1, peer2
def test_initial_handshake(): peer1, peer2 = create_peers() f1 = asyncio.Task(standards.initial_handshake(peer1, VERSION_MSG)) f2 = asyncio.Task(standards.initial_handshake(peer2, VERSION_MSG_2)) asyncio.get_event_loop().run_until_complete(asyncio.wait([f1, f2])) assert f1.result() == VERSION_MSG_2 assert f2.result() == VERSION_MSG
def test_ping_pong(): peer1, peer2 = create_peers() f1 = asyncio.Task(standards.initial_handshake(peer1, VERSION_MSG)) f2 = asyncio.Task(standards.initial_handshake(peer2, VERSION_MSG_2)) asyncio.get_event_loop().run_until_complete(asyncio.wait([f1, f2])) standards.install_ping_manager(peer1, heartbeat_rate=0.5, missing_pong_disconnect_timeout=110.1) standards.install_pong_manager(peer2) asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.6)) assert peer1.writ_data[-26:] == peer2.writ_data[-26:]
def run_peer(peer, fetcher, getheaders_add_peer, blockfetcher, inv_collector): yield from asyncio.wait_for(peer.connection_made_future, timeout=None) last_block_index = max(0, self.blockchain_view.last_block_index()) version_parameters = version_data_for_peer( peer, local_port=0, last_block_index=last_block_index, nonce=self.nonce, subversion=self.subversion) version_data = yield from initial_handshake( peer, version_parameters) if self.bloom_filter: filter_bytes, hash_function_count, tweak = self.bloom_filter.filter_load_params( ) # TODO: figure out flags flags = 0 peer.send_msg("filterload", filter=filter_bytes, hash_function_count=hash_function_count, tweak=tweak, flags=flags) last_block_index = version_data["last_block_index"] getheaders_add_peer(peer, last_block_index) blockfetcher.add_peer(peer, fetcher, last_block_index) inv_collector.add_peer(peer)
def loop_connect_to_superpeer(superpeer_ip_queue): while 1: try: pair = yield from superpeer_ip_queue.get() if pair is None: break peer_name = "%s:%d" % pair host, port = pair logging.debug("connecting to superpeer at %s", peer_name) transport, peer = yield from asyncio.get_event_loop().create_connection( lambda: BitcoinPeerProtocol(magic_header), host=host, port=port) logging.debug("connected to superpeer at %s", peer_name) yield from initial_handshake(peer, version_data_for_peer(peer)) logging.debug("handshake complete on %s", peer_name) date_address_tuples = yield from get_date_address_tuples(peer) logging.debug("got addresses from %s", peer_name) for da in date_address_tuples: timestamp_peeraddress_tuple_queue.put_nowait((-da[0], da[1])) logging.debug("closing connection to %s", peer_name) transport.close() except Exception: logging.exception("failed during connect to %s", peer_name)
def run_peer1(): r = [] yield from standards.initial_handshake(peer1, VERSION_MSG) next_message = peer1.new_get_next_message_f() t = yield from next_message() r.append(t) return r
def run_peer2(): r = [] yield from standards.initial_handshake(peer2, VERSION_MSG_2) tx_fetcher = Fetcher(peer2) tx = yield from tx_fetcher.fetch(mi(TX_LIST[0].hash()), timeout=2) r.append(tx) return r
def run_peer1(): r = [] yield from standards.initial_handshake(peer1, VERSION_MSG) next_message = peer1.new_get_next_message_f() t = yield from next_message() r.append(t) peer1.send_msg("tx", tx=TX_LIST[0]) t = yield from next_message() r.append(t) peer1.send_msg("notfound", items=[InvItem(ITEM_TYPE_TX, TX_LIST[1].hash())]) t = yield from next_message() r.append(t) peer1.send_msg("tx", tx=TX_LIST[2]) t = yield from next_message() r.append(t) items = [InvItem(ITEM_TYPE_TX, TX_LIST[3].hash())] items.append([InvItem(ITEM_TYPE_TX, TX_LIST[5].hash())]) peer1.send_msg("notfound", items=items) peer1.send_msg("tx", tx=TX_LIST[4]) return r
def loop_connect_to_superpeer(superpeer_ip_queue): while 1: try: pair = yield from superpeer_ip_queue.get() if pair is None: break peer_name = "%s:%d" % pair host, port = pair logging.debug("connecting to superpeer at %s", peer_name) transport, peer = yield from asyncio.get_event_loop( ).create_connection(lambda: BitcoinPeerProtocol(magic_header), host=host, port=port) logging.debug("connected to superpeer at %s", peer_name) yield from initial_handshake(peer, version_data_for_peer(peer)) logging.debug("handshake complete on %s", peer_name) date_address_tuples = yield from get_date_address_tuples(peer) logging.debug("got addresses from %s", peer_name) for da in date_address_tuples: timestamp_peeraddress_tuple_queue.put_nowait( (-da[0], da[1])) logging.debug("closing connection to %s", peer_name) transport.close() except Exception: logging.exception("failed during connect to %s", peer_name)
def run_peer(peer, fetcher, fast_forward_add_peer, blockfetcher, inv_collector, blockhandler): yield from asyncio.wait_for(peer.connection_made_future, timeout=None) version_parameters = version_data_for_peer(peer) version_data = yield from initial_handshake(peer, version_parameters) last_block_index = version_data["last_block_index"] fast_forward_add_peer(peer, last_block_index) blockfetcher.add_peer(peer, fetcher, last_block_index) inv_collector.add_peer(peer) blockhandler.add_peer(peer)
def run_peer(peer, fetcher, fast_forward_add_peer, blockfetcher, inv_collector, blockhandler): yield from asyncio.wait_for(peer.connection_made_future, timeout=None) version_parameters = version_data_for_peer( peer, local_port=(server_port or 0), last_block_index=block_chain.length(), nonce=self.nonce, subversion=self.subversion) version_data = yield from initial_handshake(peer, version_parameters) last_block_index = version_data["last_block_index"] fast_forward_add_peer(peer, last_block_index) blockfetcher.add_peer(peer, fetcher, last_block_index) inv_collector.add_peer(peer) blockhandler.add_peer(peer)
def test_missing_pong_disconnect(): peer1, peer2 = create_peers() f1 = asyncio.Task(standards.initial_handshake(peer1, VERSION_MSG)) f2 = asyncio.Task(standards.initial_handshake(peer2, VERSION_MSG_2)) asyncio.get_event_loop().run_until_complete(asyncio.wait([f1, f2])) standards.install_ping_manager(peer1, heartbeat_rate=0.5, missing_pong_disconnect_timeout=0.01) asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.6)) next_message = peer1.new_get_next_message_f() ## make sure peer1 is disconnected got_eof = False try: asyncio.get_event_loop().run_until_complete(asyncio.wait_for(next_message(), timeout=0.2)) except EOFError: got_eof = True assert got_eof
def test_missing_pong_disconnect(): peer1, peer2 = create_peers() f1 = asyncio.Task(standards.initial_handshake(peer1, VERSION_MSG)) f2 = asyncio.Task(standards.initial_handshake(peer2, VERSION_MSG_2)) asyncio.get_event_loop().run_until_complete(asyncio.wait([f1, f2])) standards.install_ping_manager(peer1, heartbeat_rate=0.5, missing_pong_disconnect_timeout=0.01) asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.6)) next_message = peer1.new_get_next_message_f() ## make sure peer1 is disconnected got_eof = False try: asyncio.get_event_loop().run_until_complete( asyncio.wait_for(next_message(), timeout=0.2)) except EOFError: got_eof = True assert got_eof
def run_peer(peer, fetcher, getheaders_add_peer, blockfetcher, inv_collector): yield from asyncio.wait_for(peer.connection_made_future, timeout=None) last_block_index = max(0, self.blockchain_view.last_block_index()) version_parameters = version_data_for_peer( peer, local_port=0, last_block_index=last_block_index, nonce=self.nonce, subversion=self.subversion) version_data = yield from initial_handshake(peer, version_parameters) filter_bytes, hash_function_count, tweak = self.bloom_filter.filter_load_params() # TODO: figure out flags flags = 0 peer.send_msg( "filterload", filter=filter_bytes, hash_function_count=hash_function_count, tweak=tweak, flags=flags) last_block_index = version_data["last_block_index"] getheaders_add_peer(peer, last_block_index) blockfetcher.add_peer(peer, fetcher, last_block_index) inv_collector.add_peer(peer)
def run_peer2(): r = [] yield from standards.initial_handshake(peer2, VERSION_MSG_2) tx_fetcher = Fetcher(peer2) tx = yield from tx_fetcher.fetch(mi(TX_LIST[0].hash()), timeout=5) r.append(tx) tx = yield from tx_fetcher.fetch(mi(TX_LIST[1].hash())) r.append(tx) tx = yield from tx_fetcher.fetch(mi(TX_LIST[2].hash())) r.append(tx) f1 = asyncio.Task(tx_fetcher.fetch(mi(TX_LIST[3].hash()))) f2 = asyncio.Task(tx_fetcher.fetch(mi(TX_LIST[4].hash()))) f3 = asyncio.Task(tx_fetcher.fetch(mi(TX_LIST[5].hash()))) yield from asyncio.wait([f1, f2, f3]) r.append(f1.result()) r.append(f2.result()) r.append(f3.result()) return r
def connect_to_remote(event_loop, magic_header, address_db, connections): host, port = address_db.next_address() logging.info("connecting to %s port %d", host, port) try: transport, peer = yield from event_loop.create_connection( lambda: BitcoinPeerProtocol(magic_header), host=host, port=port) except Exception: logging.exception("failed to connect to %s:%d", host, port) address_db.remove_address(host, port) address_db.save() return try: logging.info("connected to %s:%d", host, port) yield from asyncio.wait_for(peer.connection_made_future, timeout=None) version_parameters = version_data_for_peer(peer) yield from initial_handshake(peer, version_parameters) AddressKeeper(peer, address_db) address_db.add_address(host, port, int(time.time())) connections.add(peer) except Exception: logging.exception("exception talking to %s:%d", host, port) logging.info("done talking to %s:%d", host, port)
def run_peer1(): yield from standards.initial_handshake(peer1, VERSION_MSG) next_message = peer1.new_get_next_message_f() name, data = yield from next_message() peer1.send_msg("addr", date_address_tuples=DA_TUPLES) return name, data
def run_peer2(): yield from standards.initial_handshake(peer2, VERSION_MSG_2) date_address_tuples = yield from standards.get_date_address_tuples( peer2) return DA_TUPLES
def create_handshaked_peers(ip1="127.0.0.1", ip2="127.0.0.2"): peer1, peer2 = create_peers(ip1=ip1, ip2=ip2) watch_messages(peer1) watch_messages(peer2) asyncio.get_event_loop().run_until_complete(asyncio.wait([initial_handshake(peer1, VERSION_MSG), initial_handshake(peer2, VERSION_MSG_2)])) return peer1, peer2
def handshake_peers(peer1, peer2, peer_info_1={}, peer_info_2={}): msg1 = version_data_for_peer(peer1, **peer_info_1) msg2 = version_data_for_peer(peer2, **peer_info_2) asyncio.get_event_loop().run_until_complete(asyncio.wait([initial_handshake(peer1, msg1), initial_handshake(peer2, msg2)])) return peer1, peer2
def run_peer2(): yield from standards.initial_handshake(peer2, VERSION_MSG_2) date_address_tuples = yield from standards.get_date_address_tuples(peer2) return DA_TUPLES