def test_getsetnextblock(self): genesis = CBBlock.genesis() nb = self.mq.get_next_block(genesis.hash) self.assertIsNot(nb, None) self.mq.set_next_block_hash(genesis.hash, nb.hash) genesis2 = self.mq.get_block_by_hash(genesis.hash) self.assertEquals(nb.hash, genesis2.hash_next_block)
def test_list_tx_by_block(self): genesis = CBBlock.genesis() nb = self.mq.get_next_block(genesis.hash) self.assertIsNot(nb, None) self.assertEquals(nb.height, 1) txs = self.mq.list_tx_by_block(nb.hash) self.assertTrue(len(txs) > 0) tx = txs[0]
def test_snbhah(self): genesis = CBBlock.genesis() nb = self.mq.get_next_block(genesis.hash) self.assertIsNot(nb, None) self.assertEquals(nb.height, 1) self.mq.set_next_block_hash_and_height(genesis.hash, nb.hash, 1) genesis2 = self.mq.get_block_by_hash(genesis.hash) self.assertEquals(nb.hash, genesis2.hash_next_block) block1 = self.mq.get_block_by_hash(nb.hash) self.assertEquals(block1.height, 1)
# so they don't load an incompatible version of openssl which causes # random segfaults with mysql procedures # mysql module is not used here but must be loaded first on some systems from mysql import connector from cryptobi.toolbox.system.CBConfig import CBConfig from cryptobi.model.blockchains.CBBlock import CBBlock from cryptobi.model.blockchains.CBAddress import CBAddress from cryptobi.model.blockchains.CBTx import CBTxOutAddress from cryptobi.model.graph.CBAGNode import CBAGNode from cryptobi.crypto.CBUtil import CBUtil from cryptobi.db.dao.CBDAO import CBDAO config = CBConfig.get_config() dao = CBDAO.get_DAO() current_block = CBBlock.genesis() height = 0 def map_input_addresses(inputs): """ Map inputs to origin TX outputs. """ ret = [] for input in inputs: if CBUtil.safe_hash(input.txid) == bytes.fromhex("00" * 32): ix = CBTxOutAddress(input.table_seq, input.n_vout, CBUtil.safe_hash(input.txid), CBAddress.COINBASE_ADDRESS, -1, -1) ret.append(ix) else:
def __get_block_by(self, phash, column) -> CBBlock: """ Refactored get block funcs by a certain column. """ ret = None cursor = self.cnx.cursor() db = self.config.get_conf("db.db") sql = "SELECT table_seq, n_version, hash_this_block, hash_prev_block, hash_merkle_root, hash_next_block, n_time, n_bits, nonce, block_height FROM {}.cb_blockchain WHERE {} = %s".format( db, column) try: cursor.execute(sql, (phash, )) except Exception as e: print(e) cursor.close() return None for table_seq, n_version, hash_this_block, hash_prev_block, hash_merkle_root, hash_next_block, n_time, n_bits, nonce, block_height in cursor: cbb = CBBlock() cbb.table_seq = table_seq cbb.bits = n_bits cbb.hash = hash_this_block cbb.hash_next_block = hash_next_block cbb.hash_prev_block = hash_prev_block cbb.height = block_height cbb.merkle_root = hash_merkle_root cbb.n_version = n_version cbb.nonce = nonce cbb.ntime = n_time ret = cbb cursor.close() return ret
def get_latest_block(self): ret = None cursor = self.cnx.cursor() db = self.config.get_conf("db.db") sql = "SELECT table_seq, n_version, hash_this_block, hash_prev_block, hash_merkle_root, hash_next_block, n_time, n_bits, nonce, block_height FROM {}.cb_blockchain ORDER BY table_seq DESC LIMIT 1".format( db) try: cursor.execute(sql) except Exception as e: print(e) cursor.close() return None for table_seq, n_version, hash_this_block, hash_prev_block, hash_merkle_root, hash_next_block, n_time, n_bits, nonce, block_height in cursor: cbb = CBBlock() cbb.table_seq = table_seq cbb.bits = n_bits cbb.hash = hash_this_block cbb.hash_next_block = hash_next_block cbb.hash_prev_block = hash_prev_block cbb.height = block_height cbb.merkle_root = hash_merkle_root cbb.n_version = n_version cbb.nonce = nonce cbb.ntime = n_time ret = cbb cursor.close() return ret
from cryptobi.toolbox.system.CBConfig import CBConfig from cryptobi.model.blockchains.CBBlock import CBBlock from cryptobi.db.dao.CBDAO import CBDAO import sys config = CBConfig.get_config() dao_a = CBDAO() dao = dao_a.get_DAO() addr = config.get_conf("listargs") if not len(addr) > 0: config.log_error("Invalid block hash.") sys.exit(1) block_hash = bytes.fromhex(addr) if block_hash == CBBlock.genesis().hash: print(CBBlock()) sys.exit(1) block = dao.get_block_by_hash(block_hash) if block: vtx = dao.list_tx_by_block(block.hash) block.vtx = vtx print(block)
def test_getblockbyhash(self): genesis = CBBlock.genesis() nb = self.mq.get_block_by_hash(genesis.hash) self.assertEquals(nb.hash, genesis.hash)