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_override_same_height(self): private_tip = Block(CBlock(), None) private_tip.height = 2 public_tip = Block(CBlock(), None) public_tip.height = 2 with self.assertRaisesRegexp(ActionException, "private tip.*must > then public tip.*override.*"): self.executor.execute(Action.override, private_tip, public_tip)
def test_match_lead_public(self): private_tip = Block(CBlock(), None) private_tip.height = 1 public_tip = Block(CBlock(), None) public_tip.height = 2 with self.assertRaisesRegexp(ActionException, "private tip.*must >= then public tip.*match.*"): self.executor.execute(Action.match, private_tip, public_tip)
def test_adopt_same_height(self): private_tip = Block(CBlock(), None) private_tip.height = 2 public_tip = Block(CBlock(), None) public_tip.height = 2 with self.assertRaisesRegexp(ActionException, "public tip.*must > then private tip.*adopt.*"): self.executor.execute(Action.adopt, private_tip, public_tip)
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_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_check_blocks_in_flight_does_not_timeout(self): hash_ = CBlock().GetHash() self.networking.check_blocks_in_flight_interval = 2 self.networking.blocks_in_flight = {hash_: BlockInFlight(hash_, 0)} self.networking.check_blocks_in_flight() self.assertEqual(len(self.networking.blocks_in_flight), 1)
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_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_headers_message_known_blocks(self): cblock1 = CBlock(nNonce=1) block1 = Block(cblock1, None) cblock2 = CBlock(nNonce=2) block2 = Block(cblock2, None) self.chain.blocks = {block1.hash(): block1, block2.hash(): block2} self.networking.request_blocks = MagicMock() message = messages.msg_headers() message.headers = [cblock1.get_header(), cblock2.get_header()] self.networking.headers_message(self.public_connection1, message) self.assertFalse(self.public_connection1.send.called) self.assertFalse(self.chain.process_header.called) self.assertFalse(self.private_connection.send.called) self.assertTrue(self.networking.request_blocks.called) self.assertEqual(len(self.networking.request_blocks.call_args[0][1]), 0)
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_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 setUp(self): self.networking = MagicMock() self.executor = Executor(self.networking) self.first_block_chain_b = Block(CBlock(), BlockOrigin.public) self.first_block_chain_b.height = 1 self.first_block_chain_b.prevBlock = test_util.genesis_block self.first_block_chain_b.cached_hash = '1b' self.second_block_chain_b = Block(CBlock(), BlockOrigin.public) self.second_block_chain_b.height = 2 self.second_block_chain_b.prevBlock = self.first_block_chain_b self.second_block_chain_b.cached_hash = '2b' self.first_block_chain_a = Block(CBlock(), BlockOrigin.private) self.first_block_chain_a.height = 1 self.first_block_chain_a.prevBlock = test_util.genesis_block self.first_block_chain_a.cached_hash = '1a' self.second_block_chain_a = Block(CBlock(), BlockOrigin.private) self.second_block_chain_a.height = 2 self.second_block_chain_a.prevBlock = self.first_block_chain_a self.second_block_chain_a.cached_hash = '2a'
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_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_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_getdata_message_cblock_not_available(self): cblock = CBlock() hash_ = cblock.GetHash() block = Block(cblock, BlockOrigin.private) message = messages.msg_getdata() cInv = CInv() cInv.type = networking.inv_typemap['Block'] cInv.hash = hash_ message.inv = [cInv] self.chain.blocks = {hash_: block} self.networking.deferred_block_requests = {} self.networking.getdata_message(self.public_connection1, message) self.assertFalse(self.public_connection1.called) self.assertIn(hash_, self.networking.deferred_block_requests) self.assertIn(self.public_connection1.host[0], self.networking.deferred_block_requests[hash_])
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)
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)
def loadfile(filename): fd = os.open(filename, os.O_RDONLY) print("IMPORTING DATA FROM " + filename) buf = '' wanted = 4096 while True: if wanted > 0: if wanted < 4096: wanted = 4096 s = os.read(fd, wanted) if len(s) == 0: break buf += s wanted = 0 buflen = len(buf) startpos = string.find(buf, netmagic) startpos = 0 if startpos < 0: wanted = 8 continue sizepos = startpos + 4 blkpos = startpos + 8 if blkpos > buflen: wanted = 8 continue blksize = struct.unpack("<i", buf[sizepos:blkpos])[0] print "blkpos: ", blkpos, "blksize: ", blksize, "buflen: ", buflen if (blkpos + blksize) > buflen: wanted = 8 + blksize continue ser_blk = buf[blkpos:blkpos+blksize] buf = buf[blkpos+blksize:] f = cStringIO.StringIO(ser_blk) block = CBlock() block.deserialize(f) print block
import hashlib from bitcoin.core import b2x, lx, COIN, CMutableTxOut, CMutableTxIn, CMutableTransaction, CBlock from bitcoin.wallet import P2PKHBitcoinAddress receiver_pkh = bytes.fromhex('346753e81b93e3f1567a16f3009c7c65c768d865') print('receiver_pkh: "%s"' % receiver_pkh.hex()) receiver = P2PKHBitcoinAddress.from_bytes(receiver_pkh) tx = CMutableTransaction() tx.vin.append(CMutableTxIn()) tx.vout.append(CMutableTxOut(10, receiver.to_scriptPubKey())) print('tx: "%s"' % tx.serialize().hex()) print('txid: "%s"' % tx.GetTxid().hex()) blk = CBlock(vtx=[tx]) blk_mtr = blk.calc_merkle_root() print('block_mtr: "%s"' % blk_mtr.hex()) mmr = hashlib.sha256(b'\x00' + blk_mtr).digest() print('mmr: "%s"' % mmr.hex())
log = Log.Log(SETTINGS['log']) mempool = MemPool.MemPool(log) chaindb = ChainDb.ChainDb(SETTINGS['db'], log, mempool, NETWORKS[MY_NETWORK]) scanned = 0 failures = 0 for height in xrange(chaindb.getheight()): heightidx = ChainDb.HeightIdx() heightidx.deserialize(chaindb.height[str(height)]) blkhash = heightidx.blocks[0] ser_hash = ser_uint256(blkhash) f = cStringIO.StringIO(chaindb.blocks[ser_hash]) block = CBlock() block.deserialize(f) if not block.is_valid(): log.write("block %064x failed" % (blkhash,)) failures += 1 scanned += 1 if (scanned % 1000) == 0: log.write("Scanned height %d (%d failures)" % ( height, failures)) log.write("Scanned %d blocks (%d failures)" % (scanned, failures))
def setUp(self): self.first_block_chain_a = Block(None, BlockOrigin.private) self.first_block_chain_a.height = 1 self.first_block_chain_a.prevBlock = test_util.genesis_block self.first_block_chain_a.cached_hash = '1a' self.first_block_chain_a.cblock_header = CBlockHeader(nNonce=11) self.first_block_chain_a.cblock = CBlock(nNonce=11) self.second_block_chain_a = Block(None, BlockOrigin.private) self.second_block_chain_a.height = 2 self.second_block_chain_a.prevBlock = self.first_block_chain_a self.second_block_chain_a.cached_hash = '2a' self.second_block_chain_a.cblock_header = CBlockHeader(nNonce=21) self.second_block_chain_a.cblock = CBlock(nNonce=21) self.third_a_block_chain_a = Block(None, BlockOrigin.private) self.third_a_block_chain_a.height = 3 self.third_a_block_chain_a.prevBlock = self.second_block_chain_a self.third_a_block_chain_a.cached_hash = '3a_1' self.third_a_block_chain_a.cblock_header = CBlockHeader(nNonce=311) self.third_a_block_chain_a.cblock = CBlock(nNonce=311) self.third_b_block_chain_a = Block(None, BlockOrigin.private) self.third_b_block_chain_a.height = 3 self.third_b_block_chain_a.prevBlock = self.second_block_chain_a self.third_b_block_chain_a.cached_hash = '3a_2' self.third_b_block_chain_a.cblock_header = CBlockHeader(nNonce=312) self.third_b_block_chain_a.cblock = CBlock(nNonce=312) self.fourth_block_chain_a = Block(None, BlockOrigin.private) self.fourth_block_chain_a.height = 4 self.fourth_block_chain_a.prevBlock = self.third_a_block_chain_a self.fourth_block_chain_a.cached_hash = '4a' self.fourth_block_chain_a.cblock_header = CBlockHeader(nNonce=41) self.fourth_block_chain_a.cblock = CBlock(nNonce=41) self.first_block_chain_b = Block(None, BlockOrigin.public) self.first_block_chain_b.height = 1 self.first_block_chain_b.prevBlock = test_util.genesis_block self.first_block_chain_b.cached_hash = '1b' self.first_block_chain_b.cblock_header = CBlockHeader(nNonce=12) self.first_block_chain_b.cblock = CBlock(nNonce=12) self.second_block_chain_b = Block(None, BlockOrigin.public) self.second_block_chain_b.height = 2 self.second_block_chain_b.prevBlock = self.first_block_chain_b self.second_block_chain_b.cached_hash = '2b' self.second_block_chain_b.cblock_header = CBlockHeader(nNonce=22) self.second_block_chain_b.cblock = CBlock(nNonce=22) self.third_a_block_chain_b = Block(None, BlockOrigin.public) self.third_a_block_chain_b.height = 3 self.third_a_block_chain_b.prevBlock = self.second_block_chain_b self.third_a_block_chain_b.cached_hash = '3b_1' self.third_a_block_chain_b.cblock_header = CBlockHeader(nNonce=321) self.third_a_block_chain_b.cblock = CBlock(nNonce=321) self.third_b_block_chain_b = Block(None, BlockOrigin.public) self.third_b_block_chain_b.height = 3 self.third_b_block_chain_b.prevBlock = self.second_block_chain_b self.third_b_block_chain_b.cached_hash = '3b_2' self.third_b_block_chain_b.cblock_header = CBlockHeader(nNonce=322) self.third_b_block_chain_b.cblock = CBlock(nNonce=322) self.fourth_block_chain_b = Block(None, BlockOrigin.public) self.fourth_block_chain_b.height = 4 self.fourth_block_chain_b.prevBlock = self.third_a_block_chain_b self.fourth_block_chain_b.cached_hash = '4b' self.fourth_block_chain_b.cblock_header = CBlockHeader(nNonce=42) self.fourth_block_chain_b.cblock = CBlock(nNonce=42)
def loadfile(filename, start, end, chaindb): fd = os.open(filename, os.O_RDONLY) #self.log.write("IMPORTING DATA FROM " + filename) buf = '' wanted = 4096 count = 1 while True: if wanted > 0: if wanted < 4096: wanted = 4096 s = os.read(fd, wanted) if len(s) == 0: break buf += s wanted = 0 buflen = len(buf) startpos = string.find(buf, msg_start) if startpos < 0: wanted = 8 continue sizepos = startpos + 4 blkpos = startpos + 8 if blkpos > buflen: wanted = 8 continue blksize = struct.unpack("<i", buf[sizepos:blkpos])[0] if (blkpos + blksize) > buflen: wanted = 8 + blksize continue ser_blk = buf[blkpos:blkpos + blksize] buf = buf[blkpos + blksize:] f = cStringIO.StringIO(ser_blk) block = CBlock() block.deserialize(f) count += 1 skip_load = (count < start) if skip_load: if ((count % 10000) == 0): print "skiping: %d" % count continue if (end > -1) and (count >= end): print "Exit!" sys.exit(0) ret = chaindb.putblock(block) height = chaindb.getheight() #if count == 778: # print repr(block) heightidx = ChainDb.HeightIdx() heightidx.deserialize(chaindb.height(str(height))) blkhash = heightidx.blocks[0] skiped = False scanned = False #skip_scan = (height < skip_scan_height) #if not skip_scan: # if ret: # scanned = scan_vtx(chaindb,block) strBlkHash = hex(blkhash).replace('0x', '').replace('L', '') print "Count: %d Putblock: %s Height: %d BlockHash: %s" % ( count, ret, height, strBlkHash) print "tell: %d" % fd.tell() return count
def __init__(self, protover=PROTO_VERSION): super(msg_block, self).__init__(protover) self.block = CBlock()