Merge #10109: Remove SingleNodeConnCB

159fe88 Remove SingleNodeConnCB (John Newbery)

Tree-SHA512: 2fc3d060f4ac9445e657134919a871c46987d53eb98d23a858ee9515fc997be7a81923f08f2a37d07d123b55b912ae82ffa0f820d16297b044ab24dcf0788a8a
This commit is contained in:
MarcoFalke 2017-03-30 09:22:59 +02:00 committed by Pasta
parent 94aa904c06
commit cc06ef5514
No known key found for this signature in database
GPG Key ID: 0B8EB7A31A44D9C6
12 changed files with 77 additions and 80 deletions

View File

@ -40,17 +40,14 @@ from test_framework.mininode import (CBlockHeader,
CTxOut, CTxOut,
NetworkThread, NetworkThread,
NodeConn, NodeConn,
SingleNodeConnCB, NodeConnCB,
msg_block, msg_block,
msg_headers) msg_headers)
from test_framework.script import (CScript, OP_TRUE) from test_framework.script import (CScript, OP_TRUE)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (start_node, p2p_port, assert_equal) from test_framework.util import (start_node, p2p_port, assert_equal)
class BaseNode(SingleNodeConnCB): class BaseNode(NodeConnCB):
def __init__(self):
super().__init__()
def send_header_for_blocks(self, new_blocks): def send_header_for_blocks(self, new_blocks):
headers_message = msg_headers() headers_message = msg_headers()
headers_message.headers = [CBlockHeader(b) for b in new_blocks] headers_message.headers = [CBlockHeader(b) for b in new_blocks]

View File

@ -17,7 +17,6 @@ from test_framework.util import *
MAX_REQUESTS = 128 MAX_REQUESTS = 128
class TestManager(NodeConnCB): class TestManager(NodeConnCB):
# set up NodeConnCB callbacks, overriding base class
def on_getdata(self, conn, message): def on_getdata(self, conn, message):
self.log.debug("got getdata %s" % repr(message)) self.log.debug("got getdata %s" % repr(message))
# Log the requests # Log the requests
@ -30,11 +29,8 @@ class TestManager(NodeConnCB):
if not self.disconnectOkay: if not self.disconnectOkay:
raise EarlyDisconnectError(0) raise EarlyDisconnectError(0)
def __init__(self):
NodeConnCB.__init__(self)
def add_new_connection(self, connection): def add_new_connection(self, connection):
self.connection = connection super().add_connection(connection)
self.blockReqCounts = {} self.blockReqCounts = {}
self.disconnectOkay = False self.disconnectOkay = False

View File

@ -20,7 +20,7 @@ import time
# p2p messages to a node, generating the messages in the main testing logic. # p2p messages to a node, generating the messages in the main testing logic.
class TestNode(NodeConnCB): class TestNode(NodeConnCB):
def __init__(self): def __init__(self):
NodeConnCB.__init__(self) super().__init__()
self.connection = None self.connection = None
self.ping_counter = 1 self.ping_counter = 1
self.last_pong = msg_pong() self.last_pong = msg_pong()

View File

@ -58,7 +58,7 @@ from test_framework.blocktools import create_block, create_coinbase
# p2p messages to a node, generating the messages in the main testing logic. # p2p messages to a node, generating the messages in the main testing logic.
class TestNode(NodeConnCB): class TestNode(NodeConnCB):
def __init__(self): def __init__(self):
NodeConnCB.__init__(self) super().__init__()
self.connection = None self.connection = None
self.ping_counter = 1 self.ping_counter = 1
self.last_pong = msg_pong() self.last_pong = msg_pong()

View File

@ -12,9 +12,9 @@ from test_framework.blocktools import create_block, create_coinbase
from test_framework.script import CScript, OP_TRUE from test_framework.script import CScript, OP_TRUE
# TestNode: A peer we use to send messages to bitcoind, and store responses. # TestNode: A peer we use to send messages to bitcoind, and store responses.
class TestNode(SingleNodeConnCB): class TestNode(NodeConnCB):
def __init__(self): def __init__(self):
SingleNodeConnCB.__init__(self) super().__init__()
self.last_sendcmpct = [] self.last_sendcmpct = []
self.last_headers = None self.last_headers = None
self.last_inv = None self.last_inv = None

View File

@ -19,10 +19,10 @@ banscore = 10
class CLazyNode(NodeConnCB): class CLazyNode(NodeConnCB):
def __init__(self): def __init__(self):
super().__init__()
self.connection = None self.connection = None
self.unexpected_msg = False self.unexpected_msg = False
self.connected = False self.connected = False
super().__init__()
def add_connection(self, conn): def add_connection(self, conn):
self.connection = conn self.connection = conn

View File

@ -14,7 +14,7 @@ from test_framework.util import *
class TestNode(NodeConnCB): class TestNode(NodeConnCB):
def __init__(self): def __init__(self):
NodeConnCB.__init__(self) super().__init__()
self.connection = None self.connection = None
self.ping_counter = 1 self.ping_counter = 1
self.last_pong = msg_pong() self.last_pong = msg_pong()

View File

@ -27,9 +27,9 @@ from test_framework.mininode import *
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import *
class TestNode(SingleNodeConnCB): class TestNode(NodeConnCB):
def __init__(self): def __init__(self):
SingleNodeConnCB.__init__(self) super().__init__()
self.connected = False self.connected = False
self.received_version = False self.received_version = False

View File

@ -28,7 +28,7 @@ VB_PATTERN = re.compile("^Warning.*versionbit")
# p2p messages to a node, generating the messages in the main testing logic. # p2p messages to a node, generating the messages in the main testing logic.
class TestNode(NodeConnCB): class TestNode(NodeConnCB):
def __init__(self): def __init__(self):
NodeConnCB.__init__(self) super().__init__()
self.connection = None self.connection = None
self.ping_counter = 1 self.ping_counter = 1
self.last_pong = msg_pong() self.last_pong = msg_pong()

View File

@ -92,9 +92,9 @@ from test_framework.blocktools import create_block, create_coinbase
direct_fetch_response_time = 0.05 direct_fetch_response_time = 0.05
class BaseNode(SingleNodeConnCB): class BaseNode(NodeConnCB):
def __init__(self): def __init__(self):
SingleNodeConnCB.__init__(self) super().__init__()
self.last_inv = None self.last_inv = None
self.last_headers = None self.last_headers = None
self.last_block = None self.last_block = None

View File

@ -42,7 +42,7 @@ class RejectResult(object):
class TestNode(NodeConnCB): class TestNode(NodeConnCB):
def __init__(self, block_store, tx_store): def __init__(self, block_store, tx_store):
NodeConnCB.__init__(self) super().__init__()
self.conn = None self.conn = None
self.bestblockhash = None self.bestblockhash = None
self.block_store = block_store self.block_store = block_store

View File

@ -1606,25 +1606,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()
@ -1636,17 +1620,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()
@ -1656,39 +1659,31 @@ 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_pong(self, conn, message):
def on_close(self, conn): pass self.last_pong = message
def on_mempool(self, conn): pass
def on_pong(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
def on_mnlistdiff(self, conn, message): pass def on_mnlistdiff(self, conn, message): pass
def on_clsig(self, conn, message): pass def on_clsig(self, conn, message): pass
def on_islock(self, conn, message): pass def on_islock(self, conn, message): pass
# More useful callbacks and functions for NodeConnCB's which have a single NodeConn def on_verack(self, conn, message):
class SingleNodeConnCB(NodeConnCB): conn.ver_recv = conn.ver_send
def __init__(self): self.verack_received = True
NodeConnCB.__init__(self)
self.connection = None def on_version(self, conn, message):
self.ping_counter = 1 if message.nVersion >= 209:
self.last_pong = msg_pong() 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
@ -1701,9 +1696,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():
@ -1711,8 +1703,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):