Skip to content

Commit 2a52ae6

Browse files
committed
Remove duplicate method definitions in NodeConnCB subclasses
All Node classes in individual test cases subclass from NodeConnCB. Many have duplicate definitions for methods that are defined in the base class. This commit removes those duplicate definitions. This commit removes ~290 lines of duplicate code.
1 parent 52e15aa commit 2a52ae6

12 files changed

+144
-451
lines changed

test/functional/maxuploadtarget.py

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,24 @@
1010
if uploadtarget has been reached.
1111
* Verify that the upload counters are reset after 24 hours.
1212
"""
13+
from collections import defaultdict
14+
import time
1315

1416
from test_framework.mininode import *
1517
from test_framework.test_framework import BitcoinTestFramework
1618
from test_framework.util import *
17-
import time
1819

19-
# TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending
20-
# p2p messages to a node, generating the messages in the main testing logic.
2120
class TestNode(NodeConnCB):
2221
def __init__(self):
2322
super().__init__()
24-
self.connection = None
25-
self.ping_counter = 1
26-
self.last_pong = msg_pong()
27-
self.block_receive_map = {}
28-
29-
def add_connection(self, conn):
30-
self.connection = conn
31-
self.peer_disconnected = False
23+
self.block_receive_map = defaultdict(int)
3224

3325
def on_inv(self, conn, message):
3426
pass
3527

36-
# Track the last getdata message we receive (used in the test)
37-
def on_getdata(self, conn, message):
38-
self.last_getdata = message
39-
4028
def on_block(self, conn, message):
4129
message.block.calc_sha256()
42-
try:
43-
self.block_receive_map[message.block.sha256] += 1
44-
except KeyError as e:
45-
self.block_receive_map[message.block.sha256] = 1
46-
47-
def wait_for_disconnect(self):
48-
def disconnected():
49-
return self.peer_disconnected
50-
return wait_until(disconnected, timeout=10)
51-
52-
# Wrapper for the NodeConn's send_message function
53-
def send_message(self, message):
54-
self.connection.send_message(message)
55-
56-
def on_pong(self, conn, message):
57-
self.last_pong = message
58-
59-
def on_close(self, conn):
60-
self.peer_disconnected = True
30+
self.block_receive_map[message.block.sha256] += 1
6131

6232
class MaxUploadTest(BitcoinTestFramework):
6333

@@ -183,33 +153,26 @@ def run_test(self):
183153
stop_node(self.nodes[0], 0)
184154
self.nodes[0] = start_node(0, self.options.tmpdir, ["-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"])
185155

186-
#recreate/reconnect 3 test nodes
187-
test_nodes = []
188-
connections = []
189-
190-
for i in range(3):
191-
test_nodes.append(TestNode())
192-
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
193-
test_nodes[i].add_connection(connections[i])
156+
#recreate/reconnect a test node
157+
test_nodes = [TestNode()]
158+
connections = [NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[0])]
159+
test_nodes[0].add_connection(connections[0])
194160

195161
NetworkThread().start() # Start up network handling in another thread
196-
[x.wait_for_verack() for x in test_nodes]
162+
test_nodes[0].wait_for_verack()
197163

198164
#retrieve 20 blocks which should be enough to break the 1MB limit
199165
getdata_request.inv = [CInv(2, big_new_block)]
200166
for i in range(20):
201-
test_nodes[1].send_message(getdata_request)
202-
test_nodes[1].sync_with_ping()
203-
assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
167+
test_nodes[0].send_message(getdata_request)
168+
test_nodes[0].sync_with_ping()
169+
assert_equal(test_nodes[0].block_receive_map[big_new_block], i+1)
204170

205171
getdata_request.inv = [CInv(2, big_old_block)]
206-
test_nodes[1].send_message(getdata_request)
207-
test_nodes[1].wait_for_disconnect()
208-
assert_equal(len(self.nodes[0].getpeerinfo()), 3) #node is still connected because of the whitelist
209-
210-
self.log.info("Peer 1 still connected after trying to download old block (whitelisted)")
172+
test_nodes[0].send_and_ping(getdata_request)
173+
assert_equal(len(self.nodes[0].getpeerinfo()), 1) #node is still connected because of the whitelist
211174

212-
[c.disconnect_node() for c in connections]
175+
self.log.info("Peer still connected after trying to download old block (whitelisted)")
213176

214177
if __name__ == '__main__':
215178
MaxUploadTest().main()

test/functional/p2p-acceptblock.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,6 @@
5454
import time
5555
from test_framework.blocktools import create_block, create_coinbase
5656

57-
# TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending
58-
# p2p messages to a node, generating the messages in the main testing logic.
59-
class TestNode(NodeConnCB):
60-
def __init__(self):
61-
super().__init__()
62-
self.connection = None
63-
self.ping_counter = 1
64-
self.last_pong = msg_pong()
65-
66-
def add_connection(self, conn):
67-
self.connection = conn
68-
69-
# Track the last getdata message we receive (used in the test)
70-
def on_getdata(self, conn, message):
71-
self.last_getdata = message
72-
73-
# Wrapper for the NodeConn's send_message function
74-
def send_message(self, message):
75-
self.connection.send_message(message)
76-
77-
def on_pong(self, conn, message):
78-
self.last_pong = message
79-
8057
class AcceptBlockTest(BitcoinTestFramework):
8158
def add_options(self, parser):
8259
parser.add_option("--testbinary", dest="testbinary",
@@ -101,8 +78,8 @@ def setup_network(self):
10178

10279
def run_test(self):
10380
# Setup the p2p connections and start up the network thread.
104-
test_node = TestNode() # connects to node0 (not whitelisted)
105-
white_node = TestNode() # connects to node1 (whitelisted)
81+
test_node = NodeConnCB() # connects to node0 (not whitelisted)
82+
white_node = NodeConnCB() # connects to node1 (whitelisted)
10683

10784
connections = []
10885
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node))
@@ -227,12 +204,12 @@ def run_test(self):
227204
# triggers a getdata on block 2 (it should if block 2 is missing).
228205
with mininode_lock:
229206
# Clear state so we can check the getdata request
230-
test_node.last_getdata = None
207+
test_node.last_message.pop("getdata", None)
231208
test_node.send_message(msg_inv([CInv(2, blocks_h3[0].sha256)]))
232209

233210
test_node.sync_with_ping()
234211
with mininode_lock:
235-
getdata = test_node.last_getdata
212+
getdata = test_node.last_message["getdata"]
236213

237214
# Check that the getdata includes the right block
238215
assert_equal(getdata.inv[0].hash, blocks_h2f[0].sha256)

0 commit comments

Comments
 (0)