Example #1
0
def _item_resolution_scope(scope):
    output = Object(prototype=scope)
    output._can_resolve_items = False
    # output.parent = scope

    # logger.debug('item resolution scope {} (for {})'.format(output._id, scope._id))
    return output
Example #2
0
def _item_resolution_scope(scope):
    output = Object(prototype=scope)
    output._can_resolve_items = False
    # output.parent = scope

    # logger.debug('item resolution scope {} (for {})'.format(output._id, scope._id))
    return output
Example #3
0
def _new_with_arguments(prototype, arguments, arguments_scope):
    if prototype.arguments == arguments and prototype.arguments_scope == arguments_scope:
        return prototype
    output = Object(prototype=prototype)
    output.arguments = arguments
    output.arguments_scope = arguments_scope

    # logger.debug('arguments scope {} (for {})'.format(output._id, prototype._id))
    return output
Example #4
0
def _new_with_arguments(prototype, arguments, arguments_scope):
    if prototype.arguments == arguments and prototype.arguments_scope == arguments_scope:
        return prototype
    output = Object(prototype=prototype)
    output.arguments = arguments
    output.arguments_scope = arguments_scope

    # logger.debug('arguments scope {} (for {})'.format(output._id, prototype._id))
    return output
Example #5
0
def _create_native(value):
    output = Object()
    output.items = {
        'or': LV(lambda: create_scope(
            defaults=[('other', None)],
            expression=LV(lambda: resolve('self'))
        )),
        'then': LV(lambda: create_scope(
            defaults=[('callable', None)],
            expression=LV(lambda: call(resolve('callable'), [(None, LV(lambda: resolve('self')))]))
        )),
    }
    output.parent = scope_stack[0]
    output.arguments_scope = output.parent
    output.raw = value

    logger.debug('create native {} {}'.format(output._id, value))
    return output
Example #6
0
 def __new__(cls, text, description=None):
     assert isinstance(text, str)
     text = text.strip()
     assert 0 < len(text) <= cls._maxlen
     assert not re.match(r"[\n\r\t]", text)
     examples = [Example(text, created=datetime.now())]
     return Object(intent=uuid(),
                   description=description,
                   examples=examples)
Example #7
0
 def __new__(cls, intent, text):
     assert isinstance(text, str)
     text = text.strip()
     assert 0 < len(text) <= cls._maxlen
     values = [DialogNodeText(text)]
     generic = [DialogNodeOutputGeneric('text', values)]
     output = DialogNodeOutput(generic)
     name = intent.intent
     return Object(dialog_node=name,
                   description=intent.description,
                   conditions=f'#{name}',
                   output=output)
Example #8
0
def getBigramMaxesAndMins(bigrams):
    def ni(x):
        return x[x != float("-Inf")]

    maxes = Object()
    maxes.fullLength = np.max([np.max(ni(b.fullLength)) for b in bigrams])
    maxes.gap = np.max([np.max(ni(b.gap)) for b in bigrams])
    maxes.startStart = np.max([np.max(ni(b.startStart)) for b in bigrams])

    mins = Object()
    mins.fullLength = np.min([np.min(ni(b.fullLength)) for b in bigrams])
    mins.gap = np.min([np.min(ni(b.gap)) for b in bigrams])
    mins.startStart = np.min([np.min(ni(b.startStart)) for b in bigrams])

    return (maxes, mins)
Example #9
0
def compileBigramModel(data):
    COUNT_CUTOFF = 5
    TIME_CUTOFF = 5
    bigramModel = data.bigramModel
    keycodes = sorted(list(KEYCODES.values()))
    keyToIndex = dict((v, k) for (k, v) in enumerate(keycodes))
    fullLength = np.zeros([len(keycodes)] * 2) + float("-Inf")
    gap = np.zeros([len(keycodes)] * 2) + float("-Inf")
    startStart = np.zeros([len(keycodes)] * 2) + float("-Inf")
    for (k, v) in bigramModel.iteritems():
        for (m, n) in v.iteritems():
            (fl, g, ss) = zip(*n)
            if len(fl) >= COUNT_CUTOFF and abs(np.mean(fl)) < TIME_CUTOFF:
                fullLength[keyToIndex[k], keyToIndex[m]] = np.mean(fl)
            if len(g) >= COUNT_CUTOFF and abs(np.mean(g)) < TIME_CUTOFF:
                gap[keyToIndex[k], keyToIndex[m]] = np.mean(g)
            if len(ss) >= COUNT_CUTOFF and abs(np.mean(ss)) < TIME_CUTOFF:
                startStart[keyToIndex[k], keyToIndex[m]] = np.mean(ss)

    bigram = Object()
    bigram.fullLength = fullLength
    bigram.gap = gap
    bigram.startStart = startStart
    return bigram
Example #10
0
    def put_job(self):
        if self.busy: return

        temperature = self.get_temperature()
        if temperature < self.cutoff_temp:
            response = request(self.device, b'ZDX')
            if self.is_ok(response):
                if self.switch.update_time:
                    self.job.time = bytereverse(
                        uint32(long(time())) - self.job.time_delta)
                data = b''.join([
                    pack('<8I', *self.job.state),
                    pack('<3I', self.job.merkle_end, self.job.time,
                         self.job.difficulty)
                ])
                response = request(self.device,
                                   b''.join([b'>>>>>>>>', data, b'>>>>>>>>']))
                if self.is_ok(response):
                    self.busy = True
                    self.job_started = time()

                    self.last_job = Object()
                    self.last_job.header = self.job.header
                    self.last_job.merkle_end = self.job.merkle_end
                    self.last_job.time = self.job.time
                    self.last_job.difficulty = self.job.difficulty
                    self.last_job.target = self.job.target
                    self.last_job.state = self.job.state
                    self.last_job.job_id = self.job.job_id
                    self.last_job.extranonce2 = self.job.extranonce2
                    self.last_job.server = self.job.server
                    self.last_job.miner = self

                    self.check_interval = CHECK_INTERVAL
                    if not self.switch.update_time or bytereverse(
                            self.job.time) - bytereverse(
                                self.job.original_time) > 55:
                        self.update = True
                        self.job = None
                else:
                    say_line('%s: bad response when sending block data: %s',
                             (self.id(), response))
            else:
                say_line('%s: bad response when submitting job (ZDX): %s',
                         (self.id(), response))
        else:
            say_line('%s: temperature exceeds cutoff, waiting...', self.id())
Example #11
0
def _create_native(value):
    output = Object()
    output.items = {
        'or':
        LV(lambda: create_scope(defaults=[('other', None)],
                                expression=LV(lambda: resolve('self')))),
        'then':
        LV(lambda: create_scope(defaults=[('callable', None)],
                                expression=LV(lambda: call(
                                    resolve('callable'),
                                    [(None, LV(lambda: resolve('self')))])))),
    }
    output.parent = scope_stack[0]
    output.arguments_scope = output.parent
    output.raw = value

    logger.debug('create native {} {}'.format(output._id, value))
    return output
Example #12
0
    def decode(self,
               server,
               block_header,
               target,
               job_id=None,
               extranonce2=None):
        if block_header:
            job = Object()

            binary_data = block_header.decode('hex')
            data0 = np.zeros(64, np.uint32)
            data0 = np.insert(data0, [0] * 16,
                              unpack('IIIIIIIIIIIIIIII', binary_data[:64]))

            job.target = np.array(unpack('IIIIIIII', target.decode('hex')),
                                  dtype=np.uint32)
            job.header = binary_data[:68]
            job.merkle_end = np.uint32(unpack('I', binary_data[64:68])[0])
            job.time = np.uint32(unpack('I', binary_data[68:72])[0])
            job.difficulty = np.uint32(unpack('I', binary_data[72:76])[0])
            job.state = sha256(STATE, data0)
            job.f = np.zeros(8, np.uint32)
            job.state2 = partial(job.state, job.merkle_end, job.time,
                                 job.difficulty, job.f)
            job.targetQ = 2**256 / int(''.join(list(chunks(target, 2))[::-1]),
                                       16)
            job.job_id = job_id
            job.extranonce2 = extranonce2
            job.server = server

            calculateF(job.state, job.merkle_end, job.time, job.difficulty,
                       job.f, job.state2)

            if job.difficulty != self.difficulty:
                self.set_difficulty(job.difficulty)

            return job
Example #13
0
    def parse_server(self, server, mailAsUser=True):
        s = Object()
        temp = server.split('://', 1)
        if len(temp) == 1:
            s.proto = ''
            temp = temp[0]
        else:
            s.proto = temp[0]
            temp = temp[1]
        if mailAsUser:
            s.user, temp = temp.split(':', 1)
            s.pwd, s.host = temp.split('@')
        else:
            temp = temp.split('@', 1)
            if len(temp) == 1:
                s.user = ''
                s.pwd = ''
                s.host = temp[0]
            else:
                if temp[0].find(':') <> -1:
                    s.user, s.pwd = temp[0].split(':')
                else:
                    s.user = temp[0]
                    s.pwd = ''
                s.host = temp[1]

        if s.host.find('#') != -1:
            s.host, s.name = s.host.split('#')
        else:
            s.name = s.host

        return s
Example #14
0
	def decode(self, server, block_header, target, job_id = None, extranonce2 = None):
		if block_header:
			job = Object()

			binary_data = block_header.decode('hex')
			data0 = list(unpack('<16I', binary_data[:64])) + ([0] * 48)

			job.target		= unpack('<8I', target.decode('hex'))
			job.header		= binary_data[:68]
			job.merkle_end	= uint32(unpack('<I', binary_data[64:68])[0])
			job.time		= uint32(unpack('<I', binary_data[68:72])[0])
			job.difficulty	= uint32(unpack('<I', binary_data[72:76])[0])
			job.state		= sha256(STATE, data0)
			job.targetQ		= 2**256 / int(''.join(list(chunks(target, 2))[::-1]), 16)
			job.job_id		= job_id
			job.extranonce2	= extranonce2
			job.server		= server

			if job.difficulty != self.difficulty:
				self.set_difficulty(job.difficulty)
	
			return job
Example #15
0
	def decode(self, server, block_header, target, job_id = None, extranonce2 = None):
		if block_header:
			job = Object()
	
			binary_data = block_header.decode('hex')
			data0 = np.zeros(64, np.uint32)
			data0 = np.insert(data0, [0] * 16, unpack('IIIIIIIIIIIIIIII', binary_data[:64]))
	
			job.target      = np.array(unpack('IIIIIIII', target.decode('hex')), dtype=np.uint32)
			job.header      = binary_data[:68]
			job.merkle_end  = np.uint32(unpack('I', binary_data[64:68])[0])
			job.time        = np.uint32(unpack('I', binary_data[68:72])[0])
			job.difficulty  = np.uint32(unpack('I', binary_data[72:76])[0])
			job.state       = sha256(STATE, data0)
			job.f           = np.zeros(8, np.uint32)
			job.state2      = partial(job.state, job.merkle_end, job.time, job.difficulty, job.f)
			job.targetQ     = 2**256 / int(''.join(list(chunks(target, 2))[::-1]), 16)
			job.job_id      = job_id
			job.extranonce2 = extranonce2
			job.server      = server
	
			calculateF(job.state, job.merkle_end, job.time, job.difficulty, job.f, job.state2)

			if job.difficulty != self.difficulty:
				self.set_difficulty(job.difficulty)
	
			return job
Example #16
0
def collectData(infile):
    data = Object()
    userName = os.path.split(infile.name)[1].split(".")[0]

    # initialize all the container vars
    keystrokePLs = []
    keystrokePLs_key = defaultdict(list)
    keystrokePLs_domain = defaultdict(list)
    keystrokeFreqs = [0] * 250

    sessionLengths = []
    keystrokesPressedPerSession = []
    timeSpentPressingKeys = []
    websitesVisitedPerSession = []
    domainsVisitedPerSession = []
    domainVisits = Counter()
    websiteVisitLength = []  # can't currently get this one
    websiteVisitLength_domain = defaultdict(list)  # same here
    wordLengths = []  # TODO implement
    wordDurations = []  # TODO implement

    # Bigram model stuff
    bigramModel = defaultdict(lambda: defaultdict(list))

    # go through the segmented data (this is just a lot of code)
    stream = openJsonStream(infile)
    sessions = segmentJsonStream(stream)

    for session in sessions:
        keystrokesPressed = 0
        timeSpent = 0.0
        domains = set()
        websites = set()
        for page in session.data:
            if page.url is None:
                domain = "*"
                page.url = "*"
            else:
                domain = urlparse(page.url).netloc
            websites.add(page.url)
            domains.add(domain)
            domainVisits.update([domain])
            keystrokesPressed += len(page.keystrokes)

            page.keystrokes.sort(key=lambda x: x.timestamp)
            pKey = None
            pStart = 0
            pEnd = 0
            for keystroke in page.keystrokes:
                pl = keystroke.pressLength
                kc = keystroke.keycode
                start = keystroke.timestamp / 1000.0
                end = start + pl

                keystrokePLs.append(pl)
                keystrokePLs_key[kc].append(pl)
                keystrokePLs_domain[domain].append(pl)
                keystrokeFreqs[kc] += 1

                if pKey is not None:
                    bigramModel[pKey][kc].append((end - pStart, start - pEnd, start - pStart))
                pKey = kc
                pStart = start
                pEnd = end

                timeSpent += keystroke.pressLength

        sessionLengths.append(session.time)
        keystrokesPressedPerSession.append(keystrokesPressed)
        if session.time > 0.0:
            timeSpentPressingKeys.append(timeSpent / float(session.time))
        websitesVisitedPerSession.append(len(websites))
        domainsVisitedPerSession.append(len(domains))

    # store them all in data.
    data.keystrokePLs = np.array(keystrokePLs)
    data.keystrokePLs_key = dict((k, np.array(v)) for (k, v) in keystrokePLs_key.iteritems())
    data.keystrokePLs_domain = dict((k, np.array(v)) for (k, v) in keystrokePLs_domain.iteritems())
    data.keystrokeFreqs = np.array(keystrokeFreqs)
    data.sessionLengths = np.array(sessionLengths)
    data.keystrokesPressedPerSession = np.array(keystrokesPressedPerSession)
    data.timeSpentPressingKeys = np.array(timeSpentPressingKeys)
    data.websitesVisitedPerSession = np.array(websitesVisitedPerSession)
    data.domainsVisitedPerSession = np.array(domainsVisitedPerSession)
    data.domainVisits = domainVisits
    data.websiteVisitLength = websiteVisitLength  # TODO implement
    data.websiteVisitLength_domain = websiteVisitLength_domain  # TODO implement
    data.wordLengths = np.array(wordLengths)  # TODO implement
    data.wordDurations = np.array(wordDurations)  # TODO implement
    data.bigramModel = bigramModel
    data.user = userName

    # TODO: finish
    return data
Example #17
0
    def decode(self, server, block_header, target, job_id = None, extranonce2 = None):
        if block_header:
            job = Object()

            binary_data = block_header.decode('hex')

            #data0 = list(unpack('<16I', binary_data[:64])) + ([0] * 48)

            job.headerX = binary_data[:76]
            job.dataX = unpack('<19I', job.headerX)
            job.target		= unpack('<8I', target.decode('hex'))
            job.header		= binary_data[:68]
            job.merkle_end	= uint32(unpack('<I', binary_data[64:68])[0])
            job.time		= uint32(unpack('<I', binary_data[68:72])[0])
            job.difficulty	= uint32(unpack('<I', binary_data[72:76])[0])
            # job.state		= sha256(STATE, data0)
            job.targetQ		= 2**256 / int(''.join(list(chunks(target, 2))[::-1]), 16)
            job.job_id		= job_id
            job.extranonce2	= extranonce2
            job.server		= server

            if job.difficulty != self.difficulty:
                self.set_difficulty(job.difficulty)

            return job
Example #18
0
	def parse_server(self, server, mailAsUser=True):
		s = Object()
		temp = server.split('://', 1)
		if len(temp) == 1:
			s.proto = ''; temp = temp[0]
		else: s.proto = temp[0]; temp = temp[1]
		if mailAsUser:
			s.user, temp = temp.split(':', 1)
			s.pwd, s.host = temp.split('@')
		else:
			temp = temp.split('@', 1)
			if len(temp) == 1:
				s.user = ''
				s.pwd = ''
				s.host = temp[0]
			else:
				if temp[0].find(':') <> -1:
					s.user, s.pwd = temp[0].split(':')
				else:
					s.user = temp[0]
					s.pwd = ''
				s.host = temp[1]

		if s.host.find('#') != -1:
			s.host, s.name = s.host.split('#')
		else: s.name = s.host

		return s
Example #19
0
    except Exception as e:
        logger.debug(e)  # TODO narrow down exceptions
        return create_failed(e)


def evaluate_module(items={}, expression=None):
    module = create_scope(items=items, expression=expression)

    return call(module, [])


_import_cache = {}


def _import(path):
    if path.raw in _import_cache:
        return _import_cache[path.raw]
    try:
        source = modules[path.raw]
        node = _parser.parse(source)
        _import_cache[path] = node
        return node
    except KeyError as e:
        return create_failed("No such module: {}".format(path.raw))


# bootstrap scope_stack
scope_stack = []
root = Object()
scope_stack.append(root)
Example #20
0
def _new_with_self(prototype, self):
    output = Object(prototype=prototype)
    output.self = self

    # logger.debug('self scope {} (for {})'.format(output._id, prototype._id))
    return output
Example #21
0
	def mining_thread(self):
		say_line('started OpenCL miner on platform %d, device %d (%s)', (self.options.platform, self.device_index, self.device_name))

		(self.defines, rate_divisor, hashspace) = if_else(self.vectors, ('-DVECTORS', 500, 0x7FFFFFFF), ('', 1000, 0xFFFFFFFF))
		self.defines += (' -DOUTPUT_SIZE=' + str(self.output_size))
		self.defines += (' -DOUTPUT_MASK=' + str(self.output_size - 1))

		self.load_kernel()
		frame = 1.0 / max(self.frames, 3)
		unit = self.worksize * 256
		global_threads = unit * 10

		queue = cl.CommandQueue(self.context)

		last_rated_pace = last_rated = last_n_time = last_temperature = time()
		base = last_hash_rate = threads_run_pace = threads_run = 0
		output = np.zeros(self.output_size + 1, np.uint32)
		output_buffer = cl.Buffer(self.context, cl.mem_flags.WRITE_ONLY | cl.mem_flags.USE_HOST_PTR, hostbuf=output)
		self.kernel.set_arg(20, output_buffer)

		work = None
		temperature = 0
		while True:
			if self.should_stop: return

			sleep(self.frameSleep)

			if (not work) or (not self.work_queue.empty()):
				try:
					work = self.work_queue.get(True, 1)
				except Empty: continue
				else:
					if not work: continue
					nonces_left = hashspace
					state = work.state
					state2 = work.state2
					f = work.f

					self.kernel.set_arg(0, state[0])
					self.kernel.set_arg(1, state[1])
					self.kernel.set_arg(2, state[2])
					self.kernel.set_arg(3, state[3])
					self.kernel.set_arg(4, state[4])
					self.kernel.set_arg(5, state[5])
					self.kernel.set_arg(6, state[6])
					self.kernel.set_arg(7, state[7])

					self.kernel.set_arg(8, state2[1])
					self.kernel.set_arg(9, state2[2])
					self.kernel.set_arg(10, state2[3])
					self.kernel.set_arg(11, state2[5])
					self.kernel.set_arg(12, state2[6])
					self.kernel.set_arg(13, state2[7])

					self.kernel.set_arg(15, f[0])
					self.kernel.set_arg(16, f[1])
					self.kernel.set_arg(17, f[2])
					self.kernel.set_arg(18, f[3])
					self.kernel.set_arg(19, f[4])

			if temperature < self.cutoff_temp:
				self.kernel.set_arg(14, pack('I', base))
				cl.enqueue_nd_range_kernel(queue, self.kernel, (global_threads,), (self.worksize,))

				nonces_left -= global_threads
				threads_run_pace += global_threads
				threads_run += global_threads
				base = uint32(base + global_threads)
			else:
				threads_run_pace = 0
				last_rated_pace = time()
				sleep(self.cutoff_interval)

			now = time()
			if self.adapterIndex != None:
				t = now - last_temperature
				if temperature >= self.cutoff_temp or t > 1:
					last_temperature = now
					with adl_lock:
						temperature = self.get_temperature()

			t = now - last_rated_pace
			if t > 1:
				rate = (threads_run_pace / t) / rate_divisor
				last_rated_pace = now; threads_run_pace = 0
				r = last_hash_rate / rate
				if r < 0.9 or r > 1.1:
					global_threads = max(unit * int((rate * frame * rate_divisor) / unit), unit)
					last_hash_rate = rate

			t = now - last_rated
			if t > self.options.rate:
				self.update_rate(now, threads_run, t, work.targetQ, rate_divisor)
				last_rated = now; threads_run = 0

			queue.finish()
			cl.enqueue_read_buffer(queue, output_buffer, output)
			queue.finish()

			if output[self.output_size]:
				result = Object()
				result.header = work.header
				result.merkle_end = work.merkle_end
				result.time = work.time
				result.difficulty = work.difficulty
				result.target = work.target
				result.state = np.array(state)
				result.nonces = np.array(output)
				result.job_id = work.job_id
				result.extranonce2 = work.extranonce2
				result.server = work.server
				result.miner = self
				self.switch.put(result)
				output.fill(0)
				cl.enqueue_write_buffer(queue, output_buffer, output)

			if not self.switch.update_time:
				if nonces_left < 3 * global_threads * self.frames:
					self.update = True
					nonces_left += 0xFFFFFFFFFFFF
				elif 0xFFFFFFFFFFF < nonces_left < 0xFFFFFFFFFFFF:
					say_line('warning: job finished, %s is idle', self.id()) 
					work = None
			elif now - last_n_time > 1:
				work.time = bytereverse(bytereverse(work.time) + 1)
				state2 = partial(state, work.merkle_end, work.time, work.difficulty, f)
				calculateF(state, work.merkle_end, work.time, work.difficulty, f, state2)
				self.kernel.set_arg(8, state2[1])
				self.kernel.set_arg(9, state2[2])
				self.kernel.set_arg(10, state2[3])
				self.kernel.set_arg(11, state2[5])
				self.kernel.set_arg(12, state2[6])
				self.kernel.set_arg(13, state2[7])
				self.kernel.set_arg(15, f[0])
				self.kernel.set_arg(16, f[1])
				self.kernel.set_arg(17, f[2])
				self.kernel.set_arg(18, f[3])
				self.kernel.set_arg(19, f[4])
				last_n_time = now
				self.update_time_counter += 1
				if self.update_time_counter >= self.switch.max_update_time:
					self.update = True
					self.update_time_counter = 1
Example #22
0
    def mining_thread(self):
        say_line('started OpenCL miner on platform %d, device %d (%s)', (self.options.platform, self.device_index, self.device_name))

        (self.defines, rate_divisor, hashspace) = ('', 1000, 0xFFFFFFFF)
        self.defines += (' -D%sOUTPUT_SIZE=%s' % (self.defspace, str(self.output_size)))
        self.defines += (' -D%sOUTPUT_MASK=%s' % (self.defspace, str(self.output_size - 1)))
        self.defines += (' -D%sENDIAN_LITTLE=%d' % (self.defspace, self.device.endian_little))
        self.defines += (' -D%sGPU_AMD=%d' % (self.defspace, self.gpu_amd))
        self.defines += (' -I%s%s' % (self.defspace, os.getcwd()))

        say_line("Compiler defines: %s", self.defines)

        self.load_kernel()
        frame = 1.0 / max(self.frames, 3)
        unit = self.worksize * 256
        global_threads = unit * 10

        queue = cl.CommandQueue(self.context)

        last_rated_pace = last_rated = last_n_time = last_temperature = time()
        base = last_hash_rate = threads_run_pace = threads_run = 0
        output = bytearray((self.output_size + 1) * 4)
        output_buffer = cl.Buffer(self.context, cl.mem_flags.WRITE_ONLY | cl.mem_flags.USE_HOST_PTR, hostbuf=output)
        self.kernel.set_arg(12, output_buffer)

        work = None
        temperature = 0
        while True:
            if self.should_stop: return

            sleep(self.frameSleep)

            if (not work) or (not self.work_queue.empty()):
                try:
                    work = self.work_queue.get(True, 1)
                except Empty: continue
                else:
                    if not work: continue

                    nonces_left = hashspace

                    self.queue_kernel_parameters(work)

            if temperature < self.cutoff_temp:
                self.kernel.set_arg(11, pack('<I', base))
                cl.enqueue_nd_range_kernel(queue, self.kernel, (global_threads,), (self.worksize,))

                nonces_left -= global_threads
                threads_run_pace += global_threads
                threads_run += global_threads
                base = uint32(base + global_threads)
            else:
                threads_run_pace = 0
                last_rated_pace = time()
                sleep(self.cutoff_interval)

            now = time()
            if self.adapterIndex is not None:
                t = now - last_temperature
                if temperature >= self.cutoff_temp or t > 1:
                    last_temperature = now
                    with adl_lock:
                        self.temperature = self.get_temperature()
                        temperature = self.temperature

            t = now - last_rated_pace
            if t > 1:
                rate = (threads_run_pace / t) / rate_divisor
                last_rated_pace = now; threads_run_pace = 0
                r = last_hash_rate / rate
                if r < 0.9 or r > 1.1:
                    global_threads = max(unit * int((rate * frame * rate_divisor) / unit), unit)
                    last_hash_rate = rate

            t = now - last_rated
            if t > self.options.rate:
                self.update_rate(now, threads_run, t, work.targetQ, rate_divisor)
                last_rated = now; threads_run = 0

            queue.finish()
            cl.enqueue_read_buffer(queue, output_buffer, output)
            queue.finish()

            if output[-1]:
                result = Object()
                result.header = work.header
                result.headerX = work.headerX
                result.merkle_end = work.merkle_end
                result.time = work.time
                result.difficulty = work.difficulty
                result.target = work.target
                result.dataX = work.dataX[:]
                result.nonces = output[:]
                result.job_id = work.job_id
                result.extranonce2 = work.extranonce2
                result.server = work.server
                result.miner = self
                self.switch.put(result)
                output[:] = b'\x00' * len(output)
                cl.enqueue_write_buffer(queue, output_buffer, output)

                for miner in self.switch.miners:
                    miner.update = True

            if not self.switch.update_time:
                if nonces_left < 6 * global_threads * self.frames:
                    self.update = True
                    nonces_left += 0xFFFFFFFFFFFF
                elif 0xFFFFFFFFFFF < nonces_left < 0xFFFFFFFFFFFF:
                    say_line('warning: job finished, %s is idle', self.id())
                    work = None
            elif now - last_n_time > 1:
                last_n_time = now
                self.update_time_counter += 1
                if self.update_time_counter >= self.switch.max_update_time:
                    self.update = True
                    self.update_time_counter = 1
Example #23
0
	def handle_message(self, message):

		#Miner API
		if 'method' in message:

			#mining.notify
			if message['method'] == 'mining.notify':
				params = message['params']

				j = Object()

				j.job_id = params[0]
				j.prevhash = params[1]
				j.coinbase1 = params[2]
				j.coinbase2 = params[3]
				j.merkle_branch = params[4]
				j.version = params[5]
				j.nbits = params[6]
				j.ntime = params[7]
				clear_jobs = params[8]
				if clear_jobs:
					self.jobs.clear()
				j.extranonce2 = self.extranonce2_size * '00'

				j = self.refresh_job(j)

				self.jobs[j.job_id] = j
				self.current_job = j

				self.queue_work(j)
				self.switch.connection_ok()

			#mining.get_version
			if message['method'] == 'mining.get_version':
				with self.send_lock:
					self.send_message({"error": None, "id": message['id'], "result": self.user_agent})

			#mining.set_difficulty
			elif message['method'] == 'mining.set_difficulty':
				say_line("Setting new difficulty: %s", message['params'][0])
				self.server_difficulty = BASE_DIFFICULTY / message['params'][0]

			#client.reconnect
			elif message['method'] == 'client.reconnect':
				address, port = self.server().host.split(':', 1)
				(new_address, new_port, timeout) = message['params'][:3]
				if new_address: address = new_address
				if new_port != None: port = new_port
				say_line("%s asked us to reconnect to %s:%d in %d seconds", (self.server().name, address, port, timeout))
				self.server().host = address + ':' + str(port)
				Timer(timeout, self.reconnect).start()

			#client.add_peers
			elif message['method'] == 'client.add_peers':
				hosts = [{'host': host[0], 'port': host[1]} for host in message['params'][0]]
				self.switch.add_servers(hosts)

		#responses to server API requests
		elif 'result' in message:

			#response to mining.subscribe
			#store extranonce and extranonce2_size
			if message['id'] == 's':
				self.extranonce = message['result'][1]
				self.extranonce2_size = message['result'][2]
				self.subscribed = True

			#check if this is submit confirmation (message id should be in submits dictionary)
			#cleanup if necessary
			elif message['id'] in self.submits:
				miner, nonce = self.submits[message['id']][:2]
				accepted = message['result']
				self.switch.report(miner, nonce, accepted)
				del self.submits[message['id']]
				if time() - self.last_submits_cleanup > 3600:
					now = time()
					for key, value in self.submits.items():
						if now - value[2] > 3600:
							del self.submits[key]
					self.last_submits_cleanup = now

			#response to mining.authorize
			elif message['id'] == self.server().user:
				if not message['result']:
					say_line('authorization failed with %s:%s@%s', (self.server().user, self.server().pwd, self.server().host))
					self.authorized = False
				else:
					self.authorized = True
Example #24
0
def _new_with_self(prototype, self):
    output = Object(prototype=prototype)
    output.self = self

    # logger.debug('self scope {} (for {})'.format(output._id, prototype._id))
    return output
Example #25
0
    def handle_message(self, message):

        #Miner API
        if 'method' in message:

            #mining.notify
            if message['method'] == 'mining.notify':
                params = message['params']

                j = Object()

                j.job_id = params[0]
                j.prevhash = params[1]
                j.coinbase1 = params[2]
                j.coinbase2 = params[3]
                j.merkle_branch = params[4]
                j.version = params[5]
                j.nbits = params[6]
                j.ntime = params[7]
                clear_jobs = params[8]
                if clear_jobs:
                    self.jobs.clear()
                j.extranonce2 = self.extranonce2_size * '00'

                j = self.refresh_job(j)

                self.jobs[j.job_id] = j
                self.current_job = j

                self.queue_work(j)
                self.switch.connection_ok()

            #mining.get_version
            if message['method'] == 'mining.get_version':
                with self.send_lock:
                    self.send_message({
                        "error": None,
                        "id": message['id'],
                        "result": self.user_agent
                    })

            #mining.set_difficulty
            elif message['method'] == 'mining.set_difficulty':
                say_line("Setting new difficulty: %s", message['params'][0])
                self.server_difficulty = BASE_DIFFICULTY / message['params'][0]

            #client.reconnect
            elif message['method'] == 'client.reconnect':
                address, port = self.server().host.split(':', 1)
                (new_address, new_port, timeout) = message['params'][:3]
                if new_address: address = new_address
                if new_port != None: port = new_port
                say_line("%s asked us to reconnect to %s:%d in %d seconds",
                         (self.server().name, address, port, timeout))
                self.server().host = address + ':' + str(port)
                Timer(timeout, self.reconnect).start()

            #client.add_peers
            elif message['method'] == 'client.add_peers':
                hosts = [{
                    'host': host[0],
                    'port': host[1]
                } for host in message['params'][0]]
                self.switch.add_servers(hosts)

        #responses to server API requests
        elif 'result' in message:

            #response to mining.subscribe
            #store extranonce and extranonce2_size
            if message['id'] == 's':
                self.extranonce = message['result'][1]
                self.extranonce2_size = message['result'][2]
                self.subscribed = True

            #check if this is submit confirmation (message id should be in submits dictionary)
            #cleanup if necessary
            elif message['id'] in self.submits:
                miner, nonce = self.submits[message['id']][:2]
                accepted = message['result']
                self.switch.report(miner, nonce, accepted)
                del self.submits[message['id']]
                if time() - self.last_submits_cleanup > 3600:
                    now = time()
                    for key, value in self.submits.items():
                        if now - value[2] > 3600:
                            del self.submits[key]
                    self.last_submits_cleanup = now

            #response to mining.authorize
            elif message['id'] == self.server().user:
                if not message['result']:
                    say_line('authorization failed with %s:%s@%s',
                             (self.server().user, self.server().pwd,
                              self.server().host))
                    self.authorized = False
                else:
                    self.authorized = True
Example #26
0
        res = cls._search(params=params)
        if not res.ok:
            return []
        return munchify(res.json()).query.search

    @classmethod
    def snippets(cls, query, count=None):
        return [Snippet(e) for e in cls(query, 'snippet', count)]

    @classmethod
    def snippet(cls, query):
        ret = cls.snippets(query, 1)
        return ret[0] if ret else None


_units = Object(metric='m', imperial='e')
units_ = Object(m='metric', e='imperial')

Precipitation = namedtuple('Precipitation', 'total hourly snow')


class Value(dict):
    def __init__(self, units, type):
        super().__init__()
        self._units = units
        self._unit = Units[units].get(type, '')

    def _format(self, key):
        return f"{self[key]}{self._unit}"