def test_convert_amount(): """double convert gives same amount""" random.seed('Reproducible test') for i in range(10): amount = random.random() * 100 f8_amount = str('{:.8f}'.format(Decimal(amount))) int_amount = Transaction.f8_to_int(f8_amount) assert f8_amount == Transaction.int_to_f8(int_amount)
def insert_legacy_object(tx_list): test_legacy = sqlite3.connect('file:ledger_legacy?mode=memory', uri=True, timeout=1) create(test_legacy, SQL_CREATE_LEGACY) for tx in tx_list: # Creates instance from tuple data, copy to inner properties (converts to binary as well) tx = Transaction.from_legacy(tx) # Then export again test_legacy.execute("INSERT INTO transactions VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", tx.to_tuple()) return test_legacy
def balance_new2(db, address: str): q1 = db.execute( "SELECT (SELECT sum(iamount + ireward) FROM transactions WHERE recipient = ? ) " "- (SELECT sum(iamount + ifee) FROM transactions WHERE address = ?)", (address, address), ) r1 = q1.fetchone() balance = r1[0] if r1[0] else 0 return Transaction.int_to_f8(balance)
def insert_new(tx_list): # Using in-ram DB to avoid disk I/O artefacts test_new = sqlite3.connect('file:ledger_new?mode=memory', uri=True, timeout=1) create(test_new, SQL_CREATE) for tx in tx_list: # Creates instance from tuple data, copy to inner properties tx = Transaction.from_legacy(tx) # Then converts to bin and into bin tuple test_new.execute("INSERT INTO transactions VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", tx.to_bin_tuple(sqlite_encode=True)) return test_new
def balance_new(db, address: str): q1 = db.execute( "SELECT sum(iamount + ireward) FROM transactions WHERE recipient = ? ", (address,), ) r1 = q1.fetchone() credit = r1[0] if r1[0] else 0 q2 = db.execute( "SELECT sum(iamount + ifee) FROM transactions WHERE address = ? ", (address,), ) r2 = q2.fetchone() debit = r2[0] if r2[0] else 0 return Transaction.int_to_f8(credit-debit)
def from_legacy_block_data(cls, block_data: List[list], first_level_checks: bool = False, last_block_timestamp=0): blocks = [] for legacy_block in block_data: tx_list = [ Transaction.from_legacy_params(timestamp=tx[0], address=tx[1], recipient=tx[2], amount=tx[3], signature=tx[4], public_key=tx[5], operation=tx[6], openfield=tx[7]) for tx in legacy_block ] block = Block(tx_list, check_txs=first_level_checks, last_block_timestamp=last_block_timestamp) blocks.append(block) return cls(blocks, first_level_checks=first_level_checks)
def test_tx_signature(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block sleep(1) r = client.command(command="blocklast") if verbose: print(f"blocklast returns {r}") tx = Transaction.from_legacy_params(r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11]) buffer = tx.to_buffer_for_signing() db_signature_enc = r[5] db_public_key_b64encoded = r[ 6] # For rsa, once decoded, this gives a properly formatted ---begin.... pubkey db_address = r[2] signed = True try: SignerFactory.verify_bis_signature(db_signature_enc, db_public_key_b64encoded, buffer, db_address) except Exception: signed = False assert signed
if __name__ == "__main__": try: remove('tx_dataset.json') except: pass try: remove('tx_tuple_dataset.json') except: pass with sqlite3.connect('../../../Bismuth-temp/static/ledger.db', timeout=1) as ledger: # TODO: use a default path and give custom db path to command line for more flexible use depending on context ledger.text_factory = str res = ledger.execute( "select * from transactions where block_height > 700000 limit 100000" ) with open("tx_dataset.json", "w") as fp: for row in res: tx = Transaction.from_legacy(row) fp.write(tx.to_json() + "\n") res = ledger.execute( "select * from transactions where block_height > 700000 limit 100000" ) with open("tx_tuple_dataset.json", "w") as fp: for row in res: tx = Transaction.from_legacy(row) fp.write(json.dumps(tx.to_tuple()) + "\n")
def test_create_transaction(): """Can create a Transaction object""" tx = Transaction()
import sys from decimal import Decimal, getcontext, ROUND_HALF_EVEN sys.path.append('../') from bismuthcore.transaction import Transaction getcontext().rounding = ROUND_HALF_EVEN # A Test transaction TX = Transaction.from_legacy_params( block_height=1, timestamp=0.01, address='ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFab', recipient='01234567890123456789012345678901234567890123456789012345', amount='0.01000000', signature='0ABCDEF0', public_key='00112233', block_hash='0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567', fee='0.01000000', reward='0.00000000', operation='TEST', openfield='test_openfield') def test_create_transaction(): """Can create a Transaction object""" tx = Transaction() def test_convert_amount(): """double convert gives same amount"""