def dump_all_transactions(datadir, db_env): """ Dump all transactions. """ blockfile = open(os.path.join(datadir, "blk%04d.dat"%(1,)), "rb") block_datastream = BCDataStream() block_datastream.map_file(blockfile, 0) def for_each_block(block_data): block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) block_datetime = datetime.fromtimestamp(data['nTime']) dt = "%d-%02d-%02d-%02d-%02d-%02d"%(block_datetime.year, block_datetime.month, block_datetime.day, block_datetime.hour, block_datetime.minute, block_datetime.second) for txn in data['transactions']: try: for txIn in txn['txIn']: if txIn['prevout_hash'] == "\x00"*32: print 'in\t' + txn['hash'] + '\tcoinbase\t' + dt else: pk = extract_public_key(txIn['scriptSig']) print 'in\t' + txn['hash'] + '\t' + long_hex(txIn['prevout_hash'][::-1]) + '\t' + str(txIn['prevout_n']) + '\t' + pk + '\t' + dt index = 0 for txOut in txn['txOut']: pk = extract_public_key(txOut['scriptPubKey']) print 'out\t' + txn['hash'] + '\t' + str(index) + '\t' + pk + '\t' + str(txOut['value']/1.0e8) + '\t' + dt index += 1 except: pass return True scan_blocks(datadir, db_env, for_each_block) db_env.close()
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option( "--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)" ) parser.add_option( "--regex", dest="lookfor", default="/P2SH/", help="Look for string/regular expression (default: %default)" ) parser.add_option( "--n", dest="howmany", default=999999, type="int", help="Look back this many blocks (default: all)" ) parser.add_option( "--start", dest="start", default=0, type="int", help="Skip this many blocks to start (default: 0)" ) parser.add_option("--verbose", dest="verbose", default=False, action="store_true", help="Print blocks that match") (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = CachedBlockFile(db_dir) results = defaultdict(int) def count_matches(block_data): block_datastream = blockfile.get_stream(block_data["nFile"]) block_datastream.seek_file(block_data["nBlockPos"]) data = parse_Block(block_datastream) coinbase = data["transactions"][0] scriptSig = coinbase["txIn"][0]["scriptSig"] if results["skipped"] < options.start: results["skipped"] += 1 else: results["checked"] += 1 if re.search(options.lookfor, scriptSig) is not None: results["matched"] += 1 if options.verbose: print("Block %d : %s" % (block_data["nHeight"], scriptSig.encode("string_escape"))) results["searched"] += 1 return results["searched"] < options.howmany scan_blocks(db_dir, db_env, count_matches) db_env.close() percent = (100.0 * results["matched"]) / results["checked"] print("Found %d matches in %d blocks (%.1f percent)" % (results["matched"], results["checked"], percent))
def scan(): # Return the height, work, and hash value for each block result = [] def scan_callback(block_data): height = block_data['nHeight'] work = target_to_work(nbits_to_target(block_data['nBits'])) value = hash_value(long(hexlify(block_data['hash256'][::-1]), 16)) result.append((height,work,value)) return True scan_blocks(db_dir, db_env, scan_callback) return np.array(result, dtype='f8')[::-1]
def scan(): # Return the height, work, and hash value for each block result = [] def scan_callback(block_data): height = block_data['nHeight'] work = target_to_work(nbits_to_target(block_data['nBits'])) value = hash_value(long(hexlify(block_data['hash256'][::-1]), 16)) result.append((height, work, value)) return True scan_blocks(db_dir, db_env, scan_callback) return np.array(result, dtype='f8')[::-1]
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option("--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)") (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = open(os.path.join(db_dir, "blk%04d.dat" % (1, )), "rb") block_datastream = BCDataStream() block_datastream.map_file(blockfile, 0) n_transactions = {} v_transactions = {} def gather_stats(block_data): block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) block_date = date.fromtimestamp(data['nTime']) key = "%d-%02d" % (block_date.year, block_date.month) for txn in data['transactions'][1:]: for txout in txn['txOut']: if key in n_transactions: n_transactions[key] += 1 v_transactions[key] += txout['value'] else: n_transactions[key] = 1 v_transactions[key] = txout['value'] return True scan_blocks(db_dir, db_env, gather_stats) db_env.close() keys = n_transactions.keys() keys.sort() for k in keys: v = v_transactions[k] / 1.0e8 print "%s,%d,%.2f" % (k, n_transactions[k], v)
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option("--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)") (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = open(os.path.join(db_dir, "blk%04d.dat" % (1, )), "rb") block_datastream = BCDataStream() block_datastream.map_file(blockfile, 0) def gather(block_data): block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) height = block_data['nHeight'] coinbase = data['transactions'][0] scriptSig = coinbase['txIn'][0]['scriptSig'] if len(scriptSig) < 4: return True (n, ) = struct.unpack_from('<I', scriptSig[0:4]) if n < 6 * 24 * 365.25 * 100: # 200 years of blocks: print("%d: %d (%s) version: %d/%d" % (height, n, approx_date(n), block_data['b_version'], coinbase['version'])) if ord(scriptSig[0]) == 0x03: (n, ) = struct.unpack_from('<I', scriptSig[1:4] + '\0') if n < 6 * 24 * 365.25 * 100: # 200 years of blocks: print("%d: PUSH %d (%s) version: %d/%d" % (height, n, approx_date(n), block_data['b_version'], coinbase['version'])) return True scan_blocks(db_dir, db_env, gather) db_env.close()
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option("--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)") (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = open(os.path.join(db_dir, "blk%04d.dat"%(1,)), "rb") block_datastream = BCDataStream() block_datastream.map_file(blockfile, 0) n_transactions = { } v_transactions = { } def gather_stats(block_data): block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) block_date = date.fromtimestamp(data['nTime']) key = "%d-%02d"%(block_date.year, block_date.month) for txn in data['transactions'][1:]: for txout in txn['txOut']: if key in n_transactions: n_transactions[key] += 1 v_transactions[key] += txout['value'] else: n_transactions[key] = 1 v_transactions[key] = txout['value'] return True scan_blocks(db_dir, db_env, gather_stats) db_env.close() keys = n_transactions.keys() keys.sort() for k in keys: v = v_transactions[k]/1.0e8 print "%s,%d,%.2f"%(k, n_transactions[k], v)
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option("--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)") parser.add_option("--regex", dest="lookfor", default="OP_EVAL", help="Look for string/regular expression") parser.add_option("--n", dest="howmany", default=999999, type="int", help="Look back this many blocks (default: all)") parser.add_option("--verbose", dest="verbose", default=False, action="store_true", help="Print blocks that match") (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = open(os.path.join(db_dir, "blk%04d.dat"%(1,)), "rb") block_datastream = BCDataStream() block_datastream.map_file(blockfile, 0) results = defaultdict(int) def count_matches(block_data): block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) coinbase = data['transactions'][0] scriptSig = coinbase['txIn'][0]['scriptSig'] if re.search(options.lookfor, scriptSig) is not None: results['matched'] += 1 if options.verbose: print("Block %d : %s"%(block_data['nHeight'], scriptSig.encode('string_escape')) ) results['searched'] += 1 return results['searched'] < options.howmany scan_blocks(db_dir, db_env, count_matches) db_env.close() print("Found %d matches in %d blocks\n"%(results['matched'], results['searched']))
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option("--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)") parser.add_option("--regex", dest="lookfor", default="OP_EVAL", help="Look for string/regular expression") parser.add_option("--n", dest="howmany", default=-1, type="int", help="Look back this many blocks (default: all)") parser.add_option("--verbose", dest="verbose", default=False, action="store_true", help="Print blocks that match") (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = open(os.path.join(db_dir, "blk%04d.dat"%(1,)), "rb") block_datastream = BCDataStream() block_datastream.map_file(blockfile, 0) results = defaultdict(int) def count_matches(block_data): block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) coinbase = data['transactions'][0] scriptSig = coinbase['txIn'][0]['scriptSig'] if re.search(options.lookfor, scriptSig) is not None: results['matched'] += 1 if options.verbose: print("Block %d : %s"%(block_data['nHeight'], scriptSig.encode('string_escape')) ) results['searched'] += 1 return results['searched'] < options.howmany scan_blocks(db_dir, db_env, count_matches) db_env.close() print("Found %d matches in %d blocks\n"%(results['matched'], results['searched']))
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option( "--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)" ) (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = open(os.path.join(db_dir, "blk%04d.dat" % (1,)), "rb") block_datastream = BCDataStream() block_datastream.map_file(blockfile, 0) def gather(block_data): block_datastream.seek_file(block_data["nBlockPos"]) data = parse_Block(block_datastream) height = block_data["nHeight"] coinbase = data["transactions"][0] scriptSig = coinbase["txIn"][0]["scriptSig"] if len(scriptSig) < 4: return True (n,) = struct.unpack_from("<I", scriptSig[0:4]) if n < 6 * 24 * 365.25 * 100: # 200 years of blocks: print("%d: %d (%s)" % (height, n, approx_date(n))) if ord(scriptSig[0]) == 0x03: (n,) = struct.unpack_from("<I", scriptSig[1:4] + "\0") if n < 6 * 24 * 365.25 * 100: # 200 years of blocks: print("%d: PUSH %d (%s)" % (height, n, approx_date(n))) return True scan_blocks(db_dir, db_env, gather) db_env.close()
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option("--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)") (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = CachedBlockFile(db_dir) def gather(block_data): block_datastream = blockfile.get_stream(block_data['nFile']) block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) height = block_data['nHeight'] coinbase = data['transactions'][0] scriptSig = coinbase['txIn'][0]['scriptSig'] if len(scriptSig) < 4: return True (n,) = struct.unpack_from('<I', scriptSig[0:4]) if n < 6*24*365.25*100: # 200 years of blocks: print("%d: %d (%s) version: %d/%d"%(height, n, approx_date(n), block_data['b_version'],coinbase['version'])) if ord(scriptSig[0]) == 0x03: (n,) = struct.unpack_from('<I', scriptSig[1:4]+'\0') if n < 6*24*365.25*100: # 200 years of blocks: print("%d: PUSH %d (%s) version: %d/%d"%(height, n, approx_date(n), block_data['b_version'],coinbase['version'])) return True scan_blocks(db_dir, db_env, gather) db_env.close()
def dump_all_transactions(datadir, db_env): """ Dump all transactions. """ blockfile = open(os.path.join(datadir, "blk%04d.dat" % (1, )), "rb") block_datastream = BCDataStream() block_datastream.map_file(blockfile, 0) def for_each_block(block_data): block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) block_datetime = datetime.fromtimestamp(data['nTime']) dt = "%d-%02d-%02d-%02d-%02d-%02d" % ( block_datetime.year, block_datetime.month, block_datetime.day, block_datetime.hour, block_datetime.minute, block_datetime.second) for txn in data['transactions']: try: for txIn in txn['txIn']: if txIn['prevout_hash'] == "\x00" * 32: print 'in\t' + txn['hash'] + '\tcoinbase\t' + dt else: pk = extract_public_key(txIn['scriptSig']) print 'in\t' + txn['hash'] + '\t' + long_hex( txIn['prevout_hash'][::-1]) + '\t' + str( txIn['prevout_n']) + '\t' + pk + '\t' + dt index = 0 for txOut in txn['txOut']: pk = extract_public_key(txOut['scriptPubKey']) print 'out\t' + txn['hash'] + '\t' + str( index) + '\t' + pk + '\t' + str( txOut['value'] / 1.0e8) + '\t' + dt index += 1 except: pass return True scan_blocks(datadir, db_env, for_each_block) db_env.close()
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option("--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)") parser.add_option("--week", dest="week", default=False, action="store_true", help="Dump day-by-day for the last week's worth of blocks") (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = open(os.path.join(db_dir, "blk%04d.dat"%(1,)), "rb") block_datastream = BCDataStream() block_datastream.map_file(blockfile, 0) n_transactions = defaultdict(int) v_transactions = defaultdict(float) v_transactions_min = defaultdict(float) v_transactions_max = defaultdict(float) def gather_stats(block_data): block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) block_date = date.fromtimestamp(data['nTime']) key = "%d-%02d"%(block_date.year, block_date.month) for txn in data['transactions'][1:]: values = [] for txout in txn['txOut']: n_transactions[key] += 1 v_transactions[key] += txout['value'] values.append(txout['value']) v_transactions_min[key] += min(values) v_transactions_max[key] += max(values) return True def gather_stats_week(block_data, lastDate): block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) block_date = date.fromtimestamp(data['nTime']) if block_date < lastDate: return False key = "%d-%02d-%02d"%(block_date.year, block_date.month, block_date.day) for txn in data['transactions'][1:]: values = [] for txout in txn['txOut']: n_transactions[key] += 1 v_transactions[key] += txout['value'] values.append(txout['value']) v_transactions_min[key] += min(values) v_transactions_max[key] += max(values) return True if options.week: lastDate = date.fromordinal(date.today().toordinal()-7) scan_blocks(db_dir, db_env, lambda x: gather_stats_week(x, lastDate) ) else: scan_blocks(db_dir, db_env, gather_stats) db_env.close() print "date,nTransactions,minBTC,maxBTC,totalBTC" keys = n_transactions.keys() keys.sort() for k in keys: v = v_transactions[k]/1.0e8 v_min = v_transactions_min[k]/1.0e8 v_max = v_transactions_max[k]/1.0e8 # Columns are: # month n_transactions min max total # ... where min and max add up just the smallest or largest # output in each transaction; the true value of bitcoins # transferred will be somewhere between min and max. # We don't know how many are transfers-to-self, though, and # this will undercount multi-txout-transactions (which is good # right now, because they're mostly used for mining pool # payouts that arguably shouldn't count). print "%s,%d,%.2f,%.2f,%.2f"%(k, n_transactions[k], v_min, v_max, v)
def main(): import optparse parser = optparse.OptionParser(usage="%prog [options]") parser.add_option("--datadir", dest="datadir", default=None, help="Look for files here (defaults to bitcoin default)") parser.add_option( "--regex", dest="lookfor", default="/P2SH/", help="Look for string/regular expression (default: %default)") parser.add_option("--n", dest="howmany", default=999999, type="int", help="Look back this many blocks (default: all)") parser.add_option("--start", dest="start", default=0, type="int", help="Skip this many blocks to start (default: 0)") parser.add_option("--verbose", dest="verbose", default=False, action="store_true", help="Print blocks that match") (options, args) = parser.parse_args() if options.datadir is None: db_dir = determine_db_dir() else: db_dir = options.datadir try: db_env = create_env(db_dir) except DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) blockfile = CachedBlockFile(db_dir) results = defaultdict(int) def count_matches(block_data): block_datastream = blockfile.get_stream(block_data['nFile']) block_datastream.seek_file(block_data['nBlockPos']) data = parse_Block(block_datastream) coinbase = data['transactions'][0] scriptSig = coinbase['txIn'][0]['scriptSig'] if results['skipped'] < options.start: results['skipped'] += 1 else: results['checked'] += 1 if re.search(options.lookfor, scriptSig) is not None: results['matched'] += 1 if options.verbose: print("Block %d : %s" % (block_data['nHeight'], scriptSig.encode('string_escape'))) results['searched'] += 1 return results['searched'] < options.howmany scan_blocks(db_dir, db_env, count_matches) db_env.close() percent = (100.0 * results['matched']) / results['checked'] print("Found %d matches in %d blocks (%.1f percent)" % (results['matched'], results['checked'], percent))