diff --git a/test/functional/feature_wallet_rpc_basics.py b/test/functional/feature_wallet_rpc_basics.py new file mode 100755 index 0000000000..4cade8f1dd --- /dev/null +++ b/test/functional/feature_wallet_rpc_basics.py @@ -0,0 +1,199 @@ +#!/usr/bin/env python3 +# Copyright (c) 2017-present The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +"""An example functional test + +The module-level docstring should include a high-level description of +what the test is doing. It's the first thing people see when they open +the file and should give the reader information about *what* the test +is testing and *how* it's being tested +""" +# Imports should be in PEP8 ordering (std library first, then third party +# libraries then local imports). +from collections import defaultdict + +# Avoid wildcard * imports +# Use lexicographically sorted multi-line imports +from test_framework.blocktools import ( + create_block, + create_coinbase, +) +from test_framework.messages import ( + CInv, + MSG_BLOCK, +) +from test_framework.p2p import ( + P2PInterface, + msg_block, + msg_getdata, + p2p_lock, +) +from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import ( + assert_approx, + assert_equal, + assert_greater_than, +) + +# P2PInterface is a class containing callbacks to be executed when a P2P +# message is received from the node-under-test. Subclass P2PInterface and +# override the on_*() methods if you need custom behaviour. +class BaseNode(P2PInterface): + def __init__(self): + """Initialize the P2PInterface + + Used to initialize custom properties for the Node that aren't + included by default in the base class. Be aware that the P2PInterface + base class already stores a counter for each P2P message type and the + last received message of each type, which should be sufficient for the + needs of most tests. + + Call super().__init__() first for standard initialization and then + initialize custom properties.""" + super().__init__() + # Stores a dictionary of all blocks received + self.block_receive_map = defaultdict(int) + + def on_block(self, message): + """Override the standard on_block callback + + Store the hash of a received block in the dictionary.""" + self.block_receive_map[message.block.hash_int] += 1 + + def on_inv(self, message): + """Override the standard on_inv callback""" + pass + +def custom_function(): + """Do some custom behaviour + + If this function is more generally useful for other tests, consider + moving it to a module in test_framework.""" + # self.log.info("running custom_function") # Oops! Can't run self.log outside the BitcoinTestFramework + pass + + +class ExampleTest(BitcoinTestFramework): + # Each functional test is a subclass of the BitcoinTestFramework class. + + # Override the set_test_params(), skip_test_if_missing_module(), add_options(), setup_chain(), setup_network() + # and setup_nodes() methods to customize the test setup as required. + + def set_test_params(self): + """Override test parameters for your individual test. + + This method must be overridden and num_nodes must be explicitly set.""" + # By default every test loads a pre-mined chain of 200 blocks from cache. + # Set setup_clean_chain to True to skip this and start from the Genesis + # block. + self.setup_clean_chain = True + self.num_nodes = 2 + # Use self.extra_args to change command-line arguments for the nodes + self.extra_args = [[], ["-logips"]] + + # self.log.info("I've finished set_test_params") # Oops! Can't run self.log before run_test() + + # Use skip_test_if_missing_module() to skip the test if your test requires certain modules to be present. + # This test uses generate which requires wallet to be compiled + def skip_test_if_missing_module(self): + self.skip_if_no_wallet() + + # Use add_options() to add specific command-line options for your test. + # In practice this is not used very much, since the tests are mostly written + # to be run in automated environments without command-line options. + # def add_options() + # pass + + # Use setup_chain() to customize the node data directories. In practice + # this is not used very much since the default behaviour is almost always + # fine + # def setup_chain(): + # pass + + # Use setup_nodes() to customize the node start behaviour (for example if + # you don't want to start all nodes at the start of the test). + # def setup_nodes(): + # pass + + def custom_method(self): + """Do some custom behaviour for this test + + Define it in a method here because you're going to use it repeatedly. + If you think it's useful in general, consider moving it to the base + BitcoinTestFramework class so other tests can use it.""" + + self.log.info("Running custom_method") + + def setup_network(self): + """Setup the test network topology + + Often you won't need to override this, since the standard network topology + (linear: node0 <-> node1 <-> node2 <-> ...) is fine for most tests. + + If you do override this method, remember to start the nodes, assign + them to self.nodes, connect them and then sync.""" + self.setup_nodes() + self.connect_nodes(0, 1) + + def run_test(self): + """Main test logic""" + + # Create P2P connections will wait for a verack to make sure the connection is fully up + peer_messaging = self.nodes[0].add_p2p_connection(BaseNode()) + + # Generating a block on one of the nodes will get us out of IBD + blocks = [int(self.generate(self.nodes[0], sync_fun=lambda: self.sync_all(self.nodes[0:1]), nblocks=1)[0], 16)] + + # Notice above how we called an RPC by calling a method with the same + # name on the node object. Notice also how we used a keyword argument + # to specify a named RPC argument. Neither of those are defined on the + # node object. Instead there's some __getattr__() magic going on under + # the covers to dispatch unrecognised attribute calls to the RPC + # interface. + + # Logs are nice. Do plenty of them. They can be used in place of comments for + # breaking the test into sub-sections. + self.log.info("Starting test!") + + # Creating wallet for node 1 + self.nodes[0].createwallet("node0_wallet") + node0_wallet = self.nodes[0].get_wallet_rpc("node0_wallet") + + # Creating wallet for node 2 + self.nodes[1].createwallet("node1_wallet") + node1_wallet = self.nodes[1].get_wallet_rpc("node1_wallet") + + # Generating 101 blocks to node 0 wallet + blocks = self.generatetoaddress(self.nodes[0], 101, node0_wallet.getnewaddress(), sync_fun=lambda: self.sync_mempools(self.nodes[0:2])) + + + # Sending 1 BTC from node 0 wallet to node 1 wallet + node0_wallet.sendtoaddress(node1_wallet.getnewaddress(), 1) + self.sync_mempools(self.nodes[0:2]) + + # Checking that the transaction is in both node's mempools + txid = self.nodes[0].getblock(blocks[0])['tx'][0] + self.log.info(f"Txid: {txid}") + assert_equal(self.nodes[0].getmempoolinfo()['size'], 1) + assert_equal(self.nodes[1].getmempoolinfo()['size'], 1) + + # Ensures balance on node 1 is less than 49 + assert_greater_than(49, node0_wallet.getbalance()) + + # Generates another block to confirm the transaction + blocks = self.generatetoaddress(self.nodes[0], 1, node0_wallet.getnewaddress(), sync_fun=lambda: self.sync_mempools(self.nodes[0:2])) + + # Making sure the transaction is no longer in the mempool + assert_equal(self.nodes[0].getmempoolinfo()['size'], 0) + assert_equal(self.nodes[1].getmempoolinfo()['size'], 0) + + # Checking that node's 1 balance is 1 + assert_equal(node1_wallet.getbalance(), 1) + + node0_balance = node0_wallet.getbalance() + self.log.info(f"Node0 balance: {node0_balance}") + + +if __name__ == '__main__': + ExampleTest(__file__).main() diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index a4052f9102..43b67a59b7 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -348,6 +348,8 @@ 'p2p_ping.py', 'p2p_tx_privacy.py', 'rpc_getdescriptoractivity.py', + 'feature_hello_world.py', + 'feature_wallet_rpc_basics.py', 'rpc_scanblocks.py', 'tool_bitcoin.py', 'p2p_sendtxrcncl.py',