Skip to content

Instantly share code, notes, and snippets.

@sogoagain
Last active May 1, 2023 12:31
Show Gist options
  • Save sogoagain/d645de960aa4959219e83f928889d64b to your computer and use it in GitHub Desktop.
Save sogoagain/d645de960aa4959219e83f928889d64b to your computer and use it in GitHub Desktop.
Simple functional test example for Bitcoin Core
from test_framework.util import (
assert_equal,
assert_approx
)
from test_framework.test_framework import BitcoinTestFramework
class SimpleFunctionalTest(BitcoinTestFramework):
def add_options(self, parser):
self.add_wallet_options(parser)
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 3
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
def setup_network(self):
self.setup_nodes()
self.connect_nodes(0, 1)
self.connect_nodes(0, 2)
self.sync_all(self.nodes[0:2])
def run_test(self):
# Generate 1 block and test block count and immature balance
blocks = self.generate(self.nodes[0], nblocks=1, sync_fun=self.no_op)
assert_equal(self.nodes[0].getblockcount(), 1)
walletinfo_node0 = self.nodes[0].getwalletinfo()
assert_equal(walletinfo_node0['immature_balance'], 50)
assert_equal(walletinfo_node0['balance'], 0)
# Generate 100 more blocks and test block count and balance
blocks = self.generate(self.nodes[0], nblocks=100, sync_fun=self.no_op)
assert_equal(self.nodes[0].getblockcount(), 101)
assert_equal(self.nodes[0].getbalance(), 50)
# Sync nodes and test best block hash
self.sync_blocks(self.nodes[0:2])
assert_equal(self.nodes[1].getbestblockhash(), blocks[99])
assert_equal(self.nodes[2].getbestblockhash(), blocks[99])
# Test unspent transaction outputs
utxos = self.nodes[0].listunspent()
assert_equal(len(utxos), 1)
assert_equal(utxos[0]['confirmations'], 101)
# Send 1 BTC from node0 to node1
txid = self.nodes[0].sendtoaddress(address=self.nodes[1].getnewaddress(), amount=1)
# Sync nodes and test mempool
self.sync_mempools(self.nodes[0:2])
assert txid in self.nodes[1].getrawmempool()
assert txid in self.nodes[2].getrawmempool()
# Generate a block with node2 and test block count
blocks = self.generate(self.nodes[2], nblocks=1, sync_fun=self.no_op)
assert_equal(self.nodes[2].getblockcount(), 102)
# Sync nodes and test best block hash
self.sync_blocks(self.nodes[0:2])
assert_equal(self.nodes[0].getbestblockhash(), blocks[0])
assert_equal(self.nodes[1].getbestblockhash(), blocks[0])
# Test nodes' balances
assert_approx(self.nodes[0].getbalance(), 99, 0.001)
assert_equal(self.nodes[1].getbalance(), 1)
assert_equal(self.nodes[2].getbalance(), 0)
if __name__ == '__main__':
SimpleFunctionalTest().main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment