|
|
@ -1476,25 +1476,9 @@ class NodeConnCB(object): |
|
|
|
self.deliver_sleep_time = None |
|
|
|
self.deliver_sleep_time = None |
|
|
|
# Remember the services our peer has advertised |
|
|
|
# Remember the services our peer has advertised |
|
|
|
self.peer_services = None |
|
|
|
self.peer_services = None |
|
|
|
|
|
|
|
self.connection = None |
|
|
|
def set_deliver_sleep_time(self, value): |
|
|
|
self.ping_counter = 1 |
|
|
|
with mininode_lock: |
|
|
|
self.last_pong = msg_pong() |
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def deliver(self, conn, message): |
|
|
|
def deliver(self, conn, message): |
|
|
|
deliver_sleep = self.get_deliver_sleep_time() |
|
|
|
deliver_sleep = self.get_deliver_sleep_time() |
|
|
@ -1506,17 +1490,36 @@ class NodeConnCB(object): |
|
|
|
except: |
|
|
|
except: |
|
|
|
logger.exception("ERROR delivering %s" % repr(message)) |
|
|
|
logger.exception("ERROR delivering %s" % repr(message)) |
|
|
|
|
|
|
|
|
|
|
|
def on_version(self, conn, message): |
|
|
|
def set_deliver_sleep_time(self, value): |
|
|
|
if message.nVersion >= 209: |
|
|
|
with mininode_lock: |
|
|
|
conn.send_message(msg_verack()) |
|
|
|
self.deliver_sleep_time = value |
|
|
|
conn.ver_send = min(MY_VERSION, message.nVersion) |
|
|
|
|
|
|
|
if message.nVersion < 209: |
|
|
|
|
|
|
|
conn.ver_recv = conn.ver_send |
|
|
|
|
|
|
|
conn.nServices = message.nServices |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_verack(self, conn, message): |
|
|
|
def get_deliver_sleep_time(self): |
|
|
|
conn.ver_recv = conn.ver_send |
|
|
|
with mininode_lock: |
|
|
|
self.verack_received = True |
|
|
|
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): |
|
|
|
def on_inv(self, conn, message): |
|
|
|
want = msg_getdata() |
|
|
|
want = msg_getdata() |
|
|
@ -1526,37 +1529,27 @@ class NodeConnCB(object): |
|
|
|
if len(want.inv): |
|
|
|
if len(want.inv): |
|
|
|
conn.send_message(want) |
|
|
|
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): |
|
|
|
def on_ping(self, conn, message): |
|
|
|
if conn.ver_send > BIP0031_VERSION: |
|
|
|
if conn.ver_send > BIP0031_VERSION: |
|
|
|
conn.send_message(msg_pong(message.nonce)) |
|
|
|
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 |
|
|
|
def on_pong(self, conn, message): |
|
|
|
class SingleNodeConnCB(NodeConnCB): |
|
|
|
self.last_pong = message |
|
|
|
def __init__(self): |
|
|
|
|
|
|
|
NodeConnCB.__init__(self) |
|
|
|
def on_verack(self, conn, message): |
|
|
|
self.connection = None |
|
|
|
conn.ver_recv = conn.ver_send |
|
|
|
self.ping_counter = 1 |
|
|
|
self.verack_received = True |
|
|
|
self.last_pong = msg_pong() |
|
|
|
|
|
|
|
|
|
|
|
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): |
|
|
|
def add_connection(self, conn): |
|
|
|
self.connection = conn |
|
|
|
self.connection = conn |
|
|
@ -1569,9 +1562,6 @@ class SingleNodeConnCB(NodeConnCB): |
|
|
|
self.send_message(message) |
|
|
|
self.send_message(message) |
|
|
|
self.sync_with_ping() |
|
|
|
self.sync_with_ping() |
|
|
|
|
|
|
|
|
|
|
|
def on_pong(self, conn, message): |
|
|
|
|
|
|
|
self.last_pong = message |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Sync up with the node |
|
|
|
# Sync up with the node |
|
|
|
def sync_with_ping(self, timeout=30): |
|
|
|
def sync_with_ping(self, timeout=30): |
|
|
|
def received_pong(): |
|
|
|
def received_pong(): |
|
|
@ -1579,8 +1569,20 @@ class SingleNodeConnCB(NodeConnCB): |
|
|
|
self.send_message(msg_ping(nonce=self.ping_counter)) |
|
|
|
self.send_message(msg_ping(nonce=self.ping_counter)) |
|
|
|
success = wait_until(received_pong, timeout=timeout) |
|
|
|
success = wait_until(received_pong, timeout=timeout) |
|
|
|
self.ping_counter += 1 |
|
|
|
self.ping_counter += 1 |
|
|
|
|
|
|
|
|
|
|
|
return success |
|
|
|
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 |
|
|
|
# The actual NodeConn class |
|
|
|
# This class provides an interface for a p2p connection to a specified node |
|
|
|
# This class provides an interface for a p2p connection to a specified node |
|
|
|
class NodeConn(asyncore.dispatcher): |
|
|
|
class NodeConn(asyncore.dispatcher): |
|
|
|