Esempio n. 1
0
    def run_test(self):
        # Create all the connections we will need to node0 at the start because they all need to be
        # setup before we call NetworkThread().start()

        # Create a P2P connection just so that the test framework is happy we're connected
        dummyCB = NodeConnCB()
        dummyConn = NodeConn('127.0.0.1',
                             p2p_port(0),
                             self.nodes[0],
                             dummyCB,
                             nullAssocID=True)
        dummyCB.add_connection(dummyConn)

        # By setting the assocID on this second NodeConn we prevent it sending a version message
        badConnCB = TestNode()
        badConn = NodeConn('127.0.0.1',
                           p2p_port(0),
                           self.nodes[0],
                           badConnCB,
                           assocID=0x01)
        badConnCB.add_connection(badConn)

        # Start up network handling in another thread. This needs to be called
        # after the P2P connections have been created.
        NetworkThread().start()

        # Check initial state
        dummyCB.wait_for_protoconf()
        with mininode_lock:
            assert_equal(len(badConnCB.message_count), 0)

        # Send a badly formatted version message
        badConn.send_message(msg_version_bad())
        # Connection will be closed with a reject
        wait_until(lambda: badConnCB.last_reject is not None,
                   lock=mininode_lock,
                   timeout=5)
        wait_until(lambda: badConn.state == "closed",
                   lock=mininode_lock,
                   timeout=5)

        # Check clear log message was generated
        assert check_for_log_msg(
            self, "Failed to process version: (Badly formatted association ID",
            "/node0")
Esempio n. 2
0
    def start_node_with_protoconf(self, maxprotocolrecvpayloadlength, recvinvqueuefactor=0):
        """
        This method starts the node and creates a connection to the node.
        It takes care of exchanging protoconf messages between node and python connection.
        Max size of bitcoin nodes' inv message is determined by maxprotocolrecvpayloadlength.
        Max size of python connections' inv message is LEGACY_MAX_PROTOCOL_PAYLOAD_LENGTH.
        If called with parameter recvinvqueuefactor it also sets this setting on the node.

        :returns test_node
        """

        # Start the node with maxprotocolrecvpayloadlength, recvinvqueuefactor (if set)
        start_params = []
        if maxprotocolrecvpayloadlength:
            start_params.append("-maxprotocolrecvpayloadlength={} ".format(maxprotocolrecvpayloadlength))
        if recvinvqueuefactor:
            start_params.append("-recvinvqueuefactor={} ".format(recvinvqueuefactor))
        self.start_node(0, start_params)

        # Create a connection and connect to the node
        test_node = NodeConnCB()
        test_node.wanted_inv_lengths = []
        def on_getdata(conn, message):
            test_node.wanted_inv_lengths.append(len(message.inv))
        test_node.on_getdata = on_getdata
        # Send protoconf message from python node to bitcoind node
        def send_protoconf_default_msg_length(conn):
            conn.send_message(msg_protoconf(CProtoconf(1, LEGACY_MAX_PROTOCOL_PAYLOAD_LENGTH)))
        test_node.send_protoconf = send_protoconf_default_msg_length
        connections = [NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node)]
        test_node.add_connection(connections[0])
        NetworkThread().start()  # Start up network handling in another thread

        # Prepare initial block. Needed so that GETDATA can be send back.
        self.nodes[0].generate(1)

        # Receive bitcoind's protoconf and save max_recv_payload_length.
        test_node.wait_for_protoconf()
        test_node.max_recv_payload_length = test_node.last_message["protoconf"].protoconf.max_recv_payload_length

        return test_node