0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

test: Functional test for opportunistic encryption

Co-authored-by: Pieter Wuille <bitcoin-dev@wuille.net>
This commit is contained in:
dhruv 2022-09-07 15:59:49 -07:00 committed by Pieter Wuille
parent b815cce50e
commit 05d19fbcc1
3 changed files with 121 additions and 13 deletions

View file

@ -1552,6 +1552,10 @@ void V2Transport::MarkBytesSent(size_t bytes_sent) noexcept
LOCK(m_send_mutex);
if (m_send_state == SendState::V1) return m_v1_fallback.MarkBytesSent(bytes_sent);
if (m_send_state == SendState::AWAITING_KEY && m_send_pos == 0 && bytes_sent > 0) {
LogPrint(BCLog::NET, "start sending v2 handshake to peer=%d\n", m_nodeid);
}
m_send_pos += bytes_sent;
Assume(m_send_pos <= m_send_buffer.size());
if (m_send_pos >= CMessageHeader::HEADER_SIZE) {

View file

@ -12,19 +12,116 @@ from test_framework.util import assert_equal
class V2TransportTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain=True
self.num_nodes = 1
self.extra_args = [["-v2transport=0"]]
self.setup_clean_chain = True
self.num_nodes = 5
self.extra_args = [["-v2transport=1"], ["-v2transport=1"], ["-v2transport=0"], ["-v2transport=0"], ["-v2transport=0"]]
def run_test(self):
network_info = self.nodes[0].getnetworkinfo()
sending_handshake = "start sending v2 handshake to peer"
downgrading_to_v1 = "retrying with v1 transport protocol for peer"
self.disconnect_nodes(0, 1)
self.disconnect_nodes(1, 2)
self.disconnect_nodes(2, 3)
self.disconnect_nodes(3, 4)
# verify local services
network_info = self.nodes[2].getnetworkinfo()
assert_equal(int(network_info["localservices"], 16) & NODE_P2P_V2, 0)
assert "P2P_V2" not in network_info["localservicesnames"]
self.restart_node(0, ["-v2transport=1"])
network_info = self.nodes[0].getnetworkinfo()
network_info = self.nodes[1].getnetworkinfo()
assert_equal(int(network_info["localservices"], 16) & NODE_P2P_V2, NODE_P2P_V2)
assert "P2P_V2" in network_info["localservicesnames"]
# V2 nodes can sync with V2 nodes
assert_equal(self.nodes[0].getblockcount(), 0)
assert_equal(self.nodes[1].getblockcount(), 0)
with self.nodes[0].assert_debug_log(expected_msgs=[sending_handshake],
unexpected_msgs=[downgrading_to_v1]):
self.connect_nodes(0, 1, peer_advertises_v2=True)
self.generate(self.nodes[0], 5, sync_fun=lambda: self.sync_all(self.nodes[0:2]))
assert_equal(self.nodes[1].getblockcount(), 5)
# verify there is a v2 connection between node 0 and 1
node_0_info = self.nodes[0].getpeerinfo()
node_1_info = self.nodes[0].getpeerinfo()
assert_equal(len(node_0_info), 1)
assert_equal(len(node_1_info), 1)
assert_equal(node_0_info[0]["transport_protocol_type"], "v2")
assert_equal(node_1_info[0]["transport_protocol_type"], "v2")
assert_equal(len(node_0_info[0]["session_id"]), 64)
assert_equal(len(node_1_info[0]["session_id"]), 64)
assert_equal(node_0_info[0]["session_id"], node_1_info[0]["session_id"])
# V1 nodes can sync with each other
assert_equal(self.nodes[2].getblockcount(), 0)
assert_equal(self.nodes[3].getblockcount(), 0)
with self.nodes[2].assert_debug_log(expected_msgs=[],
unexpected_msgs=[sending_handshake, downgrading_to_v1]):
self.connect_nodes(2, 3, peer_advertises_v2=False)
self.generate(self.nodes[2], 8, sync_fun=lambda: self.sync_all(self.nodes[2:4]))
assert_equal(self.nodes[3].getblockcount(), 8)
assert self.nodes[0].getbestblockhash() != self.nodes[2].getbestblockhash()
# verify there is a v1 connection between node 2 and 3
node_2_info = self.nodes[2].getpeerinfo()
node_3_info = self.nodes[3].getpeerinfo()
assert_equal(len(node_2_info), 1)
assert_equal(len(node_3_info), 1)
assert_equal(node_2_info[0]["transport_protocol_type"], "v1")
assert_equal(node_3_info[0]["transport_protocol_type"], "v1")
assert_equal(len(node_2_info[0]["session_id"]), 0)
assert_equal(len(node_3_info[0]["session_id"]), 0)
# V1 nodes can sync with V2 nodes
self.disconnect_nodes(0, 1)
self.disconnect_nodes(2, 3)
with self.nodes[2].assert_debug_log(expected_msgs=[],
unexpected_msgs=[sending_handshake, downgrading_to_v1]):
self.connect_nodes(2, 1, peer_advertises_v2=False) # cannot enable v2 on v1 node
self.sync_all(self.nodes[1:3])
assert_equal(self.nodes[1].getblockcount(), 8)
assert self.nodes[0].getbestblockhash() != self.nodes[1].getbestblockhash()
# verify there is a v1 connection between node 1 and 2
node_1_info = self.nodes[1].getpeerinfo()
node_2_info = self.nodes[2].getpeerinfo()
assert_equal(len(node_1_info), 1)
assert_equal(len(node_2_info), 1)
assert_equal(node_1_info[0]["transport_protocol_type"], "v1")
assert_equal(node_2_info[0]["transport_protocol_type"], "v1")
assert_equal(len(node_1_info[0]["session_id"]), 0)
assert_equal(len(node_2_info[0]["session_id"]), 0)
# V2 nodes can sync with V1 nodes
self.disconnect_nodes(1, 2)
with self.nodes[0].assert_debug_log(expected_msgs=[],
unexpected_msgs=[sending_handshake, downgrading_to_v1]):
self.connect_nodes(0, 3, peer_advertises_v2=False)
self.sync_all([self.nodes[0], self.nodes[3]])
assert_equal(self.nodes[0].getblockcount(), 8)
# verify there is a v1 connection between node 0 and 3
node_0_info = self.nodes[0].getpeerinfo()
node_3_info = self.nodes[3].getpeerinfo()
assert_equal(len(node_0_info), 1)
assert_equal(len(node_3_info), 1)
assert_equal(node_0_info[0]["transport_protocol_type"], "v1")
assert_equal(node_3_info[0]["transport_protocol_type"], "v1")
assert_equal(len(node_0_info[0]["session_id"]), 0)
assert_equal(len(node_3_info[0]["session_id"]), 0)
# V2 node mines another block and everyone gets it
self.connect_nodes(0, 1, peer_advertises_v2=True)
self.connect_nodes(1, 2, peer_advertises_v2=False)
self.generate(self.nodes[1], 1, sync_fun=lambda: self.sync_all(self.nodes[0:4]))
assert_equal(self.nodes[0].getblockcount(), 9) # sync_all() verifies tip hashes match
# V1 node mines another block and everyone gets it
self.generate(self.nodes[3], 2, sync_fun=lambda: self.sync_all(self.nodes[0:4]))
assert_equal(self.nodes[2].getblockcount(), 11) # sync_all() verifies tip hashes match
assert_equal(self.nodes[4].getblockcount(), 0)
# Peer 4 is v1 p2p, but is falsely advertised as v2.
with self.nodes[1].assert_debug_log(expected_msgs=[sending_handshake, downgrading_to_v1]):
self.connect_nodes(1, 4, peer_advertises_v2=True)
self.sync_all()
assert_equal(self.nodes[4].getblockcount(), 11)
if __name__ == '__main__':
V2TransportTest().main()

View file

@ -581,13 +581,20 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
def wait_for_node_exit(self, i, timeout):
self.nodes[i].process.wait(timeout)
def connect_nodes(self, a, b):
def connect_nodes(self, a, b, *, peer_advertises_v2=False):
from_connection = self.nodes[a]
to_connection = self.nodes[b]
from_num_peers = 1 + len(from_connection.getpeerinfo())
to_num_peers = 1 + len(to_connection.getpeerinfo())
ip_port = "127.0.0.1:" + str(p2p_port(b))
from_connection.addnode(ip_port, "onetry")
if peer_advertises_v2:
from_connection.addnode(node=ip_port, command="onetry", v2transport=True)
else:
# skip the optional third argument (default false) for
# compatibility with older clients
from_connection.addnode(ip_port, "onetry")
# poll until version handshake complete to avoid race conditions
# with transaction relaying
# See comments in net_processing:
@ -595,12 +602,12 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
# * Must have a verack message before anything else
self.wait_until(lambda: sum(peer['version'] != 0 for peer in from_connection.getpeerinfo()) == from_num_peers)
self.wait_until(lambda: sum(peer['version'] != 0 for peer in to_connection.getpeerinfo()) == to_num_peers)
self.wait_until(lambda: sum(peer['bytesrecv_per_msg'].pop('verack', 0) == 24 for peer in from_connection.getpeerinfo()) == from_num_peers)
self.wait_until(lambda: sum(peer['bytesrecv_per_msg'].pop('verack', 0) == 24 for peer in to_connection.getpeerinfo()) == to_num_peers)
self.wait_until(lambda: sum(peer['bytesrecv_per_msg'].pop('verack', 0) >= 21 for peer in from_connection.getpeerinfo()) == from_num_peers)
self.wait_until(lambda: sum(peer['bytesrecv_per_msg'].pop('verack', 0) >= 21 for peer in to_connection.getpeerinfo()) == to_num_peers)
# The message bytes are counted before processing the message, so make
# sure it was fully processed by waiting for a ping.
self.wait_until(lambda: sum(peer["bytesrecv_per_msg"].pop("pong", 0) >= 32 for peer in from_connection.getpeerinfo()) == from_num_peers)
self.wait_until(lambda: sum(peer["bytesrecv_per_msg"].pop("pong", 0) >= 32 for peer in to_connection.getpeerinfo()) == to_num_peers)
self.wait_until(lambda: sum(peer["bytesrecv_per_msg"].pop("pong", 0) >= 29 for peer in from_connection.getpeerinfo()) == from_num_peers)
self.wait_until(lambda: sum(peer["bytesrecv_per_msg"].pop("pong", 0) >= 29 for peer in to_connection.getpeerinfo()) == to_num_peers)
def disconnect_nodes(self, a, b):
def disconnect_nodes_helper(node_a, node_b):