Questions about mining script

There are two global _Solvers(SOLVER_PRIM and SOLVER_PERF) in the mining script.

  • I think the mining script try to find just one nonce for one SHA256 quiz at same time.
    Right? or it works on more than 2 quizzes at once?

  • What’s the role of SOLVER_PERF and SOLVER_PRIM?
    Are they parellel workers on same SHA256 quiz? or different quiz?
    What’s the meaning of the variable name ‘PERF’? It means ‘performance’?

  • What is the solver swap?
    If SOLVER_PRIM found nonce, stop SOLVER_PERF?
    If SOLVER_PERF found nonce, swap solvers?
    The comment - ‘ask for a swap, the solver will be set to working on the same puzzle wihtout this, on reentry, there is no distinction between an already used hash or a new one. we could be create a new command for clarity with the same effect as a swap in the working loop.’
    at update_block() function is so hard to me to understand.
    Please let me know things in detail about the solver swap.

I’m sorry for the poor comment btw. Once a suitable nonce has been found and the publishing time has elapsed, a candidate block is built and the second solver ‘solver_perf’ tries to best this candidate block while the primary solver SOLVER_PRIM works on the next block. This has the advantage of working on a new block while also improving the past candidate block at the same time. We believe this reduces the headstart of the winning node and makes validation time less significant. Overall, this should leverage the odds against long mining streaks.

The SOLVER_PERF will work as long as the SOLVER_PRIM hasn’t published the next block, In that moment, SOLVER_PERF will switch to the newest published block and try to update that one instead. I hope you found that helpful. If you want me to go into details for any particular question don’t mind asking.

Hi~ the commenter :slight_smile:
Thanks for your replay.

I have some questions.
Q0 :
First, What’s the meaning of the word ‘_PERF’?

Q1 :
In Pow, the solver calulates sha256 hash of {prevBlockId, PublicKey, nonce}.
The ‘blockId’ is not affected by nonce value?
I think if the perf found refined nonce,
while the prime solver trying to find nonce of next block.
The quiz what the prime working on becomes invalid.
because the prevBlockId is changed by the refined nonce found by perf. Am I wrong?
In your replay, It looks like two sha256 quiz is solvable in same time.
In CTC, How to generate the blockID of a block?

Q2 :
Is the candiate block(including nonce that is found at first) sent(broadcated) to network immediately after built?

Q3 :
The SWAP command is made
to tell solvers that the quiz you are working on is over?

Q4 :
In network, How to choose the best block?

  • a block holding a nonce with highest difficulty & built before block publishing interval elapsed?

Q5 :
I understood like that
SOLVER_PRIME : Solve quiz at first, If found nonce, send nonce baton to SOLVER_PERF. And just wait next quiz.
SOLVER_PERF : Wait for baton from PRIME. Try to Refine the nonce.
SOLVER_PRIME : Receive new quiz. Solve the quiz. If found nonce, tell _PERF that “stop it. refine this nonce instead. I’m going to wait new quiz.”
SOLVER_PERF : Give up current work. Refine the new nonce of new candidate block.

Am I on same page?

Q7 :
I’m so confused with the words in code
candidate_block : PRIME is on?
candidate_block2 : PERF is on?
claim : send to network?
publish : write block to local block cache, not send block to network?

Q8 :
Luckly, SOLVER_PRIME found nonce that has great difficulty. (ex 35 difficluty. these day)
So SOLVER_PERF decide to give up refining the nonce. because it is very hard.
In this situation. the block including the nonce could be adopted as main block?

def on_accepted(self):

    global SOLVER_PRIM
    global SOLVER_PERF

    # LOGGER.info('--- swapping solvers')
    with self._lock:
        tmp = SOLVER_PERF
        SOLVER_PERF = SOLVER_PRIM
        SOLVER_PRIM = tmp

        SOLVER_PRIM._swap_solver("SOLVER_PRIM")
        SOLVER_PERF._swap_solver("SOLVER_PERF")

When is this function called?
SOLVER_PRIME becomes SOLVER_PERF after finding first nonce and there is remaining time more than 5 sec?

Yes. Once on_check_publish_block(…) finds it’s time to build a candidate block and a solution has been found already, solver are swapped. PRIM becomes PERF and keeps trying to find a better hash while the new PRIM prepares to work for the next block’s solution.

A0 :
SOLVER_PERFECTO

A1 :
The updated block is the exact same block with a different header updating the block’s Id in the process. The block that SOLVER_PERF updated does not get invalidated by updating it nor the next block is due to also updating the next block’s previous predecessor on a successful PERF update.

To generate Block Signature: def _sign_block(self, block, identity_signer)

A2 :
Both the first block found and all of the improved ones are broadcasted as normal, there is no difference between publishing the first and the improved ones.

A3 :
No, the swap command is meant to make the solvers confirm back their status to the publisher if the solver was idle nothing happens, if the solver was already working it continues working.

Consider this:
PERF → Stopped
PRIM → Working
SWAP cmd…
PRIM → PERF Working on the minted block, previously PRIM, the publisher needs to know this.
PERF → PRIM Stopped and ready to be initialized with a new quiz, then Working.
Generate new block…
Stop PERF…

SWAP cmd
PERF → PRIM ; Stopped and ready for a new quiz
PRIM → PERF ; Continues working on the prev block

A4 :
All completed blocks are published,
An updated block will trigger fork resolution against the unimproved version. Forks of length 1. Given this criteria, best difficulty wins.

A block is not published until the interval time has elapsed, the solver will continue to work on a better solution as long as it has time left.

A5 :

SOLVER_PRIME : Solve quiz at first, If found nonce, send nonce to the publisher and keep working forever. At Swap cmd, switch identity to SOLVER_PERF.

SOLVER_PERF : Try to Refine the nonce forever until told otherwise.

Time to publish the newest block (not the refined one).

SOLVER_PRIME : Receive new quiz. Previously known as PERF. Solve the quiz. If found nonce, ship it for publishing and keeps working on the same quiz. Change identity to PERF.
SOLVER_PERF : Give up current work. Change Identity. "I’m going to wait new quiz”. Solve the new nonce of new candidate block.

A7 :

candidate_block : PRIME is on? Yes
candidate_block2 : PERF is on? Yes
claim == publish : block to network

A8 :
SOLVER_PERF is never gonna give you up as long as there’s time.
SOLVER_PERF was SOLVER_PRIME at 35 diff. It will try to find a better one, if it didn’t is because it ran out of time.

In this situation. there wont be an improved block, there won’t be fork resolution. Happy days.

2 Likes

Very very helpful article to me to understand the solver.
I have no question anymore.
Thank you.
Have a nice weekend.