Blockchain-Enabled Task Offloading and Resource Allocation in Fog Computing Networks

To address the data security and user privacy issues in the task offloading process and resource allocation of the fog computing network, a blockchain-enabled fog computing network task offloading model is proposed in this paper. Furthermore, to reduce the network utility which is defined as the total energy consumption of the fog computing network and the total delay of the blockchain network, a blockchain-enabled fog computing network task offloading and resource allocation algorithm (TRBFCN) is proposed to jointly optimize the task offloading decision and resource allocation. Finally, the original nonconvex optimization problem is converted into two suboptimization problems, namely, task offloading decisions and computational resource allocations. Moreover, a two-stage Stackelberg game model is designed to obtain the optimal amount of purchased resource and the optimal resource pricing. Simulation results show that the proposed TR-BFCN algorithm can effectively reduce the network utility compared with other algorithms.


Introduction
With the rapid growth of smart devices and the development of the Internet of Things (IoT), the traditional network architecture is facing a huge flow load problem [1]. As a supplement to cloud computing, fog computing could randomly deploy a large number of fog nodes (FNs) at the edge of the network to deal with the real-time application requirements generated by devices and improve network performance [2]. In the distributed fog network, IoT devices (IDs) interact with FNs in real time. Due to the mobility of IDs, service migration across FNs will occur during the interaction [3]. Meanwhile, because FNs are distributed in an unsupervised environment, the security of the user's data during the task processing is an important challenge for fog computing networks (FCNs) [4]. Currently, to ensure the user privacy and data security in fog computing networks, the following two methods are taken into account: (i) Method 1: develop a reliable, effective mechanism to evaluate the reputation of IDs and FNs. Reputation of IDs and FNs is used to indicate their credibility [5]. If the user privacy is leaked or data tampering occurs, the reputation of the associated nodes will be decreased. Reference [6] proposed a contextaware trust evaluation algorithm to calculate the trustworthiness of users in FCNs. Reference [7] proposed a trust forwarding mechanism in FCNs, which divided the network into edge communication communities, and set the reputation of nodes in the community based on actual factors such as the communication probability and service degree. Reference [8] considered the reputation of vehicles in distributed Internet of Vehicles. The authors mainly considered familiarity, similarity, and timeliness to calculate the local reputation of vehicles. Finally, the local reputation and the historical reputation were combined to calculate the globe reputation of vehicles. Reference [9] proposed a resource allocation scheme for community-based fog computing based on reputation mechanism. A user first submits his/her task request to the community-based fog network, and then, the fog server makes a reliable resource allocation process based on multiple-layer communities and reputation calculation. Reference [10] proposed innovative approach that estimates the initial reputation values of newcomer services in web service recommendation systems through the use of the fog node's (service provider) reputation and ANN model. The quality of services and reputation of previously published services are used to estimate the reputation values of newcomer services (ii) Method 2: combine blockchain technology to achieve reliable data transmission [11]. Blockchain has unique features such as fault tolerance, security, and immutability, as well as the potential to provide decentralized databases and network operations [12]. Blockchain technology is used to realize the verification and traceability by building a decentralized ledger to record transactions between users [13]. Different from traditional digital ledgers, blockchain does not rely on a central authority [14]. In addition, the consistency of the ledgers between nodes is ensured by the consensus mechanism. Reference [15] proposed a user privacy protection scheme based on blockchain and federated learning (Blockchain-Enabled Federated Learning (FL-Block)), which proposed an autonomous learning process without any central trust agency to maintain the global model, and complete data processing and transmission through the blockchain Proof-of-Work mechanism and consensus mechanism [16]. In addition, the authors jointly considered the communication delay, consensus delay, and computation cost to optimize the block generation rate. Reference [17] studied the data security and user privacy issues in the vehicle fog networks. A lightweight anonymous authentication mechanism assisted by blockchain was proposed to realize the flexibility and anonymity of cross-center authentication, reduce the communication delay of data authentication, and build a lightweight network authentication mechanism. Reference [18] proposed a data sharing model based on blockchain. The data owner processes the original data generation according to the algorithm provided by the data requester. The data report is returned to the demander, and the data sharing platform supervises all the behaviors of the sharing parties and writes the entire sharing process into the blockchain, which not only satisfies the demander's use of data but also avoids the risk of data leakage by the data holder However, due to the limited computing capability of FNs, when FNs process task offloading and blockchain verification at the same time, there could be a shortage of resources, resulting in the task offloading time increasing or the block generation and verification delay increasing, which affects the transaction's recording time in the blockchain [19]. In order to optimize the allocation of computing capability and ensure the security of the network, we propose a task offloading and resource allocation algorithm in blockchain-enabled fog computing network (TR-BFCN) in this paper. The TR-BFCN algorithm is aimed at minimizing network utility, i.e., the total energy consumption of the FCN and the total delay of the blockchain network, by jointly optimizing the task offloading decision and resource allocation between two networks.

Network Modeling and Analysis
2.1. Network Model. A blockchain-based fog computing network (FCN) is shown in Figure 1, which includes the device layer and the fog layer. The fog layer consists of geodistributed fog nodes (FNs) to provide computing, storage, and communication resources to nearby IoT devices (IDs). Besides, blockchain is deployed in the fog layer to verify and record transactions. The task offloading process is composed of three parts: master node election, resource pricing and offloading decision-making, and blockchain consensus process. The Authorized Service Center (ASC) is a trusted edge server, which is responsible for monitoring the FNs in the coverage area and the authentication process of network entities. The ASC will generate a unique publicprivate key pair and identification code for each entity, as well as the entity's cancellation and other functions.
Firstly, IDs should finish the registration and authentication through the ASC, then they could offload tasks to associated FNs for processing. FNs with higher reputation value will be elected as blockchain nodes, which will not only provide offloading services but also block consensus services.
Secondly, IDs purchase computing resources from FNs to transfer the offloading tasks. Then, the task offloading decisions and resource allocation will be performed to minimize the network utility.
Finally, the task offloading process in the FCN is regarded as a transaction record. The elected blockchain nodes will package transactions to generate blocks and perform a block consensus verification process. The verified blocks will be added to the blockchain network, to guarantee the network security and transactions traceability. The blockchain technology is used to preserve transactions among distributed nodes, which requires additional storage capacity on devices. Moreover, the consensus algorithms used to ensure the data security of devices also require additional computing and communication resources.

Communication Model.
Let fD k , C k g denotes the task offloading request sent by ID k, where D k (bit) is the size of the task and C k (cycle/bit) is the task calculation frequency, i.e., the task processing intensity. The total system bandwidth W is divided into N resource blocks (RB) with equal bandwidth, namely, B = W/N. To avoid interference between IDs during task offloading, each RB could be only assigned to one ID and one FN. The uplink transmission rate of ID k offloading task to FN m on RB n is given as r k,n,m t ð Þ = B log 2 1 + p k,n,m h k,n,m 2 where p k,n,m denotes the transmission power from ID k to FN m through RB n; h k,n,m ðtÞ = l k,m ⋅ g k,n,m represents the q and small-scale fading g k,n,m , where c represents the average channel gain at 1 km; s k,m is the logarithmic shadow fading variable with mean 0 and standard deviation σ sh ; d k,m denotes the distance from ID k to FN m; and α is the path loss index. g k,n,m is the independent and identically distributed complex Gaussian random variables with zero mean unit variance. σ 2 represents additive white Gaussian noise. Therefore, the offloading delay t tr k,m from ID k to FN m by RB n is given as where α k,m is the allocation index between ID k and FN m.
And α k,m = 1 indicates that ID k will offload tasks to FN m, while α k,m = 0 represents that ID k will not offload tasks to FN m; b m,n is the allocation index between RB n and FN m . b m,n = 1 indicates that ID k will offload tasks to FN m on RB n, and b m,n = 0 represents that ID k will not offload tasks to FN m on RB n. Moreover, ID k could offload tasks to multiple FNs, and x k m indicates the task offloading proportional coefficient from ID k to FN m.
Thus, the total transmission energy consumption of the fog computing network can be expressed as 2.3. Calculation Model. For the computation phase, we mainly consider the task computation delay and the computation energy consumption of FN m. The task computation delay of FN mt ser k,m is given by where C k denotes the number of CPU cycles required to process the unit data size and f ðFÞ k,m (cycle/s) represents the computation resources provided by FN m for the task offloaded from ID k.
Thus, the computation energy consumption of FN m is given as where κ m is the energy consumption coefficient of FN m.

Wireless Communications and Mobile Computing
Therefore, the computation energy consumption of the FCN could be calculated by In summary, the total energy consumption of the FCN includes the transmission energy consumption and the computation energy consumption, which can be expressed as 2.4. Blockchain Network Consensus Model. Based on blockchain technology, the FCN could effectively prevent malicious FNs from tampering or copying user data and improve the reliability of data transmission. In the blockchain network, transaction records will be processed in the following two steps: (1) Block generation, where the transaction records are packaged to generate blocks through the master node (2) Block consensus, where the master node will broadcast the new block to verification nodes to form a unified ledger information; then, all verification nodes compare the hash value and return the verification results to the master node through their private key signatures If the verification is passed, the master node will add the new block to the blockchain.
Assume there are L blockchain nodes, the master node m b could process transaction records, and the block generation delay is given as where f ðBÞ m b , m b , and C b denote the computation frequency, the task processing intensity, and the processing data size of the master node m b , respectively. Assume that all verification nodes are honest and reliable; the consensus process is divided into two steps: block propagation and block verification. In the block propagation phase, the propagation rate from the master node m b to the verification node m b ′ is Correspondingly, the block transmission delay t In the block verification phase, the verification node will confirm the content of the block broadcasted by the master node, with the verification delay t ver where δ b is the number of CPU cycles required for block verification and f ðBÞ m b ′ represents the computation frequency to verify the block.
Therefore, the total delay of the blockchain network is expressed as where χ m b represents the master node indication parameter. If χ m b = 1, FN m b will be elected as the master node.

Optimization Problem Modeling
To minimize the network utility, namely, the sum of the total energy consumption of the FCN and the total delay of the blockchain network, the optimization problem could be represented as x k m D k = D k ,∀k, Wireless Communications and Mobile Computing where α is the proportion of the total energy consumption of the FCN in the optimization goal; C 1 indicates that an ID can select multiple FNs to offload tasks; C 2 indicates that an RB can only be allocated to one FN, but each FN can be allocated multiple RBs; C 3 indicates that the computation resources allocated by the FCN cannot exceed the purchased computation resources; C 4 indicates the computation resources constraint; C 5 indicates that the total delay of the FCN cannot exceed the maximum tolerable delay of the task; C 6 is the task size constraint; and C 7 is the blockchain master node constraint, which ensures that there is only one master node in each processing cycle.

Task Offloading and Resource Allocation Scheme in Blockchain-Enabled Fog Computing Network
To optimize the computation resource allocation of the FCN and the blockchain network, in this paper, we propose the task offloading and resource allocation algorithm in blockchain-enabled fog computing network (TR-BFCN), which includes the Differentiated Resource Pricing Algorithm (DRPA), task offloading decision algorithm, and computation resource allocation algorithm.

Computing Resource Allocation Algorithm Based on
Differentiated Pricing. Firstly, a two-stage Stackelberg game is considered to optimize the amount of resources to purchase and the associated price. In the Stackelberg game, the cost of ID k to purchase computation resource from FN m is expressed as where p k,m denotes the price for ID k to purchase the computation resources from FN m, and f k,m represents the amount of computation resources purchased by ID k from FN m. The revenue function of ID k to offload tasks to FN m is defined as where ϖ is the benefit factor of ID k and C k denotes the number of CPU cycles required to process the unit data size. The offloading revenue of IDs is related to the amount of offloading tasks f k,m /C k . To maximize the benefits of ID k, the optimization problem is modeled as where f ðminÞ k,m and f ðmaxÞ k,m denote the minimum and the maximum amount of computation resources that could be purchased by ID k from FN m, respectively.
The computation energy cost of offloading tasks from ID k to FN m is defined as where κ m is the energy consumption coefficient of FN. Therefore, the utility function of FN m is defined as where p k,m is the price of computation resources provided by FN m for ID k.
To maximize the utility of FN m, the price to purchase computation resources should be optimized. The dynamic price optimization problem is modeled as where C1 indicates that the price is not lower than the cost of FN m. To ensure that the revenue of FN m is nonnegative, let u m = 0, and we have p cost k,m = κ m f ðminÞ k,m . SinceU k,m ð f k,m Þis a convex function off k,m , the constraint is a linear function. The optimization problem is convex, which can be solved by the Lagrange multiplier method. The Lagrange function is defined as where λ and υ are the Lagrange multipliers. The KKT (Karush-Kuhn-Tucker) condition is used to find the partial derivative of the Lagrange function L D k,m ð f k,m , λ, υÞ with respect to the variable f k,m . The optimal solution f * k,m can be obtained by

Wireless Communications and Mobile Computing
where ½X + = max f0, Xg. The Lagrange multipliers λ m and υ m are updated by the following equations: where n is the number of iterations and θ n 1 and θ n 2 are the step sizes.
Substituting f * k,m into (18) to find the second derivative, we can obtain where Θ = p k,m + υ m − λ m ; i.e., the revenue function of FN m is a convex function with respect to the variable p k,m , and its constraints are all linear functions. Therefore, the dynamic price optimization problem can be solved by the Lagrange multiplier method, and the Lagrange function is where φ k is the Lagrange multiplier. Similarly, the KKT condition could be used to find the partial derivative of its Lagrange function L F k,m ðp k,m , φÞ with respect to the variable p k,m ; the optimal solution p * k,m is given by The Lagrange multiplier φ k is updated by where n is the number of iterations and θ n 3 is the step size. To prove that the optimal solution (f * k,m , p * k,m ) is the Nash equilibrium solution, the following definitions and lemmas are proposed. Definition 1. When the price p k,m of FN m is fixed, the following condition is satisfied: Meanwhile, when the computing resources f k,m purchased by ID k from FN m are fixed, the following condition is satisfied: Then, (f * k,m , p * k,m ) is the Nash equilibrium solution, which will be proven by the following three lemmas. Proof. Based on the first-order partial derivative of the optimal solution f * k,m , we could obtain From (29), we know that f * k,m is a monotonically decreasing function of variable p k,m . When the quotation of FN m is too high, less IDs will purchase resources, resulting in decreasing of the revenue of FN m. Therefore, the optimal price of FNs could be obtained by setting the first partial derivative of L F k,m ðp k,m , φÞ to 0 [20].☐ Lemma 4. When the optimal computing resource f * k,m purchased by ID k from FNs is fixed, the revenue function L F k,m ð p k,m , φÞ of FN m achieves the maximum at p * k,m .
Proof. Since the second-order partial derivative of L F k,m ðp k,m , φÞ with respect to variable p k,m is less than or equal to 0, L F k,m ðp k,m , φÞ is a convex function with respect to variable p k,m . Therefore, L F k,m ðp k,m , φÞ achieves the maximum at p * k,m . According to Definition 1, p * k,m is the Nash equilibrium solution.☐ Therefore, the optimal solution (f * k,m , p * k,m ) is the Nash equilibrium solution of the Stackelberg game. The dynamic resource pricing algorithm (DRPA) is shown in Algorithm 1.

Task Offloading and Transmission Resource Allocation
Algorithm. To further minimize the utility of the FCN, the task offloading decisions and transmission resource allocation algorithm is proposed. Since the optimization problem is nonconvex, we decompose it into two subproblems and 6 Wireless Communications and Mobile Computing solve them individually to minimize the total energy consumption of the FCN. Firstly, the original optimization problem could be transformed to ð30Þ Based on the optimal task offloading algorithm proposed in [21], the task offloading decisions and transmission resource allocation results in this scenario can be obtained.

Computing Resource Allocation
Algorithm. The task offloading decisions and transmission resource allocation could be obtained through the above algorithm. In addition, the alternate direction method is used to optimize the computation resource allocation in the FCN and blockchain network.
First of all, with the fixed computation resource f ðBÞ m b of the blockchain network, we could solve the computation resource allocation problem in the FCN. Due to the coupling relationship between variables in the optimization problem, we assume that when ID k offloads tasks to multiple FNs simultaneously, the offloading proportional x k m from ID k to FN m is dependent on the amount of computation resources purchased from FN m, which is expressed as Based on the Lagrange multiplier method, the optimal solution f ðFÞ k,m can be obtained. The constraint C 5 is rewritten as Then, the Lagrange function is given by where π 1 , π 2 , and π 3 are the Lagrange multipliers. Thus, the optimal solution f ðFÞ k,m of the optimization problem can be obtained: (0) Initialize f k,m , p k,m , ω, t max , λ, υ, φ, ε 1 , and ε 2 (1) While t ≤ t max (2) For k = 1, ⋯, K For m = 1, ⋯, M (4) Update p * k,m ðtÞ according to (25) (5) Update f * k,m ðtÞ according to (20) Output optimal price p * k,m ðtÞ and computation resources f * k,m ðtÞ (8) End if (9) Else (10) The algorithm does not converge, let t = t + 1, f k,m ðt − 1Þ = f * k,m ðtÞ (11) Update Lagrange multiplier λ, υ, φ according to (21), (22), and (26) (12) Update the number of iterations t = t + 1 and return to step 1 (13) End else (14) End for (15) End for (16) End while Algorithm 1: Dynamic resource pricing algorithm (DRPA).

Wireless Communications and Mobile Computing
The Lagrange multiplier update are updated by where θ n 2 is the step size. Secondly, with the obtained computation resource f ðFÞ k,m of the FCN, the blockchain node selection and computation resource allocation problem could be expressed as Based on the reputation model proposed in [22], an odd number of nodes with high reputation will be selected as blockchain nodes to participate in the blockchain consensus process. Therefore, the computing resource allocation problem in the blockchain network is transformed into The optimization problem is a linearly decreasing function; when f ðBÞ m b takes the maximum value, the objective function obtains the minimum value, and we have Finally, based on the alternating direction method, f ðFÞ k,m and f ðBÞ m b could reach the convergence, and the associated network utility could be obtained. The computation resource allocation algorithm is summarized in Algorithm 2.
The computational complexity for one round of pricing for FNs of DRPA is OðMÞ. Thus, the total computational complexity of DRPA is OðMKÞ, where K denotes the number of IDs. Considering that the loop number of DRPA is t ≤ t max , thus, the total computational complexity of DRPA is OðnÞ = Oðn 2 log nÞ.

Simulation Results and Analysis
To verify the effectiveness of task offloading and resource allocation algorithm in the blockchain-enabled fog network (TR-BFCN), the following two algorithms are used for comparison.
Random resource allocation algorithm (RAA). This algorithm uses different resource pricing to ensure the network revenue of IDs and FNs and uses random offloading decisions and transmission resource allocation.
Unified price algorithm (UPA) [23]. This algorithm adopts a unified resource pricing, i.e., each ID purchases the same computing resource from different FNs, and adopts the optimal offloading decision and transmission resource allocation. Figures 2 and 3 show the computation resources purchased by different IDs and the associated prices with K = 3, M = 10, and N = 16. For ID 1, the task processing intensity is 200 cycles/bit; for ID 2, it is 300 cycles/bit; and for ID 3, it is 400 cycles/bit. As shown in the figure, with the number of tasks increasing, the computation resources purchased by IDs increase, and the amount of computation resources purchased by different IDs from different FNs is also different. ID 1 has a lower task processing intensity and purchases fewer computation resources with a higher price, whereas Wireless Communications and Mobile Computing ID 3 has a higher task processing intensity and purchases more computation resources with a lower price. Figures 4 and 5, respectively, show the convergence of the purchased computation resources and the associated prices. The DRPA algorithm converges when the number of iterations reaches 10. It can be seen that FN 1 has the smallest energy consumption factor κ m and the lowest cost, and FN 5 has the largest energy consumption factor κ m and the highest cost. As κ m increases, the computing resources that can be purchased decrease, and resource quotations continue to increase correspondingly. Figure 6 shows the network utility versus the number of FNs. It can be seen that the network utility is a decrease function of the number of FNs, which converges when the number of iterations is greater than 12. With the increase of the number of FNs, IDs could purchase computation resources from more available FNs; thus, the total network delay and the network utility could be reduced. Moreover, as the number of FNs increases, more FNs can be selected  9 Wireless Communications and Mobile Computing as blockchain nodes, and the network utility will be further minimized. Figure 7 shows the network utility versus the number of IDs. The task transmission delay and computation delay increase when the number of offloading tasks increases, resulting in the increase of the network utility. Compared with other algorithms, the proposed TR-BFCN algorithm has the lowest network utility. For algorithm RAA, ID may offload tasks to FNs with poor channel conditions, resulting in high transmission delay and high network utility. The simulation results show that the proposed DRPA algorithm could make each FN provide different computation resources for each ID according to the task processing intensity, which can effectively reduce the network utility. Figure 8 shows the relationship between the network utility and the task processing intensity with three algorithms. The increase in task processing intensity leads to the increase in network calculation delay; thus, the total energy consumption of the network also increases. The ID in the algorithm RAA may offload tasks to the FN with poor channel conditions, which could increase the transmission delay. In the UPA algorithm, each ID purchases the same amount of computing resources from each FN. For FNs with    rich computing resources or higher processing costs, the ID has not obtained the optimal amount of computing resources to maximize its own revenue, resulting in higher network utility. Figure 9 shows the relationship between network utility and the number of FNs with different algorithms. With the number of FNs increasing, the available computing resources also increase. Therefore, the available FNs for each ID to offload tasks are increasing, and IDs could select the FN with better processing capability for task offloading, resulting in the decrease of the total network delay and the total energy consumption, as well as the network utility.

Conclusions
In this paper, we proposed a task offloading and resource allocation scheme in the blockchain-enabled FCN to minimize the network utility by optimizing the resource pricing, task offloading decisions, and resource allocation. The FCN was responsible for the task offloading and the blockchain network was responsible for the security in the data communication process. Firstly, we proposed a task offloading model in the blockchain-enabled FCN; secondly, based on the two-stage Stackelberg model, a dynamic resource pricing algorithm was proposed to ensure the fairness of transactions. Finally, we verified the feasibility and effectiveness of the proposed algorithm by simulation results.

Data Availability
In this paper, we did not use a specific dataset.

Conflicts of Interest
The authors declare that they have no conflicts of interest.