# HPoC: A Lightweight Blockchain Consensus Design for the IoT
## 5 Election Algorithm Based on Proof-of-Capability
In this paper, the RANDOM SEED is generated by the VRF and is hashed after stitching with a random number to obtain the ELECTION HASH. Because PoC uses a double-leader model, two elections will be conducted during the consensus startup phase; the LEADER node will be elected first, and then the VICE-LEADER node will be elected. After that, only the election of the VICE-LEADER will be conducted during the consensus running phase (by default, the case in which both the LEADER and the VICE-LEADER drop out at the same time is not considered). The algorithm uses a node's private key and the current timestamp to construct a VRF RANDOM SEED, as well as the VRF proof to verify it. After that, a random x-bit random number (an \(\times\)bit is the maximum number of bits in the cache data table of the whole network, which means the random number is selected in the range of 2\(\times\)) and the RANDOM SEED are stitched together to obtain the ELECTION HASH. The ELECTION HASH and the RANDOM SEED, VRF proof, and public key of this node are packaged as ELECTION DATA and disseminated to the whole network. The pseudo code of Algorithm 1 is as follows:  
```
NodeP = RandomSelect (Nodegroup) randomSeed = VrfHash (Skp, TimeStamp) proof = VrfProof (Skp, TimeStamp) randomNumber = RandomSelect (2\({}^{\circ}\)) elecHash = Hash (randomSeed + randomNumber) elecData = Package (elecHash, Pkp, randomSeed, TimeStamp, proof) Broadcast (elecData)
```  
**Algorithm 1** Election Hash Generation  
Algorithm 1 describes a process in which node P generates and broadcasts the ELECTION HASH. Then, all nodes will run the leader election algorithm after receiving the ELECTION DATA. The validity of the RANDOM SEED is first checked, and then they proceed to calculate the ELECTION HASH by traversing the cache data table, which they manage themselves (traversing the selected data and RANDOM SEED are stitched together to perform the hash calculation, and then compared with the ELECTION HASH in the ELECTION DATA). The node that discovers the random number signs it before broadcast ing the random number, signature, and public key to the whole network. After receiving the packet, all nodes in the network will check it, and if the verification is successful, they will vote for the node and append their signatures. In each term, each node can only vote once. Algorithm 2 depicts how a Node q participates in the election of the leader. Receive (elecData)  
IfTrue VRFverify (Pkp, randomSeed, TimeStamp, proof) For x in CacheTable:  
IfTrue Equare (Hash(x + randomSeed), elecHash) Signature = Sign (Skq, x)  
elecProof = Package (Signature,Pkq, x) Broadcast (elecProof)  
ReceiveVotes()  
**Algorithm 2** Leader Election  
The LEADER node is the first node to collect more than half of the network's voting signatures and to begin generating the genesis block, which records the TERM and the LEADER node's metadata. The leader node will then immediately begin the VICE-LEADER election, which is identical to the LEADER node election except that the node that performs Algorithm 1 becomes the LEADER node. The election procedure is the same in the ensuing time-of-consensus operation. Figure 1 illustrates the process of PoC consensus. At the beginning, a node selected from the whole network chooses a random number, executes the algorithm to calculate the ELECTION HASH, and generates the corresponding VRF information. After that, the ELECTION information will be broadcast to the whole network. All nodes first verify the correctness and legitimacy of the ELECTION information received. After the verification is passed, the nodes start to participate in the LEADER election. The first node to find the corresponding random number (assuming node A) signs and packages the relevant information into the ELECTION PROOF and broadcasts it. Other nodes also verify after receiving the ELECTION PROOF, and vote for node A after passing the verification. When node A collects more than half of the votes of the whole network, it automatically becomes a LEADER node and starts its first TERM.