Peer-to-Peer Energy Trading in Smart Grid Through Blockchain: A Double Auction-Based Game Theoretic Approach

In a smart grid, each residential unit with renewable energy sources can trade energy with others for profit. Buyers with insufficient energy meet their demand by buying the required energy from other houses with surplus energy. However, they will not be willing to engage in the trade if it is not beneficial. With the aim of improving participants’ profits and reducing the impacts on the grid, we study a peer-to-peer (P2P) energy trading system among prosumers using a double auction-based game theoretic approach, where the buyer adjusts the amount of energy to buy according to varying electricity price in order to maximize benefit, the auctioneer controls the game, and the seller does not participate in the game but finally achieves the maximum social welfare. The proposed method not only benefits the participants but also hides their information, such as their bids and asks, for privacy. We further study individual rationality and incentive compatibility properties in the proposed method’s auction process at the game’s unique Stackelberg equilibrium. For practical applicability, we implement our proposed energy trading system using blockchain technology to show the feasibility of real-time P2P trading. Finally, simulation results under different scenarios demonstrate the effectiveness of the proposed method.


I. INTRODUCTION
Electricity trading has changed in recent years with the advent of smart grid technologies for the following reasons. First, the increasing availability of distributed generators with intelligent infrastructure enables the residential consumer to harness energy and inject it into the distribution system. This advancement introduces new grid participants and provides more decentralization to the smart grid. Second, the larger number of participants and wide distribution area have led to more efficient management of energy trading systems, which have generally become interested in the roles of a central entity, i.e., auctioneer [1]. The auctioneer is generally responsible for energy trading systems, where their role is The associate editor coordinating the review of this manuscript and approving it for publication was Lei Wang. to manage all participants and support optimized electricity purchasing and selling.
In practice, each user always tries to increase their profit. The demand response (DR) program can reduce cost or maximize benefits by optimizing energy consumption according to variations in price. Use of a renewable energy, i.e., photovoltaic (PV), system is another way to improve the user's benefits. The user can reduce demand on the grid by using renewable energy and sell excess energy to other residential units (RUs) or the grid. Therefore, on a small scale, energy trading in the community is gradually growing, with prosumers as residential units and PV renewable resources increasingly being utilized. Each participant can trade electricity in peer-to-peer (P2P) manner [2].
However, prosumers may not be willing to participate as energy consumers in a P2P electricity trading market due to concerns about sharing private info, i.e., satisfaction in their energy demand, to the auctioneer [3], [4]. Thus, the demand for electricity and the supply of renewable energy can be unbalanced among RUs because participants' energy is traded with the grid instead of other prosumers due to concerns about privacy. Moreover, to facilitate efficient exchange of energy among all participants, the energy trading system must meet requirements for reliability (to prevent data tampering and data leakage), security (to protect participants' identities), and scalability (to ensure that an increasing number of participants can join). Thus, it is essential to design a secure decentralized energy trading system in such a way that users' behavior can be protected and the system remains manageable with a large number of prosumers.
To this end, we propose an energy trading system with multiple prosumers based on game theory and blockchain techniques without revealing each user's private information such as bids, asks, amount of energy, and user behavior. The main contributions are summarized as follows: 1) We propose a real-time P2P energy trading system that simultaneously considers DR capability and PV system usage to maximize social welfare using a double auction-based game theoretic approach. 2) We formulate the energy trading as a Stackelberg non-cooperative game between prosumers and the auctioneer. In interactions, the prosumers as buyers determine their energy requirements by solving an optimization problem to maximize social welfare. At the same time, the auctioneer achieves the maximum average social welfare per seller. 3) We prove the existence of a unique Stackelberg Equilibrium (SE) which is the optimal solution for the formulated optimization problem. Besides, we prove the individual rationality and incentive compatibility of our proposed method. Simulation results show that participants get more benefit than in two existing methods. 4) We implement our proposed algorithm using blockchain technology, which shows the feasibility of this P2P energy trading system as well as the enhanced trading security and privacy protection. We further measure throughput, latency, and running time to show the effectiveness of this blockchain-based energy trading system.
The remainder of this paper is organized as follows. In Section II, we provide a literature review on P2P energy trading and blockchain technology. In Section III, the problem is defined and mathematical models representing the participants are constructed. Section IV provides our proposed energy trading algorithm for residential users based on double auction and game theory. Section V provides the blockchain implementation. Section VI discusses the simulation results, and Section VII concludes the paper.

II. LITERATURE REVIEW
P2P energy trading has been investigated extensively [5]- [14]. In [5], the authors studied P2P energy sharing by modeling the amount of shared energy and internal energy trading prices using the Lyapunov optimization method and Stackelberg game approach. In [6], a P2P energy trading model for smart houses was proposed. The objective of the proposed approach is to optimize microgrid energy cost; thus, prices are determined so as to minimize total energy cost. The authors in [7] introduced different P2P energy sharing markets for residential customers using bill sharing (BS) and mid-market rate (MMR). A canonical coalition game, in which prosumers cooperate to trade energy and the MMR is utilized as a pricing mechanism to ensure stability, is introduced in [8] and compared to the Feed-In-Tariff scheme by participating in P2P energy trading. In [9], the researchers proposed a P2P trading mechanism for sharing energy storage ownership between multiple shared facility controllers (SFCs) and a residential community. In [10], the authors proposed P2P energy trading considering both optimality and fairness among prosumers in a cooperative market. The authors in [11] proposed a novel mechanism that performs purchase and sale of electricity automatically with increased control to householders in the P2P electricity market. In [12], a biding strategy using a Bayesian game theory that ensures efficient and fair bidding of each buyer in P2P electricity trading was introduced. The authors in [13] investigated the feasibility of P2P energy trading in a grid-tied power networks and proposed a practical P2P trading scheme considering network constraints. In [14], the authors proposed a P2P energy exchange scheme for residential prosumers with low-voltage electricity network constraints. Although all of the abovementioned papers are notable, they do not consider DR.
In the smart grid community, a consumer can continually monitor electricity prices through an energy management system (EMS). Consumers respond to dynamic pricing to maximize their respective benefits and/or minimize their electricity bills. For this reason, researchers have focused on demand response side (DRS) programs [15]- [20]. In [15], the authors proposed an incentive-based DR program which considers a reasonable amount of load change as the incentive. The authors in [16] present a stochastic bottom-up model designed to predict changes in household electricity use. However, the authors in [15] and [16] do not take user behavior into account. In [17], the authors proposed a great approach to protecting confidential user information that uses an iterative double auction in the microgrid. They also maximize the social welfare, as buyers aim to optimize their energy usage utility and payment amount whereas sellers maximize their energy consumption and reimbursement. The authors in [18], [19] used a Stackelberg game to maximize the benefit to participants of P2P energy trading considering DR without revealing satisfaction of energy demand in users' houses. The proposed method maximizes the benefits to all participants. The competition among utility companies is modeled through the Stackelberg game and the interactions between end-users are modeled as an evolutionary game. The authors in [20] proposed a price-based DR method with energy sharing between prosumers with their own PV systems in the microgrid.
An iterative algorithm is used to identify the internal price and minimize the costs to all prosumers. Nevertheless, these implementations [17]- [20] do not consider secure platform data exchange and scalability of the energy trading system as they make it easy to explore personal data and show low system reliability. To address these limitations, we propose a P2P trading mechanism for a distributed network that makes use of blockchain technology.
A blockchain is essentially a distributed open ledger in which transactions are recorded openly in a decentralized manner [21]. Each transaction is ordered and stored in a block with a timestamp. The first blockchain software was implemented with Bitcoin protocol [22]. The user generates transactions and holds them in the public ledger, then the validating nodes (miners) compete to mine a group of the transactions into blocks and earn a reward. This approach has many limitations, including its significant energy consumption, the scalability of the number of transactions per second (tps), privacy concerns regarding use of a public ledger, and singlepurpose application. All of these factors are very important when attempting to apply blockchain to a P2P energy trading system since the P2P energy trading system must support a large number of prosumers, ensure user privacy, and provide various functions. To this end, a private and consortium blockchain (permissioned blockchain) such as Hyperledger Fabric [23] is preferred in the field of P2P energy trading over public blockchains such as Bitcoin [22] and Ethereum [24].
Recently, decentralized security techniques such as blockchain have surfaced as technically competitive options for smart grid security [25], [26]. Prosumers learn the energy usage pattern of different sellers and buyers [25], manage their own energy consumption through residential demand response schemes [26], and then trade with one another, whenever applicable, within the local community to maintain a good balance between supply and demand. Further discussion of the use of blockchain-based platforms for decentralized energy supply management via P2P trading can be found in [27]- [29]. A blockchain-based electricity marketplace with a demurrage mechanism was introduced in [27] to describe a mixed complementarity problem using shift load. A generator produces an energy token by load shifting and a consumer buys this energy token to reduce their electricity bill. Another paper [28] introduced a demand side management scheme using a non-cooperative game theory approach. The proposed method was implemented in blockchain with a developed user interface. The authors of [29] proposed P2P energy exchange in the microgrid, in which energy transactions are managed using blockchain. For secured energy trading in microgrids, Elecbay is proposed in [30]. In [31] the authors utilized the IBM Hyperledger Fabric architecture to create an operational model for crowdsourced energy systems in distribution networks considering various types of energy trading transactions and crowdsources.
To demonstrate the feasibility of our proposed method, we implement a real-time P2P energy trading system using blockchain technology; we also conduct a study of the system's core components and benchmark performance to evaluate its throughput, latency, and scalability.

III. SYSTEM MODEL A. STRUCTURE OF THE SYSTEM
Let us consider an energy market system that consists of a large number of RUs. Each RU can be an individual house that has installed renewable sources such as PV systems. The entire community can generally be divided as follows: 1) Prosumers: The end-users who play as a buyer or a seller in P2P electricity trading. Each prosumer chooses their role according to their current energy consumption and generation at time slot t. 2) System Operator: The system operator who plays the role of the auctioneer in the market. This entity provides the entire energy trading service. The operator can log in via browser to manage the overall system. Prosumers can connect and sign up to the energy market network via browser after installing code from the operator. 3) Smart controller: The component that allows for exchange of information in the market. Each prosumer has its own smart controller with which to exchange information and value with others in the P2P energy trading market.
A simplified diagram of the P2P energy trading system is presented in Fig. 1. We define H as the set of prosumers inside the energy market. B and S are the set of buyers and set of sellers, respectively. We denote that there are N = |H | number of prosumers, N B = |B| number of buyers, and N S = |S| number of sellers; thus, the total number of participants in the market is N = N B + N S . Each prosumer n ∈ N can generate energy E PV n,t and consume energy x n,t at time slot t. If E PV n,t − x n,t > 0, the prosumer would become a seller to sell their energy at time slot t. Otherwise, E PV n,t − x n,t < 0, the prosumer would opt to purchase energy in order to meet their demand at time slot t. We consider a discrete-time system that is divided into different slots of equal interval t = 5 − 15 minutes. Let t = {1, 2, . . . , T } denote the set of operation time slots, while t ∈ T and T |T | gives the total number of operation time slots.

B. WELFARE MODEL OF PROSUMER
The buyer with DR can adjust its load demand according to the price or the incentive for benefit. Therefore, we assume that the buyer's profit is a utility function, in which its value is nondecreasing and concave. There exist two common types of utility functions used to model the prosumer's energy consumption behavior. They are the logarithmic utility function [32], [33] and quadratic utility function [9], [19].
In this paper, we consider a quadratic utility function to optimize the power consumption and revenue of prosumer i ∈ N at time t ∈ T .
where a i,t is a bid made by prosumer i at time slot t. θ i,t represents the level of satisfaction that prosumer i consumes a certain amount of energy at time slot t. For a higher value of θ i,t , the prosumer is willing to reduce its energy consumption, i.e., reschedule the use of flexible devices for later. Otherwise, the prosumer tries to consume more energy. This is a predetermined constant which characterizes the index of sensitivity via utility change when there is a change in x i,t .
The term x i,t represents energy demand of prosumer i and x max i,t is the maximum load for prosumer i at time t.
If the prosumer is a buyer, the welfare function of buyer i ∈ B can be defined as follows: where p t is a price determined by the auctioneer at time slot t. The prosumer solves the local programming problem (2) to choose the optimal strategy that maximizes its welfare function, as: From (3), the amount of x i,t at which buyer i achieves its maximum welfare in response to a i,t and p t can be obtained: Given (4) and the condition 0 ≤ x i,t ≤ x max i,t , we can easily calculate x * i.t as follows: To understand the objective function of (3), we outline the welfare value of a prosumer with different values of θ i,t and p t while keeping all other parameters the same (Fig. 2). At the beginning, the overall welfare continues to increase as x i,t increases. It begins to decrease after passing the peak if x i,t increases further, as shown in Fig. 2. In particular, for a lower satisfaction level θ i,t and price p t , the prosumer is willing to buy more energy from the market; otherwise, the prosumer wants to consume less energy due to increased satisfaction level and price p t .
In contrast with the buyer, the seller needs to sell its surplus energy to buyers or the grid. In typical cases, the grid buys energy at a lower price than that at which it sells energy. Hence, it is reasonable to assume that the seller would mainly be interested in selling its energy to other prosumers in the P2P market at p t . The welfare function of the seller j ∈ S can be defined as follows: where v j,t is an ask of seller j at time t and x j,t is the amount of excess electricity that the seller can sell in the market.

C. AUCTIONEER MODEL
The auctioneer coordinates and manages the auction process. The total amount of traded energy between the sellers and buyers can be described as: and where E b is total amount of buying energy K − 1 consumers and E s is total amount of selling energy J − 1 sellers. The meaning of K − 1 and J − 1 are addressed in Section IV-B.
In order to maximize the average social welfare per seller (ASWS), the objective function of the auctioneer is formulated as: where price p t is a decision variable determined by the auctioneer, and x * i is the optimal amount of energy that the buyer is willing to buy at price p t .

IV. DOUBLE AUCTION-BASED GAME THEORETIC MECHANISM FOR P2P ENERGY TRADING
P2P trading aims to maximize the welfare of both buyers and sellers and reduce dependence on the grid. To this end, we propose a double auction-based game theoretic mechanism for P2P energy trading, which can be broken down into the following steps: 1) Prosumers determine their role based on their energy generation and demand. 2) Then, prosumers register in the P2P market by submitting their bid/ask and amount of energy to the auctioneer. We note that the submitted value is always positive in terms of quantity. 3) At time slot t, the auctioneer starts to execute matching and determine the participants according to the winner determination rule. 4) Once the auction process is finished, each participant as a buyer observes the price p t from the auctioneer and solves optimization problem (3) to obtain the optimal quantity that they are willing to trade. 5) The corresponding optimal quantity received from buyer is used to compute ASWS in (9) and update p t . Steps 4 and 5 is repeated until reaching the stop condition. 6) Finally, the auctioneer determines the clearing price and the amount of energy for all participants. It is important to note that neither the buyer nor the seller has access to others' private information, such as the amount of energy to be sold by an RU or any buyer's energy demand even though the rules of the auction are known to all participants.

A. DOUBLE AUCTION
The traditional Vickrey auction scheme [34] is a sealedbid auction. Bidders submit their reservation prices to the auctioneer without knowing other participants' reservation prices. The bidder with the highest reservation price wins the auction but pays at the second-highest bid price. In this paper, we use a modified Vickrey auction [9] to find the solution for the system model in Section III. The proposed auction model consists of three elements: 1) Buyers: The RUs in set B, which have deficit demand and engage in the market to buy electricity and expect their profit in the market to be optimized. 2) Sellers: The RUs in set S, which own surplus energy at a particular time of day and expect to earn some benefit in the market instead of selling the energy to the main grid. 3) Auctioneer: The coordinator, which uses determination rules to reflect the number of buyers and sellers who engage in trade. It also takes a sender role, in that it sends buyers the price during the auction process according to the minimum and maximum price. Finally, it determines the precise quantity and price based on pre-defined rules. The proposed auction policies consist of a winnerdetermination rule, a payment rule, and an energy allocation rule.

B. WINNER DETERMINATION
Once the auction process is initiated, the auctioneer determines the number of buyers and sellers who can participate in trade based on a double auction. Winner determination is executed in the following manner. The buyers are sorted in decreasing order of their reservation bid price, i.e., a 1 > a 2 > · · · > a B (10) In contrast, the sellers are sorted in increasing order of their reservation ask price, without loss of generality, as, Once the auctioneer orders the sellers and buyers, it generates the aggregate supply and demand curves using (10) and (11), as shown in Fig. 3. The auctioneer then determines the number of participating buyers K and sellers J that satisfy a K ≥ v J from the intersection point of the two curves, which can be found using any standard numerical method [35]. We note that the number of buyers K and sellers J are determined at the intersection point, but a fundamental rule of any auction mechanism is that no participant can cheat once payment rules and allocations are established. Therefore, in this paper, we assume that K − 1 buyers and J − 1 sellers eventually engage in the market. The winner determination procedure is intuitively illustrated in Fig. 3. The blue curve describes the buyers' bids in decreasing order, and the orange curve depicts the sellers' offers in increasing order. The two curves meet at the intersection point, and a buyer in the blue area and a seller in the orange area engage in a trade.

C. PAYMENT RULE
While the winner determination procedure is used to identify the intersection point and the participants who engage in the trade, we use a combination of Vickrey auction and game theory to reflect the price p t according to the competition among buyers. The auctioneer decides on the price that the buyer needs to pay to the seller, which also allows the buyer to choose the amount of energy they are willing to buy from the seller. We note that the price range in the auction market is determined according to the Vickrey auction mechanism [34] and the second-highest reservation price. Therefore, the maximum price p max t will be the highest reservation price at the intersection of demand and supply for the participating K − 1 RUs. The minimum price p min t is the second-highest reservation price.
The interaction behaviors between buyers and sellers that are used to determine the clearance price can be modeled as a Stackelberg game with multilevel decision-making. In the Stackelberg game, multiple decision-makers (followers) respond to the leading players' decisions (leaders). The leader makes decisions from the perspective of his purpose, while followers make decisions based on the leader's decision. In this paper, the auctioneer is the leader that sets the price for energy trading, which observes the best response for the seller. The buyers are the followers who are trying to optimize individual benefits. We can formulate the game as which consists of the following components: 1) K represents the prosumers in set K − 1, which are the followers that optimize their choice in demand corresponding to the price set by the auctioneer (Au), who plays as the game leader. 2) X i is the strategy set, or amount of energy to buy, of prosumer i ∈ K − 1, which is constrained by To achieve this goal, the game considers the Stackelberg equilibrium (SE) as one possible solution. The leader obtains the optimal price with the best response from the followers, and the followers determine their optimal levels of energy consumption. At the SE, neither the leader nor any follower can benefit by changing their strategies. In more detail, we use a Stackelberg game between buyers and the auctioneer. The auctioneer sets the auction price p to maximize the average social welfare for sellers and to meet their need to sell energy. The competition among buyers is considered a noncooperative game, in which the buyers decide on the vector containing the amounts of energy x = [x 1 , x 2 , . . . , x K −1 ] that they would like to trade into the market such their benefits are maximized. The details of the auction process are shown in Fig. 4 and Algorithm 1. Solve optimization problem (3) 8: End for 9: The auctioneer computes the average social welfare ASWS using (9) 10: If ASWS ≥ ASWS * then //The auctioneer updates the optimal price and maximum average social welfare 11: p * = p and ASWS * = ASWS 12: End if //Update price 13: The SE x * t , p * t is achieved Definition 1: The Stackelberg game G defined in (12) which has a set of strategies x * , x * −i , p establishes the SE of this game if and only if it meets the following set of inequalities: and Therefore, when the game reaches the SE and the auctioneer chooses the optimal price p * t , the participants cannot increase their profit by choosing a different price other than p * t . Similarly, no prosumer can improve their social welfare by choosing other strategies different from x * i.t . Existence and Uniqueness of SE: In this section, we propose a theorem and proof to validate the game formulated in Section IV-C regarding whether an SE satisfies (13) and (14).
Theorem 1: There always exists a unique SE that satisfies (13) and (14) for one leader and multiple followers. The Stackelberg game G between the auctioneer and prosumer RUs is in the set {1, 2, . . . , K − 1}.
Proof: First, as previously mentioned, the strategy of the auctioneer is a non-empty set within the range p min t , p max t . Hence, the auctioneer always finds the optimal price that provides the maximum average social welfare for any given x chosen by different prosumers. The optimization problem can be reformulated as where p * m,t is the optimal price in the market at time slot t. p s g,t and p b g,t are the buying and selling price from the grid at time slot t, respectively. If any participating bid or ask is beyond the range, the corresponding RU will buy or sell directly from the main grid. p b m,t and p s m,t are the maximum and minimum price at which the buyer/seller can buy/sell in the market at time slot t.
Second, for any price p t , the welfare function W i in (13) is strictly concave concerning x i , ∀i ∈ {1, 2, . . . , K − 1}, i.e., the second derivative ∂ 2 W i ∂x 2 i < 0. Hence, for any price p t ∈ p min , p max , each prosumer will have a unique x i,t , which will be chosen from a bounded range and maximize W i,t . We can reformulate it as Therefore, from (15) and (16), it is clear that our proposed game will have a unique p * t and x * i,t such that the ASWS and welfare values are maximized, with which game G will consequently reach its unique SE; thus, Theorem 1 is proved.
Using mathematical formulas, we can find the unique price p * t by substituting the value of x i in (4) into (9). The auction clearance price p * t that maximizes the average social welfare of sellers can be written as

D. ENERGY ALLOCATION
Once the auction price p * t is determined, the auctioneer allocates the quantity Q to be traded by each buyer i and seller j according to the following rule: where α is a prosumer who engages in the trade and η α is the burden value of prosumer α for their contribution at time slot t. The energy balance is established under the proportional allocation rule. To clear electricity in the market, we consider the following three cases: Case 1: Equilibrium, in which the total demand of all participating buyers i ∈ {1, 2, . . . , K − 1} and total supply of all participating sellers j ∈ {1, 2, . . . , J − 1} are equivalent. Participants can trade the amount of energy that they bid or ask.
Case 2: Over-supply, in which the total supply exceeds the total demand. Every buyer i can buy all of the energy that they bid in the market, but each seller j will receive an amount of energy proportional to the burden of the oversupply, as follows: Case 3: Over-demand, in which the total demand exceeds the total supply. Every seller j can sell the amount of energy that they ask in the market, but each buyer i will receive an amount of energy proportional to the burden of the overdemand, calculated as, We note that the maximum amount of power that will be traded is min E b , E s . After the amount of energy Q α,t to trade is decided, the participants trade with the main grid if needed to meet the supply-demand of individual houses. In other words, a buyer can buy energy from the main grid at price p b g,t to meet the load demand. Otherwise, the sellers sell surplus energy to the main grid at price p s g,t .

E. PROPERTIES OF THE AUCTION PROCESS
The auction process can be considered a game in which there is always the possibility that participants will cheat (state a different amount of energy) when the auction process is executed. This section explains that the participants are not motivated to cheat their amount strategies during the auction.
In this context, we study two properties, individual rationality and incentive-compatibility, in the framework of our proposed scheme. Theorem 2: The proposed scheme delivers individual rationality for all participants in the market. The K − 1 rational buyers and J − 1 rational sellers actively participate in the mechanism to gain higher welfare.
Proof: According to the energy allocation, the buyer that wins the game can buy Q i,t ≥ 0 amount of energy.
Besides, according to the payment rule, the price paid is p * t , which is less than the winning buyer's bid, a i,t . Thus, each buyer is incentivized to take part in the game, i.e., W i ≥ 0. The seller, similarly, who wins the game can sell quantity Q j,t ≥ 0 where v j,t ≤ p * t , and is thus rewarded when the trade is successful in the market instead of selling to the main grid, i.e., W j ≥ 0. Therefore, during time slot t, both participants are incentivized to participate in energy trading, and our scheme achieves individual rationality. We note that prosumers who do not win the game at time slot t, Q α,t = 0, get no benefit at all.
Theorem 3: The auction process is incentive-compatible, which means that all participants are able to achieve the maximum SW if and only if any buyer i ∈ {1, 2, . . . , K − 1} and any seller j ∈ {1, 2, . . . , J − 1} report true valuation.
Proof: For the buyers in the auction process, according to (15), it is clear that all buyers can choose the best strategies x * = x * 1 , x * 2 , . . . , x * K −1 for any price p * t received from the auctioneer, as proven in Theorem 1, which confirms the stability of their selections in their action; thus, buyers do not have any motivation to report untruthful values. In other words, the buyer may cheat their amount by reporting untruthful values to achieve more benefit during trade, but their benefit will be decreased if they choose another strategy in Fig. 2, thus, the buyer will submit their truthful value. On the other hand, once the auction process is begun, the seller cannot modify their amount of energy to sell; thus, the seller cannot cheat in game G. Besides, when game G reaches the SE, the auctioneer conducts energy allocation according to the allocation rule as motioned in (18) i.e., x * j ∀j ∈ [1, 2, . . . , J − 1] and the auctioneer obtains zero benefit in trade, thus it does not want to change their strategies. According to the formula described in (15) and (16), it is clear that both sides achieve the maximum social welfare at SE. Therefore, none of buyers, sellers or the auctioneer is motivated to report an untruthful value for allocation if (18) is adopted for energy trading. Therefore, Theorem 3 is proven.

V. BLOCKCHAIN FOR ENERGY TRADING
In this section, we implement a real-time P2P energy trading system using blockchain technology, which performs the algorithm for optimizing DR and energy matching explained in Section IV. We describe the overall architecture of the blockchain-based energy trading system and provide a detailed explanation of the information exchange between participants and the energy trading system during the auction process.

A. REQUIREMENTS FOR BLOCKCHAIN-BASED ENERGY TRADING SYSTEMS
For P2P energy trading, the blockchain platform must meet certain requirements. It must be trustworthy (for data security), capable of fast computations for real-time energy trading algorithms, show good performance for the consensus algorithms, and ensure the privacy of user information. Furthermore, the identities of all participants must be verified prior to their participation in energy trading through blockchain. Based on this analysis, the Hyperledger Fabric was selected as it meets the requirements of energy trading systems and displays the necessary blockchain features. Our proposed blockchain-based P2P energy trading system runs fast enough to pair prosumers in real time, protects users' private information, and identifies the user to allow participation. Finally, smart contracts are implemented as the chaincode in the blockchain, which comes with smaller execution costs and allows for easier implementation compared with other public blockchains. The hierarchical architecture of our proposed blockchain-based energy trading system is shown in Fig. 5.

B. BLOCKCHAIN IMPLEMENTATION USING HYPERLEDGER FABRIC
Blockchain is a technical infrastructure that provides an immutable ledger and smart contract (chaincode) services to the application. In a blockchain, transactions are generated as needed to execute a smart contract. The peer node in the network then executes the steps in the contract in sequential order, and the result is recorded on its copy of the ledger. In a public blockchain, peers usually perform order-execute transaction. However, peers in Hyperledger Fabric perform transactions in a three-step process of execute-order-validate to increase speed. First, client transactions are executed in parallel in a sandbox to determine read-write sets without considering an order. Transactions are then ordered by an ordering service. Finally, each peer validates and commits the transactions to the blockchain in sequence.
We integrate Algorithm 1 in the smart contracts of Hyperledger Fabric with the optimization model given in Section III. The presented energy trading implementation 1 consists of three areas: end-users, decentralized applications (DAPPs), and blockchain. End-users execute the User.py source code written by Python to play the role of prosumers. If a user takes the buyer's role, it solves the optimization problem in Section III. Second, DAPPs were built on top of Hyperledger Fabric SDK, which supports communication between the end-users and smart contracts. Third, the blockchain implemented as smart contracts takes the coordinator's role in energy matching.
The operation of our proposed blockchain-based P2P energy trading system is illustrated in Fig. 6. At the pre-set time intervals, a trigger is activated and the following auction procedure is executed: first, the system operator issues a new transaction to start the matching process by calling DAPP (startGame.js.), which then broadcasts the transaction invocation request using Hyperledger Fabric SDK to the endorser peers. The endorser peers check the certificate and validate the transaction. Then, the chaincode is executed to obtain and decode all user information from the ledger database. The algorithm finds the interaction point according to the winner determination in Section IV-A. According to the payment rules, the price range and the temporary price are determined and then necessary key information (i.e., the price range, temporary price p (TP), optimal price (OP), winners, and maximum average social welfare per seller (MASWS)) is stored in the ledger to allow for tracking of the auction process. This key information is updated each time the smart contract is executed. In the next step, the endorser peers return transaction approval or rejection as part of the endorsement response to the DAPP. After the delivery of transaction approval to the committing peers, all nodes on the network will be synced with the same ledger. Finally, the process is finished by DAPP relaying the price to the users. Each user then computes their actual quantity depending on the given price. Steps 1 to 4 are then repeated in the auction process until the price reaches the minimum price. The smart contract in the blockchain gets data from the ledger, calculates the average social welfare (ASW), and updates MASWS if necessary according to the quantity received from buyers. Each transaction record is included in the blockchain stored at each peer's repository and protected by the blockchain mechanism.
As can be seen in this section, our implementation supports thousands of participating users in real-time and is easy to use and reliable.

VI. SIMULATION AND RESULTS
This section presents the results of numerical analyses and assesses the performance of the proposed approach. For ease of illustration, we conducted the simulation using ten prosumers. It is important to note that we chose to consider only ten prosumers in this simulation in order to clarify the analysis of the simulation results. Each house's energy load is selected randomly from 0.1 to 5 kWh for each time slot, and the output of PV power is calculated according to the size and efficiency of the PV system [36], in which η is 0.95 and S from 1 to 5. Prosumers' bids and asks are assumed to be float numbers chosen randomly in the range of 5 to 22, while the step size p is 0.1. For the weight factor θ i,t of satisfaction level, we assume that all buyers take the same value of 3 at all times. Finally, the simulation time is from 7 to 19 h. It is important to note that all chosen parameter values are particular to this study and can vary according to energy demand, the number of RUs, and trading policy.
The simulation was conducted in Ubuntu 16.04.04 LTS with an Intel(R) Core (TM) i7-6700 CPU @ 3.4GHz (4 core) and 20.0 GB of RAM. We implemented the blockchain using Hyperledger Fabric v1.4.7, in which there are three peers, one endorser peer and two committing peers, in one organization, and one-ordering service. We evaluate the blockchain under different transaction configurations in terms of batch, batch timeout and the number of participants to understand the impact of different configurations on the execution time of Algorithm 1.

A. PERFORMANCE OF THE PROPOSED APPROACH
We first show the convergence of the Stackelberg game in Algorithm 1 at time slot 16 in Figs. 7 and 8, where we assume that the market has five buyers and five sellers. From Fig. 8, we first note that the proposed Stackelberg game reaches the SE after 9 interactions when average social welfare per seller reaches a maximum. In addition, the amount of energy each buyer wishes to consume from the market is also observed in the game. As can be seen in Fig. 7, prosumers 1, 2 and 3 reach the SE much faster than prosumers 4 and 5. The interaction between the auctioneer and the prosumers is thought of as a game. The auction price p is updated in each iteration. Once the auction price for any buyer becomes smaller than its reservation bid, the buyer would purchase its energy from the market (supplied by the sellers). The energy consumption is adjusted in the range between the maximum and the minimum amount of energy that the buyer needs at time slot t. As a result, as shown in Fig. 7, prosumers 1, 2, and 3 with lower bids would receive all of their energy from the market much sooner, i.e., at the 1 st iteration, than prosumers 4 and 5 with higher reservation bids. After the 20 th and 24 th iterations, prosumers 4 and 5 reach the SE once the action price is high enough for them to consume more energy. In Fig. 8, the average social welfare is shown according to the price and the amount of energy consumed by the buyers. The welfare utility of all participants rises due to a rise in price p. However, when the curve starts to decrease after passing the top vertex, the buyers do not buy more energy because the social welfare per seller reaches the maximum value.
In Figs. 9 and 10, we compare our proposed scheme with DAAPM [37] and the average price mechanism in terms of total social welfare and the number of energy trades. We note that the average price mechanism is established with clearance price calculated as, b K −1 +s J −1 2 , where b K −1 is the bid of buyer K − 1 and s J −1 is the ask of seller J − 1 at the intersection point. The winner determination and allocation rules were similar to those used in our proposed scheme.
Because PV energy is self-consumed by each prosumer in time slots 10, 18, and 19, no more energy can be traded inside the market. In other words, sellers take no profit in time slots 10, 18, and 19, so the prosumer's social welfare in these time slots remains zero. On the other hand, in time slots 7 -9 and 11 -17, as shown in Figs. 9 and 10, surplus energy is traded, so social welfare and the quantity of traded energy is optimized by the proposed scheme.     9 illustrates all participants' social welfare in the intraday market where our proposed method achieved higher value than two other schemes: 114.3 cents in our proposed scheme, 97.7 cents in the average method, and negative 66.4 cents in DAAPM. This is because our proposed method allows for flexibility such that buyers can benefit by adjusting their energy consumption. The auctioneer acts on behalf of the sellers, so the scheme produces the maximum value of social welfare per seller. Therefore, the scheme always achieves a positive value of social welfare. On the other hand, the two other schemes frequently obtain negative values of social welfare. In time slots 8,11,12,13,15, and 17, the DAAPM scheme produced a negative value because of the impact of the buyer's welfare function. For a similar reason, the average method achieved a negative value at time slots 8 and 11. Fig. 10 shows the total energy traded in the market. The two other schemes achieved larger energy consumption than our method. The reason, as discussed in Section III, is that the goal of our proposed method is to maximize the social welfare of prosumers according to the given price. Therefore, each user is only willing to buy more energy if its welfare function is satisfied. In general, for higher values of θ i,t , users are not willing to consume a large amount of energy. For lower values of θ i,t , users are willing to buy more energy to meet their load demand at time slot t.

B. BLOCKCHAIN PERFORMANCE FOR ENERGY TRADING
We conducted a performance test for our proposed blockchain-based energy trading system using Hyperledger Caliper [38] for the two cases shown in Table 1 according to varying parameters. The performance of the blockchain platform is analyzed in terms of three performance metrics: throughput, latency, and scalability.
In case 1, we study the impact of varying transaction send rates on the throughput using 100, 200, 300, 400, 500, 600, and 700 transactions per second (tps).
In case 2, we study the impact of the number of transactions in a block on the throughput, where the send rates are 150, 200, 250, 300, 350, 400, 450, 500, and 550 tps and the number of transactions are 10, 20, 30, 40, 50, and 60.    Fig. 11 shows the throughput and average latency of invoke transactions under case 1. The data indicates that there is a linear increase in throughput with an increase in the send rate, with a low average latency in the beginning until the send rate reaches about 500 tps. The throughput increases slowly, and average latency grows quickly before it reaches the saturation point at about 509 tps. After the saturation point, higher send rates may cause a reduction in throughput and higher average latency due to a bottleneck in the ordering service. Fig. 12, for case 2, shows the throughput curve over different message counts, which are the number of transactions in a block. The data indicates that the throughput rises rapidly as the message count in a single block increases. When the message count reaches 50, the system throughput seems to reach a saturation point at about 500 tps. Therefore, we conclude that a larger message count increases the throughput and no bottleneck appears before the message count reaches the critical point.
One of most important goals of the proposed method is to execute energy matching in real-time. Therefore, as shown  in Fig. 13, we study how batch timeout (block timeout) and the number of participants affect the run time with the various parameters defined in Table 2. As shown in Fig. 13, as the number of participants increases, run time increases linearly, and the DAPP spends a great deal of time communicating with end-users. In general, the batch timeout is the amount of time that the system will wait before creating a block. Therefore, when increasing the batch timeout, the run time increases because the DAPP must wait to confirm the transaction even though the matching process took only milliseconds to be executed. In our system, the time required for the matching procedure was 54 -70 ms with 2000 participants. Since our algorithm has multiple interactions during the entire matching procedure, the time complexity of run time can be computed in polynomial form as x × y × z, where x is the number of transactions during the auction process, y is the waiting time necessary to validate and commit the block, and z is the waiting time necessary to receive a response on each transaction. Thus, when the batch timeout decreases, the running time decreases. A lower value of batch timeout means that blocks are created more quickly due to a decrease in batch timeout; in this instance, the presence of too many blocks negatively impacts the commit procedure. It is clear that we achieve the minimum run time at a small value of batch time, i.e., the system took 102.17 seconds to match 1000 buyers and 1000 sellers and 130.83 seconds with a batch timeout of 2 seconds with the same number of participants. Therefore, we conclude that our proposed scheme is suitable for real-time execution. Note that run time will increase when considering the time to communicate with end-users in the real world.

VII. CONCLUSION
We have presented a real-time P2P energy trading system via a game-theoretic model, in which RUs can buy or sell energy in a manner that is profitable to both sides rather than trading energy with the main grid. This structure is flexible enough to accommodate prosumers with different roles in different time slots throughout the day. Each individual prosumer's smart controller automates the trading negotiation process among prosumers. Using the double auction based approach, we have designed a whole system which consists of the winner determination, payment, and energy allocation rules of the auction. Especially, the payment rule of this scheme is aided by a one-leader multiple-follower Stackelberg game between RUs and the auctioneer. We have proposed a game, guaranteed to reach SE, that allows the auctioneer and prosumers to decide on the auction price and amount of energy that is put into the market to balance supply and demand. We further studied the properties of the auction scheme and the game, and showed that the proposed auction scheme possesses individual rationality and incentive-compatibility due to the unique Stackelberg equilibrium. Finally, an implementation of blockchain through IBM Hyperledger Fabric is discussed with optimization models. This implementation allows the system operator to manage network users to trade energy seamlessly. In this case study, we showed that the results of the system are beneficial under a variety of scenarios with different simulation parameters. Finally, the combination of the blockchain and the Stackelberg game showed that our proposed method can be executed in real-time with multiple interactions and one thousand users.
In our future work, we plan to improve the performance of the blockchain and apply our proposed scheme into the real market.