# Blockchain-Based Decentralized Federated Learning Method in Edge Computing Environment
## 3 Methodology
### Preference-Based Stable Matching Algorithm  
Since local devices need to transmit a large amount of data to edge servers, the network quality, the transmission distance between nodes, the server throughput, and other factors will largely affect the overall delay and energy consumption of the system. We design a preference-based stable matching algorithm in BD-FL, which considers the above factors that affect data transmission, so as to achieve the optimal matching and binding between local devices and edge servers. In the network environment, local devices and edge servers are abstracted into two sets, which are, respectively, denoted by the set of local devices \(K=\{k_{1},k_{2},\ldots,k_{m}\}\) and the set of edge servers \(S=\{s_{1},s_{2},\ldots,s_{n}\}\). For each \(k\in K\), it has a matching request, denoted as Equation (3),  
\[Q_{k}=(D_{k},Info_{k}), \tag{3}\]  
where \(D_{k}\) represents the data of uplink communication that local device \(k\) uploads to the edge server, mainly including information such as local model parameters, version number, local iteration time, etc. ; \(Info_{k}\) represents the state information of local device \(k\), such as bandwidth, physical location, etc. The uplink communication rate \(v_{ks}\) determines the data transmission delay, which is expressed as Equation (4) according to [32],  
\[v_{ks}=band_{k}*\log_{2}\left(1+\frac{p_{k}*g_{ks}}{N_{0}}\right)\,, \tag{4}\]  
where \(band_{k}\) represents the channel bandwidth allocated to the local device \(k\), \(p_{k}\) represents the transmit power of \(k\), \(g_{ks}\) represents the channel gain between \(k\) and the edge server \(s\), and \(N_{0}\) represents the noise power of \(s\). According to [32], the network distance between nodes is related to the channel bandwidth. It reflects the actual distance and network condition between local devices and edge servers. Assuming that \(phys_{k}\) and \(phys_{s}\) are the physical nodes corresponding to the local device and the edge server, respectively, the network distance \(distNode_{ks}\) between them is defined as Equation (5),  
\[distNode_{ks}=\frac{band_{aver}}{(band_{k}+band_{s})/2}*distPhys_{ks}\,, \tag{5}\]  
where \(distPhys_{ks}\) represents the actual distance between \(phys_{k}\) and \(phys_{s}\), which has a certain influence on the reliability of data transmission and the network latency, \(band_{aver}\) represents the average bandwidth between them, and \(band_{k}\) and \(band_{s}\) represent the actual bandwidths of \(phys_{k}\) and \(phys_{s}\). Equations (6) and (7) express the upload time and the upload energy consumption, respectively, after local device \(k\) and edge server \(s\) are bound. \[T_{ks}=\frac{D_{k}}{v_{ks}}\;, \tag{6}\]  
\[E_{ks}=p_{k}*T_{ks}=p_{k}*\frac{D_{k}}{v_{ks}}\;. \tag{7}\]  
We use the sum of weighted energy consumption and weighted delay, denoted as \(w_{k}\), as the cost function of \(k\), which is expressed by Equation (8),  
\[\omega_{k}=\mu_{k}^{e}*E_{k}+\mu_{k}^{t}*T_{k}\;, \tag{8}\]  
where \(\mu_{k}^{e},\mu_{k}^{t}\in[0,1],\mu_{k}^{e}+\mu_{k}^{t}=1\), \(\mu_{k}^{e}\) and \(\mu_{k}^{t}\), represent the energy consumption weight and the delay weight of \(k\), respectively. A larger \(\mu_{k}^{e}\) indicates a higher priority of energy consumption, and a larger \(\mu_{k}^{t}\) indicates a higher priority of latency, and vice versa. The priority of energy consumption and delay of the local device can be adjusted by modifying the value of \(\mu\). We define a coefficient matrix \(W\) to represent the cost relationship between local devices and edge servers. It is expressed by Equation (9), where \(w_{ks}\) denotes the cost when device \(k\) is bound to server \(s\), and its value can be calculated by Equation (8). \[W=\left(\omega_{ks}\right)_{m=n}= \begin{bmatrix}\omega_{11}&\omega_{12}&\dots &\omega_{1n}\\ \omega_{21}&\omega_{22}&\dots&\omega_{2n}\\ \dots&\dots&\dots&\dots\\ \omega_{m1}&\omega_{m2}&\dots&\omega_{mn}\end{bmatrix} \;.