Bitshala / BitcoinCore-PR-Review-Club

Bitcoin Core PR Review Organising repo
10 stars 2 forks source link

[Part 1/2: Introduction] Functional tests for v2 P2P encryption #24748 #46

Closed stratospher closed 7 months ago

stratospher commented 8 months ago

Session Details

Learning

This is the introductory session for a 2-part review club. The review club will focus on how the functional test framework works on current master and will not cover the changes in PR #24748. If this is your first exposure to the test framework, it is recommended to understand the basics discussed in this review club. It's a definite prerequisite to understanding PR #24748.

Summary

Questions

  1. What does this PR do at a high level? Why do we need this PR?
  2. Can you trace the functions through which an inbound connection is established in the test framework?
  3. Why is the mechanism to establish an outbound connection in the test framework different? Can you trace the functions through which an outbound connection is established in the test framework?
  4. How are P2P messages read? How are P2P messages sent?
  5. What is a version handshake? What tools can you use to observe different messages that are exchanged.
  6. Briefly explain initial v2 handshake in BIP 324 and when it would need to be done so that P2P messages exchanged are encrypted.

Extra reading

  1. Amiti's onboarding exercise on version handshake
  2. Detailed writeup about working of the functional test framework
stratospher commented 7 months ago

Summary

Overview

#!/usr/bin/env python3
# Copyright (c) 2021-2022 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.p2p import P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_greater_than

class FunInterface(P2PInterface):
    def on_version(self, message):
        super().on_version(message)
        print(message)

class FunTest(BitcoinTestFramework):
    def set_test_params(self):
        self.num_nodes = 2

    def setup_network(self):
        self.setup_nodes()
        # Don't connect the nodes

    def run_test(self):
        # info1 = self.nodes[0].getpeerinfo()
        # print(info1)
        # self.connect_nodes(0, 1)
        # print("does anything change")
        # info2 = self.nodes[0].getpeerinfo()
        # for keys in info2[0]:
        #     print(keys, info2[0][keys])
        peer_0 = self.nodes[0].add_p2p_connection(FunInterface())
        info1 = self.nodes[0].getpeerinfo()
        assert_greater_than(info1[0]['bytesrecv_per_msg']['version'], 0)
        # assert False

if __name__ == "__main__":
    FunTest().main()

1. What does this PR do at a high level? Why do we need this PR?

BIP 324 was recently merged and nodes can now send encrypted P2P messages to each other if they run the latest client. This PR adds support for BIP 324 to the dummy implementation of peer in python so that the python code can also talk to bitcoind using the encrypted protocol. We need this to test BIP 324 code (things like whether we're communicating on encrypted/unencrypted protocol, backward compatible with unencrypted protocol etc..) and also to confirm that existing tests work fine and behave as expected.

2. Can you trace the functions through which an inbound connection is established in the test framework? hint: see how TestNode::add_p2p_connection makes a call to peer_connect

3. Why is the mechanism to establish an outbound connection in the test framework different? Can you trace the functions through which an outbound connection is established in the test framework? hint: see how TestNode::add_outbound_p2p_connection makes a call to peer_accept_connection

4. How are P2P messages read? How are P2P messages sent? hint: see data_received() and send_message()

5. What is a version handshake? why do we need it? What tools can you use to observe different messages that are exchanged.