Esempio n. 1
0
def initialise_rawtransactions_db(db):
    if pytest.config.option.savescenarios:
        counterpartyd.set_options(testnet=True, **COUNTERPARTYD_OPTIONS)
        cursor = db.cursor()
        cursor.execute('DROP TABLE  IF EXISTS raw_transactions')
        cursor.execute('CREATE TABLE IF NOT EXISTS raw_transactions(tx_hash TEXT UNIQUE, tx_hex TEXT, tx_json TEXT)')
        with open(CURR_DIR + '/fixtures/unspent_outputs.json', 'r') as listunspent_test_file:
                wallet_unspent = json.load(listunspent_test_file)
                for output in wallet_unspent:
                    txid = binascii.hexlify(bitcoinlib.core.lx(output['txid'])).decode()
                    tx = bitcoin.decode_raw_transaction(output['txhex'])
                    cursor.execute('INSERT INTO raw_transactions VALUES (?, ?, ?)', (txid, output['txhex'], json.dumps(tx)))
        cursor.close()
Esempio n. 2
0
def insert_raw_transaction(raw_transaction, db, rawtransactions_db):
    # one transaction per block
    block_index, block_hash, block_time = create_next_block(db)

    cursor = db.cursor()
    tx_index = block_index - config.BURN_START + 1
    tx = bitcoin.decode_raw_transaction(raw_transaction)

    tx_hash = hashlib.sha256('{}{}'.format(tx_index,raw_transaction).encode('utf-8')).hexdigest()
    #print(tx_hash)
    tx['txid'] = tx_hash
    if pytest.config.option.savescenarios:
        save_rawtransaction(rawtransactions_db, tx_hash, raw_transaction, json.dumps(tx))

    source, destination, btc_amount, fee, data = blocks.get_tx_info2(raw_transaction)
    transaction = (tx_index, tx_hash, block_index, block_hash, block_time, source, destination, btc_amount, fee, data, True)
    cursor.execute('''INSERT INTO transactions VALUES (?,?,?,?,?,?,?,?,?,?,?)''', transaction)
    tx = list(cursor.execute('''SELECT * FROM transactions WHERE tx_index = ?''', (tx_index,)))[0]
    cursor.close()

    blocks.parse_block(db, block_index, block_time)
    return tx
Esempio n. 3
0
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
Esempio n. 4
0
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