Esempio n. 1
0
 def _send_work(self):
     try:
         x, got_response = self.wb.get_work(
             *self.wb.preprocess_request("" if self.username is None else self.username)
         )
     except:
         log.err()
         self.transport.loseConnection()
         return
     jobid = str(random.randrange(2 ** 128))
     self.other.svc_mining.rpc_set_difficulty(
         bitcoin_data.target_to_difficulty_alt(x["share_target"], self.wb.net.DUMB_SCRYPT_DIFF)
     ).addErrback(lambda err: None)
     self.other.svc_mining.rpc_notify(
         jobid,  # jobid
         getwork._swap4(pack.IntType(256).pack(x["previous_block"])).encode("hex"),  # prevhash
         x["coinb1"].encode("hex"),  # coinb1
         x["coinb2"].encode("hex"),  # coinb2
         [pack.IntType(256).pack(s).encode("hex") for s in x["merkle_link"]["branch"]],  # merkle_branch
         getwork._swap4(pack.IntType(32).pack(x["version"])).encode("hex"),  # version
         getwork._swap4(pack.IntType(32).pack(x["bits"].bits)).encode("hex"),  # nbits
         getwork._swap4(pack.IntType(32).pack(x["timestamp"])).encode("hex"),  # ntime
         True,  # clean_jobs
     ).addErrback(lambda err: None)
     self.handler_map[jobid] = x, got_response
Esempio n. 2
0
 def _send_work(self):
     try:
         x, got_response = self.wb.get_work(*self.wb.preprocess_request(
             '' if self.username is None else self.username))
     except:
         log.err()
         self.transport.loseConnection()
         return
     jobid = str(random.randrange(2**128))
     self.other.svc_mining.rpc_set_difficulty(
         bitcoin_data.target_to_difficulty(x['share_target']) *
         self.wb.net.DUMB_SCRYPT_DIFF).addErrback(lambda err: None)
     self.other.svc_mining.rpc_notify(
         jobid,  # jobid
         getwork._swap4(pack.IntType(256).pack(
             x['previous_block'])).encode('hex'),  # prevhash
         x['coinb1'].encode('hex'),  # coinb1
         x['coinb2'].encode('hex'),  # coinb2
         [
             pack.IntType(256).pack(s).encode('hex')
             for s in x['merkle_link']['branch']
         ],  # merkle_branch
         getwork._swap4(pack.IntType(32).pack(
             x['version'])).encode('hex'),  # version
         getwork._swap4(pack.IntType(32).pack(
             x['bits'].bits)).encode('hex'),  # nbits
         getwork._swap4(pack.IntType(32).pack(
             x['timestamp'])).encode('hex'),  # ntime
         True,  # clean_jobs
     ).addErrback(lambda err: None)
     self.handler_map[jobid] = x, got_response
Esempio n. 3
0
    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)
Esempio n. 4
0
    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']
            ),  # new_packed_gentx has witness data stripped
            timestamp=pack.IntType(32).unpack(
                getwork._swap4(ntime.decode('hex'))),
            bits=x['bits'],
            nonce=pack.IntType(32).unpack(getwork._swap4(nonce.decode('hex'))),
        )

        # Disconnect miners with large DOA rates to prevent DoS
        res = got_response(header, worker_name, coinb_nonce)
        if len(self.wb._inner.my_share_hashes) > 20:
            if float(len(self.wb._inner.my_doa_share_hashes)) / float(
                    len(self.wb._inner.my_share_hashes)) > 0.60:
                self.transport.loseConnection()

        # Disconnect miners with large hash > target to prevent DoS
        if self.wb._inner.total_hashes > 20:
            if float(self.wb._inner.invalid_hashes) / float(
                    self.wb._inner.total_hashes) > 0.05:
                self.transport.loseConnection()

        return res
Esempio n. 5
0
 def _send_work(self):
     try:
         x, got_response = self.wb.get_work(*self.wb.preprocess_request('' if self.username is None else self.username))
     except:
         log.err()
         self.transport.loseConnection()
         return
     jobid = str(random.randrange(2**128))
     clean_jobs = False
     if x['previous_block'] != self.previous_block:
         clean_jobs = True
         self.previous_block = x['previous_block']
     self.other.svc_mining.rpc_set_difficulty(bitcoin_data.target_to_difficulty(x['share_target'])*self.wb.net.DUMB_SCRYPT_DIFF).addErrback(lambda err: None)
     self.other.svc_mining.rpc_notify(
         jobid, # jobid
         getwork._swap4(pack.IntType(256).pack(x['previous_block'])).encode('hex'), # prevhash
         x['coinb1'].encode('hex'), # coinb1
         x['coinb2'].encode('hex'), # coinb2
         [pack.IntType(256).pack(s).encode('hex') for s in x['merkle_link']['branch']], # merkle_branch
         getwork._swap4(pack.IntType(32).pack(x['version'])).encode('hex'), # version
         getwork._swap4(pack.IntType(32).pack(x['bits'].bits)).encode('hex'), # nbits
         getwork._swap4(pack.IntType(32).pack(x['timestamp'])).encode('hex'), # ntime
         clean_jobs, # clean_jobs
     ).addErrback(lambda err: None)
     self.handler_map[jobid] = x, got_response
Esempio n. 6
0
    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)
Esempio n. 7
0
 def _send_work(self):
     try:
         x, got_response = self.wb.get_work(*self.wb.preprocess_request('' if self.username is None else self.username))
     except:
         log.err()
         self.transport.loseConnection()
         return
     if self.desired_pseudoshare_target: # Not none or zero, if we calculate better Difficulty setting?
         self.fixed_target = True
         # set new target for difficulty, based on time for it's generation?
         self.target = self.desired_pseudoshare_target
         self.target = max(self.target, int(x['bits'].target))
     else:
         self.fixed_target = False
         self.target = x['share_target'] if self.target == None else max(x['min_share_target'], self.target)
     jobid = str(random.randrange(2**128))
     self.other.svc_mining.rpc_set_difficulty(bitcoin_data.target_to_difficulty(self.target)*self.wb.net.DUMB_SCRYPT_DIFF).addErrback(lambda err: None)
     self.other.svc_mining.rpc_notify(
         jobid, # jobid
         getwork._swap4(pack.IntType(256).pack(x['previous_block'])).encode('hex'), # prevhash
         x['coinb1'].encode('hex'), # coinb1
         x['coinb2'].encode('hex'), # coinb2
         [pack.IntType(256).pack(s).encode('hex') for s in x['merkle_link']['branch']], # merkle_branch
         getwork._swap4(pack.IntType(32).pack(x['version'])).encode('hex'), # version
         getwork._swap4(pack.IntType(32).pack(x['bits'].bits)).encode('hex'), # nbits
         getwork._swap4(pack.IntType(32).pack(x['timestamp'])).encode('hex'), # ntime
         True, # clean_jobs
     ).addErrback(lambda err: None)
     self.handler_map[jobid] = x, got_response
Esempio n. 8
0
    def rpc_submit(self, worker_name, job_id, extranonce2, ntime, nonce, version_bits = None, *args):
        #asicboost: version_bits is the version mask that the miner used
        worker_name = worker_name.strip()
        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!'''
            #self.other.svc_client.rpc_reconnect().addErrback(lambda err: None)
            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']

        job_version = x['version']
        nversion = job_version
        #check if miner changed bits that they were not supposed to change
        if version_bits:
            if ((~self.pool_version_mask) & int(version_bits,16)) != 0:
                #todo: how to raise error back to miner?
                #protocol does not say error needs to be returned but ckpool returns
                #{"error": "Invalid version mask", "id": "id", "result":""}
                raise ValueError("Invalid version mask {0}".format(version_bits))
            nversion = (job_version & ~self.pool_version_mask) | (int(version_bits,16) & self.pool_version_mask)
            #nversion = nversion & int(version_bits,16)

        header = dict(
            version=nversion,
            previous_block=x['previous_block'],
            merkle_root=bitcoin_data.check_merkle_link(bitcoin_data.hash256(new_packed_gentx), x['merkle_link']), # new_packed_gentx has witness data stripped
            timestamp=pack.IntType(32).unpack(getwork._swap4(ntime.decode('hex'))),
            bits=x['bits'],
            nonce=pack.IntType(32).unpack(getwork._swap4(nonce.decode('hex'))),
        )
        result = got_response(header, worker_name, coinb_nonce, self.target) # self.target from RPC response

        # adjust difficulty on this stratum to target ~10sec/pseudoshare!!!
        if not self.fixed_target:
            self.recent_shares.append(time.time())
            if len(self.recent_shares) > 12 or (time.time() - self.recent_shares[0]) > 10*len(self.recent_shares)*self.share_rate:
                old_time = self.recent_shares[0]
                del self.recent_shares[0]
                olddiff = bitcoin_data.target_to_difficulty(self.target)
                # calculate new target based on time needed for generating previous shares
                self.target = int(self.target * clip((time.time() - old_time)/(len(self.recent_shares)*self.share_rate), 0.5, 2.) + 0.5)
                newtarget = clip(self.target, self.wb.net.SANE_TARGET_RANGE[0], self.wb.net.SANE_TARGET_RANGE[1])
                if newtarget != self.target:
                    print "Clipping target from %064x to %064x" % (self.target, newtarget)
                    self.target = newtarget
                self.target = max(x['min_share_target'], self.target)
                self.recent_shares = [time.time()]
                self._send_work()

        return result
Esempio n. 9
0
 def _send_work(self):
     x, got_response = self.wb.get_work(*self.wb.preprocess_request('' if self.username is None else self.username))
     jobid = str(random.randrange(2**128))
     self.other.svc_mining.rpc_set_difficulty(bitcoin_data.target_to_difficulty(x['share_target'])).addErrback(lambda err: None)
     self.other.svc_mining.rpc_notify(
         jobid, # jobid
         getwork._swap4(pack.IntType(256).pack(x['previous_block'])).encode('hex'), # prevhash
         x['coinb1'].encode('hex'), # coinb1
         x['coinb2'].encode('hex'), # coinb2
         [pack.IntType(256).pack(s).encode('hex') for s in x['merkle_link']['branch']], # merkle_branch
         getwork._swap4(pack.IntType(32).pack(x['version'])).encode('hex'), # version
         getwork._swap4(pack.IntType(32).pack(x['bits'].bits)).encode('hex'), # nbits
         getwork._swap4(pack.IntType(32).pack(x['timestamp'])).encode('hex'), # ntime
         True, # clean_jobs
     ).addErrback(lambda err: None)
     self.handler_map[jobid] = x, got_response
Esempio n. 10
0
 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)
Esempio n. 11
0
    def rpc_submit(self,
                   worker_name,
                   job_id,
                   extranonce2,
                   ntime,
                   nonce,
                   version_bits=None,
                   *args):
        #asicboost: version_bits is the version mask that the miner used
        worker_name = worker_name.strip()
        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!'''
            #self.other.svc_client.rpc_reconnect().addErrback(lambda err: None)
            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']

        job_version = x['version']
        nversion = job_version
        #check if miner changed bits that they were not supposed to change
        #print version_bits
        if version_bits:
            if ((~self.pool_version_mask) & int(version_bits, 16)) != 0:
                #todo: how to raise error back to miner?
                #protocol does not say error needs to be returned but ckpool returns
                #{"error": "Invalid version mask", "id": "id", "result":""}
                raise ValueError(
                    "Invalid version mask {0}".format(version_bits))
            nversion = (job_version & ~self.pool_version_mask) | (
                int(version_bits, 16) & self.pool_version_mask)
            #nversion = nversion & int(version_bits,16)

        header = dict(
            version=nversion,
            previous_block=x['previous_block'],
            merkle_root=bitcoin_data.check_merkle_link(
                bitcoin_data.hash256(new_packed_gentx), x['merkle_link']
            ),  # new_packed_gentx has witness data stripped
            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)