def test_send_inv_public_blocks(self): block1 = Block(CBlock(), BlockOrigin.public) block1.cached_hash = 'hash1' block2 = Block(CBlock(), BlockOrigin.public) block2.cached_hash = 'hash2' self.networking.send_inv([block1, block2]) self.assertFalse(self.public_connection1.send.called) self.assertFalse(self.public_connection2.send.called) self.assertTrue(self.private_connection.send.called) inv = self.private_connection.send.call_args[0][1].inv self.assertEqual(len(inv), 2)
def test_insert_block_initializing_false(self): prevBlock = Block(CBlock(), BlockOrigin.private) prevBlock.cached_hash = 'hash2' prevBlock.height = 45 block = Block(CBlock(), BlockOrigin.private) block.cached_hash = 'hash1' self.chain.tips = [prevBlock] self.chain.initializing = False self.chain.insert_block(prevBlock, block) retrieved_block = self.chain.tips[0] self.assertEqual(retrieved_block, block) self.assertEqual(retrieved_block.transfer_allowed, False)
def test_insert_block(self): prevBlock = Block(CBlock(), BlockOrigin.private) prevBlock.cached_hash = 'hash2' prevBlock.height = 45 block = Block(CBlock(), BlockOrigin.private) block.cached_hash = 'hash1' self.chain.tips = [prevBlock] self.chain.insert_block(prevBlock, block) self.assertFalse(prevBlock in self.chain.tips) self.assertEqual(len(self.chain.tips), 1) retrieved_block = self.chain.tips[0] self.assertEqual(retrieved_block, block) self.assertEqual(retrieved_block.prevBlock, prevBlock) self.assertEqual(retrieved_block.height, 46)
def test_block_message(self): message = messages.msg_block() cblock = CBlock() message.block = cblock block = Block(None, BlockOrigin.private) block.cached_hash = message.block.GetHash() self.chain.blocks = {block.hash(): block} self.networking.block_message(self.private_connection, message) self.assertEqual(self.chain.blocks[block.hash()].cblock, cblock)
def test_send_inv_blocks(self): block1 = Block(CBlock(), BlockOrigin.public) block1.cached_hash = 'hash1' block2 = Block(CBlock(), BlockOrigin.public) block2.cached_hash = 'hash2' block3 = Block(CBlock(), BlockOrigin.public) block3.cached_hash = 'hash3' block4 = Block(CBlock(), BlockOrigin.private) block4.cached_hash = 'hash4' block5 = Block(CBlock(), BlockOrigin.private) block5.cached_hash = 'hash5' self.networking.send_inv([block1, block2, block3, block4, block5]) self.assertTrue(self.private_connection.send.called) inv = self.private_connection.send.call_args[0][1].inv self.assertEqual(len(inv), 3) self.assertTrue(self.public_connection1.send.called) self.assertTrue(self.public_connection2.send.called) self.assertEqual(len(self.public_connection2.send.call_args[0][1].inv), 2)
def test_block_message_two_times(self): message = messages.msg_block() cblock1 = CBlock(nNonce=1) cblock2 = CBlock(nNonce=2) message.block = cblock1 block = Block(None, BlockOrigin.private) block.cached_hash = message.block.GetHash() self.chain.blocks = {block.hash(): block} self.networking.block_message(self.private_connection, message) message.block = cblock2 self.networking.block_message(self.private_connection, message)
def test_block_message_remove_from_blocks_in_flight(self): message = messages.msg_block() cblock = CBlock() message.block = cblock block = Block(None, BlockOrigin.private) block.cached_hash = message.block.GetHash() self.chain.blocks = {block.hash(): block} self.networking.blocks_in_flight = {block.hash(): 'in_flight'} self.networking.block_message(self.private_connection, message) self.assertEqual(len(self.networking.blocks_in_flight), 0)
def test_request_get_headers_very_long_chain(self, mock): first_block = Block(CBlock(), BlockOrigin.public) first_block.prevBlock = None first_block.cached_hash = '0' tmp = first_block for i in range(1, 17): block = Block(CBlock(), BlockOrigin.public) block.prevBlock = tmp block.cached_hash = str(i) tmp = block mock.return_value = tmp headers = chainutil.request_get_headers([MagicMock()], BlockOrigin.public) self.assertEqual(len(headers), 5) self.assertEqual(headers[0], '16') self.assertEqual(headers[1], '15') self.assertEqual(headers[2], '13') self.assertEqual(headers[3], '9') self.assertEqual(headers[4], '1')
def test_adopt_two_blocks_lead_public(self): third_block_chain_b = Block(CBlock(), BlockOrigin.public) third_block_chain_b.height = 3 third_block_chain_b.prevBlock = self.second_block_chain_b third_block_chain_b.cached_hash = '3b' self.executor.execute(Action.adopt, self.first_block_chain_a, third_block_chain_b) self.assertTrue(self.networking.send_inv.called) blocks = [block.hash() for block in self.networking.send_inv.call_args[0][0]] self.assertEqual(len(blocks), 3) self.assertTrue('1b' in blocks) self.assertTrue('2b' in blocks) self.assertTrue('3b' in blocks)
def test_override_two_blocks_lead_private(self): third_block_chain_a = Block(CBlock(), BlockOrigin.private) third_block_chain_a.height = 3 third_block_chain_a.prevBlock = self.second_block_chain_a third_block_chain_a.cached_hash = '3a' self.executor.execute(Action.override, third_block_chain_a, self.first_block_chain_b) self.assertTrue(self.networking.send_inv.called) blocks = [block.hash() for block in self.networking.send_inv.call_args[0][0]] self.assertEqual(len(blocks), 3) self.assertTrue('1a' in blocks) self.assertTrue('2a' in blocks) self.assertTrue('1b' in blocks)
def test_block_message_deferred_requests(self): message = messages.msg_block() cblock = CBlock() hash_ = cblock.GetHash() message.block = cblock block = Block(None, BlockOrigin.private) block.cached_hash = hash_ self.networking.deferred_block_requests = \ {hash_: [self.private_connection.host[0], self.public_connection2.host[0]]} self.networking.send_block = MagicMock() self.networking.block_message(self.public_connection1, message) self.assertEqual(len(self.networking.deferred_block_requests), 0) self.assertEqual(self.networking.send_block.call_count, 2) self.assertEqual(self.networking.send_block.call_args[0][1], cblock)