def parse_hex(unsigned_tx_hex): tx = bitcoin.rpc('decoderawtransaction', [unsigned_tx_hex]) source, destination, btc_amount, fee, data = blocks.get_tx_info(tx) cursor = db.cursor() tx_hash = hashlib.sha256(chr(tx_index).encode('utf-8')).hexdigest() global tx_index block_index = config.BURN_START + tx_index block_hash = hashlib.sha512(chr(block_index).encode('utf-8')).hexdigest() block_time = block_index * 10000000 cursor.execute( '''INSERT INTO blocks( block_index, block_hash, block_time) VALUES(?,?,?)''', (block_index, block_hash, block_time)) cursor.execute( '''INSERT INTO transactions( tx_index, tx_hash, block_index, block_time, source, destination, btc_amount, fee, data) VALUES(?,?,?,?,?,?,?,?,?)''', (tx_index, tx_hash, block_index, tx_index, source, destination, btc_amount, fee, data)) cursor.execute( '''SELECT * FROM transactions \ WHERE tx_index=?''', (tx_index, )) tx = cursor.fetchall()[0] blocks.parse_tx(db, tx) # After parsing every transaction, check that the credits, debits sum properly. cursor.execute('''SELECT * FROM balances''') for balance in cursor.fetchall(): amount = 0 cursor.execute( '''SELECT * FROM debits \ WHERE (address = ? AND asset = ?)''', (balance['address'], balance['asset'])) for debit in cursor.fetchall(): amount -= debit['amount'] cursor.execute( '''SELECT * FROM credits \ WHERE (address = ? AND asset = ?)''', (balance['address'], balance['asset'])) for credit in cursor.fetchall(): amount += credit['amount'] assert amount == balance['amount'] tx_index += 1 cursor.close()
def parse_hex (unsigned_tx_hex): tx = bitcoin.rpc('decoderawtransaction', [unsigned_tx_hex]) source, destination, btc_amount, fee, data = blocks.get_tx_info(tx) parse_hex_cursor = db.cursor() tx_hash = hashlib.sha256(chr(tx_index).encode('utf-8')).hexdigest() global tx_index parse_hex_cursor.execute('''INSERT INTO transactions( tx_index, tx_hash, block_index, block_time, source, destination, btc_amount, fee, data) VALUES(?,?,?,?,?,?,?,?,?)''', (tx_index, tx_hash, tx_index, tx_index, source, destination, btc_amount, fee, data) ) parse_hex_cursor.execute('''SELECT * FROM transactions \ WHERE tx_index=?''', (tx_index,)) tx = parse_hex_cursor.fetchall()[0] heaps = blocks.init_heaps(db) blocks.parse_tx(db, tx, heaps) # After parsing every transaction, check that the credits, debits sum properly. cursor.execute('''SELECT * FROM balances''') for balance in cursor.fetchall(): amount = 0 cursor.execute('''SELECT * FROM debits \ WHERE (address = ? AND asset = ?)''', (balance['address'], balance['asset'])) for debit in cursor.fetchall(): amount -= debit['amount'] cursor.execute('''SELECT * FROM credits \ WHERE (address = ? AND asset = ?)''', (balance['address'], balance['asset'])) for credit in cursor.fetchall(): amount += credit['amount'] assert amount == balance['amount'] tx_index += 1 parse_hex_cursor.close()
def parse_hex (unsigned_tx_hex): tx = bitcoin.decode_raw_transaction(unsigned_tx_hex) cursor = db.cursor() tx_hash = hashlib.sha256(chr(tx_index).encode('utf-8')).hexdigest() global tx_index block_index = config.BURN_START + tx_index block_hash = hashlib.sha512(chr(block_index).encode('utf-8')).hexdigest() block_time = block_index * 10000000 source, destination, btc_amount, fee, data = blocks.get_tx_info(tx, block_index) cursor.execute('''INSERT INTO blocks( block_index, block_hash, block_time) VALUES(?,?,?)''', (block_index, block_hash, block_time) ) cursor.execute('''INSERT INTO transactions( tx_index, tx_hash, block_index, block_time, source, destination, btc_amount, fee, data) VALUES(?,?,?,?,?,?,?,?,?)''', (tx_index, tx_hash, block_index, tx_index, source, destination, btc_amount, fee, data) ) txes = list(cursor.execute('''SELECT * FROM transactions \ WHERE tx_index=?''', (tx_index,))) assert len(txes) == 1 tx = txes[0] blocks.parse_tx(db, tx) # After parsing every transaction, check that the credits, debits sum properly. cursor.execute('''SELECT * FROM balances''') for balance in cursor.fetchall(): quantity = 0 cursor.execute('''SELECT * FROM debits \ WHERE (address = ? AND asset = ?)''', (balance['address'], balance['asset'])) for debit in cursor.fetchall(): quantity -= debit['quantity'] cursor.execute('''SELECT * FROM credits \ WHERE (address = ? AND asset = ?)''', (balance['address'], balance['asset'])) for credit in cursor.fetchall(): quantity += credit['quantity'] assert quantity == balance['quantity'] tx_index += 1 cursor.close() return tx