# Questions about mining script

**URL:** <https://community.creditcoin.org/t/questions-about-mining-script/324>\
**Category:** Mining\
**Created:** [June 23, 2021, 12:53am UTC](https://community.creditcoin.org/t/questions-about-mining-script/324 "2021-06-23T00:53:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![doebrmann](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@doebrmann](https://community.creditcoin.org/u/doebrmann)\
**Post date:** [June 23, 2021, 12:53am UTC](https://community.creditcoin.org/t/questions-about-mining-script/324/1 "2021-06-23T00:53:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hilt](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@Hilt](https://community.creditcoin.org/u/Hilt)\
**Post date:** [June 24, 2021, 4:33am UTC](https://community.creditcoin.org/t/questions-about-mining-script/324/2 "2021-06-24T04:33:50Z")

</div>

> [@doebrmann](#):
>
> update\_block(

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.

---

<div class="post-metadata">

**Author:** ![Hilt](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@Hilt](https://community.creditcoin.org/u/Hilt)\
**Post date:** [June 24, 2021, 4:35am UTC](https://community.creditcoin.org/t/questions-about-mining-script/324/3 "2021-06-24T04:35:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![doebrmann](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@doebrmann](https://community.creditcoin.org/u/doebrmann)\
**Post date:** [June 24, 2021, 7:05am UTC](https://community.creditcoin.org/t/questions-about-mining-script/324/4 "2021-06-24T07:05:04Z")

</div>

Hi~ the commenter 🙂  
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?

---

<div class="post-metadata">

**Author:** ![doebrmann](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@doebrmann](https://community.creditcoin.org/u/doebrmann)\
**Post date:** [June 24, 2021, 9:51am UTC](https://community.creditcoin.org/t/questions-about-mining-script/324/5 "2021-06-24T09:51:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Hilt](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@Hilt](https://community.creditcoin.org/u/Hilt)\
**Post date:** [June 25, 2021, 2:39pm UTC](https://community.creditcoin.org/t/questions-about-mining-script/324/6 "2021-06-25T14:39:06Z")

</div>

Yes. Once [on\_check\_publish\_block(…)](https://github.com/gluwa/Creditcoin/blob/dev/Creditcoin/PoW/hotfix/publisher.py) 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.

---

<div class="post-metadata">

**Author:** ![Hilt](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@Hilt](https://community.creditcoin.org/u/Hilt)\
**Post date:** [June 25, 2021, 3:32pm UTC](https://community.creditcoin.org/t/questions-about-mining-script/324/7 "2021-06-25T15:32:21Z")

</div>

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)](https://github.com/gluwa/Creditcoin/blob/dev/Creditcoin/PoW/hotfix/publisher.py)

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.

---

<div class="post-metadata">

**Author:** ![doebrmann](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@doebrmann](https://community.creditcoin.org/u/doebrmann)\
**Post date:** [June 25, 2021, 8:51pm UTC](https://community.creditcoin.org/t/questions-about-mining-script/324/8 "2021-06-25T20:51:18Z")

</div>

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