def latest_tx(): json_transaction = list() for tx in db.get_all_unconfirmed_tx(blockchain.wallet.address): txins = tx.txins txouts = tx.txouts from_addr = list() to_addr = list() amount = 0 for txin in txins: if txin.prev_tx_out_idx != -1: pubkey_hash = Wallet.get_address(txin.pubkey) if pubkey_hash not in from_addr: from_addr.append(pubkey_hash) for txout in txouts: value = txout.value script_pub_key = txout.scriptPubKey if len(script_pub_key) == 5: recv_addr = get_address_from_ripemd160(script_pub_key[2]) to_addr.append({'receiver': recv_addr, 'value': value}) new_tx = { 'txid': tx.txid, 'senders': from_addr, 'receivers': to_addr, 'amount': amount, 'timestamp': tx.timestamp } json_transaction.append(new_tx) response = {'latest_tx': json_transaction} return json.dumps(response), 200
def unconfirm_tx_info(): txid = request.args.get('txid') for tx in db.get_all_unconfirmed_tx(blockchain.wallet.address): if tx.txid == txid: return json.dumps(tx.json_output()), 200 return 'not exist!', 200
def get_balance_by_db(self, from_addr): """ 获取from_addr可以用于交易的TxOutput(未使用过的),读取交易池和区块链的本地副本,避免被加锁 :param from_addr: <str>发送方钱包地址 :return: <int> """ unspent_txout_list = list() spent_txout_list = list() balance = 0 # Step1:遍历交易池中已经发生过的交易(未打包进区块,未确认) # 备注:要从最新的交易开始遍历!!!!! current_transactions = db.get_all_unconfirmed_tx(self.wallet.address) current_transactions = sorted(current_transactions, key=lambda x: x.timestamp, reverse=False) for i in range(len(current_transactions)): unconfirmed_tx = current_transactions[len(current_transactions) - 1 - i] txid = unconfirmed_tx.txid # 遍历当前交易下所有的TxInput if not unconfirmed_tx.is_coinbase(): # print 'txid:', txid # 记录当前tx下被from_addr被使用过的上一次交易的输出,即记录txid和out_idx for txin in unconfirmed_tx.txins: if txin.can_unlock_txoutput_with(from_addr): spent_txid = txin.prev_txid spent_tx_out_idx = txin.prev_tx_out_idx spent_txout_list.append((spent_txid, spent_tx_out_idx)) # 遍历交易下所有的未使用过的TxOutput for out_idx in range(len(unconfirmed_tx.txouts)): txout = unconfirmed_tx.txouts[out_idx] if not (txid, out_idx) in spent_txout_list: if txout.can_be_unlocked_with(from_addr): unspent_txout_list.append((txid, out_idx, txout)) # -------------------------------------------------- # Step2:获取from_addr下可以未使用过的TxOutput(打包在区块,已确认) block_height = db.get_block_height(self.wallet.address) for i in range(block_height): block = db.get_block_data_by_index(self.wallet.address, block_height - 1 - i) # 1.获取区块下的所有的交易 transactions = block.get_transactions() # 备注:要从最新的交易开始遍历!!!!! for k in range(len(transactions)): tx = transactions[len(transactions) - 1 - k] if not self.verify_transaction(tx): # 校验交易是否有效 continue txid = tx.txid # 当前交易的id # 2.遍历某个交易下所有的TxInput if not tx.is_coinbase(): # 记录当前tx下被from_addr被使用过的上一次交易的输出,即记录txid和out_idx for txin in tx.txins: if txin.can_unlock_txoutput_with(from_addr): spent_txid = txin.prev_txid spent_tx_out_idx = txin.prev_tx_out_idx spent_txout_list.append( (spent_txid, spent_tx_out_idx)) # 3.遍历某个交易下所有的未使用过的TxOutput for out_idx in range(len(tx.txouts)): txout = tx.txouts[out_idx] if not (txid, out_idx) in spent_txout_list: if txout.can_be_unlocked_with(from_addr): unspent_txout_list.append((txid, out_idx, txout)) # Step2:计算这些未使用过的TxOutput输出之和 for txid, out_idx, txout in unspent_txout_list: balance += txout.value return balance