Пример #1
0
    def fill_from_rpc(self, data):
        '''Convert getblocktemplate result into BlockTemplate instance'''
        if 'height' not in data:
            log.info("Waiting for new work...")
            self.prevhash_hex = self.blank_hash
            self.broadcast_args = self.build_fake_broadcast_args()
            return

        txhashes = [None] + [
            util.ser_uint320(int(t['hash'], 16)) for t in data['transactions']
        ]
        mt = merkletree.MerkleTree(txhashes)

        self.height = data['height']
        self.nVersion = data['version']
        self.hashPrevBlock = int(data['previousblockhash'], 16)
        self.nBits = int(data['bits'], 16)
        self.nBitsHex = data['bits']

        self.hashMerkleRoot = 0
        self.nTime = 0
        self.nNonce = 0
        self.cbTxTime = int(self.timestamper.time())
        self.nTxTime = self.cbTxTime * 1000
        self.nHashCoin = 0
        self.sigchecksum = 0

        coinbase = CoinbaseTransaction(self.timestamper, self.coinbaser,
                                       data['coinbasevalue'],
                                       data['coinbaseaux']['flags'],
                                       data['height'],
                                       settings.COINBASE_EXTRAS, self.cbTxTime)

        self.vtx = [
            coinbase,
        ]
        for tx in data['transactions']:
            t = halfnode.CTransaction()
            t.deserialize(StringIO.StringIO(binascii.unhexlify(tx['data'])))
            self.vtx.append(t)

        self.curtime = data['curtime']
        self.timedelta = self.curtime - int(self.timestamper.time())
        self.merkletree = mt
        self.target = int((data['target']), 16)
        log.info("Block height: %i network difficulty: %s" %
                 (self.height, self.diff_to_t(self.target)))

        # Reversed prevhash
        #self.prevhash_bin = binascii.unhexlify(util.reverse_hash_80(data['previousblockhash']))
        self.prevhash_bin = binascii.unhexlify(
            util.rev(data['previousblockhash']))
        self.prevhash_hex = "%080x" % self.hashPrevBlock
        #log.info("%s\n", repr(self))

        self.broadcast_args = self.build_broadcast_args()
Пример #2
0
    def fill_from_rpc(self, data):
        '''Convert getblocktemplate result into BlockTemplate instance'''

        #txhashes = [None] + [ binascii.unhexlify(t['hash']) for t in data['transactions'] ]
        txhashes = [None] + [
            util.ser_uint256(int(t['hash'], 16)) for t in data['transactions']
        ]
        mt = merkletree.MerkleTree(txhashes)

        coinbase = CoinbaseTransaction(self.timestamper, self.coinbaser,
                                       data['coinbasevalue'],
                                       data['coinbaseaux']['flags'],
                                       data['height'],
                                       settings.COINBASE_EXTRAS,
                                       data['curtime'])

        self.height = data['height']
        self.nVersion = data['version']
        self.hashPrevBlock = int(data['previousblockhash'], 16)
        self.nBits = int(data['bits'], 16)

        self.hashMerkleRoot = 0
        self.nTime = 0
        self.nNonce = 0
        self.vtx = [
            coinbase,
        ]

        for tx in data['transactions']:
            t = halfnode.CTransaction()
            t.deserialize(StringIO.StringIO(binascii.unhexlify(tx['data'])))
            self.vtx.append(t)

        self.curtime = data['curtime']
        self.timedelta = self.curtime - int(self.timestamper.time())
        self.merkletree = mt
        self.target = util.uint256_from_compact(self.nBits)

        # Reversed prevhash
        self.prevhash_bin = binascii.unhexlify(
            util.reverse_hash(data['previousblockhash']))
        self.prevhash_hex = "%064x" % self.hashPrevBlock

        self.broadcast_args = self.build_broadcast_args()
        log.info("Block height: %i network difficulty: %s" %
                 (self.height, util.diff_to_target(self.target)))
Пример #3
0
    def fill_from_rpc(self, data, aux_data):
        '''Convert getblocktemplate result into BlockTemplate instance'''

        self.auxs = aux_data
        self.tree, self.merkle_size = util.make_auxpow_tree(aux_data)
        self.aux_targets = [None for i in self.auxs]
        merkle_leaves = [('0' * 64) for x in range(self.merkle_size)]

        for chain in range(len(self.auxs)):
            merkle_index = self.tree[self.auxs[chain]['chainid']]
            merkle_leaves[merkle_index] = self.auxs[chain]['hash']
            target = self.auxs[chain]['target'].decode('hex')[::-1].encode(
                'hex')
            self.aux_targets[chain] = int(target, 16)
            log.info("Merged Chain: %i network difficulty: %s" %
                     (self.auxs[chain]['chainid'],
                      float(util.diff_to_target(self.aux_targets[chain]))))

        self.merkle_hashes = [int(t, 16) for t in merkle_leaves]
        self.mm_data = '\xfa\xbemm' + util.aux_pow_coinbase_type.pack(
            dict(
                merkle_root=util.merkle_hash(self.merkle_hashes),
                size=self.merkle_size,
                nonce=0,
            ))

        txhashes = [None] + [
            util.ser_uint256(int(t['hash'], 16)) for t in data['transactions']
        ]
        mt = merkletree.MerkleTree(txhashes)

        coinbase = CoinbaseTransaction(self.timestamper, self.coinbaser,
                                       data['coinbasevalue'],
                                       data['coinbaseaux']['flags'],
                                       data['height'],
                                       settings.COINBASE_EXTRAS + self.mm_data,
                                       data['curtime'])

        self.height = data['height']
        self.nVersion = data['version']
        self.hashPrevBlock = int(data['previousblockhash'], 16)
        self.nBits = int(data['bits'], 16)
        self.hashMerkleRoot = 0
        self.nTime = 0
        self.nNonce = 0
        self.vtx = [
            coinbase,
        ]

        for tx in data['transactions']:
            t = halfnode.CTransaction()
            t.deserialize(StringIO.StringIO(binascii.unhexlify(tx['data'])))
            self.vtx.append(t)

        self.curtime = data['curtime']
        self.timedelta = self.curtime - int(self.timestamper.time())
        self.merkletree = mt
        self.target = util.uint256_from_compact(self.nBits)
        log.info("MainNet Block Height: %i network difficulty: %s" %
                 (self.height, float(util.diff_to_target(self.target))))

        # Reversed prevhash
        self.prevhash_bin = binascii.unhexlify(
            util.reverse_hash(data['previousblockhash']))
        self.prevhash_hex = "%064x" % self.hashPrevBlock

        self.broadcast_args = self.build_broadcast_args()
    def fill_from_rpc(self, data):
        '''Convert getblocktemplate result into BlockTemplate instance'''

        commitment = None
        nTime = data['curtime'] if data.has_key('curtime') else None

        if settings.COINDAEMON_HAS_SEGWIT:
            txids = []
            hashes = [None] + [
                util.ser_uint256(int(t['hash'], 16))
                for t in data['transactions']
            ]
            try:
                txids = [None] + [
                    util.ser_uint256(int(t['txid'], 16))
                    for t in data['transactions']
                ]
                mt = merkletree.MerkleTree(txids)
            except KeyError:
                mt = merkletree.MerkleTree(hashes)

            wmt = merkletree.MerkleTree(hashes).withFirst(
                binascii.unhexlify(
                    '0000000000000000000000000000000000000000000000000000000000000000'
                ))
            self.witness = SHA256.new(
                SHA256.new(wmt + witness_nonce).digest()).digest()
            commitment = b'\x6a' + struct.pack(
                ">b",
                len(self.witness) +
                len(witness_magic)) + witness_magic + self.witness
            try:
                default_witness = data['default_witness_commitment']
                commitment_check = binascii.unhexlify(default_witness)
                if (commitment != commitment_check):
                    print(
                        "calculated witness does not match supplied one! This block probably will not be accepted!"
                    )
                    commitment = commitment_check
            except KeyError:
                pass
            self.witness = commitment[6:]
        else:
            txhashes = [None] + [
                util.ser_uint256(int(t['hash'], 16))
                for t in data['transactions']
            ]
            mt = merkletree.MerkleTree(txhashes)

        coinbase = CoinbaseTransaction(self.timestamper, self.coinbaser,
                                       data['coinbasevalue'],
                                       data['coinbaseaux']['flags'],
                                       data['height'], commitment,
                                       settings.COINBASE_EXTRAS, nTime)

        self.height = data['height']
        self.nVersion = data['version']
        self.hashPrevBlock = int(data['previousblockhash'], 16)
        self.nBits = int(data['bits'], 16)
        self.hashMerkleRoot = 0
        self.nTime = 0
        self.nNonce = 0
        self.vtx = [
            coinbase,
        ]

        for tx in data['transactions']:
            t = TxBlob()
            t.deserialize(binascii.unhexlify(tx['data']))
            self.vtx.append(t)

        self.curtime = data['curtime']
        self.timedelta = self.curtime - int(self.timestamper.time())
        self.merkletree = mt
        self.target = util.uint256_from_compact(self.nBits)

        # Reversed prevhash
        self.prevhash_bin = binascii.unhexlify(
            util.reverse_hash(data['previousblockhash']))
        self.prevhash_hex = "%064x" % self.hashPrevBlock

        self.broadcast_args = self.build_broadcast_args()