Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Neelaksh-Singh/1b787a7723058a284c5cb43f93ace905 to your computer and use it in GitHub Desktop.

Select an option

Save Neelaksh-Singh/1b787a7723058a284c5cb43f93ace905 to your computer and use it in GitHub Desktop.
Exercise for Chaincode Labs Bitcoin Protocol Dev Seminar
#!/usr/bin/env python3
# Copyright (c) 2017-2021 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
''' This is a custom test for chaincode problem statement :->
"Getting node 1 to mine another block, send it to node 2,
and check that node 2 received it."
'''
from collections import defaultdict
from test_framework.blocktools import (create_block, create_coinbase)
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,
)
class BaseNode(P2PInterface):
def __init__(self):
super().__init__()
self.block_receive_map = defaultdict(int)
def on_block(self, message):
message.block.calc_sha256()
self.block_receive_map[message.block.sha256] += 1
def on_inv(self, message):
pass
class MyTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 3
# self.extra_args = [[], []]
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
def setup_network(self):
self.setup_nodes()
# self.log.info("Connect node0 and node1")
self.connect_nodes(0, 1)
self.connect_nodes(1,2)
def run_test(self):
"""Main test logic"""
self.generate(self.nodes[0],1)
self.sync_blocks()
print("success")
print(f"Latest Blockhash for node 1 is {self.nodes[1].getbestblockhash()}")
print(f"Latest Blockhash for node 2 is {self.nodes[2].getbestblockhash()}")
assert_equal(self.nodes[1].getbestblockhash(), self.nodes[2].getbestblockhash())
if __name__ == '__main__':
chaincodeExcercise = MyTest()
chaincodeExcercise.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment