mirror of
https://github.com/kvazar-network/kevacoin.git
synced 2025-01-11 07:37:54 +00: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:
parent
79af9fbd8c
commit
159fe88abf
@ -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]
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
@ -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()
|
||||||
|
@ -15,9 +15,9 @@ from test_framework.blocktools import create_block, create_coinbase, add_witness
|
|||||||
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
|
||||||
|
@ -24,9 +24,9 @@ def allInvsMatch(invsExpected, testnode):
|
|||||||
|
|
||||||
# TestNode: bare-bones "peer". Used to track which invs are received from a node
|
# TestNode: bare-bones "peer". Used to track which invs are received from a node
|
||||||
# and to send the node feefilter messages.
|
# and to send the node feefilter messages.
|
||||||
class TestNode(SingleNodeConnCB):
|
class TestNode(NodeConnCB):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
SingleNodeConnCB.__init__(self)
|
super().__init__()
|
||||||
self.txinvs = []
|
self.txinvs = []
|
||||||
|
|
||||||
def on_inv(self, conn, message):
|
def on_inv(self, conn, message):
|
||||||
|
@ -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
|
||||||
|
@ -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()
|
||||||
|
@ -32,10 +32,9 @@ def get_virtual_size(witness_block):
|
|||||||
vsize = int((3*base_size + total_size + 3)/4)
|
vsize = int((3*base_size + total_size + 3)/4)
|
||||||
return vsize
|
return vsize
|
||||||
|
|
||||||
# Note: we can reduce code by using SingleNodeConnCB (in master, not 0.12)
|
|
||||||
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(0)
|
self.last_pong = msg_pong(0)
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
@ -81,9 +81,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
|
||||||
|
@ -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
|
||||||
|
@ -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):
|
||||||
|
Loading…
Reference in New Issue
Block a user