本文整理汇总了Python中BCDataStream.map_file方法的典型用法代码示例。如果您正苦于以下问题:Python BCDataStream.map_file方法的具体用法?Python BCDataStream.map_file怎么用?Python BCDataStream.map_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BCDataStream
的用法示例。
在下文中一共展示了BCDataStream.map_file方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dump_all_transactions
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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: _dump_tx
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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: _dump_tx
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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
示例4: main
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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))
示例5: _dump_block
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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: _dump_block
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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
示例7: main
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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)
示例8: main
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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()
示例9: CachedBlockFile
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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
示例10: _dump_block
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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
示例11: main
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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)
示例12: dumpblock
# 需要导入模块: import BCDataStream [as 别名]
# 或者: from BCDataStream import map_file [as 别名]
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()