Exemplo n.º 1
0
def dump_utxos(datadir, output_dir, n, convert_segwit, maxT=0, debug=True):

    i = 0
    k = 1

    print('new file')
    f = new_utxo_file(output_dir, k)
    for value in ldb_iter(datadir):

        tx_hash, height, index, amt, script = value
        if convert_segwit:
            script = unwitness(script, debug)

        if debug:
            print(k, i, hexlify(tx_hash[::-1]), height, index,
                  amt, hexlify(script))

        f.write(struct.pack('<QQ', amt, len(script)))
        f.write(script)
        f.write('\n')

        i += 1
        if i % n == 0:
            f.close()

            k += 1
            print('new file: {}'.format(k))
            f = new_utxo_file(output_dir, k)

        if maxT != 0 and i >= maxT:
            break

    f.close()
Exemplo n.º 2
0
def dump_utxos(datadir, output_dir, n, convert_segwit, maxT, debug, fileNum):
    # print("Starting to write Z-transactions")
    j = 0  #total utxo
    i = 0  #keep track of utxo per file
    k = fileNum  #relative number of file
    n = 0  #number of files written

    print('new file')
    f = new_utxo_file(output_dir, k)
    n += 1
    print('new_utxo_file path: ', f)

    for value in ldb_iter(datadir):
        tx_hash, height, index, amt, script = value

        if debug:
            print "Height: %d" % height
            print "Reversed: "
            reversedString = hexlify(tx_hash)
            print("".join(
                reversed([
                    reversedString[x:x + 2]
                    for x in range(0, len(reversedString), 2)
                ])))
            print ""

        if convert_segwit:
            script = unwitness(script, debug)

        f.write(struct.pack('<QQ', amt, len(script)))
        f.write(script)

        # append first sha256(transaction + its length)
        sha = hashlib.sha256()
        sha.update(struct.pack('<QQ', amt, len(script)) + script)
        sha256_hash = sha.digest()
        # print("SHA256: ", hexlify(sha256_hash))
        # print("SHA256: ", hexlify(sha256_hash[::-1]))
        f.write(sha256_hash[::-1])
        f.write('\n')

        i += 1
        j += 1
        if i >= maxT:
            f.close()
            k += 1
            i = 0
            f = new_utxo_file(output_dir, k)
            n += 1

    f.close()
    print("##########################################")
    print("Total T written: \t%d" % j)
    print("##########################################")
    return {'t_transactions_total': j, 't_files_written': n}
Exemplo n.º 3
0
def write_vmcp_data(output_dir, k, vmcp_file):
    def addr_to_script(addr):
        if addr[:2] == 't3':
            scr = 'a914' + a2b_hashed_base58(addr)[2:].encode('hex') + '87'
            return scr.decode('hex')

        assert addr[:2] == 't1'
        k = Key.from_text(addr)
        enc = '76a914' + k.hash160()[1:].encode('hex') + '88ac'
        return enc.decode('hex')

    reader = csv.reader(open(vmcp_file, 'rb'), delimiter=",", quotechar='"')
    print 'writing vmcp data from {} to utxo-{}'.format(
        vmcp_file, utxo_file_name(output_dir, k))

    balances = {}
    for i, line in enumerate(reader):
        if i == 0:
            continue

        line[0] = addr_to_script(line[0])
        assert line[0] not in balances
        assert len(line) == 5
        balances[line[0]] = int(float(line[-1]) * 100E6)

    f = new_utxo_file(output_dir, k)
    for script, amt in balances.iteritems():
        f.write(struct.pack('<QQ', amt, len(script)))
        f.write(script)
        f.write('\n')

    f.close()
    print 'wrote {} records'.format(i)
Exemplo n.º 4
0
def dump_joinsplits(datadir, output_dir, n, maxT, fileNumber, magic):
    trans_counter_perfile = 0 #keep track of transcations per file
    trans_z_total = 0
    maxBlockFile = 9999
    blkFile = 0
    file_num = fileNumber
    duplicates = 0
    files_written = 0
    hashStore = {}

    print("Extracting joinsplits from " + datadir + "/blocks/blk" + '{0:0>5}'.format(blkFile) + ".dat")
    joinsplits = read_blockfile(datadir + "/blocks/blk" + '{0:0>5}'.format(blkFile) + ".dat", magic)

    while len(joinsplits) != 0:
        f = new_utxo_file(output_dir, file_num)  #open a new file
        files_written += 1
        for value in joinsplits:
            lengthStr = "{0:b}".format(len(value)) #bytes length of the transaction
            #format binary length in big-endian (32 bit) format
            if (len(lengthStr) < 32 ):
                while len(lengthStr) < 32:
                    lengthStr = "{0:b}".format(0) + lengthStr
            m = hashlib.md5()
            m.update(value)
            md5_hash = m.digest()

            if md5_hash in hashStore:
                print("Found a duplicate transaction...skipping.")
                duplicates += 1
                joinsplits = joinsplits[1:] #remove duplicate
                continue

            hashStore[md5_hash] = 1
            f.write(lengthStr) #write length of the transaction
            f.write(value)#write actual z-utxo
            # append first sha256(transaction + its length)
            sha = hashlib.sha256()
            sha.update(lengthStr + value)
            sha256_hash = sha.digest()
            # print("SHA256: ", hexlify(sha256_hash))
            # print("SHA256: ", hexlify(sha256_hash[::-1]))
            f.write(sha256_hash[::-1])

            trans_counter_perfile += 1
            trans_z_total += 1

            if maxT != 0 and trans_counter_perfile >= maxT:
                break

        #remove objects from array that were written
        joinsplits = joinsplits[trans_counter_perfile:]
        trans_counter_perfile = 0
        if(len(joinsplits) == 0 and (blkFile <= maxBlockFile)):
            try:
                blkFile += 1
                print("Extracting joinsplits from " + datadir + "/blocks/blk" + '{0:0>5}'.format(blkFile) + ".dat")
                joinsplits = read_blockfile(datadir + "/blocks/blk" + '{0:0>5}'.format(blkFile) + ".dat", magic)
                print()
            except IOError:
                print("Oops! File %s/blocks/blk0000%i.dat doesn't exist..." % (datadir, blkFile))
                f.close()
                break
        file_num += 1
        f.close()

    print("##########################################")
    print 'Found duplicates: \t%d' % duplicates
    print 'Total Z written: \t%s' % trans_z_total
    return { 'z_transactions_total': trans_z_total, 'z_files_written': files_written }