Skip to content

Commit 2ca6dd2

Browse files
committed
Update tests to the new mocktime and RequestObject
1 parent 9d29659 commit 2ca6dd2

File tree

7 files changed

+50
-23
lines changed

7 files changed

+50
-23
lines changed

src/net_processing.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ std::chrono::microseconds CalculateObjectGetDataTime(const CInv& inv, std::chron
632632
return process_time;
633633
}
634634

635-
void RequestObject(CNodeState* state, const CInv& inv, std::chrono::microseconds current_time, bool fForce = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
635+
void RequestObject(CNodeState* state, const CInv& inv, std::chrono::microseconds current_time, bool fForce = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
636636
{
637637
CNodeState::TxDownloadState& peer_download_state = state->m_tx_download;
638638
if (peer_download_state.m_tx_announced.size() >= MAX_PEER_TX_ANNOUNCEMENTS || peer_download_state.m_tx_announced.count(inv)) {
@@ -660,7 +660,7 @@ void RequestObject(NodeId nodeId, const CInv& inv, std::chrono::microseconds cur
660660
AssertLockHeld(cs_main);
661661
auto* state = State(nodeId);
662662
if (!state) {
663-
return;
663+
return;
664664
}
665665

666666
RequestObject(state, inv, current_time, fForce);
@@ -2400,8 +2400,25 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
24002400
}
24012401

24022402
else if (strCommand == NetMsgType::NOTFOUND) {
2403-
// We do not care about the NOTFOUND message (for now), but logging an Unknown Command
2404-
// message is undesirable as we transmit it ourselves.
2403+
// Remove the NOTFOUND transactions from the peer
2404+
LOCK(cs_main);
2405+
CNodeState *state = State(pfrom->GetId());
2406+
std::vector<CInv> vInv;
2407+
vRecv >> vInv;
2408+
if (vInv.size() <= MAX_PEER_TX_IN_FLIGHT + MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2409+
for (CInv &inv : vInv) {
2410+
// If we receive a NOTFOUND message for a txid we requested, erase
2411+
// it from our data structures for this peer.
2412+
auto in_flight_it = state->m_tx_download.m_tx_in_flight.find(inv);
2413+
if (in_flight_it == state->m_tx_download.m_tx_in_flight.end()) {
2414+
// Skip any further work if this is a spurious NOTFOUND
2415+
// message.
2416+
continue;
2417+
}
2418+
state->m_tx_download.m_tx_in_flight.erase(in_flight_it);
2419+
state->m_tx_download.m_tx_announced.erase(inv);
2420+
}
2421+
}
24052422
return true;
24062423
}
24072424

src/net_processing.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ extern RecursiveMutex cs_main; // !TODO: change mutex to cs_orphans
1515
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
1616
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 25;
1717
/** Expiration time for orphan transactions in seconds */
18-
static const int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
18+
static const int64_t ORPHAN_TX_EXPIRE_TIME = 5 * 60;
1919
/** Minimum time between orphan transactions expire time checks in seconds */
20-
static const int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
20+
static const int64_t ORPHAN_TX_EXPIRE_INTERVAL = 1 * 60;
2121
/** Default for -blockspamfilter, use header spam filter */
2222
static const bool DEFAULT_BLOCK_SPAM_FILTER = true;
2323
/** Default for -blockspamfiltermaxsize, maximum size of the list of indexes in the block spam filter */

test/functional/p2p_invalid_messages.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
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
)
15-
from test_framework.messages import CTxIn, COutPoint, msg_mnping
1616
from test_framework.test_framework import PivxTestFramework
1717
from test_framework.util import (
1818
assert_equal,
@@ -212,24 +212,26 @@ def test_large_inv(self):
212212
def test_fill_askfor(self):
213213
self.nodes[0].generate(1) # IBD
214214
conn = self.nodes[0].add_p2p_connection(InvReceiver())
215+
self.disable_mocktime()
215216
invs = []
216217
blockhash = int(self.nodes[0].getbestblockhash(), 16)
217-
for _ in range(50000):
218+
total_requests = 100
219+
for _ in range(total_requests):
218220
mnp = msg_mnping(CTxIn(COutPoint(getrandbits(256))), blockhash, int(time.time()))
219-
conn.vec_mnp[mnp.get_hash()] = mnp
220-
invs.append(messages.CInv(15, mnp.get_hash()))
221-
assert_equal(len(conn.vec_mnp), 50000)
222-
assert_equal(len(invs), 50000)
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)
223226
msg = messages.msg_inv(invs)
224227
conn.send_message(msg)
225-
conn.wait_for_p2p_messages(50000)
226-
228+
conn.wait_for_p2p_messages(total_requests)
227229
# Prior #2611 the node was blocking any follow-up request.
228230
mnp = msg_mnping(CTxIn(COutPoint(getrandbits(256))), getrandbits(256), int(time.time()))
229231
conn.vec_mnp[mnp.get_hash()] = mnp
230232
msg = messages.msg_inv([messages.CInv(15, mnp.get_hash())])
231233
conn.send_and_ping(msg)
232-
conn.wait_for_p2p_messages(50001)
234+
conn.wait_for_p2p_messages(total_requests + 1)
233235
self.nodes[0].disconnect_p2ps()
234236

235237
def test_resource_exhaustion(self):

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):

test/functional/test_framework/test_framework.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ def wait_until_mnsync_finished(self):
10371037
raise AssertionError("Unable to complete mnsync: %s" % str(synced))
10381038

10391039

1040-
def wait_until_mn_status(self, status, mnTxHash, _timeout, orEmpty=False, with_ping_mns=None):
1040+
def wait_until_mn_status(self, status, mnTxHash, _timeout, orEmpty=False, with_ping_mns=None, wait_func = None):
10411041
if with_ping_mns is None:
10421042
with_ping_mns = []
10431043
nodes_status = [None] * self.num_nodes
@@ -1054,6 +1054,8 @@ def all_synced():
10541054
time.sleep(2)
10551055
timeout = time.time() + _timeout
10561056
while not all_synced() and time.time() < timeout:
1057+
if wait_func is not None:
1058+
wait_func()
10571059
for i in range(self.num_nodes):
10581060
if not node_synced(i):
10591061
nodes_status[i] = self.get_mn_status(self.nodes[i], mnTxHash)
@@ -1066,10 +1068,10 @@ def all_synced():
10661068
raise AssertionError(strErr)
10671069

10681070

1069-
def wait_until_mn_enabled(self, mnTxHash, _timeout, _with_ping_mns=None):
1071+
def wait_until_mn_enabled(self, mnTxHash, _timeout, _with_ping_mns=None, wait_func = None):
10701072
if _with_ping_mns is None:
10711073
_with_ping_mns = []
1072-
self.wait_until_mn_status("ENABLED", mnTxHash, _timeout, with_ping_mns=_with_ping_mns)
1074+
self.wait_until_mn_status("ENABLED", mnTxHash, _timeout, with_ping_mns=_with_ping_mns, wait_func=wait_func)
10731075

10741076

10751077
def wait_until_mn_preenabled(self, mnTxHash, _timeout, _with_ping_mns=None):

test/functional/tiertwo_governance_invalid_budget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ def setupContext(self):
127127
self.wait_until_mn_preenabled(self.mnOneCollateral.hash, 40)
128128
self.wait_until_mn_preenabled(self.mnOneCollateral.hash, 40)
129129
self.send_3_pings([self.mn1, self.mn2])
130-
self.wait_until_mn_enabled(self.mnOneCollateral.hash, 120, [self.mn1, self.mn2])
131-
self.wait_until_mn_enabled(self.mnOneCollateral.hash, 120, [self.mn1, self.mn2])
130+
self.wait_until_mn_enabled(self.mnOneCollateral.hash, 120, [self.mn1, self.mn2], lambda: self.advance_mocktime(2))
131+
self.wait_until_mn_enabled(self.mnTwoCollateral.hash, 120, [self.mn1, self.mn2], lambda: self.advance_mocktime(2))
132132

133133
# activate sporks
134134
self.log.info("Masternodes enabled. Activating sporks.")

test/functional/tiertwo_governance_reorg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def run_test(self):
8585
self.wait_until_mn_preenabled(self.mnOneCollateral.hash, 40)
8686
self.wait_until_mn_preenabled(self.mnOneCollateral.hash, 40)
8787
self.send_3_pings([mn1, mn2])
88-
self.wait_until_mn_enabled(self.mnOneCollateral.hash, 120, [mn1, mn2])
89-
self.wait_until_mn_enabled(self.mnOneCollateral.hash, 120, [mn1, mn2])
88+
self.wait_until_mn_enabled(self.mnOneCollateral.hash, 120, [mn1, mn2], lambda: self.advance_mocktime(2))
89+
self.wait_until_mn_enabled(self.mnOneCollateral.hash, 120, [mn1, mn2], lambda: self.advance_mocktime(2))
9090

9191
# activate sporks
9292
self.log.info("Masternodes enabled. Activating sporks.")

0 commit comments

Comments
 (0)