# Blockchain Technology for Secure Communication and Formation Control in Smart Drone Swarms
## 4 Materials and Methods
### Proposed Scheme
Activation of the declared mission is carried out using the following function:  
```
1 functionactivateMission(uint16 missionId)publiconlyLeader{
2 require(missions[missionId].id==missionId,\"Nouchmission\");
3 missions[missionId].active=true;
4 emitMissionActivated(missionId);
5}
```  
**Listing 4**. Mission activation function. In the suggested smart contract, a mechanism for confirming that the leader is alive has also been included. For the followers to be certain that their leader is still active, the leader must send a heartbeat message that can be read by them. An indicative threshold of 180 s has been used, even though this can be easily modified. If the leader does not report his status, a new leader-election process will follow. The outcome of this process will favor the drone with the highest battery life. Leader's functionality flowchart can be seen below (Figure 5). As for the followers, the first thing they do is make sure their leader is still active. Followers can inspect whether a heartbeat message has been sent by the leader by invoking the smart contract's checkLeaderStatus method (Listing 5). If not, they go through a process of choosing a new leader. The drone with the highest battery life is chosen as the new leader of the swarm, presuming that all of the drones in the swarm are of the same type (so there are no connectivity, design, or other issues). Drones can use the following function to periodically report their battery levels (Listing 6):  
```
1 functioncheckLeaderStatus()public{
2 if(block.timestamp-lastHeartbeat>heartbeatTimeout){
3 leaderIsAlive=false;
4 if(msg.sender!=leader){
5 electNeLeader();
6}
7}
8} }
```  
Listing 5: Checking the leader's status. ```
1 functionsubmitBatteryLevel(uint8 batteryLevel)public{
2 require(drones[msg.sender],\"Onlydronescansubmitbatterylevel.\");
3 batteryLevels[msg.sender]=batteryLevel;
4}
```  
Listing 6: Battery level submission for followers. The aforementioned procedure does not take place when the leader is active. The mission and the declared formation can now be checked by followers by reading the data that their leader has submitted. Knowing the position of their leader, they can select the location of the formation that is closest to them. A formula is required to translate angles and distances into coordinates in order to create a formation scheme. The Haversine equation is the ideal solution for this problem. If the coordinates of two points are \((\phi_{1},\lambda_{1}),(\phi_{2},\lambda_{2})\)  
Figure 5: Leader’s functionality flowchart. (latitude, longitude), the central angle \(\theta\) between the two points can be determined using the equation:  
\[\mathit{hav}(\theta)=\mathit{hav}(\phi_{2}-\phi_{1})+\mathit{cos}(\phi_{1}) \mathit{cos}(\phi_{2})\mathit{hav}(\lambda_{2}-\lambda_{1}), \tag{1}\]  
where \(\mathit{hav}\) is the Haversine function \(\mathit{hav}(\theta)=\mathit{sin}^{2}(\frac{\theta}{2})\). Subsequently, the distance between the two points along the geodesic can be calculated using the formula \(d=r\theta\), where \(r\) is the earth's radius. The desired coordinates for the formation can be determined by following the same approach in reverse. For example, if the desired formation is V, for angles \(\theta\in\{-30,30\}\) and distances \(d=10\) m between drones, the inverse Haversine formula calculates the desired coordinates. This information can be used by the drones to determine the location that is closest to them (through a loop). Assume that they are located at \((\phi_{d},\lambda_{d})\). If the coordinates determined using the Haversine formula are \((\phi_{i},\lambda_{i})\), by using the Euclidean distance equation  
\[r_{i}=\sqrt{(\lambda_{d}-\lambda_{i})^{2}+(\phi_{d}-\phi_{i})^{2}}, \tag{2}\]  
for all of them, the position nearest to them can be calculated. First, the available positions in the formation are examined by the drones through the blockchain. If the position is free, a submission process will occur through which the position will be submitted to the blockchain.