mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 09:46:52 -05:00
Refactor the functional test
This commit is contained in:
parent
83ad65f31b
commit
0d04784af1
1 changed files with 14 additions and 33 deletions
|
@ -5,13 +5,8 @@
|
||||||
"""Test addr response caching"""
|
"""Test addr response caching"""
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from test_framework.messages import (
|
|
||||||
CAddress,
|
from test_framework.messages import msg_getaddr
|
||||||
NODE_NETWORK,
|
|
||||||
NODE_WITNESS,
|
|
||||||
msg_addr,
|
|
||||||
msg_getaddr,
|
|
||||||
)
|
|
||||||
from test_framework.p2p import (
|
from test_framework.p2p import (
|
||||||
P2PInterface,
|
P2PInterface,
|
||||||
p2p_lock
|
p2p_lock
|
||||||
|
@ -21,21 +16,9 @@ from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# As defined in net_processing.
|
||||||
MAX_ADDR_TO_SEND = 1000
|
MAX_ADDR_TO_SEND = 1000
|
||||||
|
MAX_PCT_ADDR_TO_SEND = 23
|
||||||
def gen_addrs(n):
|
|
||||||
addrs = []
|
|
||||||
for i in range(n):
|
|
||||||
addr = CAddress()
|
|
||||||
addr.time = int(time.time())
|
|
||||||
addr.nServices = NODE_NETWORK | NODE_WITNESS
|
|
||||||
# Use first octets to occupy different AddrMan buckets
|
|
||||||
first_octet = i >> 8
|
|
||||||
second_octet = i % 256
|
|
||||||
addr.ip = "{}.{}.1.1".format(first_octet, second_octet)
|
|
||||||
addr.port = 8333
|
|
||||||
addrs.append(addr)
|
|
||||||
return addrs
|
|
||||||
|
|
||||||
class AddrReceiver(P2PInterface):
|
class AddrReceiver(P2PInterface):
|
||||||
|
|
||||||
|
@ -62,18 +45,16 @@ class AddrTest(BitcoinTestFramework):
|
||||||
self.num_nodes = 1
|
self.num_nodes = 1
|
||||||
|
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
self.log.info('Create connection that sends and requests addr messages')
|
|
||||||
addr_source = self.nodes[0].add_p2p_connection(P2PInterface())
|
|
||||||
|
|
||||||
msg_send_addrs = msg_addr()
|
|
||||||
self.log.info('Fill peer AddrMan with a lot of records')
|
self.log.info('Fill peer AddrMan with a lot of records')
|
||||||
# Since these addrs are sent from the same source, not all of them will be stored,
|
for i in range(10000):
|
||||||
# because we allocate a limited number of AddrMan buckets per addr source.
|
first_octet = i >> 8
|
||||||
total_addrs = 10000
|
second_octet = i % 256
|
||||||
addrs = gen_addrs(total_addrs)
|
a = "{}.{}.1.1".format(first_octet, second_octet)
|
||||||
for i in range(int(total_addrs/MAX_ADDR_TO_SEND)):
|
self.nodes[0].addpeeraddress(a, 8333)
|
||||||
msg_send_addrs.addrs = addrs[i * MAX_ADDR_TO_SEND:(i + 1) * MAX_ADDR_TO_SEND]
|
|
||||||
addr_source.send_and_ping(msg_send_addrs)
|
# Need to make sure we hit MAX_ADDR_TO_SEND records in the addr response later because
|
||||||
|
# only a fraction of all known addresses can be cached and returned.
|
||||||
|
assert(len(self.nodes[0].getnodeaddresses(0)) > int(MAX_ADDR_TO_SEND / (MAX_PCT_ADDR_TO_SEND / 100)))
|
||||||
|
|
||||||
responses = []
|
responses = []
|
||||||
self.log.info('Send many addr requests within short time to receive same response')
|
self.log.info('Send many addr requests within short time to receive same response')
|
||||||
|
@ -89,7 +70,7 @@ class AddrTest(BitcoinTestFramework):
|
||||||
responses.append(addr_receiver.get_received_addrs())
|
responses.append(addr_receiver.get_received_addrs())
|
||||||
for response in responses[1:]:
|
for response in responses[1:]:
|
||||||
assert_equal(response, responses[0])
|
assert_equal(response, responses[0])
|
||||||
assert(len(response) < MAX_ADDR_TO_SEND)
|
assert(len(response) == MAX_ADDR_TO_SEND)
|
||||||
|
|
||||||
cur_mock_time += 3 * 24 * 60 * 60
|
cur_mock_time += 3 * 24 * 60 * 60
|
||||||
self.nodes[0].setmocktime(cur_mock_time)
|
self.nodes[0].setmocktime(cur_mock_time)
|
||||||
|
|
Loading…
Add table
Reference in a new issue