# A Compute and Wait in PoW (CW-PoW) Consensus Algorithm for Preserving Energy Consumption
## 6 Implementation  
In our experiment, we simulated each node by a process, where we implemented multi-process programming. In several tests, we created a different number of processes, each of them starting with the creation of a random number \(blockID\gets random()\), which is considered as the ID of the candidate block that the process is working on. After that, each process follows the execution of the proposed algorithm (Algorithm 1). Using five rounds and ten processes, the test took about 5 min, and we got the result shown in Figure 2. The execution at University of El Oued was defined in Python using a mainframe made up of 32 dual-processor nodes of 10 cores each. The intensive computing unit has a management node with the following specifications: Processor: Intel Xeon (R) E5-2660 v3 @ 2.60 GHz x 20, memory: 64 GB of RAM, disk: 2 TB HDD, OS: RedHat Enterprise Linux Server 7.2, OS type: 64 bit. The computing unit has 32 nodes. Each node has the following characteristics: 10 physical cores, storage capacity: 500 GB HDD and available memory: 64 GB. The Proposed technique can be defined by the following algorithm:  
```
0:\(NbrS\), \(NbrR\)
0:\(solution\)
1:procedureCons(\(NbrS\), \(NbrR\))
2:\(sols\gets 0\)
3:\(round1D\gets 0\)
4:\(currentRound\gets 1\)
5:\(q\gets Queue()\)
6:\(blockID\gets random()\)
7:while\(currentRound<=NbrR\)do
8:while\(solution\) not found do
9:\(search\) for\(solution\)
10:endwhile
11:\(sols\gets sols+1\)
12:if\(currentRound\) is \(NbrR\)then
13:\(print(I\) am the \(winner)\), \(Exit()\)
14:endif
15:\(Broadcast(currentRound,\ solution)\)
16:\(put\ on(q,\ solution)\)
17:while\(sols<NbrS\)do
18:\(Nothing\)
19:endwhile
20:\(sols\gets 0\)
21:\(currentRound\gets currentRound+1\)
22:\(Round1D\leftarrow\sum_{i=1}^{NbrS}q(i)\)
23:\(Broadcast(currentRound,\ Round1D)\)
24:endwhile
25:endprocedure
```  
**Algorithm 1** Consensus algorithm  
We repeated the experiment tens of times in each scenario (each input variation). For example, in Figure 3 with the first scenario where the number of processes is equal to 5 and the number of rounds equal to 5, the gain was 12.07. We repeated this test several times and we obtained different values (between 11 and 14), but we took the most frequent value which was 12.07. We did this at each input change (NbrR, NbrP = (5, 5); (5, 10); (5, 15); (5, 20). We did not take the average of measurements, but we considered the most frequent value. Figure 2: Compute and wait example execution. The formal definition of gain: In each round, the process searches for a solution (compute time). Then the process will wait until it receives the latest solution. We will consider this wait time as a gain, and we can formulate the gain rate as follows: _gain rate_ = _wait time/total execution time_. If the total execution time is the wait and compute time, we obtain:  
\[gain\ rate=wait\ time/(wait\ time+compute\ time) \tag{4}\]  
We let \(C_{i,x}\) (respectively, \(W_{i,x}\)) be the compute (respectively, wait) time of the process \(i\) in round \(x\). The gain of the process \(i\) can be formulated as follows:  
\[gain_{i}=\sum_{j=1}^{NbrP}(W_{i,j}/(W_{i,j}+C_{i,j})) \tag{5}\]  
The average gain rate (network gain rate) is:  
\[Gain=\sum_{i=1}^{NbrP}(gain_{i}/NbrP) \tag{6}\]  
In the scenario shown in Figure 2, we have set the number of rounds \(NbrR=5\), \(NbrP=NbrS=10\), where NbrP (respectively, NbrS) is the number of processes (respectively, of solutions). We do not put different values between \(NbrP\) and \(NbrS\) in order to study the pure effect of the number of rounds. We notice that there is a real compute and wait in each round for most of the processes. Process 10 is the fastest because it has the minimum execution time and the maximum waiting time, that is why it has the highest gain rate (45.52%). On the other hand, process 1 (\(p1\)) is the heaviest process, because \(p1\) has the highest execution and the lowest waiting time, and thus the lowest gain rate. For \(p1\), the gain rate = wait/(wait + execution) = \(0/(0+195)=0\).