示例#1
0
 def handle_header(new_header, valid=False):
     new_hash = self.net.PARENT.POW_FUNC(
         bitcoin_data.block_header_type.pack(new_header))
     # check that header matches current target
     if new_hash > self.bitcoind_work.value['bits'].target:
         return
     if not valid:
         try:
             _ = (yield self.bitcoind.rpc_getblockheader(new_hash))
         except:
             return
     bitcoind_best_block = self.bitcoind_work.value['previous_block']
     if (self.best_block_header.value is None or
         (new_header['previous_block'] == bitcoind_best_block
          and bitcoin_data.hash_groestl(
              bitcoin_data.block_header_type.pack(
                  self.best_block_header.value)) == bitcoind_best_block
          )  # new is child of current and previous is current
             or (bitcoin_data.hash_groestl(
                 bitcoin_data.block_header_type.pack(new_header))
                 == bitcoind_best_block
                 and self.best_block_header.value['previous_block'] !=
                 bitcoind_best_block)
         ):  # new is current and previous is not a child of current
         self.best_block_header.set(new_header)
示例#2
0
def submit_block_p2p(block, factory, net):
    if factory.conn.value is None:
        print >> sys.stderr, 'No groestlcoind connection when block submittal attempted! %s%064x' % (
            net.PARENT.BLOCK_EXPLORER_URL_PREFIX,
            bitcoin_data.hash_groestl(
                bitcoin_data.block_header_type.pack(block['header'])))
        raise deferral.RetrySilentlyException()
    factory.conn.value.send_block(block=block)
示例#3
0
 def from_header(cls, header):
     return cls(
         bitcoin_data.hash_groestl(
             bitcoin_data.block_header_type.pack(header)),
         header['previous_block'])
示例#4
0
    def __init__(self, net, peer_addr, contents):
        dynamic_types = self.get_dynamic_types(net)
        self.share_info_type = dynamic_types['share_info_type']
        self.share_type = dynamic_types['share_type']
        self.ref_type = dynamic_types['ref_type']

        self.net = net
        self.peer_addr = peer_addr
        self.contents = contents

        self.min_header = contents['min_header']
        self.share_info = contents['share_info']
        self.hash_link = contents['hash_link']
        self.merkle_link = contents['merkle_link']

        segwit_activated = is_segwit_activated(self.VERSION, net)

        if not (2 <= len(self.share_info['share_data']['coinbase']) <= 100):
            raise ValueError(
                '''bad coinbase size! %i bytes''' %
                (len(self.share_info['share_data']['coinbase']), ))

        if len(self.merkle_link['branch']) > 16 or (segwit_activated and len(
                self.share_info['segwit_data']['txid_merkle_link']['branch']) >
                                                    16):
            raise ValueError('merkle branch too long!')

        assert not self.hash_link['extra_data'], repr(
            self.hash_link['extra_data'])

        self.share_data = self.share_info['share_data']
        self.max_target = self.share_info['max_bits'].target
        self.target = self.share_info['bits'].target
        self.timestamp = self.share_info['timestamp']
        self.previous_hash = self.share_data['previous_share_hash']
        self.new_script = bitcoin_data.pubkey_hash_to_script2(
            self.share_data['pubkey_hash'],
            self.share_data['pubkey_hash_version'], net.PARENT)
        self.desired_version = self.share_data['desired_version']
        self.absheight = self.share_info['absheight']
        self.abswork = self.share_info['abswork']

        n = set()
        for share_count, tx_count in self.iter_transaction_hash_refs():
            assert share_count < 110
            if share_count == 0:
                n.add(tx_count)
        assert n == set(range(len(self.share_info['new_transaction_hashes'])))

        self.gentx_hash = check_hash_link(
            self.hash_link,
            self.get_ref_hash(net, self.share_info,
                              contents['ref_merkle_link']) +
            pack.IntType(64).pack(self.contents['last_txout_nonce']) +
            pack.IntType(32).pack(0),
            self.gentx_before_refhash,
        )
        merkle_root = bitcoin_data.check_merkle_link(
            self.gentx_hash, self.share_info['segwit_data']['txid_merkle_link']
            if segwit_activated else self.merkle_link)
        self.header = dict(self.min_header, merkle_root=merkle_root)
        self.pow_hash = net.PARENT.POW_FUNC(
            bitcoin_data.block_header_type.pack(self.header))
        self.hash = self.header_hash = bitcoin_data.hash_groestl(
            bitcoin_data.block_header_type.pack(self.header))

        if self.target > net.MAX_TARGET:
            from p2pool import p2p
            raise p2p.PeerMisbehavingError('share target invalid')

        if self.pow_hash > self.target:
            from p2pool import p2p
            raise p2p.PeerMisbehavingError('share PoW invalid')

        self.new_transaction_hashes = self.share_info['new_transaction_hashes']

        # XXX eww
        self.time_seen = time.time()