Exemplo n.º 1
0
def scan_block(rpc_raw):
    blockcount = rpc_raw.getblockcount()
    bkscan = BlockchainScan.objects.all().first() #Attempt to pick up where we left off.
    if not bkscan: #First scan!
        bkscan = BlockchainScan(last_index=(blockcount-1000))
        bkscan.save()
    current_index = bkscan.last_index
    on_latest_block = True if current_index >= blockcount else False

    processed_transactions = {}
    if on_latest_block:
        #If we are on the latest block, we'll be scanning the mempool later
        mpscan = MemPoolScan.objects.all().first()
        if not mpscan:
            mpscan = MemPoolScan(txids_scanned=json.dumps({}))
            mpscan.save()

        #get tx_ids we already scanned in mempool.
        processed_transactions = json.loads(mpscan.txids_scanned)

    try:
        block_hash = rpc_raw.getblockhash(current_index) #convert block index to hash
        print "...scanning block", current_index
        bi = rpc_raw.getblock(block_hash) #get list of tx_ids in block
        for tx_id in bi['tx']:
            if tx_id not in processed_transactions: #only process transactions once
                block_time = bi['time'] if 'time' in bi else datetime.datetime.now()
                parse_transaction(rpc_raw, tx_id, current_index, block_time)

        current_index += 1
        BlockchainScan.objects.all().update(last_index=current_index)

        if on_latest_block:
            print "wiping mempool"
            #wipe mempool scan (assume mempool transactions were added to this block)
            MemPoolScan.objects.all().update(txids_scanned="{}")
            processed_transactions = {}

    except JSONRPCException:
        pass #at last block

    if on_latest_block:
        #Let's scan that mempool!
        print "processing mempool..."
        unconfirmed_transactions = rpc_raw.getrawmempool()
        count_new = 0
        for tx_id in unconfirmed_transactions:
            if tx_id not in processed_transactions:
                parse_transaction(rpc_raw, tx_id, current_index, datetime.datetime.now())
                processed_transactions[tx_id] = 1
                count_new += 1
        print "(found", count_new, "transactions)"

        MemPoolScan.objects.all().update(txids_scanned=json.dumps(processed_transactions))

        return True, blockcount - current_index
    return False, blockcount - current_index
Exemplo n.º 2
0
def get_blockchain_scan_status(rpc_raw):
    blockcount = rpc_raw.getblockcount()
    bkscan = BlockchainScan.objects.all().first() #Attempt to pick up where we left off.
    if not bkscan: #First scan!
        bkscan = BlockchainScan(last_index=(blockcount-1000))
        bkscan.save()
    current_index = bkscan.last_index
    on_latest_block = True if current_index >= blockcount else False
    return on_latest_block, blockcount - current_index