def get_block(rpc, session, hash): block = session.query(core.Block).filter_by(hash=hash).first() if block is not None: return block, block.transactions try: hash_string = hash_integer_to_string(hash) block_dict = rpc.getblock(hash_string) except HTTPError: raise MissingBlockHeader(hash) kwargs = {} kwargs['version'] = int(block_dict['version']) if int(block_dict['height']) > 0: kwargs['parent_hash'] = hash_string_to_integer( block_dict['previousblockhash']) else: kwargs['parent_hash'] = 0 kwargs['merkle_hash'] = hash_string_to_integer(block_dict['merkleroot']) kwargs['time'] = datetime.utcfromtimestamp(block_dict['time']) kwargs['bits'] = hash_string_to_integer(block_dict['bits'], 4) kwargs['nonce'] = int(block_dict['nonce']) block = core.Block(**kwargs) assert block.hash == hash for tx_hash_string in block_dict['tx']: tx_hash = hash_string_to_integer(tx_hash_string) tx = session.query(core.Transaction).filter_by(hash=tx_hash).first() if tx is None: try: tx_raw = rpc.getrawtransaction(tx_hash_string) except HTTPError: raise MissingTransaction(tx_hash) tx_file = StringIO(tx_raw.decode('hex')) tx = core.Transaction.deserialize(tx_file) assert not tx_file.read() assert tx.hash == tx_hash block.transactions.append(tx) session.add(block) session.flush() return block, block.transactions
def get_block(rpc, session, hash): block = session.query(core.Block).filter_by(hash=hash).first() if block is not None: return block, block.transactions try: hash_string = hash_integer_to_string(hash) block_dict = rpc.getblock(hash_string) except HTTPError: raise MissingBlockHeader(hash) kwargs = {} kwargs['version'] = int(block_dict['version']) if int(block_dict['height']) > 0: kwargs['parent_hash'] = hash_string_to_integer(block_dict['previousblockhash']) else: kwargs['parent_hash'] = 0 kwargs['merkle_hash'] = hash_string_to_integer(block_dict['merkleroot']) kwargs['time'] = datetime.utcfromtimestamp(block_dict['time']) kwargs['bits'] = hash_string_to_integer(block_dict['bits'], 4) kwargs['nonce'] = int(block_dict['nonce']) block = core.Block(**kwargs) assert block.hash == hash for tx_hash_string in block_dict['tx']: tx_hash = hash_string_to_integer(tx_hash_string) tx = session.query(core.Transaction).filter_by(hash=tx_hash).first() if tx is None: try: tx_raw = rpc.getrawtransaction(tx_hash_string) except HTTPError: raise MissingTransaction(tx_hash) tx_file = StringIO(tx_raw.decode('hex')) tx = core.Transaction.deserialize(tx_file) assert not tx_file.read() assert tx.hash == tx_hash block.transactions.append(tx) session.add(block) session.flush() return block, block.transactions
def process_result_value(self, value, dialect): if value is None: return None file_ = StringIO(value) bitlength = deserialize_varint(file_) return self._implicit + Bits(bytes=file_.read(), length=bitlength)
genesis_hash = hash256(parameters.genesis[:80]).intdigest(), testnet = parameters.testnet, pubkey_hash_prefix = parameters.pubkey_hash_prefix, script_hash_prefix = parameters.script_hash_prefix, secret_prefix = parameters.secret_prefix) session.add(chain) session.commit() genesis_block_hash_string = rpc.getblockhash(0) genesis_block_hash = hash_string_to_integer(genesis_block_hash_string) genesis_file = StringIO(CHAIN_PARAMETERS[FLAGS.network].genesis) genesis_block = core.Block.deserialize(genesis_file) genesis_transactions = list(deserialize_list(genesis_file, core.Transaction.deserialize)) assert not genesis_file.read() for tx in genesis_transactions: genesis_block.transactions.append( session.query(core.Transaction).filter_by(hash=tx.hash).first() or tx) assert genesis_block.hash == genesis_block_hash assert genesis_block.merkle_hash == merkle(genesis_transactions) query = session.query(core.Block).filter_by(hash=genesis_block.hash) if query.count(): genesis_block = query.first() else: session.add(genesis_block) session.commit()
def process_result_value(self, value, dialect): if value is None: return None file_ = StringIO(value) bitlength = deserialize_varint(file_) return self._implicit + Bits(bytes=file_.read(), length=bitlength)
testnet=parameters.testnet, pubkey_hash_prefix=parameters.pubkey_hash_prefix, script_hash_prefix=parameters.script_hash_prefix, secret_prefix=parameters.secret_prefix) session.add(chain) session.commit() genesis_block_hash_string = rpc.getblockhash(0) genesis_block_hash = hash_string_to_integer(genesis_block_hash_string) genesis_file = StringIO(CHAIN_PARAMETERS[FLAGS.network].genesis) genesis_block = core.Block.deserialize(genesis_file) genesis_transactions = list( deserialize_list(genesis_file, core.Transaction.deserialize)) assert not genesis_file.read() for tx in genesis_transactions: genesis_block.transactions.append( session.query(core.Transaction).filter_by(hash=tx.hash).first() or tx) assert genesis_block.hash == genesis_block_hash assert genesis_block.merkle_hash == merkle(genesis_transactions) query = session.query(core.Block).filter_by(hash=genesis_block.hash) if query.count(): genesis_block = query.first() else: session.add(genesis_block) session.commit()