Example #1
0
def read_blockhash(blockhash):
    prev = None
    nxt = None
    try:
        b = Block.get(Block.hash == blockhash)
        if b.height > 0:
            prev = Block.get(Block.height == b.height - 1)
    except Block.DoesNotExist:
        return HTMLResponse(status_code=404)
    try:
        nxt = Block.get(Block.height == b.height + 1, Block.orphaned == False)
    except Block.DoesNotExist:
        pass
    txs = list(
        Transaction.select().where(Transaction.block == b.hash).execute())

    txs = list(
        map(
            lambda tx: {
                'txid': tx.txid,
                'timestamp': int(tx.timestamp.timestamp()),
                'addresses_in': tx.addresses_in,
                'addresses_out': tx.addresses_out
            }, txs))

    def func(a):
        return 1 if 'null' in a['addresses_in'] else 0

    txs.sort(key=func, reverse=True)

    pool = None
    cb = bytes(b.coinbase)
    for key, value in POOLS.items():
        if cb.find(key.encode()) != -1:
            pool = value
    res = {
        'height': b.height,
        'hash': b.hash,
        'timestamp': int(b.timestamp.timestamp()),
        'merkleroot': b.merkle_root,
        'txs': txs,
        'difficulty': b.difficulty,
        'size': b.size,
        'version_hex': bytes(b.version).hex(),
        'version': struct.unpack('i', bytes(b.version))[0],
        'bits': bytes(b.bits).hex(),
        'nonce': b.nonce,
        'pool': pool,
    }
    if prev:
        res['previousblockhash'] = prev.hash
    if nxt:
        res['nextblockhash'] = nxt.hash
    return res
Example #2
0
def read_block_txs(block : str):
    try:
        b = Block.get(Block.hash == block)
    except Block.DoesNotExist:
        return HTMLResponse(status_code=404)
    res = {
        'txs': []
    }

    txs = Transaction.select().where(Transaction.txid.in_(b.tx))
    block = get_latest_block()
    for tx in txs:
        is_coinbase = (len(tx.vin) == 0)
        res['txs'].append({
            'blockhash': b.hash,
            'blockheight': b.height,
            'blocktime': int(b.timestamp.timestamp()),
            'confirmations': get_confirmations(b.height, block=block),
            'isCoinBase': is_coinbase,
            'txid': tx.txid,
            'valueOut': tx.output_value,
            'vin': tx.vin,
            'vout': tx.vout,
        })
        return res