def update(tx): if TransactionUtils.isCFTransation(tx): CoinSqlite3().exec_sql('Update TransactionInfo set `version`=?,`lock_time`=?,`parentBlockId`=?,`state`=?, `type` = ? ,original_hash = ?, target_amount = ?, pubkey = ?, end_time = ?, pre_hash = ?, lack_amount = ? , cert = ? where hash = ?', tx.version, tx.lock_time, tx.getBlockHash(), tx.state, 2, tx.cf_header.original_hash, tx.cf_header.target_amount, tx.cf_header.pubkey, tx.cf_header.end_time, tx.cf_header.pre_hash, tx.cf_header.lack_amount, tx.cf_header.cert, tx.hash()) else: CoinSqlite3().exec_sql('Update TransactionInfo set `version`=?,`lock_time`=?,`parentBlockId`=?, `state`=?, `type` = ? where hash = ?', tx.version, tx.lock_time, tx.getBlockHash(), tx.state, 1, tx.hash()) for index, txIn in enumerate(tx.txs_in): TransactionInDao.save(txIn, tx, index) for index, txOut in enumerate(tx.txs_out): TransactionOutDao.save(txOut, tx, index)
def insert(tx): if TransactionUtils.isCFTransation(tx): CoinSqlite3().exec_sql('INSERT INTO TransactionInfo(hash, version,lock_time,parentBlockId,state,type,original_hash, target_amount, pubkey, end_time, pre_hash, lack_amount, cert) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)', tx.hash(), tx.version, tx.lock_time, tx.getBlockHash(), tx.state, 2, tx.cf_header.original_hash, tx.cf_header.target_amount, tx.cf_header.pubkey, tx.cf_header.end_time, tx.cf_header.pre_hash, tx.cf_header.lack_amount, tx.cf_header.cert) else: CoinSqlite3().exec_sql('INSERT INTO TransactionInfo(hash, version,lock_time,parentBlockId,state,type) VALUES (?,?,?,?,?,?)', tx.hash(), tx.version, tx.lock_time, tx.getBlockHash(), tx.state, 1) for index, txIn in enumerate(tx.txs_in): TransactionInDao.save(txIn, tx, index) for index, txOut in enumerate(tx.txs_out): TransactionOutDao.save(txOut, tx, index)
def searchMySecrets(): c = CoinSqlite3()._exec_sql('Select * from SecretKeyInfo Where privateKey != \'\'') secrets = [] for tmp in c.fetchall(): secret = SecretKey(tmp[1], tmp[3], tmp[2], tmp[4], tmp[0], tmp[5]) secrets.append(secret) return secrets
def search(): c = CoinSqlite3()._exec_sql('Select * from SecretKeyInfo') secrets = [] for tmp in c.fetchall(): secret = SecretKey(tmp[1], tmp[3], tmp[2], tmp[4], tmp[0]) secrets.append(secret) return secrets
def searchCertByPubAddr(pubkey_addr): c = CoinSqlite3()._exec_sql('Select cert from SecretKeyInfo where pubicAddress = ?', pubkey_addr) tmp = c.fetchone() if tmp == None: return '' else: return '''-----BEGIN CERTIFICATE-----
def isPreCFlinked(cf): if cf.cf_header.pre_hash == '' or cf.cf_header.pre_hash == Constants.ZERO_HASH: return False else: c = CoinSqlite3()._exec_sql('Select * from TransactionInfo where pre_hash = ?', cf.cf_header.pre_hash) s = c.fetchone() return s != None
def searchParentBlockHash(tx): c = CoinSqlite3()._exec_sql('Select parentBlockId from TransactionInfo where hash = ?', tx.hash()) tmp = c.fetchone() if tmp == None: return None else: parentBlockId = tmp[0] return parentBlockId
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 searchParentTransactionHash(txout): c = CoinSqlite3()._exec_sql( 'Select parentTxId from TransactionInfoOut where `id` = ?', txout.uid) tmp = c.fetchone() if tmp == None: return None else: parentTxId = tmp[0] return parentTxId
def searchAllCFDict(): c = CoinSqlite3()._exec_sql( 'Select * from TransactionInfo where type =2 and target_amount = lack_amount and parentBlockId != \'\'' ) src_cfs = __getSearchResult(c) allCFDict = {} for src_cf in src_cfs: cfs = searchCFTcsByOriginal_hash(src_cf.hash()) cfs.insert(0, src_cf) allCFDict[src_cf.hash()] = cfs return allCFDict
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 searchUnlinkedBlock(): c = CoinSqlite3()._exec_sql('Select previous_block_hash from BlockInfo') preBlockHashs = [] for tmp in c.fetchall(): preBlockHashs.append(tmp[0]) blocks = searchAll() unlinkedBlocks = [] for block in blocks: if block.hash() not in preBlockHashs: unlinkedBlocks.append(block) return unlinkedBlocks
def save(txOut, tx, index): deleteOld(tx, index) pubicAddress = txOut.address() end_time = 0 if TransactionUtils.isCFTransation(tx): if 0 == index: end_time = tx.cf_header.end_time CoinSqlite3().exec_sql( 'INSERT INTO TransactionInfoOut(coin_value, script, parentBlockId, parentTxId, state, `index`, pubicAddress, isToMe, usedState, end_time, isMyTx) VALUES (?,?,?,?,?,?,?,?,?,?,?)', txOut.coin_value, txOut.script, tx.getBlockHash(), tx.hash(), txOut.state, index, pubicAddress, SecretKeyDao.isMypubicAddress(pubicAddress), 0, end_time, SecretKeyDao.isMypubicAddress(txOut.address()))
def searchByIndex(parentTxId, index): c = CoinSqlite3()._exec_sql( 'Select * from TransactionInfoOut where parentTxId = ? And `index` = ?', parentTxId, index) return __getSearchResultSingle(c)
def search(parentBlockId, parentTxId): c = CoinSqlite3()._exec_sql( 'Select * from TransactionInfoOut where parentTxId = ?', parentTxId) return __getSearchResult(c)
def searchById(id): c = CoinSqlite3()._exec_sql( 'Select * from TransactionInfoOut where id = ? ', id) return __getSearchResultSingle(c)
def searchSpendById(id): c = CoinSqlite3()._exec_sql( 'Select * from TransactionInfoOut where id = ? ', id) tmp = c.fetchone() spend = Spendable(tmp[1], tmp[2], tmp[4], tmp[6]) return spend
def searchMyUnUsedNomalTxOuts(): #不包含未用的众筹交易 c = CoinSqlite3()._exec_sql( 'Select * from TransactionInfoOut where isMyTx = 1 and usedState = 0') return __getSearchResult(c)
def searchMyTotalTxOuts(): #包含未成功的众筹交易 c = CoinSqlite3()._exec_sql( 'Select * from TransactionInfoOut where end_time < ? and isToMe = 1 and parentBlockId != \'\'', int(time.time())) return __getSearchResult(c)
def insert(secret): CoinSqlite3().exec_sql('INSERT INTO SecretKeyInfo(publicKey, privateKey,pubicAddress, cert, sec_num) VALUES (?,?,?,?,?)', str(secret.publicKey), str(secret.privateKey), str(secret.pubicAddress), str(secret.cert), int(secret.sec_num))
def searchMyTxOuts(): c = CoinSqlite3()._exec_sql( 'Select * from TransactionInfoOut where isMyTx = 1') return __getSearchResult(c)
def updateEndTimeToZero(tx): CoinSqlite3().exec_sql( 'Update TransactionInfoOut set `end_time`= ? where `parentTxId` = ? and `index` = 0', int(time.time()), tx.hash())
def searchAll(): c = CoinSqlite3()._exec_sql('Select * from TransactionInfoOut') return __getSearchResult(c)
def updateAllLinkedCFTransationOut(hash): CoinSqlite3().exec_sql( 'Update TransactionInfoOut set `usedState` = 1 where `parentTxId` = ? and `index` = 0', hash)
def deleteOld(tx, index): CoinSqlite3().exec_sql( 'Delete from TransactionInfoOut where parentTxId = ? And `Index` = ?', tx.hash(), index)
def setStateUsed(hash, index): CoinSqlite3().exec_sql( 'Update TransactionInfoOut set `usedState`=1 where `parentTxId` = ? and `index` = ?', hash, index)
def updateState(tx_out, tx_out_id): CoinSqlite3().exec_sql( 'Update TransactionInfoOut set `usedState`=? where `id` = ?', tx_out.usedState, tx_out_id)
def isExist(secret): tmp = CoinSqlite3()._exec_sql('Select * from SecretKeyInfo where publicKey = ?', str(secret.publicKey)) s = tmp.fetchone() return s != None