def rpc_submit(self, worker_name, job_id, extranonce2, ntime, nonce, birthdayA, birthdayB): print 'Submit recieved' if job_id not in self.handler_map: print >>sys.stderr, '''Couldn't link returned work's job id with its handler. This should only happen if this process was recently restarted!''' return False x, got_response = self.handler_map[job_id] coinb_nonce = extranonce2.decode('hex') print "COINBN: %i NL: %i" % (len(coinb_nonce), self.wb.COINBASE_NONCE_LENGTH) assert len(coinb_nonce) == self.wb.COINBASE_NONCE_LENGTH new_packed_gentx = x['coinb1'] + coinb_nonce + x['coinb2'] print new_packed_gentx.encode('hex'); print bitcoin_data.hash256(new_packed_gentx) print bitcoin_data.check_merkle_link(bitcoin_data.hash256(new_packed_gentx), x['merkle_link']) print birthdayA print birthdayB header = dict( version=x['version'], previous_block=x['previous_block'], merkle_root=bitcoin_data.check_merkle_link(bitcoin_data.hash256(new_packed_gentx), x['merkle_link']), timestamp=pack.IntType(32).unpack(ntime.decode('hex')), bits=x['bits'], nonce=pack.IntType(32).unpack(nonce.decode('hex')), birthdayA=pack.IntType(32).unpack(birthdayA.decode('hex')), birthdayB=pack.IntType(32).unpack(birthdayB.decode('hex')), ) return got_response(header, worker_name, coinb_nonce)
def get_ref_hash(cls, net, share_info, ref_merkle_link): return pack.IntType(256).pack( bitcoin_data.check_merkle_link( bitcoin_data.hash256(cls.ref_type.pack(dict(identifier=net.IDENTIFIER, share_info=share_info))), ref_merkle_link, ) )
def rpc_submit(self, worker_name, job_id, extranonce2, ntime, nonce): if job_id not in self.handler_map: print >>sys.stderr, '''Couldn't link returned work's job id with its handler. This should only happen if this process was recently restarted!''' return False x, got_response = self.handler_map[job_id] coinb_nonce = extranonce2.decode('hex') assert len(coinb_nonce) == self.wb.COINBASE_NONCE_LENGTH new_packed_gentx = x['coinb1'] + coinb_nonce + x['coinb2'] header = dict( version=x['version'], previous_block=x['previous_block'], merkle_root=bitcoin_data.check_merkle_link(bitcoin_data.hash256(new_packed_gentx), x['merkle_link']), timestamp=pack.IntType(32).unpack(getwork._swap4(ntime.decode('hex'),True)), bits=x['bits'], nonce=pack.IntType(32).unpack(getwork._swap4(nonce.decode('hex'),True)), ) #print "stratum: jobid: %s" %(job_id) #print "stratum bits: %s %X" % (hex(header['bits'].bits), header['bits'].target) #print "stratum: timestamp: %X" % (header['timestamp']) #print "stratum: extranonce2: %s" % (extranonce2) #print "stratum: nonce: %s %s\n" % (hex(header['nonce']),nonce) #print "stratum: coinb1: %s\n" % (x['coinb1'].encode('hex')) #print "stratum: coinb2: %s\n" % (x['coinb2'].encode('hex')) print "stratum: header: \n %s\n" % (str(header)) #print "stratum: new_packed_gentx:\n %s\n" % (new_packed_gentx.encode('hex')) #print "stratum: merkle_root: %s \n\n" % (header['merkle_root']) return got_response(header, worker_name, coinb_nonce)
def rpc_submit(self, worker_name, job_id, extranonce2, ntime, nonce): if job_id not in self.handler_map: print >>sys.stderr, '''Couldn't link returned work's job id with its handler. This should only happen if this process was recently restarted!''' return False x, got_response = self.handler_map[job_id] coinb_nonce = getwork._swap4(extranonce2.decode('hex')) assert len(coinb_nonce) == self.wb.COINBASE_NONCE_LENGTH new_packed_gentx = x['coinb1'] + coinb_nonce + x['coinb2'] #build header header = dict( version=x['version'], previous_block=x['previous_block'], merkle_root=bitcoin_data.check_merkle_link(bitcoin_data.hash256(new_packed_gentx), x['merkle_link']), timestamp=pack.IntType(32).unpack(getwork._swap4(ntime.decode('hex'),True)), bits=x['bits'], nonce=pack.IntType(32).unpack(getwork._swap4(nonce.decode('hex'),True)), ) #print "stratum: nonce %s extranonce2: %s" %(nonce, extranonce2) # print "stratum: header:%s\n" % (str(header)) # print "stratum: merkle_root %s" % (hex(header['merkle_root'])) # print "stratum: merkle_link %s" % (repr(x['merkle_link'])) # print "stratum: new_packed_gentx %s" % (repr(new_packed_gentx)) # #definition of got_response is in work.py return got_response(header, worker_name, coinb_nonce)
def __init__(self, net, peer_addr, contents): 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"] 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: 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.desired_version = self.share_data["desired_version"] 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(32).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.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.hash256(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()
def __init__(self, net, peer_addr, contents): 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'] 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: 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.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.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.hash256(bitcoin_data.block_header_type.pack(self.header)) self.hash = self.header_hash = net.PARENT.BLOCKHASH_FUNC(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()
def _getwork(self, request, data, long_poll): request.setHeader('X-Long-Polling', '/long-polling') request.setHeader('X-Roll-NTime', 'expire=100') request.setHeader('X-Is-P2Pool', 'true') if request.getHeader('Host') is not None: request.setHeader('X-Stratum', 'stratum+tcp://' + request.getHeader('Host')) if data is not None: header = getwork.decode_data(data) if header['merkle_root'] not in self.merkle_root_to_handler: print >>sys.stderr, '''Couldn't link returned work's merkle root with its handler. This should only happen if this process was recently restarted!''' defer.returnValue(False) defer.returnValue(self.merkle_root_to_handler[header['merkle_root']](header, request.getUser() if request.getUser() is not None else '', '\0'*self.worker_bridge.COINBASE_NONCE_LENGTH)) if p2pool.DEBUG: id = random.randrange(1000, 10000) print 'POLL %i START is_long_poll=%r user_agent=%r user=%r' % (id, long_poll, request.getHeader('User-Agent'), request.getUser()) if long_poll: request_id = request.getClientIP(), request.getHeader('Authorization') if self.worker_views.get(request_id, self.worker_bridge.new_work_event.times) != self.worker_bridge.new_work_event.times: if p2pool.DEBUG: print 'POLL %i PUSH' % (id,) else: if p2pool.DEBUG: print 'POLL %i WAITING' % (id,) yield self.worker_bridge.new_work_event.get_deferred() self.worker_views[request_id] = self.worker_bridge.new_work_event.times x, handler = self.worker_bridge.get_work(*self.worker_bridge.preprocess_request(request.getUser() if request.getUser() is not None else '')) res = getwork.BlockAttempt( version=x['version'], previous_block=x['previous_block'], merkle_root=bitcoin_data.check_merkle_link(bitcoin_data.hash256(x['coinb1'] + '\0'*self.worker_bridge.COINBASE_NONCE_LENGTH + x['coinb2']), x['merkle_link']), timestamp=x['timestamp'], bits=x['bits'], share_target=x['share_target'], ) assert res.merkle_root not in self.merkle_root_to_handler self.merkle_root_to_handler[res.merkle_root] = handler if p2pool.DEBUG: print 'POLL %i END identifier=%i' % (id, self.worker_bridge.new_work_event.times) extra_params = {} if request.getHeader('User-Agent') == 'Jephis PIC Miner': # ASICMINER BE Blades apparently have a buffer overflow bug and # can't handle much extra in the getwork response extra_params = {} else: extra_params = dict(identifier=str(self.worker_bridge.new_work_event.times), submitold=True) defer.returnValue(res.getwork(**extra_params))
def __init__(self, net, peer, common, merkle_link, other_txs): self.net = net self.peer = peer self.common = common self.min_header = common['min_header'] self.share_info = common['share_info'] self.hash_link = common['hash_link'] self.merkle_link = merkle_link self.other_txs = other_txs if len(self.share_info['share_data']['coinbase']) > 100: raise ValueError('''coinbase too large! %i bytes''' % (len(self.self.share_data['coinbase']),)) if len(merkle_link['branch']) > 16: raise ValueError('merkle branch too long!') if p2pool.DEBUG and other_txs is not None and bitcoin_data.calculate_merkle_link([0] + [bitcoin_data.hash256(bitcoin_data.tx_type.pack(x)) for x in other_txs], 0) != merkle_link: raise ValueError('merkle_link and other_txs do not match') 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.desired_version = self.share_data['desired_version'] if self.timestamp < net.SWITCH_TIME: from p2pool import p2p raise p2p.PeerMisbehavingError('peer sent a new-style share with a timestamp before the switch time') self.gentx_hash = check_hash_link( self.hash_link, self.get_ref_hash(net, self.share_info, common['ref_merkle_link']) + pack.IntType(32).pack(0), self.gentx_before_refhash, ) merkle_root = bitcoin_data.check_merkle_link(self.gentx_hash, 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.hash256(bitcoin_data.block_header_type.pack(self.header)) if self.pow_hash > self.target: raise p2p.PeerMisbehavingError('share PoW invalid') if other_txs is not None and not self.pow_hash <= self.header['bits'].target: raise ValueError('other_txs provided when not a block solution') if other_txs is None and self.pow_hash <= self.header['bits'].target: raise ValueError('other_txs not provided when a block solution') # XXX eww self.time_seen = time.time()
def rpc_submit(self, worker_name, job_id, extranonce2, ntime, nonce): if job_id not in self.handler_map: print >>sys.stderr, '''Couldn't link returned work's job id with its handler. This should only happen if this process was recently restarted!''' return False x, got_response = self.handler_map[job_id] coinb_nonce = extranonce2.decode('hex') assert len(coinb_nonce) == self.wb.COINBASE_NONCE_LENGTH new_packed_gentx = x['coinb1'] + coinb_nonce + x['coinb2'] header = dict( version=x['version'], previous_block=x['previous_block'], merkle_root=bitcoin_data.check_merkle_link(bitcoin_data.sha3(new_packed_gentx), x['merkle_link']), timestamp=pack.IntType(32).unpack(getwork._swap4(ntime.decode('hex'))), bits=x['bits'], nonce=pack.IntType(32).unpack(getwork._swap4(nonce.decode('hex'))), ) return got_response(header, worker_name, coinb_nonce)
def _getwork(self, request, data, long_poll): request.setHeader("X-Long-Polling", "/long-polling") request.setHeader("X-Roll-NTime", "expire=100") request.setHeader("X-Is-P2Pool", "true") # if request.getHeader('Host') is not None: # request.setHeader('X-Stratum', 'stratum+tcp://' + request.getHeader('Host')) if data is not None: header = getwork.decode_data(data) if header["merkle_root"] not in self.merkle_root_to_handler: print >> sys.stderr, """Couldn't link returned work's merkle root with its handler. This should only happen if this process was recently restarted!""" defer.returnValue(False) defer.returnValue( self.merkle_root_to_handler[header["merkle_root"]]( header, request.getUser() if request.getUser() is not None else "", "\0" * self.worker_bridge.COINBASE_NONCE_LENGTH, ) ) if p2pool.DEBUG: id = random.randrange(1000, 10000) print "POLL %i START is_long_poll=%r user_agent=%r user=%r" % ( id, long_poll, request.getHeader("User-Agent"), request.getUser(), ) if long_poll: request_id = request.getClientIP(), request.getHeader("Authorization") if ( self.worker_views.get(request_id, self.worker_bridge.new_work_event.times) != self.worker_bridge.new_work_event.times ): if p2pool.DEBUG: print "POLL %i PUSH" % (id,) else: if p2pool.DEBUG: print "POLL %i WAITING" % (id,) yield self.worker_bridge.new_work_event.get_deferred() self.worker_views[request_id] = self.worker_bridge.new_work_event.times x, handler = self.worker_bridge.get_work( *self.worker_bridge.preprocess_request(request.getUser() if request.getUser() is not None else "") ) res = getwork.BlockAttempt( version=x["version"], previous_block=x["previous_block"], merkle_root=bitcoin_data.check_merkle_link( bitcoin_data.hash256(x["coinb1"] + "\0" * self.worker_bridge.COINBASE_NONCE_LENGTH + x["coinb2"]), x["merkle_link"], ), timestamp=x["timestamp"], bits=x["bits"], share_target=x["share_target"], ) assert res.merkle_root not in self.merkle_root_to_handler self.merkle_root_to_handler[res.merkle_root] = handler if p2pool.DEBUG: print "POLL %i END identifier=%i" % (id, self.worker_bridge.new_work_event.times) defer.returnValue(res.getwork(identifier=str(self.worker_bridge.new_work_event.times), submitold=True))
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.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.hash256( 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()
def get_ref_hash(cls, net, share_info, ref_merkle_link): return pack.IntType(256).pack(bitcoin_data.check_merkle_link(bitcoin_data.hash256(cls.ref_type.pack(dict( identifier=net.IDENTIFIER, share_info=share_info, ))), ref_merkle_link))