区块链趋势高防服务器租用 服务器比特币区块数据的解析python实现国内服务器使用

  1. 11 月前

    区块链趋势高防服务器租用 服务器比特币区块数据的解析python实现国内服务器使用
    服务器咨询敏敏QQ:2853898501 / 3007425289 电话微信:18316411879

    这里我主要解析下比特币的区块数据是怎么样的
    区块整体结构
    一个完整的区块结构主要由以下几部分构成:
    QQ截图20191122095020.png
    区块头结构
    QQ截图20191122095030.png

    区块头(80字节):
    0100000055bd840a78798ad0da853f68974f3d183e2bd1db6a842c1feecf222a00000000ff104ccb05421ab93e63f8c3ce5c2c2e9dbb37de2764b3a3175c8166562cac7d51b96a49ffff001d283e9e70

    版本号(4字节):01000000

    父区块头hash值(32字节):55bd840a78798ad0da853f68974f3d183e2bd1db6a842c1feecf222a00000000

    merkle根(32字节):ff104ccb05421ab93e63f8c3ce5c2c2e9dbb37de2764b3a3175c8166562cac7d

    时间戳(4字节):51b96a49

    难度(4字节):ffff001d

    Nonce(4字节):283e9e70

    代码实现:

    class block():

    def __init__(self, handle):

    if type(handle)==str: handle=fstring(handle)
    self.magic = handle.read(4)
    self.size = sum([ord(handle.read(1))*(256**x) for x in range(4)])
    startingpos = handle.tell()

    self.version = sum([ord(handle.read(1))*(256**x) for x in range(4)])
    self.prevblock = handle.read(32)[::-1].encode('hex')
    self.merkleroot = handle.read(32)[::-1].encode('hex')
    self.timestamp = time.gmtime(sum([ord(handle.read(1))*(256**x) for x in range(4)]))
    self.bits = handle.read(4)
    self.nonce = sum([ord(handle.read(1))*(256**x) for x in range(4)])

    handle.seek(startingpos)
    self.header = handle.read(80)
    self.hash = hash256(self.header)[::-1].encode('hex')

    self.txcount = ord(handle.read(1))
    if self.txcount>=253: self.txcount = sum([ord(handle.read(1))*(256**x) for x in range(2*(self.txcount-252))])
    self.tx = [transaction(handle) for txn in range(self.txcount)]
    self.size = handle.tell() - startingpos; handle.seek(startingpos)
    self.raw = handle.read(self.size)
    #self.merkletree
    #self.isvalid() to verify merkle tree, hash, and difficulty

    交易同理,只要掌握了它的数据结构解析出来就很简单了,只是表示形式不同而已,这里可以参考:

    https://www.jianshu.com/p/8289df50112f

    解析交易代码实现:

    class transaction():

    def __init__(self, handle):

    if type(handle)==str: handle=fstring(handle)
    startingpos = handle.tell()

    self.version = sum([ord(handle.read(1))*(256**x) for x in range(4)])
    self.numinputs = ord(handle.read(1))
    if self.numinputs>=253: self.numinputs = sum([ord(handle.read(1))*(256**x) for x in range(2*(self.numinputs-252))])
    self.inputs = [txinput(handle) for inputnum in range(self.numinputs)]
    self.numoutputs = ord(handle.read(1))
    if self.numoutputs>=253: self.numoutputs = sum([ord(handle.read(1))*(256**x) for x in range(2*(self.numoutputs-252))])
    self.outputs = [txoutput(handle) for outputnum in range(self.numoutputs)]
    self.locktime = sum([ord(handle.read(1))*(256**x) for x in range(4)])
    self.size = handle.tell() - startingpos; handle.seek(startingpos)
    self.raw = handle.read(self.size)
    self.hash = hash256(self.raw)[::-1].encode('hex')
    以上是比较原始的方法,你可以借助其他工具,我这里总结了一些,希望对你有用;

    (1)本地rpc的方法:
    https://www.jianshu.com/p/514512224e68

    bitcoind -damon

    bitcoin-cli -rpcconnect=127.0.0.1 -rpcuser=rpc -rpcpassword=123 getblock 00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048

    (2) 直接读取
    了解区块数据结构即可,参考 https://github.com/rigmarole/blk-reader

    (3) 借用工具
    https://github.com/alecalve/python-bitcoin-blockchain-parser

    (4) 借助API
    https://github.com/blockchain/api-v1-client-python

 

或者 注册后参与话题.