Ejemplo n.º 1
0
def main():
    print('Starting ingestion at height %d' % start)

    connection = mysql.connector.connect(host=mysql_host,
                                         user=mysql_user,
                                         database=mysql_database,
                                         port=mysql_port,
                                         password=mysql_password)
    cursor = connection.cursor()

    blockchain = Blockchain(path, cache=block_index_cache)
    for block, block_undo in blockchain.get_ordered_blocks(start=start,
                                                           end=end):
        t = time.time()

        if block_undo is None:
            continue

        block_data = to_block_data(blockchain, block, block_undo)
        insert_block_data(connection, block_data)

        print(time.time(), time.time() - t, block.height, 'block_count')

    cursor.close()
    connection.close()
def extract_input_output_main_from_blockchain(request):

    start = 0
    stop = 0

    if 'start' in request.GET:
        start = int(request.GET['start'])
    if 'stop' in request.GET:
        stop = int(request.GET['stop'])

    blockchain = Blockchain(BLOCK_DATA_DIR)
    print("blocks accessed")
    threads = []

    for block in blockchain.get_ordered_blocks(BLOCK_DATA_DIR + '/index',
                                               start=start,
                                               end=stop):
        thread1 = myThread(block)
        thread1.start()
        threads.append(thread1)

        for thread in threads:
            thread.join()

        count_thread = threading.active_count()

        while count_thread > MAX_NUM_OF_THREAD:
            print("threading active_count >>>>>>>>>>>>" + str(count_thread))
            continue

    return JsonResponse({"res": ""}, status=200)
Ejemplo n.º 3
0
    def noncesRelatedToBitiodineAddresses(self, caddresses, ctag):
        filem = UtilFileManager()
        arqName = "ClusterNoncesOutput_" + self.clusterType + "_" + str(ctag)
        countFindings = 0
        blockNumber = 0
        blockchain = Blockchain(
            os.path.expanduser(sp.configBlockchainPath + 'blocks'))
        for block in blockchain.get_ordered_blocks(
                os.path.expanduser(sp.configBlockchainPath + "blocks/index"),
                start=0):
            blockNumber = blockNumber + 1
            nonce = block.header.nonce

            transaction = block.transactions[0]
            #Get outputs from coinbase transaction
            for output in transaction.outputs:
                #Get addresses
                for outAddr in output.addresses:

                    for strAddr in caddresses:
                        if outAddr._address == strAddr:
                            #save that nonce
                            filem.saveInFile(arqName, nonce)
                            self.append(nonce)
                            countFindings = countFindings + 1

        if countFindings > 0:
            scalc = Statistics()
            scalc.printStatistics("Nonces", arqName, filem)
        return countFindings
Ejemplo n.º 4
0
def main():
    blockchain = Blockchain(
        os.path.expanduser(
            '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks')
    )  #D:\Courses\Capstone\PyBC\pybit\Blocks
    acc_btc_per_address = {}
    script_count = {}
    script_count["p2pk"] = 0
    script_count["p2pkh"] = 0
    script_count["p2ms"] = 0
    script_count["p2sh"] = 0
    for block in blockchain.get_unordered_blocks():
        for tx in block.transactions:
            for output in tx.outputs:
                address = output.addresses
                if output.is_pubkey():
                    script_count["p2pk"] += 1
                elif output.is_pubkeyhash():
                    script_count["p2pkh"] += 1
                elif output.is_multisig():
                    script_count["p2ms"] += 1
                elif output.is_p2sh():
                    script_count["p2sh"] += 1
                else:
                    continue
                if address[0] in acc_btc_per_address.keys():
                    acc_btc_per_address[address[0]] = acc_btc_per_address[
                        address[0]] + output.value
                else:
                    acc_btc_per_address[address[0]] = output.value

    analyze = {}
    analyze["0-1"] = 0
    analyze["1-10"] = 0
    analyze["10-100"] = 0
    analyze["100-1000"] = 0
    analyze["1000-10000"] = 0
    analyze["10000-100000"] = 0
    analyze[">=100000"] = 0

    for address in acc_btc_per_address.keys():
        btc_value = acc_btc_per_address[address] / 100000000
        if btc_value < 1:
            analyze["0-1"] += 1
        elif btc_value >= 1 and btc_value < 10:
            analyze["1-10"] += 1
        elif btc_value >= 10 and btc_value < 100:
            analyze["10-100"] += 1
        elif btc_value >= 100 and btc_value < 1000:
            analyze["100-1000"] += 1
        elif btc_value >= 1000 and btc_value < 10000:
            analyze["1000-10000"] += 1
        elif btc_value >= 10000 and btc_value < 100000:
            analyze["10000-100000"] += 1
        elif btc_value >= 100000:
            analyze[">=100000"] += 1
    print(analyze)

    for script in script_count.keys():
        print("Address type:", script, "count:", script_count[script])
def main():
    uri = "bolt://localhost:7687"
    driver = GraphDatabase.driver(uri, auth=("neo4j", "n"))
    blockchain = Blockchain(
        os.path.expanduser(
            '/media/varun/DATA/Bitcoin data/Blockdata_testing/1'))
    blocks = blockchain.get_unordered_blocks()
    with driver.session() as session:
        #session.write_transaction(create_addresses, blocks)
        create_inputs(session, blocks)
        create_outputs(session, blocks)
    # with driver.session() as session:
    #     create_outputs(session, blocks)

    # session.write_transaction(create_blocks,blocks)
    # with driver.session() as session2:
    #     session2.write_transaction(create_transaction, blocks)
    # tx_hash_handle = open("txhash_00.txt","w+")
    # tx_index = open("txindex_00.txt", "w+")
    #   #D:\Courses\Capstone\PyBC\pybit\Blocks
    # for block in blockchain.get_unordered_blocks():
    #     for tx in block.transactions:
    #         for input in tx.inputs:
    #             tx_hash_handle.write(str(input.transaction_hash)+"\n")
    #             tx_index.write(str(input.transaction_index)+"\n")
    #
    # tx_hash_handle.flush()
    # tx_index.flush()
    # tx_index.close()
    # tx_hash_handle.close()
    driver.close()
Ejemplo n.º 6
0
def main():
    blockchain = Blockchain(os.path.expanduser('./blocks'))
    for block in blockchain.get_unordered_blocks():
        for tx in block.transactions:
            for no, output in enumerate(tx.outputs):
                print(tx.hash, output.addresses, output.value)
                sys.exit(0)
Ejemplo n.º 7
0
def main():
    blockchain = Blockchain(
        os.path.expanduser(
            '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks'))
    for block in blockchain.get_unordered_blocks():
        for transaction in block.transactions:
            for output in transaction.outputs:
                if output.is_unknown():
                    print(block.header.timestamp, output.script.value)
Ejemplo n.º 8
0
def main():
    blockchain = Blockchain(
        os.path.expanduser(
            '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks')
    )  #D:\Courses\Capstone\PyBC\pybit\Blocks
    for block in blockchain.get_unordered_blocks():
        for tx in block.transactions:
            for no, output in enumerate(tx.outputs):
                print("tx=%s outputno=%d type=%s value=%s" %
                      (tx.hash, no, output.type, output.value))
Ejemplo n.º 9
0
    def process(self):
        count = 0
        blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
        for block in blockchain.get_unordered_blocks():
            timestamp = block.header.timestamp
            if (timestamp.year == 2017):
                for tx in block.transactions:
                    self.output_queue.put((tx, block.header.timestamp))

        self.output_queue.put(None)  #ending condition
Ejemplo n.º 10
0
    def process(self):
        count = 0
        blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
        for block in blockchain.get_ordered_blocks(os.path.expanduser(
                '/home/teh_devs/Downloads/bitcoin_analysis_sa/index'),
                                                   start=self.start_block,
                                                   end=self.end_block):
            for tx in block.transactions:
                self.output_queue.put((tx, block.header.timestamp))

        self.output_queue.put(None)  #ending condition
Ejemplo n.º 11
0
def get_block_transactions(block_height):
    blockchain = Blockchain("datas")

    for block in blockchain.get_ordered_blocks(
            "datas/index",
            end=block_height,
            start=block_height + 1,
            cache="super-big-index.pickle",
    ):
        print(block.height)
        for tx in block.transactions:
            yield tx
Ejemplo n.º 12
0
def main():
    prepare_dirs()

    blockchain = Blockchain(BLOCK)
    #build_address_db(blockchain, INDEX, end=200001)
    #print('Building Address DB done')

    for block in blockchain.get_ordered_blocks(INDEX, start=200000,
                                               end=210000):
        result = process_block(block)
        archive_result(result, block.height)
        print('Done: {0:d} - {1:s}'.format(block.height, block.hash))
Ejemplo n.º 13
0
 def __init__(self, chain, chainpath):
     self.chain = chain
     self.chainpath = chainpath
     self.last_block = 0
     if not os.path.isdir("txdata"):
         os.mkdir("txdata")
     try:
         f = open("txdata/" + self.chain + "-balances.pickle", "rb")
         self.balances = pickle.load(f)
     except:
         self.balances = {}
     self.blockchain = Blockchain(os.path.expanduser(self.chainpath),
                                  chain_const[self.chain]["pch"])
Ejemplo n.º 14
0
def main():
    blockchain = Blockchain(
        os.path.expanduser(
            '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks')
    )  #D:\Courses\Capstone\PyBC\pybit\Blocks
    trans_per_address = {}
    same_transaction_counter = 0

    for block in blockchain.get_unordered_blocks():
        for tx in block.transactions:
            for output in tx.outputs:
                address = output.addresses
                if len(address) != 0:
                    if address[0] in trans_per_address.keys():
                        same_transaction_counter += 1
                        trans_per_address[address[0].hash] += 1
                    else:
                        trans_per_address[address[0].hash] = 1

    analyze = {}
    analyze["1-2"] = 0
    analyze["2-4"] = 0
    analyze["4-10"] = 0
    analyze["10-100"] = 0
    analyze["100-1000"] = 0
    analyze["1000-10000"] = 0
    analyze["10000-100000"] = 0
    analyze[">=100000"] = 0

    for address in trans_per_address.keys():
        trans_value = trans_per_address[address]
        if trans_value < 2:
            analyze["1-2"] += 1
        elif trans_value >= 2 and trans_value < 4:
            analyze["2-4"] += 1
        elif trans_value >= 4 and trans_value < 10:
            analyze["4-10"] += 1
        elif trans_value >= 10 and trans_value < 100:
            analyze["10-100"] += 1
        elif trans_value >= 100 and trans_value < 1000:
            analyze["100-1000"] += 1
        elif trans_value >= 1000 and trans_value < 10000:
            analyze["1000-10000"] += 1
        elif trans_value >= 10000 and trans_value < 100000:
            analyze["10000-100000"] += 1
        elif trans_value >= 100000:
            analyze[">=100000"] += 1
    print(analyze)
    print(same_transaction_counter)
Ejemplo n.º 15
0
def main():
    blockchain = Blockchain(
        os.path.expanduser(
            '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks')
    )  #D:\Courses\Capstone\PyBC\pybit\Blocks
    timelist = []
    n_transactions = {}
    n_transactions_count = 0
    n_col_transactions = []  # transactions with the same txid
    block_counter = 0
    tx_per_block = {}
    for block in blockchain.get_unordered_blocks():
        block_counter += 1
        timelist.append(block.header.timestamp)
        tx_counter = 0
        for tx in block.transactions:
            n_transactions_count += 1
            tx_counter += 1
            if tx.txid in n_transactions.keys():
                n_transactions[tx.txid].append(
                    block.header.timestamp)  #adding timestamp to the
                n_col_transactions.append(tx.txid)

            else:
                n_transactions[tx.txid] = []
                n_transactions[tx.txid].append(block.header.timestamp)

            # for no, output in enumerate(tx.outputs):
            #     print("tx=%s outputno=%d type=%s value=%s" % (tx.hash, no, output.type, output.value))
        tx_per_block[block.hash] = tx_counter

    print("the start date: ", min(timelist), "the end date", max(timelist),
          "of the datset")
    print("Total number of Transactions:", n_transactions_count)
    for txid in n_col_transactions:
        print("Same hash collision hash:", txid, "at time",
              n_transactions[txid], "boolean segwit: ", tx.is_segwit,
              "boolean isCoinbase:", tx.is_coinbase())

    print("total number of blocks in the datset are:", block_counter)
    max_transaction_inblock = max(tx_per_block, key=tx_per_block.get)
    print("The maximum transactions in a block are: ",
          tx_per_block[max_transaction_inblock], "in block",
          max_transaction_inblock)
    min_transaction_inblock = min(tx_per_block, key=tx_per_block.get)
    print("The minimum transactions in a block are: ",
          tx_per_block[min_transaction_inblock], "in block",
          min_transaction_inblock)
Ejemplo n.º 16
0
def main():
    blockchain = Blockchain(
        os.path.expanduser(
            '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks')
    )  #D:\Courses\Capstone\PyBC\pybit\Blocks
    max_btc_per_address = {}

    for block in blockchain.get_unordered_blocks():
        for tx in block.transactions:
            for output in tx.outputs:
                address = output.addresses
                if len(address) != 0:
                    if address[0] in max_btc_per_address.keys():
                        if max_btc_per_address[address[0]] < output.value:
                            max_btc_per_address[address[0]] = output.value
                    else:
                        max_btc_per_address[address[0]] = output.value

    analyze = {}
    analyze["0-0.1"] = 0
    analyze["0.1-10"] = 0
    analyze["10-100"] = 0
    analyze["100-1000"] = 0
    analyze["1000-10000"] = 0
    analyze["10000-100000"] = 0
    analyze[">=100000"] = 0

    for address in max_btc_per_address.keys():
        btc_value = max_btc_per_address[address] / 100000000
        if btc_value < 0.1:
            analyze["0-0.1"] += 1
        elif btc_value >= 1 and btc_value < 10:
            analyze["0.1-10"] += 1
        elif btc_value >= 10 and btc_value < 100:
            analyze["10-100"] += 1
        elif btc_value >= 100 and btc_value < 1000:
            analyze["100-1000"] += 1
        elif btc_value >= 1000 and btc_value < 10000:
            analyze["1000-10000"] += 1
        elif btc_value >= 10000 and btc_value < 100000:
            analyze["10000-100000"] += 1
        elif btc_value >= 100000:
            analyze[">=100000"] += 1
    print(analyze)
Ejemplo n.º 17
0
def main():

    blockchain = Blockchain(
        os.path.expanduser(
            '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks'))
    for block in blockchain.get_unordered_blocks():
        for transaction in block.transactions:
            coinbase = transaction.inputs[0]

            # Some coinbase scripts are not valid scripts
            try:
                script_operations = coinbase.script.operations
            except CScriptInvalidError:
                break

        # An operation is a CScriptOP or pushed bytes
            for operation in script_operations:
                if type(operation) == bytes and len(operation) > 3 \
                        and is_ascii_text(operation):
                    print(block.header.timestamp, operation.decode("ascii"))
            break
Ejemplo n.º 18
0
def main():
    height = 0

    if os.path.exists(progress_file):
        with open(progress_file) as f:
            height = f.readline().rstrip()

    if height == 'COMPLETE':
        print('Ingestion is complete')
        sys.exit(0)

    height = int(height) + 1

    print('Starting ingestion at height %d' % height)
    connection = mysql.connector.connect(host=mysql_host,
                                         user=mysql_user,
                                         database=mysql_database,
                                         password=mysql_password)
    cursor = connection.cursor()

    blockchain = Blockchain(path, cache=block_index_cache)
    for block, block_undo in blockchain.get_ordered_blocks(start=height):
        t = time.time()

        if block_undo is None:
            continue

        block_data = to_block_data(blockchain, block, block_undo)
        insert_block_data(connection, block_data)

        with open(progress_file, 'w') as f:
            f.write(str(block.height) + '\n')
        print(time.time(), time.time() - t, block.height, 'block_count')

    with open(progress_file, 'w') as f:
        f.write('COMPLETE\n')

    cursor.close()
    connection.close()
Ejemplo n.º 19
0
def process_block(block):
	client = MongoClient("mongodb://localhost:27017")
	ba = client["ba"]
	utxo = ba["utxo"]

	for tx in block.transactions:
		tx_hash = tx.hash
		result = utxo.find({"tx_hash":tx_hash}).count()
		if result > 0:
			pass
		with open('missing_tx', "a") as outfile:
	    	outfile.write((tx_hash + '\n'))
		for index,output in enumerate(tx.outputs):
			try:
				document = {
					"tx_hash":tx_hash,
					"index":index,
					"address":output.addresses[0].address,
					"amount":output.value / 100000000
				}
				utxo.insert(document)
			except Exception as e:
				f = open('logs', 'a')
				f.write(e)
				f.write("TX HASH" + str(tx_hash)+ '\n')
				f.close()
		
	# Free RAM
	del block
	gc.collect()



blockchain = Blockchain(os.path.expanduser('/home/shared/bitcoin/blocks'))
count = 0
encountered = False
for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), end=1000):
    print("height=%d block=%s" % (block.height, block.hash))
	process_block(block)
Ejemplo n.º 20
0
 def read_data(self, show_progress: bool = False):
     """
     从区块数据生成器里依次构建
     :param show_progress: 是否显示进度
     :return:
     """
     block_chain = Blockchain(self.dir_blocks)
     index = 0
     for i, block in enumerate(
             block_chain.get_ordered_blocks(index=self.dir_index,
                                            cache=self.index_cache)):
         if block.height < self.min_height:
             continue
         if block.height > self.max_height:
             break
         index += 1
         self.from_block(block)
         if show_progress:
             rate = index / (self.max_height - self.min_height + 1)
             sys.stdout.write(
                 f'已完成 {rate * 100:.1f}% -- {index}/{self.max_height - self.min_height + 1}\r'
             )
             sys.stdout.flush()
def parse(filepath, filename):
    target_path = "/data/result/"
    j = filename.split(".")
    add_str = j[0]

    file_blk = open(target_path + add_str + ".blk.txt",'a')
    file_trans = open(target_path + add_str + ".trans.txt",'a')
    file_input = open(target_path + add_str + ".input.txt",'a')
    file_output = open(target_path + add_str + ".output.txt",'a')

    blockchain = Blockchain(os.path.expanduser(filepath + filename))
    for block in blockchain.get_unordered_blocks():
        file_blk.write(block.hash +"\t"+ str(block.size) +"\t"+ str(block.header.version) +"\t"+ block.header.previous_block_hash +"\t"+ block.header.merkle_root 
            + "\t"+ str(block.header.timestamp) +"\t"+ str(block.header.bits) +"\t"+ str(block.header.nonce) +"\t"+ str(block.n_transactions) + "\t" + add_str + "\n")
        for tx_index,tx in enumerate(block.transactions):
            try:
                file_trans.write(block.hash + "\t" + str(tx_index) + "\t" + tx.hash + "\t" + str(tx.version) + "\t" + str(tx.locktime) + "\t" + str(tx.n_inputs) 
                     + "\t" + str(tx.n_outputs) + "\t" + str(tx.is_segwit) + "\t" + add_str + "\n")
                for input_index, input in enumerate(tx.inputs):
                    transaction_hash = input.transaction_hash
                    transaction_index = input.transaction_index
                    if tx.is_coinbase():
                        transaction_hash = "coinbase"
                        transaction_index = 0
                    file_input.write(tx.hash + "\t" + str(input.sequence_number) + "\t" + transaction_hash+ "\t" + str(transaction_index) + "\t" + add_str + "\n")
                for output_index, output in enumerate(tx.outputs):
                    address_list = []
                    for address in output.addresses:
                        address_list.append(address.address)
                    file_output.write(tx.hash + "\t" + str(output_index) + "\t" + output.type+ "\t" + ",".join(address_list)+ "\t" + str(output.value) + "\t" + add_str + "\n")
            except Exception as e:
                print(e)
    file_blk.close()
    file_trans.close()
    file_input.close()
    file_output.close()
Ejemplo n.º 22
0
def process_chunk(BLOCK_PATH, INDEX_PATH, start):
    """
    Processes a chunk of Bitcoin blocks (start to start+1000) and returns the transaction outputs

    :param BLOCK_PATH:  str, the path to the Bitcoin blocks
    :param INDEX_PATH:  str, the path to the LevelDB Bitcoin index
    :param start:       int, the block height to start at
    :return:            list, a list of tuples. One tuple per transaction, where each tuple contains the transaction id
                        and a serialized representation of a list of transaction outputs as bytestring.
    """
    re_data = []
    # Load Blockchain, ignore Read Locks imposed by other instances of the process
    blockchain = Blockchain(BLOCK_PATH, ignoreLocks=True)
    blockchain = blockchain.get_ordered_blocks(INDEX_PATH,
                                               start=start,
                                               end=start + 1000)
    for block in blockchain:
        for tx in block.transactions:
            tx_id = tx.txid
            # Create a list of outputs, where each output is itself a list comprising value, receiving address and
            # output number.
            outputs = []
            for o in range(len(tx.outputs)):
                try:
                    addr = tx.outputs[o].addresses[0].address
                    val = tx.outputs[o].value
                    outputs.append([val, addr, o])
                except Exception as e:
                    val = tx.outputs[o].value
                    outputs.append([val, 'unknown', o])
                    pass
            # Add the output list of the transaction and append it to the collector list. Serialization for the
            # the database is performed here because it is costly and should be done in parallel.
            re_data.append((tx_id, pickle.dumps(outputs)))

    return re_data
Ejemplo n.º 23
0
 def __init__(self, *args, **kwargs):
     super(LoadBLKFilesToStgBase, self).__init__(*args, **kwargs)
     self.blockchain = Blockchain(self.input())
Ejemplo n.º 24
0
    print('P2SH:  ', input_id, full_script)
    cur = conn.cursor()        
    cur.execute("INSERT INTO p2sh (input_id, full_script) VALUES (%s,%s)", (input_id, full_script))
    conn.commit()
def writetooutput_script(output_id, full_script):
        print('OUTPUT_SCRIPT:  ', output_id, full_script)
        cur = conn.cursor()        
        cur.execute("INSERT INTO output_script (output_id, full_script) VALUES (%s,%s)", (output_id, full_script))
        conn.commit()
def fetch_single_data(table, column, id):
    cur.execute("SELECT %s FROM %s WHERE id = %s", (column, table, id))
    return cur.fetchone()


# Instanciate the Blockchain by giving the path to the directory 
blockchain = Blockchain(sys.argv[1])
print(blockchain.get_main_chain())
#ordere blocks according to height starting with genesis block
#orderedblockchain= blockchain.get_main_chain()
#print(type(orderedblockchain))
#print(orderedblockchain[0])
#get relevant data from relevant blocks and save to db
def saveblockinfo(block):   
    height = block.height
    id = block.hash
    header = block.header
    timestamp = time.mktime(header.timestamp.timetuple())
    #writetoblock(id, height, timestamp)
    writetoblock(id, height, timestamp)

#returns type of transaction
Ejemplo n.º 25
0
import redis
import pickle
import plyvel

from collections import deque
from flask import Flask, request, render_template, g
from blockchain_parser.blockchain import Blockchain

CACHE_RANGE = 200

# first transaction 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
app = Flask(__name__)
red = redis.Redis(host="localhost", port=6379, db=0)
red.flushall()

blockchain = Blockchain("datas")

# force the creation of the index
if os.path.exists("super-big-index.pickle"):
    os.remove("super-big-index.pickle")

print("Creating index")
next(
    blockchain.get_ordered_blocks("datas/index",
                                  cache="super-big-index.pickle"))
print("Index created")

pldb = plyvel.DB('tx_to_block/', create_if_missing=False)


class IllegalState(Exception):
Ejemplo n.º 26
0
app.logger.debug("Starting blockchain finder...")

app.logger.debug("Connecting to redis...")
while True:
    try:
        red = redis.Redis(host="redis", port=6379, db=0)
        red.flushall()
        app.logger.debug("Redis connection OK")
        break
    except:
        app.logger.debug("Redis not ready yet")
        time.sleep(1)

app.logger.debug("Reading blockchain...")
blockchain = Blockchain("/blockchain/blocks")
levelDBMap = dict()
levelDBMapLock = threading.Lock()


def loadIndex(self, index):
    with levelDBMapLock:
        if index not in levelDBMap:
            db = plyvel.DB(index, compression=None)
            blockIndexes = [
                DBBlockIndex(format_hash(k[1:]), v) for k, v in db.iterator()
                if k[0] == ord('b')
            ]
            db.close()
            blockIndexes.sort(key=lambda x: x.height)
            levelDBMap[index] = blockIndexes
Ejemplo n.º 27
0
import os
from blockchain_parser.blockchain import Blockchain
import datetime
import leveldb
from binascii import a2b_hex, b2a_hex
import pymongo
from pymongo import MongoClient
import subprocess
import urllib

start = datetime.datetime.now()
blockchain = Blockchain('/data1/bitcoin/blocks/')
index_path = '/data1/bitcoin/blocks/index'
back_path = '/data/workdir/data/bitcoinparser/bitcoin_index_backup/index'
txhash_address = leveldb.LevelDB("/data/workdir/data/bitcoinparser/leveldb/")

block_column = [
    "height", "hash", "version", "previous_block_hash", "merkle_root",
    "timestamp", "bits", "difficulty", "nonce", "n_transactions"
]
tx_header_column = [
    "height", "tx_index", "version", "locktime", "txhash", "n_inputs",
    "n_outputs", "is_segwit"
]
tx_output_column = [
    "height", "tx_index", "txhash", "output_num", "value", "address", "script",
    "type"
]
tx_input_column = [
    "height", "tx_index", "input_num", "txhash", "transaction_index",
    "sequence_number", "script", "witnesses", "value", "address"
Ejemplo n.º 28
0
import sys
sys.path.append('..')
from blockchain_parser.blockchain import Blockchain
from blockchain_parser.script import CScriptInvalidError

def is_ascii_text(op):
    return all(32 <= x <= 127 for x in op)

def as_utf8_text(x):
    try:
        return x.decode('UTF-8')
    except UnicodeDecodeError:
        return None

blockchain = Blockchain(sys.argv[1])
for block in blockchain.get_ordered_blocks(sys.argv[1] + '/index'):
    for transaction in block.transactions:

        coinbase = transaction.inputs[0]

        try:
            script_operations = [op for op in coinbase.script.operations if type(op) == bytes]
        except CScriptInvalidError:
            break

        # An operation is a CScriptOP or pushed bytes
        for operation in script_operations:
            text = as_utf8_text(operation)
            if text and len(operation.strip(b'\x00')) >= 20:
                # print(block.header.timestamp, text)
import hashlib
import binascii

from blockchain_parser.blockchain import Blockchain
from blockchain_parser.script import CScriptInvalidError


def is_ascii_text(op):
    return all(32 <= x <= 127 for x in op)

def hexify(value,zeros):
	try:
		return hex(value).replace('0x','').replace('L','').zfill(zeros)
	except:
		return value.encode('hex').zfill(zeros)

blockchain = Blockchain(sys.argv[1])
h = 0
print("height,version, h0, h1, h2, h3, h4, h5, h6, h7, r0, r1, r2, r3, r4, r5, r6, r7, timestamp, bits, nonce")
for block in blockchain.get_unordered_blocks():
	header = block.header
	r = [header.version]
	r += struct.unpack("<IIIIIIII",binascii.unhexlify(header.previous_block_hash)[::-1])
	r += struct.unpack("<IIIIIIII",binascii.unhexlify(header.merkle_root)[::-1])
	r += struct.unpack("<I",bytes(header.hex[68:72]))
	r += [header.bits]
	r += [header.nonce]
	
	print(str(h)+","+str(r).replace("[","").replace("]",""))
	h += 1
# Copyright (C) 2015 The bitcoin-blockchain-parser developers
#
# This file is part of bitcoin-blockchain-parser.
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-bitcoinlib, including this file, may be copied, modified,
# propagated, or distributed except according to the terms contained in the
# LICENSE file.

import sys
from blockchain_parser.blockchain import Blockchain


blockchain = Blockchain(sys.argv[1])
for block in blockchain.get_unordered_blocks():
    for transaction in block.transactions:
        for output in transaction.outputs:
            if output.is_unknown():
                print(block.header.timestamp, output.script.value)
Ejemplo n.º 31
0
from blockchain_parser.blockchain import Blockchain
import time

start_time = time.time()
path = "/Volumes/C/bitcoin-blockchain/blockchain/blocks/"
start_file = path + 'blk00000.dat'
#years = [2011, 2012, 2013, 2014]
years = [2011, 2012]
processed_years = set()

blockchain = Blockchain(path)

for block in blockchain.get_unordered_blocks():
    if block.header.timestamp.year in years:
        new_file = blockchain.selected_blk_file
        if new_file != start_file or block.header.timestamp.year not in processed_years:
            processed_years.add(block.header.timestamp.year)
            start_file = new_file
            print("File: " + new_file + "   Year:" +
                  str(block.header.timestamp.year))

print("--- %s seconds ---" % (time.time() - start_time))
            f_list = [float(i) for i in line.split(",") if i.strip()]

    #colors 003399, 3F5D7D
    freq, bins, patches = plt.hist(f_list, 256, color="#003399")
    plt.xlabel(xlabelName, fontsize=16) #, fontsize=25

    plt.ylabel("Frequency", fontsize=16)
    plt.savefig("results/"+xlabelName.strip()+"_chunk"+str(countFileChunk)+".png", bbox_inches="tight")  

    plt.close()
    os.remove(fileName) #AM file may get big.


#the API filters orphan blocks
#                                       ##CHANGE HERE TO YOUR CONFIG
blockchain = Blockchain(os.path.expanduser('~/snap/bitcoin-core/common/.bitcoin/blocks'))
bitcoinlocalpath = '/home/aagiron/snap/bitcoin-core/common/.bitcoin/blocks'

#Initial file names
LSBytesFileName = "extracted/LSBytesFromEveryNonce_FullBlockchainChunk1.data"
Bytes1FileName = "extracted/Bytes1FromEveryNonce_FullBlockchainChunk1.data"
Bytes2FileName = "extracted/Bytes2FromEveryNonce_FullBlockchainChunk1.data"
MSBytesFileName = "extracted/MSBytesFromEveryNonce_FullBlockchainChunk1.data"
AM_fileName = "extracted/AM_Chunk1.txt"

#open files
arqLSBytes = open(LSBytesFileName, 'ab')
arqBytes1 = open(Bytes1FileName, 'ab')
arqBytes2 = open(Bytes2FileName, 'ab')
arqMSBytes = open(MSBytesFileName, 'ab')
arqArithmeticMean = open(AM_fileName, 'a')