2015/04/10 | parse.py block
类别(有的没的) | 评论(0) | 阅读(29) | 发表于 01:45

 import blkreader

import time
import sys
from os.path import expanduser
 
# change this to the folder where blk0001.dat is located
try:
    homedir = expanduser("~")
    typedir = '/.%s/blocks/' % sys.argv[1]
    targetblocks = 'blk000%s.dat' % sys.argv[2]
    btcdir = str(homedir + typedir + targetblocks)
    if sys.argv[3]:
        moredetail = sys.argv[3]
        print btcdir,moredetail
    else:
        moredetail = False
except:
    print 'Tips:execute coinType blockNumber (true or empty)'
    #btcdir = '/home/litecoin/.litecoin/blocks/blk00001.dat'
# read file
 
def parseblocks():
    blkread = blkreader.blkfile(btcdir)
    genesisblock = blkread.getblock()
    print
    print 'GENESIS BLOCK'
    print ' magic value:   ', genesisblock.magic.encode('hex')
    print ' version:       ', genesisblock.version
    print ' size:          ', genesisblock.size, 'bytes'
    print ' hash:          ', genesisblock.hash
    print ' previous block:', genesisblock.prevblock
    print ' merkle root:   ', genesisblock.merkleroot
    print ' timestamp:     ', time.asctime(genesisblock.timestamp)
    print ' bits:          ', genesisblock.bits.encode('hex')
    print ' nonce:         ', genesisblock.nonce
    print ' header:        ', genesisblock.header.encode('hex')
    print ' number of transactions:', genesisblock.txcount
    for tx in genesisblock.tx:
        print '  tx', genesisblock.tx.index(tx)
        print '   version:', tx.version
        print '   size:   ', tx.size
        print '   hash:   ', tx.hash
        print '   number of inputs:', tx.numinputs
        try:
            if moredetail:
                for txin in tx.inputs:
                   print '    input', tx.inputs.index(txin)
                   print '    previous output:', txin.prevouthash
                   print '    n:', txin.prevoutn
 
                   if txin.prevouthash=='0'*64: 
                       print '    coinbase:', txin.coinbase.__repr__()
                   else: 
                       print '    scriptSig:', txin.script.encode('hex')
 
                   print '    sequence:', txin.sequence
 
                print '   number of outputs:', tx.numoutputs
 
                for txout in tx.outputs:
                   print '    output', tx.outputs.index(txout)
                   print '    value:', txout.value
                   print '    script:', txout.script.encode('hex')
                   print '   lock time:', tx.locktime
        except:
            print 'For more Detail,Use true option in the end '
 
    #print ' raw block:     ',genesisblock.raw.encode('hex')
    print
    print 'position:%s' % btcdir, blkread.tell()
    print
    #print '2nd block,raw:',blkread.readblock().encode('hex')
 
try:
    parseblocks()
except:
    print "No such file!"
 
0

评论Comments

日志分类
首页[193]
flash As[107]
有的没的[59]
数码照片[4]
自由世界[19]
blender[4]