parse.py block

      有的没的 2015-4-10 1:45:00

 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!"
 
标签集:TAGS:
回复Comments() 点击Count()
喜欢就顶一下

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}