# Blockchain Based Secure Routing and Trust Management in Wireless Sensor Networks +
## 4 System Model
### Trust Evaluation Mechanism  
The traditional systems lead to issues such as lack of trust, single point of failure, high computational cost, etc. The SNs may behave selfishly in the networks due to the limited resources. Furthermore, any malicious node can become part of the network and perform malicious activities. To tackle these issues, a blockchain is used with our trust evaluation mechanism. The trust value of all nodes is calculated by considering the Forwarding Rate (FR), Response Time (RT) and Delayed Transmission (DT). After the trust evaluation, the trust values are stored in the blockchain. The blockchain provides data immutability wherein data cannot be tampered with by the malicious nodes. Moreover, no node can repudiate its action as the blockchain provides traceability in the trust evaluation process. Therefore, the trust value of each SN is calculated to remove selfish and malicious nodes from the network. The trust value of each SN is compared with a predefined threshold and then malicious and legitimate nodes are classified considering this threshold, motivated by [19]. We have considered legitimate and malicious nodes in the network and assumed that there are no external factors and harsh environmental conditions that can affect the objective parameters: FR, RT and DT. This means that the node cannot be a malfunctioning node due to its internal technical faults and harsh network conditions. Moreover, the trust value is calculated considering DT, FR and RT, which are completely dependent on the behavior of SNs. This research assumption is supported by the works of [46; 47; 48] The steps necessary for the trust evaluation of the SNs are mentioned below. Step 1: ANs determine the states of SNs as either alive or dead. Step 2: The Node Communication Quality (NCQ) is computed for the alive SNs on the basis of DT, FR and RT. Step 3: The number of successful and unsuccessful communications is computed based on the NCQ. When the value of the NCQ is greater than the threshold, it is considered as a successful communication, otherwise as a unsuccessful communication. Step 4: The trust values of the SNs are determined on the basis of successful and unsuccessful communications. Step 5: When the trust values of SNs are greater than the threshold, they are considered as legitimate nodes, otherwise malicious nodes. Step 6: After the trust evaluation, the ANs send the trust values of the SNs to the BSs and malicious nodes are removed from the network. ```
1 begin
2 ifVerifyID(\(ID_{A}\)) = errorthen
3 return error message ();
4
5 end if/*\(PUB\) is public blockchain*/
6/*\(PRB\) is private blockchain*/
7/*\(S_{PUB}\) is the smart contract of public blockchain*/
8/*\(S_{PRB}\) is the smart contract of private blockchain*/
9 ifNodeExists (\(A_{AN_{ID}}\), \(PUB\)) = error|| Verifailize (\(A_{AN_{ID}}\)) = errorthen
10 return error message ();
11\(A_{AN_{ID}}\) is considered as malicious \(AN\);
12\(A_{AN_{ID}}\) is permanently stored in \(PUB\) through \(S_{PUB}\);
13
14 end if
15 ifNodeExists (\(B_{AN_{ID}}\), \(PUB\)) = error|| Verifailize (\(B_{AN_{ID}}\)) = errorthen
16 return error message ();
17\(B_{AN_{ID}}\) is considered as malicious \(BN\);
18\(B_{AN_{ID}}\) is permanently stored in \(PUB\) through \(S_{PUB}\);
19
20 end if
21 if\(BS_{ID}\) ensures the identitythen
22 return nodes \(A_{AN_{ID}}\) and \(B_{AN_{ID}}\) establish a secure connection;
23
24 end if
25
26 end if
```  
**Algorithm 2**Mutual authentication  
In our model, the routing is improved by the trust evaluation mechanism in our system model. First of all, the trust value of each node is calculated considering DT, RT and FR. After the calculation of the trust value, the nodes with a trust value lower than the predefined threshold are considered as malicious nodes and removed from the network. So, only legitimate nodes remain in the network and participate in the routing mechanism. In this way, the routing mechanism is improved by a trust evaluation mechanism. In the network, the SNs are divided into alive and dead nodes.