# Blockchain-Based Decentralized Federated Learning Method in Edge Computing Environment
## 3 Methodology
### R-PBFT Consensus Algorithm  
To improve the model training efficiency of BD-FL, we propose a reputation-based practical Byzantine fault tolerance (R-PBFT) consensus algorithm. In the BD-FL environment, due to the large number of nodes, the traditional PBFT algorithm will lead to a sharp increase in communication cost and network bandwidth consumption in the consensus process, which is easy to cause network congestion and increase the delay of model training. In addition, the master node election in PBFT adopts the modulus calculation, which cannot guarantee the optimal master node of the election, thus affecting the consistency and reducing the reliability and security of the system. The R-PBFT consensus algorithm combines the characteristics of BD-FL and designs the following improvements to solve the shortcomings of traditional PBFT for BD-FL. * Remove the client node. In the traditional PBFT algorithm, the request phase and the reply phase occur between the client and the master nodes. However, in the blockchain structure, information is broadcast between nodes in the form of P2P, without the participation of the client. Therefore, we remove the request and reply phases of the client node in the consistency protocol, modify the C/S structure of PBFT to a distributed topology, and divide all nodes into master and slave nodes. * Optimize the consistency protocol. The five phases of consensus in PBFT are changed to three phases, including the pre-preparation phase, preparation phase, and confirmation phase. In the pre-preparation phase, the master node broadcasts blocks to other slave nodes. In the preparation phase, the slave node broadcasts the block verification results to other slave nodes and master nodes. In the confirmation phase, traditional PBFT requires mutual interaction between nodes. We simplify it as all slave nodes send verification results to the master node, and the master node makes a decision on the consensus results, thus reducing the communication overhead of consensus. * Introduce reputation mechanism. The main purpose of the reputation mechanism is to make the nodes with high reliability easier to be elected as the master node. Each node will be divided into different reputation levels according to the reputation value, and then each node will be rewarded or punished based on its performance in each round of consensus. According to a preset reputation threshold, nodes can be dynamically transformed in different reputation levels. When the current round of consensus is completed, the system performs the reputation mechanism to assign each node to different reputation levels according to the reputation value and then selects the node with the highest score among the trusted nodes as the master node for the next round of consensus. Figure 2 shows the execution flow of a round of consensus, where \(P\) denotes the master node and \(S\) denotes the slave node. According to the reputation value \(R\) of each node, the reputation mechanism divides it into three different reputation levels, namely trusted node, normal node, and unreliable node. \(R\) is a real number between 0 and 1, and its size reflects the reliability of the node. For the trusted node, the range of \(R\) is (0.8,1], and the node of this level generated valid blocks multiple times. For the normal node, the range of \(R\) is (0.3,0.8], and the node of this level generated unqualified blocks, but less often. For the unreliable nodes, the range of \(R\) is [0,0.3], and the node of this level generated unqualified blocks many times. The unreliable node will not participate in the election of the master node and the consensus process of the blockchain and only saves block data. Each node updates its \(R\) according to the following rules after a consensus, so as to dynamically transform in different reputation levels. (1) The \(R\) of the node will be increased by 0.01 for each successful consensus participation. (2) If the master node successfully generates a valid block, its \(R\) will be increased by 0.02.