def sync_with_ping(self, timeout=30): def received_pong(): return (self.last_pong.nonce == self.ping_counter) self.connection.send_message(msg_ping(nonce=self.ping_counter)) success = wait_until(received_pong, timeout) self.ping_counter += 1 return success
def sync_with_ping(self, timeout=30): self.connection.send_message(msg_ping(nonce=self.ping_counter)) sleep_time = 0.05 while timeout > 0: with mininode_lock: if self.last_pong.nonce == self.ping_counter: self.ping_counter += 1 return time.sleep(sleep_time) timeout -= sleep_time fail("Should have received pong")
def run_test(self): # Setup the p2p connections and start up the network thread. self.no_verack_node = TestNode() # never send verack self.no_version_node = TestNode() # never send version (just ping) self.no_send_node = TestNode() # never send anything connections = [] connections.append( NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_verack_node)) connections.append( NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_version_node, send_version=False)) connections.append( NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_send_node, send_version=False)) self.no_verack_node.add_connection(connections[0]) self.no_version_node.add_connection(connections[1]) self.no_send_node.add_connection(connections[2]) NetworkThread().start() # Start up network handling in another thread sleep(1) assert (self.no_verack_node.connected) assert (self.no_version_node.connected) assert (self.no_send_node.connected) ping_msg = msg_ping() connections[0].send_message(ping_msg) connections[1].send_message(ping_msg) sleep(30) assert "version" in self.no_verack_node.last_message assert (self.no_verack_node.connected) assert (self.no_version_node.connected) assert (self.no_send_node.connected) connections[0].send_message(ping_msg) connections[1].send_message(ping_msg) sleep(31) assert (not self.no_verack_node.connected) assert (not self.no_version_node.connected) assert (not self.no_send_node.connected)
def sync_with_ping(self, timeout=30): self.connection.send_message(msg_ping(nonce=self.ping_counter)) received_pong = False sleep_time = 0.05 while not received_pong and timeout > 0: time.sleep(sleep_time) timeout -= sleep_time with mininode_lock: if self.last_pong.nonce == self.ping_counter: received_pong = True self.ping_counter += 1 return received_pong
def sync_with_ping(self, timeout=30): self.connection.send_message(msg_ping(nonce=self.ping_counter)) received_pong = False sleep_time = 0.05 while not received_pong and timeout > 0: time.sleep(sleep_time) timeout -= sleep_time with mininode_lock: if self.last_pong.nonce == self.ping_counter: received_pong = True self.ping_counter += 1 return received_pong
def sync_with_ping(self, timeout=30, waiting_for=None): self.connection.send_message(msg_ping(nonce=self.ping_counter)) sleep_time = 0.05 while timeout > 0: with mininode_lock: ready = True if waiting_for is None else waiting_for( self) is not None if ready and self.last_pong.nonce == self.ping_counter: self.ping_counter += 1 return time.sleep(sleep_time) timeout -= sleep_time fail("Should have received pong")
def run_test(self): # Setup 2 associations; one with BlockPriority policy and one with Default policy associations_stream_policies = [ BlockPriorityStreamPolicy(), DefaultStreamPolicy() ] with self.run_node_with_associations( "Simple Association Setup", 0, [], associations_stream_policies, cb_class=MyAssociationCB) as associations: # Check we got an association ID and some known stream policies back for association in associations: assert (association.association_id) assert (association.recvd_stream_policy_names) # Check nodes peer info looks correct peerinfo = self.nodes[0].getpeerinfo() assert_equal(len(peerinfo), len(associations)) for i in range(len(associations_stream_policies)): assert_equal( len(peerinfo[i]["streams"]), len(associations_stream_policies[i].additional_streams) + 1) # Check sending and receiving messages works for association in associations: # Check initial pong message count is 0 assert_equal(association.callbacks.pong_count, 0) # Send ping association.send_message(msg_ping()) # Wait for callback to indicate we got the pong on the association wait_until(lambda: association.callbacks.pong_count > 0) # Check type of stream pong came over is correct for the stream policy in use if type(association.stream_policy ) is BlockPriorityStreamPolicy: assert_equal(association.callbacks.pong_stream, StreamType.DATA1) elif type(association.stream_policy) is DefaultStreamPolicy: assert_equal(association.callbacks.pong_stream, StreamType.GENERAL)
def run_test(self): # Try to connect to a node using an invalid checksum on version message bad_interface = BadVersionP2PInterface() self.nodes[0].add_p2p_connection(bad_interface) # Also connect to a node with a valid version message interface = P2PInterface() connection = self.nodes[1].add_p2p_connection(interface) network_thread_start() # The invalid version message should cause a disconnect on the first # connection because we are now banned bad_interface.wait_for_disconnect() # Node with valid version message should connect successfully connection.wait_for_verack() # Create a valid message valid_message = msg_ping(interface.ping_counter) def wait_for_ping(): def check_ping(): if not interface.last_message.get("pong"): return False return interface.last_message[ "pong"].nonce == interface.ping_counter wait_until(check_ping, lock=mininode_lock) interface.ping_counter += 1 # The valid message is accepted connection.send_message(valid_message) wait_for_ping() # Make an invalid copy of the valid message with an invalid checksum invalid_message = msg_bad_checksum(connection, valid_message) # The invalid message should cause a disconnect because we are now # banned connection.send_raw_message(invalid_message) interface.wait_for_disconnect()
def run_test(self): test = TestManager() # Launch Sprout, Overwinter, and Sapling mininodes nodes = [] for x in xrange(10): nodes.append( NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SPROUT_PROTO_VERSION)) nodes.append( NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", OVERWINTER_PROTO_VERSION)) nodes.append( NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SAPLING_PROTO_VERSION)) # Start up network handling in another thread NetworkThread().start() # Sprout consensus rules apply at block height 9 self.nodes[0].generate(9) assert_equal(9, self.nodes[0].getblockcount()) # Verify mininodes are still connected to zelcashd node peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(10, versions.count(SPROUT_PROTO_VERSION)) assert_equal(10, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(10, versions.count(SAPLING_PROTO_VERSION)) # Overwinter consensus rules activate at block height 10 self.nodes[0].generate(1) assert_equal(10, self.nodes[0].getblockcount()) print('Overwinter active') # Mininodes send ping message to zelcashd node. pingCounter = 1 for node in nodes: node.send_message(msg_ping(pingCounter)) pingCounter = pingCounter + 1 time.sleep(3) # Verify Sprout mininodes have been dropped, while Overwinter and # Sapling mininodes are still connected. peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(0, versions.count(SPROUT_PROTO_VERSION)) assert_equal(10, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(10, versions.count(SAPLING_PROTO_VERSION)) # Extend the Overwinter chain with another block. self.nodes[0].generate(1) # Connect a new Overwinter mininode to the zelcashd node, which is accepted. nodes.append( NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", OVERWINTER_PROTO_VERSION)) time.sleep(3) assert_equal(21, len(self.nodes[0].getpeerinfo())) # Connect a new Sapling mininode to the zelcashd node, which is accepted. nodes.append( NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SAPLING_PROTO_VERSION)) time.sleep(3) assert_equal(22, len(self.nodes[0].getpeerinfo())) # Try to connect a new Sprout mininode to the zelcashd node, which is rejected. sprout = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SPROUT_PROTO_VERSION) nodes.append(sprout) time.sleep(3) assert ("Version must be 170003 or greater" in str(sprout.rejectMessage)) # Verify that only Overwinter and Sapling mininodes are connected. peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(0, versions.count(SPROUT_PROTO_VERSION)) assert_equal(11, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(11, versions.count(SAPLING_PROTO_VERSION)) # Sapling consensus rules activate at block height 15 self.nodes[0].generate(4) assert_equal(15, self.nodes[0].getblockcount()) print('Sapling active') # Mininodes send ping message to zelcashd node. pingCounter = 1 for node in nodes: node.send_message(msg_ping(pingCounter)) pingCounter = pingCounter + 1 time.sleep(3) # Verify Sprout and Overwinter mininodes have been dropped, while # Sapling mininodes are still connected. peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(0, versions.count(SPROUT_PROTO_VERSION)) assert_equal(0, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(11, versions.count(SAPLING_PROTO_VERSION)) # Extend the Sapling chain with another block. self.nodes[0].generate(1) # Connect a new Sapling mininode to the zelcashd node, which is accepted. nodes.append( NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SAPLING_PROTO_VERSION)) time.sleep(3) assert_equal(12, len(self.nodes[0].getpeerinfo())) # Try to connect a new Sprout mininode to the zelcashd node, which is rejected. sprout = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SPROUT_PROTO_VERSION) nodes.append(sprout) time.sleep(3) assert ("Version must be 170006 or greater" in str(sprout.rejectMessage)) # Try to connect a new Overwinter mininode to the zelcashd node, which is rejected. sprout = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", OVERWINTER_PROTO_VERSION) nodes.append(sprout) time.sleep(3) assert ("Version must be 170006 or greater" in str(sprout.rejectMessage)) # Verify that only Sapling mininodes are connected. peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(0, versions.count(SPROUT_PROTO_VERSION)) assert_equal(0, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(12, versions.count(SAPLING_PROTO_VERSION)) for node in nodes: node.disconnect_node()
def run_test(self): test = TestManager() # Launch Sprout, Overwinter, and Sapling mininodes nodes = [] for x in xrange(10): nodes.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SPROUT_PROTO_VERSION)) nodes.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", OVERWINTER_PROTO_VERSION)) nodes.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SAPLING_PROTO_VERSION)) # Start up network handling in another thread NetworkThread().start() # Sprout consensus rules apply at block height 9 self.nodes[0].generate(9) assert_equal(9, self.nodes[0].getblockcount()) # Verify mininodes are still connected to zcashd node peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(10, versions.count(SPROUT_PROTO_VERSION)) assert_equal(10, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(10, versions.count(SAPLING_PROTO_VERSION)) # Overwinter consensus rules activate at block height 10 self.nodes[0].generate(1) assert_equal(10, self.nodes[0].getblockcount()) print('Overwinter active') # Mininodes send ping message to zcashd node. pingCounter = 1 for node in nodes: node.send_message(msg_ping(pingCounter)) pingCounter = pingCounter + 1 time.sleep(3) # Verify Sprout mininodes have been dropped, while Overwinter and # Sapling mininodes are still connected. peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(0, versions.count(SPROUT_PROTO_VERSION)) assert_equal(10, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(10, versions.count(SAPLING_PROTO_VERSION)) # Extend the Overwinter chain with another block. self.nodes[0].generate(1) # Connect a new Overwinter mininode to the zcashd node, which is accepted. nodes.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", OVERWINTER_PROTO_VERSION)) time.sleep(3) assert_equal(21, len(self.nodes[0].getpeerinfo())) # Connect a new Sapling mininode to the zcashd node, which is accepted. nodes.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SAPLING_PROTO_VERSION)) time.sleep(3) assert_equal(22, len(self.nodes[0].getpeerinfo())) # Try to connect a new Sprout mininode to the zcashd node, which is rejected. sprout = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SPROUT_PROTO_VERSION) nodes.append(sprout) time.sleep(3) assert("Version must be 170003 or greater" in str(sprout.rejectMessage)) # Verify that only Overwinter and Sapling mininodes are connected. peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(0, versions.count(SPROUT_PROTO_VERSION)) assert_equal(11, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(11, versions.count(SAPLING_PROTO_VERSION)) # Sapling consensus rules activate at block height 15 self.nodes[0].generate(4) assert_equal(15, self.nodes[0].getblockcount()) print('Sapling active') # Mininodes send ping message to zcashd node. pingCounter = 1 for node in nodes: node.send_message(msg_ping(pingCounter)) pingCounter = pingCounter + 1 time.sleep(3) # Verify Sprout and Overwinter mininodes have been dropped, while # Sapling mininodes are still connected. peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(0, versions.count(SPROUT_PROTO_VERSION)) assert_equal(0, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(11, versions.count(SAPLING_PROTO_VERSION)) # Extend the Sapling chain with another block. self.nodes[0].generate(1) # Connect a new Sapling mininode to the zcashd node, which is accepted. nodes.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SAPLING_PROTO_VERSION)) time.sleep(3) assert_equal(12, len(self.nodes[0].getpeerinfo())) # Try to connect a new Sprout mininode to the zcashd node, which is rejected. sprout = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", SPROUT_PROTO_VERSION) nodes.append(sprout) time.sleep(3) assert("Version must be 170006 or greater" in str(sprout.rejectMessage)) # Try to connect a new Overwinter mininode to the zcashd node, which is rejected. sprout = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test, "regtest", OVERWINTER_PROTO_VERSION) nodes.append(sprout) time.sleep(3) assert("Version must be 170006 or greater" in str(sprout.rejectMessage)) # Verify that only Sapling mininodes are connected. peerinfo = self.nodes[0].getpeerinfo() versions = [x["version"] for x in peerinfo] assert_equal(0, versions.count(SPROUT_PROTO_VERSION)) assert_equal(0, versions.count(OVERWINTER_PROTO_VERSION)) assert_equal(12, versions.count(SAPLING_PROTO_VERSION)) for node in nodes: node.disconnect_node()