Пример #1
0
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()
Пример #2
0
def _dump_tx(datadir, tx_hash, tx_pos):
  blockfile = open(os.path.join(datadir, "blk%04d.dat"%(tx_pos[0],)), "rb")
  ds = BCDataStream()
  ds.map_file(blockfile, tx_pos[2])
  d = parse_Transaction(ds)
  print deserialize_Transaction(d)
  ds.close_file()
  blockfile.close()
Пример #3
0
def _dump_tx(datadir, tx_hash, tx_pos):
  blockfile = open(os.path.join(datadir, "blk%04d.dat"%(tx_pos[0],)), "rb")
  ds = BCDataStream()
  ds.map_file(blockfile, tx_pos[2])
  d = parse_Transaction(ds)
  print deserialize_Transaction(d)
  ds.close_file()
  blockfile.close()
Пример #4
0
def _dump_tx(datadir, tx_hash, tx_pos):
  BLOCK_HEADER_SIZE = 80
  blockfile = open(os.path.join(datadir, "blocks","blk%05d.dat"%(tx_pos[0],)), "rb")
  ds = BCDataStream()
  ds.map_file(blockfile, tx_pos[1]+BLOCK_HEADER_SIZE+tx_pos[2])
  tx = parse_Transaction(ds)
  tx['hash'] = tx_hash[::-1]
  ds.close_file()
  blockfile.close()
  return tx
Пример #5
0
def _dump_block(datadir, nFile, nBlockPos, blkhash):
  blockfile = open(os.path.join(datadir,"blocks","blk%05d.dat"%(nFile,)), "rb")
  ds = BCDataStream()
  ds.map_file(blockfile, nBlockPos)
  block_start = ds.read_cursor
  block = parse_Block(ds)
  block_end = ds.read_cursor
  block['blk_size'] = (block_end - block_start)
  ds.close_file()
  blockfile.close()
  return block
Пример #6
0
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 = 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 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))
Пример #7
0
def _dump_tx(datadir, tx_hash, tx_pos):
    BLOCK_HEADER_SIZE = 80
    blockfile = open(
        os.path.join(datadir, "blocks", "blk%05d.dat" % (tx_pos[0], )), "rb")
    ds = BCDataStream()
    ds.map_file(blockfile, tx_pos[1] + BLOCK_HEADER_SIZE + tx_pos[2])
    tx = parse_Transaction(ds)
    tx['hash'] = tx_hash[::-1]
    ds.close_file()
    blockfile.close()
    return tx
Пример #8
0
def _dump_block(datadir, nFile, nBlockPos, blkhash):
    blockfile = open(
        os.path.join(datadir, "blocks", "blk%05d.dat" % (nFile, )), "rb")
    ds = BCDataStream()
    ds.map_file(blockfile, nBlockPos)
    block_start = ds.read_cursor
    block = parse_Block(ds)
    block_end = ds.read_cursor
    block['blk_size'] = (block_end - block_start)
    ds.close_file()
    blockfile.close()
    return block
Пример #9
0
def _dump_block(datadir, nFile, nBlockPos, hash256, hashNext, do_print=True):
    blockfile = open(os.path.join(datadir, "blk%04d.dat" % (nFile,)), "rb")
    ds = BCDataStream()
    ds.map_file(blockfile, nBlockPos)
    d = parse_Block(ds)
    block_string = deserialize_Block(d)
    ds.close_file()
    blockfile.close()
    if do_print:
        print "BLOCK " + long_hex(hash256[::-1])
        print "Next block: " + long_hex(hashNext[::-1])
        print block_string
    return block_string
Пример #10
0
def _dump_block(datadir, nFile, nBlockPos, hash256, hashNext, do_print=True):
    blockfile = open(os.path.join(datadir, "blk%04d.dat" % (nFile, )), "rb")
    ds = BCDataStream()
    ds.map_file(blockfile, nBlockPos)
    d = parse_Block(ds)
    block_string = deserialize_Block(d)
    ds.close_file()
    blockfile.close()
    if do_print:
        print "BLOCK " + long_hex(hash256[::-1])
        print "Next block: " + long_hex(hashNext[::-1])
        print block_string
    return block_string
Пример #11
0
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)
Пример #12
0
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()
Пример #13
0
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)
Пример #14
0
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']))
Пример #15
0
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()
Пример #16
0
class CachedBlockFile(object):
  def __init__(self, db_dir):
    self.datastream = None
    self.file = None
    self.n = None
    self.db_dir = db_dir

  def get_stream(self, n):
    if self.n == n:
      return self.datastream
    if self.datastream is not None:
      self.datastream.close_file()
      self.file.close()
    self.n = n
    self.file = open(os.path.join(self.db_dir, "blk%04d.dat"%(n,)), "rb")
    self.datastream = BCDataStream()
    self.datastream.map_file(self.file, 0)
    return self.datastream
Пример #17
0
class CachedBlockFile(object):
    def __init__(self, db_dir):
        self.datastream = None
        self.file = None
        self.n = None
        self.db_dir = db_dir

    def get_stream(self, n):
        if self.n == n:
            return self.datastream
        if self.datastream is not None:
            self.datastream.close_file()
            self.file.close()
        self.n = n
        self.file = open(os.path.join(self.db_dir, "blk%04d.dat" % (n, )),
                         "rb")
        self.datastream = BCDataStream()
        self.datastream.map_file(self.file, 0)
        return self.datastream
Пример #18
0
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()
Пример #19
0
def _dump_block(datadir,
                nFile,
                nBlockPos,
                hash256,
                hashNext,
                do_print=True,
                print_raw_tx=False,
                print_json=False):
    blockfile = open(os.path.join(datadir, "blk%04d.dat" % (nFile, )), "rb")
    ds = BCDataStream()
    ds.map_file(blockfile, nBlockPos)
    d = parse_Block(ds)
    block_string = deserialize_Block(d, print_raw_tx)
    ds.close_file()
    blockfile.close()
    if do_print:
        print "BLOCK " + long_hex(hash256[::-1])
        print "Next block: " + long_hex(hashNext[::-1])
        print block_string
    elif print_json:
        import json
        print json.dumps({
            'version':
            d['version'],
            'previousblockhash':
            d['hashPrev'][::-1].encode('hex'),
            'transactions':
            [tx_hex['__data__'].encode('hex') for tx_hex in d['transactions']],
            'time':
            d['nTime'],
            'bits':
            hex(d['nBits']).lstrip("0x"),
            'nonce':
            d['nNonce']
        })

    return block_string
Пример #20
0
def _dump_block(datadir, nFile, nBlockPos, hash256, hashNext, do_print=True, print_raw_tx=False, print_json=False):
  blockfile = open(os.path.join(datadir, "blk%04d.dat"%(nFile,)), "rb")
  ds = BCDataStream()
  ds.map_file(blockfile, nBlockPos)
  d = parse_Block(ds)
  block_string = deserialize_Block(d, print_raw_tx)
  ds.close_file()
  blockfile.close()
  if do_print:
    print "BLOCK "+long_hex(hash256[::-1])
    print "Next block: "+long_hex(hashNext[::-1])
    print block_string
  elif print_json:
    import json
    print json.dumps({
                        'version': d['version'],
                        'previousblockhash': d['hashPrev'][::-1].encode('hex'),
                        'transactions' : [ tx_hex['__data__'].encode('hex') for tx_hex in d['transactions'] ],
                        'time' : d['nTime'],
                        'bits' : hex(d['nBits']).lstrip("0x"),
                        'nonce' : d['nNonce']
                      })

  return block_string
Пример #21
0
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)
Пример #22
0
 def dumpblock(self,block_position, block_time, opened_file):
     ds = BCDataStream()
     ds.map_file(opened_file, block_position)
     d = parse_Block(ds)
     deserialize_Block(d, block_time, self.addresses)
     ds.close_file()
Пример #23
0
 def dumpblock(self, block_position, block_time, opened_file):
     ds = BCDataStream()
     ds.map_file(opened_file, block_position)
     d = parse_Block(ds)
     deserialize_Block(d, block_time, self.addresses)
     ds.close_file()