def __update(blockChain, preHeight): CoinSqlite3().exec_sql( 'Update BlockInfo set `version`=?,`previous_block_hash`=?,`merkle_root`=?,`timestamp`=?,`difficulty`=?,`nonce`=?,`state`=?,`height`=? where hash = ?', blockChain.version, blockChain.previous_block_hash, blockChain.merkle_root, blockChain.timestamp, blockChain.difficulty, blockChain.nonce, blockChain.state, preHeight + 1, blockChain.hash()) for tx in blockChain.txs: TransactionDao.save(tx)
def __insert(blockChain, preHeight): CoinSqlite3().exec_sql( 'INSERT INTO BlockInfo(hash, version,previous_block_hash,merkle_root,timestamp,difficulty,nonce,state,height) VALUES (?,?,?,?,?,?,?,?,?)', blockChain.hash(), blockChain.version, blockChain.previous_block_hash, blockChain.merkle_root, blockChain.timestamp, blockChain.difficulty, blockChain.nonce, blockChain.state, preHeight + 1) for tx in blockChain.txs: TransactionDao.save(tx)
def test(): tx = createNewBitcoinTx([['13DaZ9nfmJLfzU6oBnD2sdCiDmf3M5fmLx', 12345]]) tx = TransactionDao.searchByHash(tx.hash()) tx2 = createNormalBitCoinTx([tx.txs_out[0].uid], [['1693NYwCPZYAdF1pYdVfrfCR6c9acpNGQd', 1230], ['1693NYwCPZYAdF1pYdVfrfCR6c9acpNGQd', 1230], ['1693NYwCPZYAdF1pYdVfrfCR6c9acpNGQd', 10]]) tx2 = TransactionDao.searchByHash(tx2.hash()) cf = createNewCFBitCoinTx(1000, '1DXNcbPEavwHQHtgPrjhkbG62f9SZBXp4v', 2 * int(time.time()), [tx2.txs_out[2].uid]) cf2 = createNormalCFBitCoinTx([tx2.txs_out[0].uid], cf.hash(), 500, [[]], '1693NYwCPZYAdF1pYdVfrfCR6c9acpNGQd') createNormalCFBitCoinTx([tx2.txs_out[1].uid], cf2.hash(), 500, [[]], '1693NYwCPZYAdF1pYdVfrfCR6c9acpNGQd')
def total_out(self): total = sum(tx_out.coin_value for tx_out in self.txs_out) if TransactionUtils.isCFTransation(self): if self.cf_header.lack_amount <= 0: pre_tx = TransactionDao.searchByHash(self.cf_header.pre_hash) total = total - self.txs_out[0].coin_value + pre_tx.cf_header.lack_amount - self.cf_header.lack_amount return total
def createNormalCFTransaction(pre_out_ids, pre_cf_hash, spendValue, otherPublicAddrToValueDict, refund_addr): tx_ins = __get_tx_ins(pre_out_ids) pre_cf = TransactionDao.searchByHash(pre_cf_hash) cf_header = pre_cf.cf_header cf_header.lack_amount = cf_header.lack_amount - spendValue cf_header.pre_hash = pre_cf_hash if cf_header.original_hash == '' or cf_header.original_hash == Constants.ZERO_HASH: cf_header.original_hash = pre_cf_hash # cfscript = b'' if cf_header.lack_amount <= 0: #CF suceess outValue = cf_header.target_amount cfscript = standard_tx_out_script(cf_header.pubkey) # elif cf_header.lack_amount == cf_header.target_amount:#CF start # outValue = 0 # cfscript = ScriptPayToAddress(refund_addr).script() else: #CF ing outValue = spendValue cfscript = standard_tx_out_script(refund_addr) #outValue = cf_header.target_amount if cf_header.lack_amount == 0 else spendValue cfout = TransactionOut(outValue, cfscript, 0, 0, cf_header.end_time) tx_outs = __get_tx_outs(otherPublicAddrToValueDict) tx_outs.insert(0, cfout) cf = TransactionCF(cf_header, Constants.VERSION, tx_ins, tx_outs, Constants.LOCK_TIME, None, 0) if verify(cf): insert(cf) return cf
def get_my_txs(): txs = TransactionDao.searchAll() # for i in range(5): # txs.append(WTransaction()) wtxs = [] for tx in txs: wtxs.append(WTransaction(tx)) return wtxs
def getTxinPublicAddressByPre(txin): if txin.is_coinbase(): return "(coinbase)" pre_tx = TransactionDao.searchByHash(txin.previous_hash) if pre_tx != None: pre_txout = pre_tx.txs_out[txin.previous_index] return pre_txout.address() else: return None
def searchParentBlock(tx): blockHash = None if isinstance(tx, Transaction): blockHash = TransactionDao.searchParentBlockHash(tx) elif isinstance(tx, TransactionOut): blockHash = TransactionOutDao.searchParentBlockHash(tx) if blockHash == None: return None else: return BlockchainDao.search(blockHash)
def isCFTransationOut(txout): #index不为0 肯定不是众筹 if txout.index != 0: return False tx_hash = TransactionOutDao.searchParentTransactionHash(txout) tx = TransactionDao.searchByHash(tx_hash) if tx == None: return False else: return isCFTransation(tx)
def search(hash): c = CoinSqlite3()._exec_sql('Select * from BlockInfo where hash = ?', hash) tmp = c.fetchone() if tmp != None: txs = TransactionDao.search(hash) sort_txs = __sort_txs(txs) block = Block(tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7], sort_txs, tmp[8], tmp[9], tmp[10], tmp[0]) for tx in txs: tx.block = block return block
def searchAll(): c = CoinSqlite3()._exec_sql('Select * from BlockInfo') blocks = [] for tmp in c.fetchall(): txs = TransactionDao.search(tmp[1]) sort_txs = __sort_txs(txs) block = Block(tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7], sort_txs, tmp[8], tmp[9], tmp[10], tmp[0]) for tx in txs: tx.block = block blocks.append(block) return blocks
def get_all_transactions(): try: orders = TransactionDao.get_transactions() if len(orders): return MessageService.generate_success_message('', orders) else: return MessageService.generate_custom_message( 'No orders were found', []) except Exception as e: return MessageService.generate_internal_server_error(e)
def get_transactions_by_game_id(game_id): try: orders = TransactionDao.retrieve_transactions_by_game_id(game_id) if len(orders): return MessageService.generate_success_message( '', process_transactions_projection()) else: return MessageService.generate_custom_message( 'No orders were found', []) except Exception as e: return MessageService.generate_internal_server_error(e)
def get_transaction_by_order_number(order_number): try: order = TransactionDao.retrieve_transaction_by_order_number( order_number) if order: return MessageService.generate_success_message( '', order.to_dictionary()) else: return MessageService.generate_custom_message( 'No order with that order number could be found', {}) except Exception as e: return MessageService.generate_internal_server_error(e)
def get_transactions_by_user(token): try: user_data = AuthService.decode_token(token) transactions = TransactionDao.retrieve_transactions_by_user( user_data['id']) if not len(transactions): return MessageService.generate_custom_message( 'No orders were found', []) return MessageService.generate_success_message('', transactions) except Exception as e: return MessageService.generate_internal_server_error(e)
def set_details_out_txs(tx_outs): for tx_out in tx_outs: tx_hash = TransactionOutDao.searchParentTransactionHash(tx_out) tx = TransactionDao.searchByHash(tx_hash) in_addr = [] for tx_in in tx.txs_in: in_addr.append(TransactionUtils.getTxinPublicAddressByPre(tx_in)) tx_out.in_addr = in_addr if TransactionUtils.isCFTransationOut(tx_out): txout_type = '众筹交易' else: txout_type = '普通交易' tx_out.type = txout_type return tx_outs
def add_order(payload): try: if check_fields_existance_in_payload(payload, 'user_id', 'game_id', 'total'): order = TransactionDao.create_order(payload) if order: return MessageService.generate_success_message('success', {}) else: return MessageService.generate_custom_message( 'The order could not be created', 500, {}) else: return MessageService.missing_fields_request except Exception as e: return MessageService.generate_internal_server_error(e)
def modify_transaction_status(payload): if not check_fields_existance_in_payload(payload, 'status', 'order_number'): return MessageService.missing_fields_request try: order = TransactionDao.update_transaction(payload) if order: return MessageService.generate_success_message( '', order.to_dictionary()) else: return MessageService.generate_custom_message( 'the order could not be updated', {}) except Exception as e: return MessageService.generate_internal_server_error(e)
def __init__(self, version, txs_in, txs_out, lock_time=0, unspents=None, state = 0, uid = 0): """ The part of a Tx that specifies where the Bitcoin comes from. """ self.uid = uid self.version = version self.txs_in = txs_in self.txs_out = txs_out self.lock_time = lock_time self.state = state if None == unspents: unspents = TransactionDao.unspents_from_db(txs_in) self.unspents = unspents or [] for tx_in in self.txs_in: assert type(tx_in) == self.TransactionIn for tx_out in self.txs_out: assert type(tx_out) == self.TransactionOut
def insert(tx): if TransactionDao.isExist(tx): return if isCFTransation(tx): if TransactionDao.isPreCFlinked(tx): return if tx.cf_header.lack_amount <= 0: tx.cf_header.end_time = int(time.time()) updatePreOutState(tx) # 保存新交易 TransactionDao.save(tx) # 如果是众筹成功 刷新所有众筹交易的状态为不可用 if isCFTransation(tx): if tx.cf_header.lack_amount <= 0: TransactionDao.updateAllLinkedCFTransationOut(tx) TransactionOutDao.updateEndTimeToZero(tx) #把众筹初始发起的tx usedstate设置为1 TransactionDao.updateFirstCFState(tx)
def get_tx(tx_id): tx = TransactionDao.searchByHash(tx_id) wtx = WTransaction(tx) # wtx.get_detail() return wtx
def __getSearchResultSingle(c): return TransactionDao.__getSearchResultSingle(c)