Example #1
0
    def run(self):
        _time_start = time()
        logging.debug(
            "solve(): %04d-%06d: solving for %s,  current parent is %s",
            self.client_id,
            self.attempt,
            self.difficulty[:10],
            self.git_parent_id,
        )

        commit = Commit()
        commit.tree = self.git_tree_id
        commit.parents = [self.git_parent_id]
        author = "Rivo Laks <*****@*****.**>"
        commit.author = commit.committer = author
        commit.commit_time = commit.author_time = int(time())
        tz = parse_timezone("+0200")[0]
        commit.commit_timezone = commit.author_timezone = tz
        commit_message_template = "       Give me a Gitcoin %04d-%06d-%09x"
        commit.message = commit_message_template % (self.client_id, self.attempt, 1)

        hash_src = commit._header()
        for chunk in commit.as_raw_chunks():
            hash_src += chunk
        # Cut out the iteration number at the end which is 9 bytes
        hash_src = hash_src[:-9]
        assert len(hash_src) == 256

        _time_loop = time()
        i = 0
        block_size = 120000
        while not self.should_stop:
            sleep(0.001)
            # hasher.solve() returns either None, or the iteration number, if that iteration seems to match.
            result = hasher.solve(bytearray(hash_src), i, i + block_size, self.difficulty)
            if result is not None:
                logging.info("Hasher found something: %s", result)
                i = result
            else:
                i += block_size
                continue

            commit_tail = "%09x" % i
            commit_id = hashlib.sha1(hash_src + commit_tail).hexdigest()

            if commit_id < self.difficulty:
                logging.info("#" * 120)
                logging.info("   S U C C E S S  ! ! !")
                logging.info(
                    "solve(): %04d-%06d-%09x: SUCCESS: found %s / %s",
                    self.client_id,
                    self.attempt,
                    i,
                    commit_id,
                    self.difficulty[:10],
                )
                logging.info("#" * 120)

                commit.message = commit_message_template % (self.client_id, self.attempt, i)
                assert commit.id == commit_id
                commit_body = "".join(commit._serialize())
                # print commit.id, commit_id
                # print "'%s'" % commit_body
                # print "'%s'" % ''.join(hash_src + commit_tail)

                logging.debug("Commit body is: '%s'", commit_body)
                # subprocess.Popen('git hash-object -t commit --stdin -w'.split(), stdin=subprocess.PIPE,
                #                 stdout=subprocess.PIPE).communicate(commit_body)

                # subprocess.check_output(('git reset --hard %s' % commit.id).split())

                self.socket.sendall("C %40s %40s %5d\n" % (self.git_parent_id, commit_id, len(commit_body)))
                self.socket.sendall(commit_body + "\n")

                return True
            else:
                print "!" * 120
                print "ERROR!!!"
                print "hashes don't match, mine is", commit_tail, commit_id

        _time_end = time()
        speed_mh_s = i / 1000000.0 / (_time_end - _time_loop) if i > 0 else 0
        logging.debug(
            "solve(): %04d-%06d: stopped with %d iterations, speed was %.3f MH/s + %.3f ms startup",
            self.client_id,
            self.attempt,
            i,
            speed_mh_s,
            (_time_loop - _time_start) * 1000,
        )
        return False