def test_getdata_message_with_two_blocks(self):
        cblock1 = CBlock()
        block1 = Block(cblock1, BlockOrigin.private)
        block1.cblock = cblock1
        cInv1 = CInv()
        cInv1.type = networking.inv_typemap['Block']
        cInv1.hash = cblock1.GetHash()
        cblock2 = CBlock()
        block2 = Block(cblock2, BlockOrigin.private)
        block2.cblock = cblock2
        cInv2 = CInv()
        cInv2.type = networking.inv_typemap['Block']
        cInv2.hash = cblock2.GetHash()
        message = messages.msg_getdata()
        message.inv = [cInv1, cInv2]

        self.chain.blocks = {
            cblock1.GetHash(): block1,
            cblock2.GetHash(): block2
        }

        self.networking.getdata_message(self.public_connection1, message)

        self.assertTrue(self.public_connection1.send.called)
        self.assertEqual(self.public_connection1.send.call_count, 2)
    def test_getheaders_message_no_block_found(self, mock):
        message = messages.msg_getheaders()
        block1 = Block('cblock_header1', BlockOrigin.private)
        block1.cblock = CBlock(nNonce=1)
        block2 = Block('cblock_header2', BlockOrigin.private)
        block2.cblock = CBlock(nNonce=2)
        mock.return_value = [block1, block2]

        self.networking.getheaders_message(self.private_connection, message)

        self.assertTrue(self.private_connection.send.called)
        self.assertEqual(
            len(self.private_connection.send.call_args[0][1].headers), 2)
        self.assertEqual(
            self.private_connection.send.call_args[0][1].headers[0],
            block1.cblock)
        self.assertEqual(
            self.private_connection.send.call_args[0][1].headers[1],
            block2.cblock)
    def test_getdata_message_with_block(self):
        cblock = CBlock()
        block = Block(cblock, BlockOrigin.private)
        block.cblock = cblock
        message = messages.msg_getdata()
        cInv = CInv()
        cInv.type = networking.inv_typemap['Block']
        cInv.hash = cblock.GetHash()
        message.inv = [cInv]

        self.chain.blocks = {cblock.GetHash(): block}

        self.networking.getdata_message(self.public_connection1, message)

        self.assertTrue(self.public_connection1.send.called)
        self.assertEqual(self.public_connection1.send.call_args[0][0], 'block')
        self.assertEqual(self.public_connection1.send.call_args[0][1].block,
                         cblock)
from bitcoin import core
from chain import Block
from chain import BlockOrigin

genesis_hash = core.CoreRegTestParams.GENESIS_BLOCK.GetHash()
genesis_block = Block(core.CoreRegTestParams.GENESIS_BLOCK, BlockOrigin.public)
genesis_block.height = 0
genesis_block.transfer_allowed = True
genesis_block.cblock = core.CoreRegTestParams.GENESIS_BLOCK