mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 06:28:31 +01:00
Remove SingleNodeConnCB
This commit merges the NodeConnCB and SingleNodeConnCB into a single class (called NodeConnCB). The original intent for the NodeConnCB was to be able to have a python 'mininode' connect to multiple running bitcoinds. This has never been used and can be achieved more easily by having multiple NodeConns backed by a common datastore if it is ever needed. The changes in mininode.py are just code moves (and merging the two classes into a single class). The code changes in the individual test cases are changing the subclasses to subclass from NodeConnCB instead of SingleNodeConnCB. There is a lot of duplicate code in the subclasses that can be removed in future commits.
This commit is contained in:
@@ -1476,25 +1476,9 @@ class NodeConnCB(object):
|
||||
self.deliver_sleep_time = None
|
||||
# Remember the services our peer has advertised
|
||||
self.peer_services = None
|
||||
|
||||
def set_deliver_sleep_time(self, value):
|
||||
with mininode_lock:
|
||||
self.deliver_sleep_time = value
|
||||
|
||||
def get_deliver_sleep_time(self):
|
||||
with mininode_lock:
|
||||
return self.deliver_sleep_time
|
||||
|
||||
# Spin until verack message is received from the node.
|
||||
# Tests may want to use this as a signal that the test can begin.
|
||||
# This can be called from the testing thread, so it needs to acquire the
|
||||
# global lock.
|
||||
def wait_for_verack(self):
|
||||
while True:
|
||||
with mininode_lock:
|
||||
if self.verack_received:
|
||||
return
|
||||
time.sleep(0.05)
|
||||
self.connection = None
|
||||
self.ping_counter = 1
|
||||
self.last_pong = msg_pong()
|
||||
|
||||
def deliver(self, conn, message):
|
||||
deliver_sleep = self.get_deliver_sleep_time()
|
||||
@@ -1506,17 +1490,36 @@ class NodeConnCB(object):
|
||||
except:
|
||||
logger.exception("ERROR delivering %s" % repr(message))
|
||||
|
||||
def on_version(self, conn, message):
|
||||
if message.nVersion >= 209:
|
||||
conn.send_message(msg_verack())
|
||||
conn.ver_send = min(MY_VERSION, message.nVersion)
|
||||
if message.nVersion < 209:
|
||||
conn.ver_recv = conn.ver_send
|
||||
conn.nServices = message.nServices
|
||||
def set_deliver_sleep_time(self, value):
|
||||
with mininode_lock:
|
||||
self.deliver_sleep_time = value
|
||||
|
||||
def on_verack(self, conn, message):
|
||||
conn.ver_recv = conn.ver_send
|
||||
self.verack_received = True
|
||||
def get_deliver_sleep_time(self):
|
||||
with mininode_lock:
|
||||
return self.deliver_sleep_time
|
||||
|
||||
# Callbacks which can be overridden by subclasses
|
||||
#################################################
|
||||
|
||||
def on_addr(self, conn, message): pass
|
||||
def on_alert(self, conn, message): pass
|
||||
def on_block(self, conn, message): pass
|
||||
def on_blocktxn(self, conn, message): pass
|
||||
def on_close(self, conn): pass
|
||||
def on_cmpctblock(self, conn, message): pass
|
||||
def on_feefilter(self, conn, message): pass
|
||||
def on_getaddr(self, conn, message): pass
|
||||
def on_getblocks(self, conn, message): pass
|
||||
def on_getblocktxn(self, conn, message): pass
|
||||
def on_getdata(self, conn, message): pass
|
||||
def on_getheaders(self, conn, message): pass
|
||||
def on_headers(self, conn, message): pass
|
||||
def on_mempool(self, conn): pass
|
||||
def on_open(self, conn): pass
|
||||
def on_reject(self, conn, message): pass
|
||||
def on_sendcmpct(self, conn, message): pass
|
||||
def on_sendheaders(self, conn, message): pass
|
||||
def on_tx(self, conn, message): pass
|
||||
|
||||
def on_inv(self, conn, message):
|
||||
want = msg_getdata()
|
||||
@@ -1526,37 +1529,27 @@ class NodeConnCB(object):
|
||||
if len(want.inv):
|
||||
conn.send_message(want)
|
||||
|
||||
def on_addr(self, conn, message): pass
|
||||
def on_alert(self, conn, message): pass
|
||||
def on_getdata(self, conn, message): pass
|
||||
def on_getblocks(self, conn, message): pass
|
||||
def on_tx(self, conn, message): pass
|
||||
def on_block(self, conn, message): pass
|
||||
def on_getaddr(self, conn, message): pass
|
||||
def on_headers(self, conn, message): pass
|
||||
def on_getheaders(self, conn, message): pass
|
||||
def on_ping(self, conn, message):
|
||||
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
|
||||
def on_feefilter(self, conn, message): pass
|
||||
def on_sendheaders(self, conn, message): pass
|
||||
def on_sendcmpct(self, conn, message): pass
|
||||
def on_cmpctblock(self, conn, message): pass
|
||||
def on_getblocktxn(self, conn, message): pass
|
||||
def on_blocktxn(self, conn, message): pass
|
||||
|
||||
# More useful callbacks and functions for NodeConnCB's which have a single NodeConn
|
||||
class SingleNodeConnCB(NodeConnCB):
|
||||
def __init__(self):
|
||||
NodeConnCB.__init__(self)
|
||||
self.connection = None
|
||||
self.ping_counter = 1
|
||||
self.last_pong = msg_pong()
|
||||
def on_pong(self, conn, message):
|
||||
self.last_pong = message
|
||||
|
||||
def on_verack(self, conn, message):
|
||||
conn.ver_recv = conn.ver_send
|
||||
self.verack_received = True
|
||||
|
||||
def on_version(self, conn, message):
|
||||
if message.nVersion >= 209:
|
||||
conn.send_message(msg_verack())
|
||||
conn.ver_send = min(MY_VERSION, message.nVersion)
|
||||
if message.nVersion < 209:
|
||||
conn.ver_recv = conn.ver_send
|
||||
conn.nServices = message.nServices
|
||||
|
||||
# Helper functions
|
||||
##################
|
||||
|
||||
def add_connection(self, conn):
|
||||
self.connection = conn
|
||||
@@ -1569,9 +1562,6 @@ class SingleNodeConnCB(NodeConnCB):
|
||||
self.send_message(message)
|
||||
self.sync_with_ping()
|
||||
|
||||
def on_pong(self, conn, message):
|
||||
self.last_pong = message
|
||||
|
||||
# Sync up with the node
|
||||
def sync_with_ping(self, timeout=30):
|
||||
def received_pong():
|
||||
@@ -1579,8 +1569,20 @@ class SingleNodeConnCB(NodeConnCB):
|
||||
self.send_message(msg_ping(nonce=self.ping_counter))
|
||||
success = wait_until(received_pong, timeout=timeout)
|
||||
self.ping_counter += 1
|
||||
|
||||
return success
|
||||
|
||||
# Spin until verack message is received from the node.
|
||||
# Tests may want to use this as a signal that the test can begin.
|
||||
# This can be called from the testing thread, so it needs to acquire the
|
||||
# global lock.
|
||||
def wait_for_verack(self):
|
||||
while True:
|
||||
with mininode_lock:
|
||||
if self.verack_received:
|
||||
return
|
||||
time.sleep(0.05)
|
||||
|
||||
# The actual NodeConn class
|
||||
# This class provides an interface for a p2p connection to a specified node
|
||||
class NodeConn(asyncore.dispatcher):
|
||||
|
||||
Reference in New Issue
Block a user