class BitcoinRpc(): bitcoin_proxy = None def __init__(self): self.bitcoin_proxy = Proxy() def get_new_address(self): return self.bitcoin_proxy.getnewaddress() def get_balance(self): return self.bitcoin_proxy.getbalance() def get_transaction(self, txid): return self.bitcoin_proxy.gettransaction(lx(txid))
def _sync_block(self): p = Proxy() blockcount = p.getblockcount() print("sync blockcount = " + str(blockcount)) with closing(sqlite3.connect(self.dbname)) as connection: cursor = connection.cursor() checkedcount = self._get_checked_count(cursor) print("sync checkedcount = " + str(checkedcount)) if checkedcount < CHECKPOINT: checkedcount = CHECKPOINT lastblock = blockcount - CONFIRMATIONS if lastblock <= 0: return for i in range(checkedcount + 1, blockcount - CONFIRMATIONS): if self.stopped: break print("sync blockheight = " + str(i)) block = p.getblockbynumber(i) if block is None: continue if BLOCK_KEY_TX not in block.keys(): continue for transactionid in block[BLOCK_KEY_TX]: transaction = p.gettransaction(transactionid) if transaction is None: continue if TRANSACTION_KEY_AMOUNT not in transaction.keys(): continue amount = transaction[TRANSACTION_KEY_AMOUNT] if amount <= 0: print("sync amount div") continue amount = CWalletSyncher._str_round_down8(amount) print("sync amount = " + amount) if TRANSACTION_KEY_DETAILS not in transaction.keys(): print("sync dont have details!!") continue print("sync found receive!!") details = transaction[TRANSACTION_KEY_DETAILS] for detail in details: category = detail[TRANSACTION_KEY_CATEGORY] if (category != TRANSACTION_VALUE_CATEGORY_RECEIVE): continue address = detail[TRANSACTION_KEY_ADDRESS] print("sync receive address = " + address) with self.dblock: row = self.dbaccessor.get_user_by_address( cursor, address) if row is not None: height = row[ walletdb.WalletNum.LASTSYNCBLOCK.value] print("sync receive username = "******" balance = " + row[walletdb.WalletNum.BALANCE.value] + " height = " + str(row[walletdb.WalletNum.LASTSYNCBLOCK. value])) src_balance = CWalletSyncher._round_down8( str(row[walletdb.WalletNum.BALANCE.value])) dst_balance = src_balance + \ CWalletSyncher._round_down8(amount) if dst_balance < CWalletSyncher._round_down8( "0.0"): dst_balance = CWalletSyncher._round_down8( "0.0") print("sync update balance = " + str(dst_balance) + " amount = " + amount) if not self.dbaccessor.update_balance_with_blockheight( cursor, row[walletdb.WalletNum.ID.value], dst_balance, i): print("sync update faild.") return self.logger.debug( "username="******" before=" + CWalletSyncher._str_round_down8( src_balance) + " after=" + CWalletSyncher._str_round_down8( dst_balance) + " height=" + str(i)) break pass self._update_checked_count(cursor, i) connection.commit()