Skip to content

Instantly share code, notes, and snippets.

@jarolrod
Created September 28, 2020 15:23
Show Gist options
  • Save jarolrod/1d670fadaa3759ace74aab527931fc0d to your computer and use it in GitHub Desktop.
Save jarolrod/1d670fadaa3759ace74aab527931fc0d to your computer and use it in GitHub Desktop.
Standalone Test
#!/usr/bin/env python3
# Copyright (c) 2017-2019 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
__author__ = "Jarol Rodriguez"
__email__ = "jarolrod@tutanota.com"
""" Preperation for the 2020 Fall Study Group at Chaincode Labs
Requirements:
1. Command [node0] to mine a block
2. Send [node0]'s mined block to [node1]
3. Check that [node0] recieved the Block
Setup:
- Two Nodes:
- node0: Under Test
- create a p2p connection to node2
- mine a block
- node1: Under Test
- used to check that node0 was able to succesfully send a p2pmessage
"""
from collections import defaultdict
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_equal,
connect_nodes,
)
class BlockSendTest(BitcoinTestFramework):
def set_test_params(self):
# Use cached transactions as a base
self.setup_clean_chain = False
""" Initialize Nodes using Base Node Class
Per specs, base node class keeps track of each P2P message type
- This is sufficient for the requirements of this test
- overriding of base class is not needed
"""
self.num_nodes = 2
""" Enter Block Only Mode
As per Test Requirements, we should only expect Blocks
1. Txs wont be used
2. If Txs wont be used, then Tx's can be ignored
3. : Therefore, Tx's can be ignored (1,2)
4. Enabling "-blocksonly" tells Network not to send us any Txs
5. It is appropriate to enter "-blocksonly" mode if Txs are ignored
c. : Therefore, "-blocksonly" Mode is appropriate (3,4,5)
"""
self.extra_args = [["-blocksonly"],["-blocksonly"]]
""" Ensuring nodes are Synced
a. Get the current bestblockhash() of each node
b. Check that they are the same
c. If the hashes are the same, the nodes have been synced
"""
def checkSync(self):
node0Tip = int(self.nodes[0].getbestblockhash(), 16)
node1Tip = int(self.nodes[1].getbestblockhash(), 16)
return (node0Tip == node1Tip)
def run_test(self):
peer_messaging = self.nodes[0].add_p2p_connection(P2PInterface())
# Generating a block on one of the nodes will get us out of IBD
blocks = [int(self.nodes[0].generate(nblocks=1)[0], 16)]
self.sync_all(self.nodes[0:1])
# check sync
assert (self.checkSync())
# Use generate() rpc call in order to get new block
self.log.info("Generate a new block on node[0].")
oldBlockTip = int(self.nodes[0].getbestblockhash(), 16)
newBlockTip = int(self.nodes[0].generate(nblocks=1)[0], 16)
# Check that node[0]'s old and new block tip are different
# This signals a succesful new block
self.log.info("Check that new Block was successful.")
assert (not(oldBlockTip == newBlockTip))
# Generate() should have already synced node0 and node 1
self.log.info("Check that node[1] has recieved the block from node[0].")
node1Tip = int(self.nodes[1].getbestblockhash(), 16)
assert(newBlockTip == node1Tip)
# Check for succesful send
self.log.info("Check that node[0] has sent the block to node[1]")
node1Tip = int(self.nodes[1].getbestblockhash(), 16)
assert (self.checkSync())
if __name__ == '__main__':
BlockSendTest().main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment