Skip to content

Commit

Permalink
Set for every instance and fix test peer
Browse files Browse the repository at this point in the history
  • Loading branch information
Liquid369 committed May 9, 2023
1 parent 01e2e26 commit eb08a9c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
12 changes: 11 additions & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ struct CNodeState {
int nBlocksInFlight;
//! Whether we consider this a preferred download peer.
bool fPreferredDownload;
//! Addresses processed
uint64_t amt_addr_processed = 0;
//! Addresses rate limited
uint64_t amt_addr_rate_limited = 0;

CNodeBlocks nodeBlocks;

Expand Down Expand Up @@ -498,6 +502,9 @@ bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats)
if (queue.pindex)
stats.vHeightInFlight.push_back(queue.pindex->nHeight);
}

stats.m_addr_processed = state->amt_addr_processed;
stats.m_addr_rate_limited = state->amt_addr_rate_limited;
return true;
}

Expand Down Expand Up @@ -1513,7 +1520,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
}
pfrom->m_addr_token_timestamp = current_time;

const bool rate_limited = pfrom->fGetAddr;
const bool rate_limited = true;
uint64_t num_proc = 0;
uint64_t num_rate_limit = 0;
Shuffle(vAddr.begin(), vAddr.end(), FastRandomContext());
Expand Down Expand Up @@ -1551,6 +1558,9 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
}
pfrom->m_addr_processed += num_proc;
pfrom->m_addr_rate_limited += num_rate_limit;
CNodeState* state = State(pfrom->GetId());
state->amt_addr_processed += num_proc;
state->amt_addr_rate_limited += num_rate_limit;
LogPrint(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) from peer=%d\n",
vAddr.size(), num_proc, num_rate_limit, pfrom->GetId());
connman->AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
Expand Down
25 changes: 12 additions & 13 deletions test/functional/p2p_addr_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ def on_getaddr(self, message):

@property
def tokens(self):
with mininode_lock:
return self._tokens
return self._tokens

def increment_tokens(self, n):
# When we move mocktime forward, the node increments the addr relay tokens for its peers
with mininode_lock:
self._tokens += n
self._tokens += n


class AddrTest(PivxTestFramework):
Expand Down Expand Up @@ -94,18 +92,19 @@ def setup_rand_addr_msg(self, num):
msg.addrs = addrs
return msg

def send_addrs_and_test_rate_limiting(self, peer, no_relay, new_addrs, total_addrs):
def send_addrs_and_test_rate_limiting(self, peer, new_addrs, total_addrs):
"""Send an addr message and check that the number of addresses processed and rate-limited is as expected"""

peer.send_and_ping(self.setup_rand_addr_msg(new_addrs))

peerinfo = self.nodes[0].getpeerinfo()[0]
peerinfo = self.nodes[0].getpeerinfo()[2]
addrs_processed = peerinfo['addr_processed']
addrs_rate_limited = peerinfo['addr_rate_limited']
self.log.info(f'addrs_processed = {addrs_processed}, addrs_rate_limited = {addrs_rate_limited}')
self.log.info(f'peer_tokens = {peer.tokens}')

assert_equal(addrs_processed, 0)
assert_equal(addrs_rate_limited, 0)
assert_equal(addrs_processed, min(total_addrs, peer.tokens))
assert_equal(addrs_rate_limited, max(0, total_addrs - peer.tokens))

def rate_limit_tests(self):

Expand All @@ -117,29 +116,29 @@ def rate_limit_tests(self):
self.log.info(f'Test rate limiting of addr processing for inbound peers')

# Send 600 addresses.
self.send_addrs_and_test_rate_limiting(peer, False, 600, 600)
self.send_addrs_and_test_rate_limiting(peer, 600, 600)

# Send 400 more addresses.
self.send_addrs_and_test_rate_limiting(peer, False, 400, 1000)
self.send_addrs_and_test_rate_limiting(peer, 400, 1000)

# Send 10 more. As we reached the processing limit for nodes, no more addresses should be procesesd.
self.send_addrs_and_test_rate_limiting(peer, False, 10, 1010)
self.send_addrs_and_test_rate_limiting(peer, 10, 1010)

# Advance the time by 100 seconds, permitting the processing of 10 more addresses.
# Send 200 and verify that 10 are processed.
self.mocktime += 100
self.nodes[0].setmocktime(self.mocktime)
peer.increment_tokens(10)

self.send_addrs_and_test_rate_limiting(peer, False, 200, 1210)
self.send_addrs_and_test_rate_limiting(peer, 200, 1210)

# Advance the time by 1000 seconds, permitting the processing of 100 more addresses.
# Send 200 and verify that 100 are processed.
self.mocktime += 1000
self.nodes[0].setmocktime(self.mocktime)
peer.increment_tokens(100)

self.send_addrs_and_test_rate_limiting(peer, False, 200, 1410)
self.send_addrs_and_test_rate_limiting(peer, 200, 1410)

self.nodes[0].disconnect_p2ps()

Expand Down

0 comments on commit eb08a9c

Please sign in to comment.