Skip to content

Commit 9e6caff

Browse files
committed
Add fill_askfor test again and fix hash
1 parent 488d34c commit 9e6caff

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

test/functional/p2p_invalid_messages.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
import time
99

1010
from test_framework import messages
11+
from test_framework.messages import CTxIn, COutPoint, msg_mnping
1112
from test_framework.mininode import (
1213
P2PDataStore,
1314
P2PInterface,
1415
)
1516
from test_framework.test_framework import PivxTestFramework
1617
from test_framework.util import (
18+
assert_equal,
1719
hex_str_to_bytes,
1820
wait_until,
1921
)
22+
from random import getrandbits
2023

2124
MSG_LIMIT = 2 * 1024 * 1024 # 2MB, per MAX_PROTOCOL_MESSAGE_LENGTH
2225
VALID_DATA_LIMIT = MSG_LIMIT - 5 # Account for the 5-byte length prefix
@@ -52,7 +55,6 @@ def on_getdata(self, message):
5255
if inv.type == 15: # MNPING
5356
self.send_message(self.vec_mnp[inv.hash])
5457
self.getdata_count+=1
55-
print(self.getdata_count)
5658

5759
def wait_for_p2p_messages(self, n_messages):
5860
wait_until(lambda: self.getdata_count == n_messages, timeout=60)
@@ -74,6 +76,7 @@ def run_test(self):
7476
self.test_addrv2_unrecognized_network()
7577
self.test_large_inv()
7678
self.test_resource_exhaustion()
79+
self.test_fill_askfor()
7780

7881
def test_magic_bytes(self):
7982
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
@@ -206,6 +209,31 @@ def test_large_inv(self):
206209
conn.send_and_ping(msg)
207210
self.nodes[0].disconnect_p2ps()
208211

212+
def test_fill_askfor(self):
213+
self.nodes[0].generate(1) # IBD
214+
conn = self.nodes[0].add_p2p_connection(InvReceiver())
215+
self.disable_mocktime()
216+
invs = []
217+
blockhash = int(self.nodes[0].getbestblockhash(), 16)
218+
total_requests = 100
219+
for _ in range(total_requests):
220+
mnp = msg_mnping(CTxIn(COutPoint(getrandbits(256))), blockhash, int(time.time()))
221+
hash = mnp.get_hash()
222+
conn.vec_mnp[hash] = mnp
223+
invs.append(messages.CInv(15, hash))
224+
assert_equal(len(conn.vec_mnp), total_requests)
225+
assert_equal(len(invs), total_requests)
226+
msg = messages.msg_inv(invs)
227+
conn.send_message(msg)
228+
conn.wait_for_p2p_messages(total_requests)
229+
# Prior #2611 the node was blocking any follow-up request.
230+
mnp = msg_mnping(CTxIn(COutPoint(getrandbits(256))), getrandbits(256), int(time.time()))
231+
conn.vec_mnp[mnp.get_hash()] = mnp
232+
msg = messages.msg_inv([messages.CInv(15, mnp.get_hash())])
233+
conn.send_and_ping(msg)
234+
conn.wait_for_p2p_messages(total_requests + 1)
235+
self.nodes[0].disconnect_p2ps()
236+
209237
def test_resource_exhaustion(self):
210238
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
211239
conn2 = self.nodes[0].add_p2p_connection(P2PDataStore())

test/functional/test_framework/messages.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,10 +1246,16 @@ def serialize(self):
12461246
r += ser_string(self.vch_sig)
12471247
r += struct.pack("<I", self.mess_version)
12481248
return r
1249+
def serialize_for_hash(self):
1250+
r = b""
1251+
r += self.vin.serialize()
1252+
r += ser_uint256(self.blockhash)
1253+
r += struct.pack("<q", self.sigtime)
1254+
return r
12491255

12501256
def get_hash(self):
12511257
if self.cached_hash == b"":
1252-
self.cached_hash = uint256_from_str(hash256(self.serialize()))
1258+
self.cached_hash = uint256_from_str(hash256(self.serialize_for_hash()))
12531259
return self.cached_hash
12541260

12551261
def __repr__(self):

0 commit comments

Comments
 (0)