aboutsummaryrefslogtreecommitdiff
path: root/qa/rpc-tests/test_framework/mininode.py
diff options
context:
space:
mode:
Diffstat (limited to 'qa/rpc-tests/test_framework/mininode.py')
-rwxr-xr-xqa/rpc-tests/test_framework/mininode.py96
1 files changed, 50 insertions, 46 deletions
diff --git a/qa/rpc-tests/test_framework/mininode.py b/qa/rpc-tests/test_framework/mininode.py
index 91daa4ab1f..aace17a043 100755
--- a/qa/rpc-tests/test_framework/mininode.py
+++ b/qa/rpc-tests/test_framework/mininode.py
@@ -4,23 +4,21 @@
# Copyright (c) 2010-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
-
-#
-# mininode.py - Bitcoin P2P network half-a-node
-#
-# This python code was modified from ArtForz' public domain half-a-node, as
-# found in the mini-node branch of http://github.com/jgarzik/pynode.
-#
-# NodeConn: an object which manages p2p connectivity to a bitcoin node
-# NodeConnCB: a base class that describes the interface for receiving
-# callbacks with network messages from a NodeConn
-# CBlock, CTransaction, CBlockHeader, CTxIn, CTxOut, etc....:
-# data structures that should map to corresponding structures in
-# bitcoin/primitives
-# msg_block, msg_tx, msg_headers, etc.:
-# data structures that represent network messages
-# ser_*, deser_*: functions that handle serialization/deserialization
-
+"""Bitcoin P2P network half-a-node.
+
+This python code was modified from ArtForz' public domain half-a-node, as
+found in the mini-node branch of http://github.com/jgarzik/pynode.
+
+NodeConn: an object which manages p2p connectivity to a bitcoin node
+NodeConnCB: a base class that describes the interface for receiving
+ callbacks with network messages from a NodeConn
+CBlock, CTransaction, CBlockHeader, CTxIn, CTxOut, etc....:
+ data structures that should map to corresponding structures in
+ bitcoin/primitives
+msg_block, msg_tx, msg_headers, etc.:
+ data structures that represent network messages
+ser_*, deser_*: functions that handle serialization/deserialization
+"""
import struct
import socket
@@ -53,6 +51,8 @@ NODE_GETUTXO = (1 << 1)
NODE_BLOOM = (1 << 2)
NODE_WITNESS = (1 << 3)
+logger = logging.getLogger("TestFramework.mininode")
+
# Keep our own socket map for asyncore, so that we can track disconnects
# ourselves (to workaround an issue with closing an asyncore socket when
# using select)
@@ -1504,8 +1504,7 @@ class NodeConnCB(object):
try:
getattr(self, 'on_' + message.command.decode('ascii'))(conn, message)
except:
- print("ERROR delivering %s (%s)" % (repr(message),
- sys.exc_info()[0]))
+ logger.exception("ERROR delivering %s" % repr(message))
def on_version(self, conn, message):
if message.nVersion >= 209:
@@ -1540,6 +1539,7 @@ class NodeConnCB(object):
if conn.ver_send > BIP0031_VERSION:
conn.send_message(msg_pong(message.nonce))
def on_reject(self, conn, message): pass
+ def on_open(self, conn): pass
def on_close(self, conn): pass
def on_mempool(self, conn): pass
def on_pong(self, conn, message): pass
@@ -1614,9 +1614,8 @@ class NodeConn(asyncore.dispatcher):
"regtest": b"\xfa\xbf\xb5\xda", # regtest
}
- def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=NODE_NETWORK):
+ def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=NODE_NETWORK, send_version=True):
asyncore.dispatcher.__init__(self, map=mininode_socket_map)
- self.log = logging.getLogger("NodeConn(%s:%d)" % (dstaddr, dstport))
self.dstaddr = dstaddr
self.dstport = dstport
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -1631,16 +1630,17 @@ class NodeConn(asyncore.dispatcher):
self.disconnect = False
self.nServices = 0
- # stuff version msg into sendbuf
- vt = msg_version()
- vt.nServices = services
- vt.addrTo.ip = self.dstaddr
- vt.addrTo.port = self.dstport
- vt.addrFrom.ip = "0.0.0.0"
- vt.addrFrom.port = 0
- self.send_message(vt, True)
- print('MiniNode: Connecting to Bitcoin Node IP # ' + dstaddr + ':' \
- + str(dstport))
+ if send_version:
+ # stuff version msg into sendbuf
+ vt = msg_version()
+ vt.nServices = services
+ vt.addrTo.ip = self.dstaddr
+ vt.addrTo.port = self.dstport
+ vt.addrFrom.ip = "0.0.0.0"
+ vt.addrFrom.port = 0
+ self.send_message(vt, True)
+
+ logger.info('Connecting to Bitcoin Node: %s:%d' % (self.dstaddr, self.dstport))
try:
self.connect((dstaddr, dstport))
@@ -1648,16 +1648,14 @@ class NodeConn(asyncore.dispatcher):
self.handle_close()
self.rpc = rpc
- def show_debug_msg(self, msg):
- self.log.debug(msg)
-
def handle_connect(self):
- self.show_debug_msg("MiniNode: Connected & Listening: \n")
- self.state = "connected"
+ if self.state != "connected":
+ logger.debug("Connected & Listening: %s:%d" % (self.dstaddr, self.dstport))
+ self.state = "connected"
+ self.cb.on_open(self)
def handle_close(self):
- self.show_debug_msg("MiniNode: Closing Connection to %s:%d... "
- % (self.dstaddr, self.dstport))
+ logger.debug("Closing connection to: %s:%d" % (self.dstaddr, self.dstport))
self.state = "closed"
self.recvbuf = b""
self.sendbuf = b""
@@ -1681,11 +1679,20 @@ class NodeConn(asyncore.dispatcher):
def writable(self):
with mininode_lock:
+ pre_connection = self.state == "connecting"
length = len(self.sendbuf)
- return (length > 0)
+ return (length > 0 or pre_connection)
def handle_write(self):
with mininode_lock:
+ # asyncore does not expose socket connection, only the first read/write
+ # event, thus we must check connection manually here to know when we
+ # actually connect
+ if self.state == "connecting":
+ self.handle_connect()
+ if not self.writable():
+ return
+
try:
sent = self.send(self.sendbuf)
except:
@@ -1730,17 +1737,14 @@ class NodeConn(asyncore.dispatcher):
t.deserialize(f)
self.got_message(t)
else:
- self.show_debug_msg("Unknown command: '" + command + "' " +
- repr(msg))
+ logger.warning("Received unknown command from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, command, repr(msg)))
except Exception as e:
- print('got_data:', repr(e))
- # import traceback
- # traceback.print_tb(sys.exc_info()[2])
+ logger.exception('got_data:', repr(e))
def send_message(self, message, pushbuf=False):
if self.state != "connected" and not pushbuf:
raise IOError('Not connected, no pushbuf')
- self.show_debug_msg("Send %s" % repr(message))
+ logger.debug("Send message to %s:%d: %s" % (self.dstaddr, self.dstport, repr(message)))
command = message.command
data = message.serialize()
tmsg = self.MAGIC_BYTES[self.network]
@@ -1762,7 +1766,7 @@ class NodeConn(asyncore.dispatcher):
self.messagemap[b'ping'] = msg_ping_prebip31
if self.last_sent + 30 * 60 < time.time():
self.send_message(self.messagemap[b'ping']())
- self.show_debug_msg("Recv %s" % repr(message))
+ logger.debug("Received message from %s:%d: %s" % (self.dstaddr, self.dstport, repr(message)))
self.cb.deliver(self, message)
def disconnect_node(self):