A novel decentralized platform for peer-to-peer energy trading market with blockchain technology

Peer-to-Peer (P2P) energy trading, which allows energy consumers/producers to directly trade with each other, is one of the new paradigms driven by the decarbonization, decentralization, and digitalization of the energy supply chain. Additionally, the rise of blockchain technology suggests unprecedented socio-economic benefits for energy systems, especially when coupled with P2P energy trading. Despite such future prospects in energy systems, three key challenges might hinder the full integration of P2P energy trading and blockchain. First, it is quite complicated to design a decentralized P2P market that keeps a fair balance between economic efficiency and information privacy. Secondly, with the proliferation of storage devices, new P2P market designs are needed to account for their inter-temporal dependencies. Thirdly, a practical implementation of blockchain technology for P2P trading is required, which can facilitate efficient trading in a secured and fraud-resilient way, while eliminating any intermediaries’ costs. In this paper, we develop a new decentralized P2P energy trading platform to address all the aforementioned challenges. Our platform consists of two key layers: market and blockchain. The market layer features a parallel and short-term pool-structured auction and is cleared using a novel decentralized Ant-Colony Optimization method. This market arrangement guarantees a near-optimally efficient market solution, preserves players’ privacy, and allows inter-temporal market products trading. The blockchain layer offers a high level of automation, security, and fast real-time settlements through smart contract implementation. Finally, using real-world data, we simulate the functionality of the platform regarding energy trading, market clearing, smart contract operations, and blockchain-based settlements.


Introduction
Local P2P energy trading brings multifold benefits to all involved stakeholders.On the demand side, prosumers are given the opportunity to engage in a true decentralized form of energy trading without the need of an intermediary.For example, they can act as sellers when they have energy production surplus from their Photo-Voltaic (PV) panels, as buyers to charge their electric vehicles (EVs), or both if they have stand-alone batteries.On the societal level, local P2P energy trading can promote renewable energy integration either by incentivizing prosumers to invest in local distributed energy resources (DERs) or encouraging consumers to purchase green energy certificateswhich ensure they consume only green energy-if they are incapable of investing in renewable energy sources (RES) [1].Finally, on the grid level, P2P trading can provide the necessary flexibility to system operators to manage network congestions resulting from the lack of DERs operation coordination among prosumers and the high intermittency of renewables.By optimally managing and coordinating all flexible decentralized market designs, efficient market clearing methods should be in place that also protect the prosumers' privacy while maximizing their profits.
Developing a complete and adequate P2P energy market also faces other challenges regarding data storage and security.Indeed, P2P market mechanism must be complemented with a secured and trusted distributed system that records all the transactions in an immutable, transparent and tamper-proof manner.By enabling an automated trading and billing system, energy costs and price signals can be delivered to all prosumers in an efficient way, inducing more active participation in energy trading.An excellent tool and emerging technology to foster value in the secure exchange of energy and data is blockchain technology [1,6].Blockchain technology fits most naturally in the P2P energy trading domain.It can provide an innovative trading platform with immutable registration and recording of assets, generation and consumption data, notably in complex energy systems with many subsystems interactions and intersections.Moreover, it can also help energy utilities exploit new information sources that could unleash more flexibility from the energy system, especially with the wide integration of DERs and renewables.As a result, blockchain has been drawing much attention among industries and research scholars as an integral tool in the energy sector digitalized future, and earning the label of a ''disruptive technology'' [7].
The new dynamics of energy systems' digitalization have called the attention of many researchers towards digital energy platforms.A platform is a digital blueprint that shows how an ecosystem functions, allowing users to interact with each other, and trade products or services [8].In that sense, in order to fully exploit the potential of P2P energy trading with blockchain technology, the objective of this work is to structure a trusted decentralized platform that offer realtime, efficient, and secured orchestration of all distributed flexible devices and sources within a distributed network.To the best of our knowledge, there are no existing P2P energy platform in the literature that addresses the aforementioned challenges.Therefore, in this paper, we design and develop a decentralized P2P energy trading platform that is composed of two layers: (1) Market layer: an efficient decentralized short-term parallel market, offering a diverse range of market products, and with a novel clearing method, achieving near-optimum social welfare solutions.(2) Blockchain layer: integrated with the market layer to ensure data security and privacy, fast settlements, and low transactions costs.The rest of this section elaborates on how this work differs from related literature and what the main contributions are.

Literature review
Our proposed platform consists of a market layer and a blockchain layer.Accordingly, we first discuss the related academic literature on P2P market mechanisms and clearing methods.Then, we provide a comprehensive review on the related academic research, as well as relevant active projects using blockchain for energy markets.

P2P market mechanisms and clearing methods
The discussion surrounding energy systems decentralization has drawn much attention among researchers to look into P2P energy trading.Ref. [9] provides a thorough and comprehensive review and discussion on the design of peer-to-peer markets, as well as their challenges and opportunities.Other studies focusing on different structure aspects and design classification for P2P markets according to the optimization objective and market clearing methods, can be found in [4,5].Ref. [2] offers an interesting view for the structure and design of P2P markets, as they are classified using general criteria by auction models, multi-agent models and analytical models.Ref. [10] explores the potential benefits of implementing game and auction theoretic models in P2P energy trading settings.The authors further explore the feasibility of game theoretic approaches in P2P energy trading settings in [11,12] by studying the feasibility of social cooperation between prosumers in establishing their sustainable participation in P2P trading markets.
Most P2P trading platforms developed in recent literature rely on a so-called Continuous Double Auction (CDA) design as for their market mechanism [13,14].In [15], an auction mechanism based on a combinatorial double auction framework is proposed that facilitates energy trading between micro-grids.The model uses a hybrid optimization method of particle swarm optimization (PSO) and genetic algorithms (GA) to clear the auction market.[16] proposes a P2P market framework based on CDA for low-voltage networks.Another CDA based P2P energy trading market, which runs on one-hour ahead basis, is introduced in [17].
In a CDA format, buyers and sellers place their buy and sell orders respectively, hence the term double; and the market is in a continuous state of clearing, hence the term continuous.A CDA design is an efficient market mechanism that allows prosumers to trade with each other through bilateral agreements, thus the specific traded quantities amongst them can be easily determined.However, the CDA design has certain limitations when used for energy systems.One of the most important limitations is that CDA is incapable of adequately integrating energy products with inter-temporal commitments.The complexity of inter-temporality originates from market bids that have dependencies on multiple time periods within the time horizon of the market, much like the payback (rebound) effect in demand flexibility markets [18].In P2P markets, the inter-temporal impact of market bids can be due to the unique nature of charging/discharging of batteries and/or uninterruptible loads, such as washing machines and dryers.Indeed, the continuous state of matching bids means that the market is only able to accommodate single time-step bids that are composed of a single quantity and price.The market clearing mechanism of the CDA is only dependent on the time that it is being traded.Thus, the CDA's orders matching process limits the prosumers' options to a single time slot for buying and selling energy.These limitations affect the types of flexible local devices that can participate in the market.For example, battery storage systems often need to charge by a certain amount over a certain period of time, or require maintaining a continuous charging or discharging state for consecutive periods to avoid negative effects on the battery's life and efficiency.
Other literature focus on different angles in the paradigm of P2P energy trading.For example, a non-cooperative game theory simulation approach for a P2P energy market model is proposed in [19].In [20], a multi-agent-based framework is presented to model the behavior of prosumers with respect to different P2P sharing mechanisms.Ref. [21] assesses the impact of P2P energy trading mechanisms between smart homes by proposing an energy cost optimization algorithm that manages the trading processes.In [22], a decentralized P2P market based on the multi-bilateral economic dispatch formulation is proposed.A prosumer-centric framework for a P2P trading platform is proposed in [23] which motivates prosumer to participate in the trading activities.Ref. [12] offers an interesting insight into a coalition formation game for prosumers with battery systems.There are also some studies that consider different P2P trading settings that are without bidding capabilities, in which a central authority manages (sells and buys) the energy between the local prosumers.In [24], a P2P market managed by a central agent is proposed that access to all local resources and manages the supply and demand as well as price settings.Such setting in [24] allows battery owners to participate.However, the inter-temporal impact is based a non-stationary battery storage which does not allow stationary battery storage such as EVs to participate in the market.A similar work is presented in [25] where an energy sharing structure, also without bidding capabilities, is managed by a central energy sharing agent that sells/buys energy to/from prosumers depending on their local PV generation capacity.
The decentralized design of P2P markets requires an efficient decentralized optimization algorithm that enables the market players to clear the market while protecting their identities, maximizing the social welfare, and being able to handle the complexity arising from the inter-temporal characteristics of market products.The first two requirements are usually addressed in the literature.However, little attention has been given to decentralized methods that can handle the trading of market products with inter-temporal characteristics.For example, one of the common decentralized methods used in literature is the alternating direction multiplier method (ADMM) [26].Refs.[24,27] adopt ADMM as clearing algorithms of the decentralized P2P markets.However, both market mechanisms are of simple nature with market products that consist of a single quantity and price.Other studies adopt decentralized methods for the clearing P2P energy markets but do not account for the inter-temporal behavior of different market products.For example, in [22], a decentralized P2P market based on the multibilateral economic dispatch formulation is presented, where the authors use a consensus+innovation method for decentralized market clearing.The market setup only allows market players to trade one bid per player with a single quantity and price.A distributed primal-dual gradient descent method for distributed clearing as adopted to clear the P2P marker proposed in [28].However, they only consider single bids of quantity and price in their P2P market design.Similarly, Ref. [29] does not investigate the inter-temporal market products in the adaptive segmentation method used for the P2P market clearing.Even though all previous work provided high quality contributions, more research is yet needed to add more complexity in the market bids that can be traded in P2P settings.This is what this research is aiming to explore and present.

Blockchain based energy markets
The proliferation of blockchain start-ups and projects have raised the interest of many researchers towards their application in local energy trading.As a result, multiple start-ups and businesses worldwide are racing to take a leading role in developing blockchain platforms that facilitates decentralized and local P2P energy trading [30,31].In a recent work, [32] provide an interesting study that assesses the potential of blockchain on the energy system's actor configuration.EnerChain1 is a wholesale trading platform based on blockchain with the objective of enabling the trading of smart energy products, optimizing the grid management process and P2P trading.Another blockchain platform developed for customers access to wholesale electricity markets is called Grid+. 2 This platform behaves like an energy retailer that enable prosumers to save in the energy bills using artificial intelligent (AI) agents.Energy21 and Stedin3 developed a blockchain platform that allows peer trading between local markets and with wholesale electricity markets.Other examples of decentralized P2P trading platforms are Volt Markets, 4 Pylon,5 LO3 Energy project6 [33], and Power Ledger. 7In the Netherlands, Alliander in cooperation with Spectral Energy are developing a P2P trading platform based on blockchain. 8In the literature, the blockchain implementation in energy trading has been a subject of interest as well.In [34], a transactive decentralized platform for energy trading among micro-grids is presented which uses a blockchain-based smart contract to record the transactions on the blockchain.A demandside management program based on blockchain technology is presented by [35].The proposed model uses the blockchain distributed ledger for data storage and implements smart contracts to ensure the delivery of required energy volumes for demand response events.In [36], an energy blockchain environment is implemented for electricity trading using a game-theory model approach.In [37], smart contracts in blockchain were used to enable secure and efficient trading in a distributed energy system.Refs.[38,39] introduces a P2P market based on double CDA model, and integrated with blockchain technology.

Contribution
Although the current literature has provided significant contributions in the area of P2P energy trading platforms, there are still key areas that require further investigation.Take for example Ref. [40], where a well-formulated blockchain-based energy management platform for trading between micro-grids is presented.However, the decentralized market modeled follows a bilateral-form (much like a CDA mechanism).This arrangement limits the inter-temporal dependency and clearing setup to a single time slot; hence, excluding a diverse range of inter-temporal market products.Such limitation can be accredited to the complexity of the ADMM used for market clearing.Furthermore, blockchain technology is widely involved in the market clearing process which can be challenging in realistic settings.While blockchain as settlement mechanism has unprecedented potentials in future local energy systems, there is a need to address practical challenges such as scalability and fraud management.
In general, the potential of blockchain in the area of P2P energy trading should be well comprehended and recognized from a more practical perspective.Also, there are yet some key steps that needs to be taken towards efficient future local energy trading platforms.Based on the comprehensive literature review carried out here, we are aiming in this work to fill the above-mentioned research gaps by developing a new decentralized local energy trading platform, called DeTrade, with a practical and realistic integration of blockchain technology.Our key contributions are: • Market mechanism: development of a decentralized short-term multi-stage, and multi-period parallel auction called DeMarket.
The DeMarket is a pool-structured P2P market, which is new to the literature.The market's objective is to maximize the social welfare of all prosumers while enabling the trading of market products with and without inter-temporal dependency.• Decentralized market clearing method: The DeMarket is complemented by a novel decentralized market clearing method called Decentralized Ant-Colony Optimization DACO.The developed method is able to handle the complexity of the market products modeled.Also, the DACO produces a near-optimal market solution, within a small error margin of the optimal solution, in a reasonable number of iterations, while respecting the information privacy of the prosumers.• Blockchain integration: The trading in the DeMarket is carried out in a secure and efficient manner by integrating blockchain logic in a more practical way.This adds automated, fast, and real-time settlements to the DeMarket.In particular, we leverage smart contract functionality to manage balances of digital tokens, called EuroTokens, that represent monetary value outside the blockchain.
• Simulation based on real-world data: Using a distribution network real-world data, a simulation is carried out to showcase how the decentralized clearing method will perform when compared with a central method for market clearing.In addition, the smart contract is implemented in the Solidity programming language, deployed on a permissioned blockchain using Hyperledger Burrow, and evaluated on our nation-wide research cluster with the output from the decentralized clearing method.
The paper is arranged as follows.Section 2 provides some background information about local energy markets and P2P trading.Section 3 describes the general structure of our decentralized local energy trading platform, DeTrade.Section 4 presents the market layer of the DeTrade, as well as the developed DACO method.Section 5 describes the functionality of the blockchain layer, especially the use and implementation of smart contracts.Section 6 presents a case study carried out on a real distribution feeder.Section 7 provides some discussion points along with assumptions and limitation considered in this work.Finally, Section 8 concludes the work presented in the paper.

P2P trading framework
This section provides some background information about P2P energy trading, its challenges and opportunities, and the value of blockchain within such a framework.

Market design
Local P2P energy trading is a market setup that enables prosumers in a local network to directly trade energy with each other [41].Based on the local market prices and energy availability, prosumers take the role of sellers or buyers, aiming to maximize their profits.Potential sellers include those with local generators (such as diesel generators or PV panels), while any prosumer can act as a buyer to partially or fully serve his/her demand at any point in time [23].Prosumers who own energy storage systems (for example batteries and EVs) can either take the role of a buyer or a seller by charging or discharging their capacity.The design of P2P markets is a multi-dimensional problem.Following [42], we focus on three of the most important dimensions: • Temporal dimension: refers to the time in advance the delivery of electricity is contracted at.It is common to have a series of consecutive markets with multiple clearing stages for a number of future delivery periods.Electricity markets can start from yearsahead and up to minutes before the actual time of delivery [43].
Local energy markets are expected to be designed on short-term basis, e.g. from day-ahead and up to real-time of operation.This allows prosumers to better manage their local devices and flexibility.• Contractual dimension: defines the basis upon which buyers and sellers trade.In general, contracts can take the form of bilateral trading or pool market trading, both of which can be mapped to the P2P structure.According to [44] and with slightly changing their terminology, we can categorize P2P structures into: (1) Full P2P market, where peers can negotiate directly with each other in a complete decentralized manner with no central authority.This is a strict P2P market with multi-bilateral contracts.(2) Pool-based structured P2P market, which requires a community manager that manages trades within its community and with neighboring communities.This structure does not uniquely match a certain buyer to a certain seller; rather it creates a pool of buyers and sellers, ensuring overall supply-demand matching.(3) Hybrid P2P market, which combines the design of the two above structures.• Price clearing dimension: defines the price setting mechanisms implemented in the market.Two arrangements are typically made for price clearing in electricity markets [24]: (1) Uniform pricing, where the market supply and demand are matched, and the resulting price is set to be the market price for all buyers and sellers.(2) Discriminatory pricing (also known as pay-as-bid pricing) is when the activated customers pay/receive according to their bidding prices.
Existing literature on P2P energy trading opted for a CDA design for the P2P markets that are short-term, full P2P, and with discriminatory pricing mechanism.Indeed, a full P2P contractual setup is well-aligned with the basic concept of P2P trading.However, it limits the types of market products that can be traded.For this reason and unlike most existing literature, in this paper, we choose to design our P2P market as a pool-based with uniform pricing mechanism (see Section 4).

Opportunities and challenges
The future prospects to P2P energy trading are undeniable and requires a thorough investigation, as done by [44].On the economic side, and with the decentralized structures of P2P markets, all intermediary entities and their corresponding costs are eliminated, which induces more competition among prosumers; hence, improving market liquidity and social welfare [45].Furthermore, P2P markets have the potential of be the main drivers to incentivize investments in DERs, which in return reduce the societal carbon emission level.In terms of social impacts, P2P markets increase transparency with regards to the source of energy, thus giving consumers the freedom to choose the type of energy they can purchase.This transparency allows eco-friendly and conscious prosumers to serve their load demand only from green energy sources; something that is not possible without P2P trading.Also, the concept of P2P trading, which is a form of shared economy [46,47], cultivates trust and collaboration as resources are shared within the community.Finally, the grid's resiliency and security are directly improved, since P2P markets raise the prosumers' awareness regarding the network capabilities.
On the other hand, some challenges and barriers must be levered to reach the full potential of P2P trading [48,49].One of these challenges is that P2P markets require additional regulations, for example, to ensure fairness, and to define the legal boundaries of market players and their eligibility for taxing and network usage tariffs [17].Also, from a technical aspect, it is difficult to differentiate between energy traded from/to the grid and in the P2P market.This can be alleviated by the introduction of smart meters; albeit only when there exists a highly secured database for P2P transactions.On the organizational side, it is essential to incentivize market players to actively engage in trading [50], otherwise the lack of market competition can lead to sub-optimal clearing prices.Additionally, market prices should be more competitive and attractive than the status quo; i.e., smaller than retail prices for buying energy, and larger than Feed-in tariff (FIT) for selling energy.Finally, on the social side, P2P trading is highly influenced by individual decisions and interactions, and this additional uncertainty caused by human behavior can be challenging to handle [23].

Blockchain potential and value
Blockchain was developed to enable a secure and reliable distributed cash system through a digital currency called Bitcoin [51].In its most basic form, blockchain is a tool that allows storing and sharing a digital database that contains an expanding log of digital transaction, data records or any other form of data [52].Such transactions are aggregated into so-called blocks, where every block is time-stamped, immutable, traceable and cryptographically linked to previous blocks, thus forming a chain of blocks, or blockchain [53].Furthermore, users who have a digital copy of the ledger, collaborate in order to verify and safeguard the transactions, thus maintaining the integrity of the distributed ledger.Beside the high security and functionality of blockchain, it enables the so-called smart contracts.Smart contracts are self-executing programs that reside on the blockchain ledger carrying out specific functions if certain pre-programmed conditions or rules are met.Smart contracts can be programmed to react to certain events in the system.For example, contract owners (prosumers) experiencing shortage of energy generated from their PV generation, can pre-program their smart contracts to automatically buy from other prosumers to meet any deficit in energy [54].
Blockchain technology can provide several benefits to energy systems, as shown by [55].For example, it enables automated and efficient billing systems for local DERs and prosumers [56].This can eliminate third parties, remove transaction intermediary costs, maintain fairness in trading, and enable accurate billing between traders [57].Consequently, it facilitates P2P energy trading by offering fast and real-time settlements.This promotes more investment and better management on DERs.In addition, smart contracts ensure supply and demand are closely matched through automatic trades, thus improving system reliability.Another important benefit of blockchain in energy systems is that it enables full transparency.This means that prosumers can choose their suppliers according to their preferences, for example one can trade only with prosumers who use sustainable sources.Blockchain can also benefit energy systems in terms of privacy and security through its cryptographic and digital signatures features.Its tamper-proof nature can digitally secure all relevant information with high resiliency to attacks and forgery.This in turn, generates trust among all traders and facilitates the verification of shared information.Finally, in the era of energy decentralization and digitalization, having a central point for data may not be the most efficient approach, which makes blockchain technology very appealing.

Decentralized energy trading platform (DeTrade)
Here, we introduce the general structure of the proposed Decentralized P2P Energy Trading Platform, called DeTrade.The DeTrade enables prosumers (peers) to trade among each other, while guaranteeing the privacy and security of their identities and market bids.As stated earlier, DeTrade is composed of two main layers: the market layer and the blockchain layer (see Fig. 1).The market layer encompasses a decentralized P2P energy market, called 'DeMarket', which is a shortterm parallel auction market that allows prosumers to trade various market products.An AI smart agent is assigned to every prosumer, with the following responsibilities: (1) monitoring (through direct access to smart meters) and controlling consumption and production of prosumers' local devices, (2) acting as the bidding entity in the DeMarket, (3) being responsible for market clearing (see Section 4.4), and (4) serving as a contact point with the blockchain layer.In the blockchain layer, the market results derived from the decentralized market clearing method are stored in a secured manner (see Section 4.4).In addition, a smart contract is implemented to enable efficient real-time settlements by ensuring that money is only transferred if the energy traded is actually delivered (see Section 5.2).
It should be noted that all processes carried out on the market and blockchain layers are separated.This means that all communication and information exchanges among the agents take place on the market layer only.The only direct communication that takes place between the two layers is through the agents after they clear the DeMarket and store the results in a smart contract on the blockchain layer.In Section 4, we introduce the DeMarket structure and products, the social welfare maximization problem, and the developed decentralized method for market clearing.Section 5 explains the functionality of the blockchain layer along with the smart contract developed.

DeTrade: Market Layer
In this section, we introduce the market structure and products, and accordingly formulate the market optimization problem.

Market structure
The general setting of the proposed DeMarket follows a different arrangement to the commonly-used in the literature of full P2P market with a CDA mechanism.The DeMarket is a short-term and parallel poolbased market that adopts a uniform pricing mechanism and is divided into multiple stages of market clearing and multiple periods of energy delivery.The time-ahead and pool-based features of the market are similar to the current electricity spot markets, such as day-ahead and intra-day markets [58].
Fig. 2 presents the general structure of the DeMarket.The market's temporal dimension,  , entails two time horizons, called the clearing horizon   and the delivery horizon   ; i.e.,  = {  ,   }.The delivery horizon is divided to  equal consecutive periods, where the length of a single period ranges from minutes to hours.The consecutive delivery periods allow the trading of time dependent market products, as will be explained later in the types of market products.In every period, sellers and buyers inject and withdraw respectively their allocated energy.In the clearing horizon, the DeMarket includes  consecutive stages.During every stage, all delivery periods are cleared together (hence, the word 'parallel auction') accounting for all inter-temporal dependencies.Due to the decentralized structure of the DeMarket, reaching the optimal clearing of all the delivery periods takes several stages of information exchange among the prosumers, (see Section 4.4).Indeed, in a centralized structure of the P2P market, the delivery periods can be optimally cleared at once.
The time sequence of the DeMarket starts at time step −, with the clearing horizon, which ends at time step −1.Then, the energy delivery horizon starts at time step 1 and the market terminates at time step .The clearing horizon stages can be written as   = {−, … , −1}, and the delivery horizon periods as   = {1, … , }.The realization of the final stage market clearing results as well as storing them in the smart contract (on the blockchain) take place between the clearing horizon at −1 and the switching time to the delivery horizon at time step 0.

Market products
The bidding protocol of the DeMarket incorporates the market bidding behavior of prosumers in terms of 'quantity bidding' with the following market mechanism assumptions: (1) Prosumers are truthful in terms of their market bid unit pricing, which is a public information within the DACO method steps (see Section 4.4), (2) The quantities attached with the prices are within the actual capacity limits of the devices used by the prosumers in their bidding (see Section 4.4), and (3) Since the smart contract is able to detect any fraudulent information submitted by the prosumers (see Section 5.2), prosumers have no incentive to over-commit above their actual capacity.
We define a market product as a market acceptable bid format, in terms of energy-price relationships.The DeMarket's structure allows trading of a diverse range of market products.In this paper, we account for two types of market products: single products and continuous products.Before we explain these products, it should be noted that the DeMarket is not limited to these market products and it can be easily extended to incorporate other types of bidding structures if needed in future.
• Single product: A single product reflects bids that are only valid for a single delivery period and do not have inter-temporal dependencies.This product allows multiple blocks of quantities and prices to be traded within a single time period.This product type suits those suppliers (consumers) that have an increasing (decreasing) valuation of energy depending on the energy quantity.For example, single products can be used by prosumers offering supply bids with ramp-up constraints for their local devices, such as local diesel generators.Besides, prosumers with demand bids can use this type of product to either charge their battery devices, or to directly supply their household demand.Fig. 3(a) and (b), respectively, show an example of a three-blocks supply (in an increasing merit order), and a two-block demand (in a decreasing merit order) single product.
• Continuous product: A continuous product reflects bids with intertemporal dependencies, which can spread across multiple delivery periods.This is a ''take it all or leave it'' type of bid, which means that this product does not allow partial quantity bidding.A prosumer with a non-zero continuous product bids for its full quantity and all the delivery periods of that market product at the same time.If the bid is accepted, it will be activated for full quantity and for all delivery periods the market product entails.If it gets rejected, nothing will be activated.Unlike single products, continuous products feature only one single block, which is reasonable since partial quantity bidding is not allowed with these products.Fig. 3(c) and (d), respectively, show an example of a supply and demand continuous product that stretches   A. Esmat et al. across two consecutive periods.Continuous products are very relevant with uninterruptible loads: household devices that operate on cyclebasis, such as washing machines and dryers, which require to maintain their state of operation until they finish their cycle.Moreover, devices such as batteries and EVs can also benefit from the inter-temporal commitments that this market product can provide, since they also need to maintain their state of operation (charging or discharging) for a specific number of consecutive periods.

Social welfare maximization problem
The objective of the market layer of DeMarket, the general bidding strategy of the prosumers, is to maximize the overall social welfare, which is the summation of the utility of all buyers minus the summation of cost of all sellers [4] across all delivery periods.To be more precise, sellers and buyers submit bids in each stage of clearing horizon for all periods of the delivery horizon in parallel.The goal of the DeMarket is to maximize the social welfare across all delivery periods when we are at the last trading stage.Before formulating social welfare maximization problem, we introduce some notation (See Table 7 for a list of notation).
Let  be the set of all market players.Any market player can either belong to the set of buyers  or sellers , but not both; i.e.,  ∪  =  and  ∩  = ∅.Let  ,  be the set of all quantity bids of market player  at clearing stage  for delivery period .Such a quantity bid has multiple components, depending on whether it is a single or a continuous product.Denote    and    , respectively, the set of all single and continuous products of market player .
A continuous product has a single component which is the quantity.For a market player  ∈ , denote  , , the quantity bid of a continuous product  ∈    that is traded at clearing stage  for delivery period .According to ''take it all or leave it'' property of a continuous product, it can either be fully activated or not being used at all.Thus, if A continuous product can be active (non-zero) for two or more consecutive periods.This means that for any delivery period , the total continuous quantity from product  and market player  at clearing stage  should account for all continuous products that are scheduled for any delivery period  ≤  and might continue to exist at . Mathematically, the total continuous quantity from product  and market player  at clearing stage  for delivery period  is ∑ ≤  , , .A single product might have different prices within one clearing stage.Let  , be the set of all price blocks within one clearing period of the single product  ∈    of market player .To linearlize our problem formulation, we express a single product by the sum of all products in each price block.For single products, the energy quantity that can be activated from every block is constrained between the minimum and maximum quantities of that block.While usually market bids with multiple blocks are presented in an aggregated form (see Fig. 3(a) or (b)), we model it by splitting these aggregated blocks into individual blocks.In this way, we can identify the amount of activated energy per every block separately.Therefore, for single product  ∈    of market player  in clearing stage  for delivery period , we denote by  , ,, and  , ,, , respectively, the minimum and maximum bounds that limit the amount of energy that can activated from every block.Accordingly, we have Combining both products, the set of all quantity decision variables for market player  at delivery period  for a clearing time  is given by Accordingly, the set of all decision variables can be written as At any clearing stage , the market should ensure the matching between the total scheduled supply, for all sellers  ∈ , and the total scheduled demand, for all buyers  ∈ , for all delivery periods .It should be noted that the DeMarket does not guarantee that the supply and the demand flexibility are met fully (or all market bids will be allocated).
Here, we assume the DeMarket is an on-grid system, thus, it does not substitute the dependency on the grid, but rather it reduces it.It ensures that market equilibrium is achieved (if exists), and that the scheduled offered supply matches the scheduled demand.Given the notation introduced so far, we can mathematically express the balance between supply and demand as Therefore, the utility of buyers and the cost of sellers can also be formulated accordingly.Let  , , and  , ,, , respectively, be the price (in ce/kWh) for every block of energy of continuous and single product  of a market player , where  in the single product refers to the block.Given the decision variables, let  ,  ( ,  ) and  ,  ( ,  ) be, respectively, the utility and cost of buyer  and seller  that are incurred at clearing period  for an upcoming delivery period .This can be expressed as Using these elements, we can formulate the optimum market solution to be the one that maximizes the overall social welfare, which we formulated as a mixed-integer linear programming (MILP) Every clearing stage is a parallel auction that clears prices of all delivery periods all at once.Since the DeMarket is following a uniform pricing-based mechanism, the optimization results of Eq. ( 8), yield a set of  marginal prices, one for each delivery period.These marginal prices represent the intersection between the supply and demand in every period.Denote   for any  ∈   the clearing price a seller will receive in the delivery period.Given that the market uses a uniform pricing, a seller pays −  for buying one unit of energy in the same delivery period.With this notation, the total revenue/cost of a given market player  ∈ , denoted by  , ,, , is given by Indeed, it is interesting to also compute the revenue of market players.We use Eq. ( 9) for this purpose in our case study to shed some lights on this matter.

Decentralized Ant-Colony Optimization algorithm (DACO)
The key challenge in decentralized P2P markets clearing methods is the trade-off between market efficiency and information privacy.On one hand, maximizing social welfare requires full market products and bids visibility.On the other hand, prosumers do not want to expose their full capacity to protect their identities.Exposing full capacity increases the chance of uniquely identifying prosumers, for example if one prosumer can identify the bidders, he/she can use this information to play strategically and manipulate the market [3].
With that in mind, here we propose and implement a novel decentralized market clearing method that balances between the market efficiency and information privacy.The commonly used alternative, ADMM (See Section 1.1.1),becomes very complex, when dealing with inequality constraints and binary variables such as those formulated in the previous In addition, ADMM is a sequential optimization process, which makes it time expensive.For these reasons we develop another method to resolve these issues.
The developed method here is a Decentralized variation of the Ant-Colony Optimization method, called 'DACO', that relies on the cooperative behavior of the market players to clear the DeMarket.The ant-colony optimization 'ACO' method [59] is inspired by the cooperative behavior of ant colonies in nature, as they deposit pheromones in their food exploration trips.Such pheromones measure the ants' satisfaction level for a given path and guide successive ants to choose the paths with highest pheromones levels, i.e. shortest paths to the food source.The ACO method is a highly efficient algorithm with the ability to achieve optimal solutions given suitable parameters and design schemes [60][61][62].In the DACO method, each agent is running a local ACO process but with a different set of information.Every local ACO is composed of a number of iterations.At the end of every stage, every agent shares their market optimization outcome, which can either yield a feasible or a non-feasible solution.
The DACO adequately balances between maximizing the efficiency of the market while protecting the information privacy of the prosumers.This is achieved by adopting a unique design for the information exchange process.Indeed, agents often shade their bid quantities below the actual full capacity in every clearing stage, and then share the new shaded bids with the rest of the agents.This quantity shading protects prosumers from being uniquely associated or identified from their full capacity bids.Additionally, agents, or prosumers, that choose to misreport their bidding quantities and fail to deliver their (faulty) cleared amounts, will not receive their respective costs, as enforced by the smart contract (see Section 5.2).By leveraging the versatility and the unique design of the DACO, an efficient market solution can be achieved with a limited number of clearing stages within the clearing horizon that provides close-to-optimal market solution.
Finally, another unique nature of the DACO is its parallel structure which minimizes the computational time.Furthermore, it does not impose any complexities in dealing with inequality constraints and binary variables, thus allowing a diverse range of market products to be modeled.In the following, the steps of the DACO algorithm are explained.

DACO steps
Fig. 4 depicts all processes taking place in the DACO, which are explained in the following.
• DACO start: In the first stage and before the agents' iterations begin, all agents send an initial set of information, which serves as their guess for their allocation in the market solution.Here, we refer to this set of information as a solution packet.The solution packet is composed of different energy quantities (to buy or sell), previously formulated in Eq. ( 3), and their corresponding costs for a given delivery period.The initial solution packet is composed of random quantities but within the capacities of the actual bids.In the next iterations, the agents update the quantities shared in the solution packets in every stage according to the pheromone levels (see description of pheromone updating and Eqs. ( 11) and ( 12)).However, the prices remain constant across all stages.
• Ants touring: In the first iteration, every agent solves the objective function (8) with respect to its own feasible space and the solution packets they received from all other agents.The ants movement in the feasibility space follows the general probabilistic movement equation of ACO [59], described in Eq. (10).The equation describes the probability  ,,, , of ant  ∈   of agent , to move from edge  to , in iteration  during clearing stage .The pheromones level, denoted  ,, , , represents the amount of pheromones assigned by every ant to every edge.The edge visibility  ,, , represents the bidding quantities' costs in the solution packets.The control parameters  and  are set to balance between the influence of pheromones and cost values on the ants' movement from one edge to another.Fig. 5 shows an example of a system with 3 agents.In the first iteration, agents 2 and 3 send their solution packets to agent 1.Then, agent's 1 ants explore different possible market solutions between its own search space and the solution packets received.Since all the agents are trying to solve the same function ( 8), their efforts are not since they have different sets of information.
• Local fitness evaluation: Every ant's tour represents a possible solution to the market problem.After all ants complete their initial tours, the fitness of every tour is evaluated.The fitness is evaluated based on two criteria: the social welfare and the net balance of the trading across the delivery horizon.The values of the weighted average of the fitness criteria are used to select the best tour.This metric identifies the solution with the maximum social welfare and minimum absolute net balance.Furthermore, a rejection criterion is included, which eliminates market solutions with net energy balance that surpass a permissible marginal error of ±5%.
• Pheromone updating: The DACO uniquely introduces two levels of pheromones updating.First level is a local pheromone updating based on the satisfaction levels of the ants to their tours.Every ant traces back its steps and deposit pheromones on the edges it visited.The goal is to allow the ants to explore new edges with low amount of pheromones concentration.The updating rule for the new local pheromones  +1,, , in iteration  + 1 is given in Eq. ( 11), where  ,, , is ant's  fitness evaluation for its tour.The evaporation rate, denoted   , is adjusted to prevent ants from always selecting edges with high amounts of pheromones concentration, thus allowing further exploration of new paths.

𝜏 𝑖𝑡+1,𝑎𝑔,𝑟 𝑛,𝑚
= (1 −   ) ,, , +    ,, , (11) The second level of pheromone updating takes place after all agents finish their own local ACO process.The agents identify the best tour their ants achieved in the initial stage.This information is shared among all agents, which helps in assessing the quality of their initial guess in the solution packet.Accordingly, every agent updates the pheromones of their solution space to increase the probability of sending improved and new values in future stages.The global pheromones updating rule follows Eq. ( 12), where  ,+1 , is the new pheromones assigned to every agent in their own feasibility space for the clearing stage  + 1.The initial amount of pheromone assigned by the agents in the first clearing stage is denoted   0 .The new pheromones to be deposited in the search space is denoted  , , , which represents the agents' satisfaction with the best tours achieved.Finally,   is the global evaporation rate [63], where 0 ≤   ≤ 1.This value balances between exploring new areas and sticking with areas with high pheromones concentration.
• Convergence check: The DACO converges if the global pheromones of the agents reach an unchanged value compared to the previous stage.Otherwise, all agents submit their final best solution at the end of the clearing horizon at step −1, summing up to an  number of stages.After the DACO terminates, all agents may reach nearly similar solutions with small margins of error between them.Finally, all agents submit their own solutions to the smart contract.Based on the smart contract logic (see Section 5.2), the agents will adhere to a single optimal solution in the delivery horizon.

Complexities and trade-offs
Similar to all optimization algorithms, there exist certain tradeoffs and complexities.Most importantly, there are trade-offs when balancing between the quality of the market solution, the privacy of the traders, and the computational time of the algorithm.Better market solutions might mean larger computational complexity.In particular, increasing the number of ants leads to better market solutions, though at the cost of higher computational complexity.Similarly, the traders' privacy and market solution quality are closely related.Exposing more private information about available quantity leads to larger social welfare and better market solutions, though at the cost of raising privacy concerns.
We evaluate the economic efficiency of the DACO method with respect to the optimal market solution.Suppose that the social welfare of the sub-optimal market solution found by the DACO is , and the optimal market solution   is as formulated in Eq. ( 8).We define the market solution error  to be the relative error between  and   : There is also a pre-defined acceptable target solution error  * which sets an upper bound on how large the solution error can be; i.e.,  ≤  * .We use this constraint (also in Section 6) to measure whether or not the DACO solution error is within the acceptable range with respect to the social welfare maximum solution.

DeTrade: Blockchain layer
In this section, we elaborate on the role of blockchain layer for the market layer to ensure efficient and secure real-time settlements.In the context of DeTrade, blockchain technology offers three distinct advantages compared to traditional solutions using a trusted third party.First, blockchain technology offers a semi-decentralized database where households themselves can securely write to and manage the database, even in the context of mutual distrust and Byzantine behavior.This model is more robust against manipulation of stored data, since it usually requires adversarial behavior of a majority of users to manipulate the database.In comparison, when using a centralized data store, users have to trust the storage operator to not manipulate the recorded data in their advantage.Second, all included transactions on a blockchain are public and verifiable by other participants.This allows participants to monitor each other's behavior and punish misbehaving users accordingly.Third, the expressive power of blockchain-based scripting language enables a wide range of algorithms that can run on a blockchain, e.g., automated money transfers.
In DeTrade, the blockchain layer leverages the desired properties of blockchain technology through three functionalities.First, we use the tamper-proof properties of the blockchain data structure for state storage, specifically to securely store the market clearing results computed in the market layer (see Section 4.4).Second, we represent monetary value in the form of digital tokens on the blockchain.This process is called tokenization and has seen an increasing adoption over the past few years [64].Third, we leverage blockchain technology to automatically and securely transfer digital tokens when the energy traded is actually delivered or consumed.

Permissionless vs. permissioned blockchain
types of blockchains are introduced in the literature: permissionless and permissioned.In a permissionless blockchain, users can freely join the network and participate in the consensus algorithm that maintains and secures the distributed ledger.On the other hand, participation in a permissioned blockchain is managed by an access control mechanism.For example, participants have to be manually approved by a central trusted party before they can submit transactions to the blockchain.We identify two main advantages of permissioned blockchains over permissionless blockchains.First, permissioned blockchains have a well-defined network membership which renders various targeted threats to system integrity, e.g., uncontrolled identity creation, impossible.Second, network upgrades in a permissioned setting compared to a permissionless setting thanks to their clearly defined authority and governance model.This makes it easier to change the rules and constraints of the network in a permissioned setting in comparison with permissionless blockchain, in which modifying the rules is often a complicated and long-term process.
Given these differences, we adopt a permissioned blockchain within DeTrade where the network is managed by the agents assigned to every prosumer.Furthermore, we assume that the smart meters are trusted devices and their reports are truthful.We argue this is a reasonable assumption since manufacturers take active measure to prevent tampering of smart meters.The participation of every agent is approved by a trusted third party, such as an energy supplier.Note that this does not violate our requirement for full decentralization, since this party is not directly responsible for managing or controlling the market.Only authorized AI agents have write access to the blockchain.

Smart contract functionality
The key component in the blockchain layer is the smart contract, which efficiently manages the settlement process, see Fig. 1.Popularized by the Ethereum platform [65], a smart contract is application logic running on the blockchain and has its correct execution enforced by the adopted consensus mechanism [66].Smart contracts are often used to securely manage digital tokens residing on the blockchain.In our setting, every AI agent owns and operates an account, and is able to invoke the smart contract by sending a unique transaction to the blockchain layer.In most blockchain systems, an account consists of two cryptographic keys: (1) The public key, which is an identifier for the operated account, and (2) the private key, which is used to digitally sign all transactions sent to the smart contract.
The smart contract carries out two main functionalities: it stores the market solution results submitted by the agents and manages the balances of digital tokens used for buying and selling energy.We represent (real-world) monetary value through a special token, called EuroTokens.One of the major concerns of blockchain-based digital tokens is their price volatility, as digital assets' trading price can fluctuate based on their community interest.In our case, if agents are capable of selfissuing EuroTokens without any constraints or monitoring, owning and exchanging of EuroTokens becomes worthless.To overcome this issue, we have the smart contract representing digital tokens that are backed by real-world currencies.These real-world currencies are managed by a trusted third party (TTP), whose role can be fulfilled by a notary or financial institution such as a bank.Only the TTP is allowed to create new EuroTokens since its public key is authorized in the smart contract.The TTP's responsibilities is two-fold: it mints (creates) EuroTokens for new agents joining the market and converts tokens to monetary value if agents wish to leave the market.
Recall that agents in DeTrade do not directly trade energy and Eu-roTokens with other agents.Instead, during each period in the delivery horizon, the energy traded is either injected/consumed in/from the grid, and the EuroTokens of the buying agents are transferred from their accounts to a special account managed by the smart contract, called the pool account.After all energy has been delivered, the pool account aggregates all paid tokens during each delivery period and re-distributes tokens in the pool account according to the market clearing results.The participating agents interact with the smart contract according to the following steps: • Market enrollment and token minting: The TTP creates a new account in the smart contract for new prosumers participating in the market and authorizes the prosumers' AI agents to interact with the smart contract.The prosumers buy an amount of EuroTokens from the TTP with a real-world currency, which is then minted and transferred to their new account.The prosumer is now able to participate in the DeMarket.
• Role specification: Every participant indicates at the start of the clearing horizon whether they are participating as a buyer or a seller.This information is used by the smart contract to correctly re-distribute EuroTokens from the pool account to energy sellers.
• Storing market clearing results: At the end of clearing horizon, all agents embed the market solution that they acquired in a transaction and submit it to the smart contract.Thereafter, the smart contract automatically verifies these results.
• Verification of market clearing results: We add validation logic to the smart contract that verifies the consistency of all submitted market solutions by the agents.This is to prevent individual agents from actively manipulating the clearing results to their advantage.Specifically, this step validates that the submitted clearing results are well-formatted and sufficiently similar.This is done by computing the percentage difference between all the submitted values of the market results (pair-wise).Any result that surpass a permissible difference of 5% is excluded.This assumption is reasonable since all near-optimal market results should be within a close and similar range of accuracy.The outcome of this validation step is reflected in the smart contract state.The participating agents can query if the results have been validated.In the situation where one or more submitted results are deemed invalid by the validation logic, the erroneous result can be traced back to a specific agent and optionally be excluded from the market.The smart contract gives the final market solution that all agents should adhere to.Similar to the fitness evaluation process in Section 4.4, the choice of the final market solution is based on computing the weighted average of the two fitness criteria, the social welfare and absolute net balance.Thus, this method identifies the market solution that has the maximum social welfare and minimum absolute net balance.
• Energy trading: During the delivery horizon, all buyers and sellers exchange the traded energy according to the final market clearing results provided by the smart contract.Here, the role of the smart meter is key to verify that the traded energy is actually injected or received.Subsequently, the agents initiate a EuroToken transfer to the pool account.

Smart contract implementation
The smart contract in DeTrade is implemented in the Solidity programming language [67].Solidity compiles smart contracts to bytecode and execute them within the Ethereum Virtual Machine.Although Solidity is popularized by Ethereum, there are several blockchains that adopt such programming model, e.g., EOS 9 and Hyperledger Burrow. 10  9 https://eos.io 10https://www.hyperledger.org/projects/hyperledger-burrow/The smart contract implementation follows the interactions described in Section 5.2.In Table 1, we list the smart contract methods, along with their corresponding scope and allowed callers (e.g., some methods can only be invoked by the TTP).The individual accounts EuroToken balances are stored in a mapping, and their initial balances are set to zero.The TTP is the only entity allowed to publish new EuroTokens by issuing a special transaction to invoke the mintEuroToken method.
Here, the smart contract is updated on discrete-event basis, every time a new transaction takes place.Finally, we identify two security threats in DeTrade.First, all agents are prohibited from tampering or updating with their EuroToken balance mapping, since they hold real-monetary value.This is addressed by limiting token minting only to the TTP.Second, even though we assume a weak trust model, the smart contract can detect invalid market clearing results.They can originate from active fraud attempts to manipulate the energy prices or caused by malfunctioning agents.
The validateAllClearingResults method verifies that the market clearing results reported by smart meters.To further increase the security of our smart contract, we have devised 27 unit tests that thoroughly verify correct behavior of implemented methods.

Simulation & results
In order to showcase how the developed DeTrade operates, we use in our simulation real-life distribution feeder network data located in the Midwest U.S. [68].The feeder configuration is shown in Fig. 6.The feeder connects 17 primary nodes with customers equipped with smart meters measuring hourly energy consumption (kWh).Low voltage customers are connected to these primary nodes via secondary distribution transformers (120/240 V).Here, for privacy concerns, only the aggregated hourly consumption at every node is available for a single year.For the sake of simplicity and without loss of generality, it is assumed that every node represents a single prosumer (market player), thus the trading can be carried out between different nodes in the feeder.

DeTrade setup
In the given feeder, we consider the number of market players participating in the DeMarket is  = 6, with 3 buyers and 3 sellers;  ∈  = {1, 2, 3} and  ∈  = {4, 5, 6}.Based on the available consumption data, we choose a random day and hour during the day to carry out our simulation.Furthermore, we simulate the DeTrade to operate on a onehour-ahead basis, with four 15-minute delivery periods,   = {1, … , 4}.All prosumers in our setting are assumed to be grid connected, thus they will not experience power outage if the supply and demand (traded through the DeTrade) do not match in real-time due to uncertainties or forecast errors (see Section 7.1).To induce more participation from the prosumers, the market products' prices as well as the market cleared prices must be competitive compared to the regular grid prices.This means that the cleared selling and buying prices must be, respectively, more than the FIT, and less than the regular retail buying prices.In this simulation, we use the Netherlands' tariff values.The FIT for solar rooftop PV panels range from 2.2 ce/kWh to 3.8 ce/kWh [69], and the retail tariffs for household consumption range from 9.21 ce/kWh to 18 ce/kWh depending on the peak hour. 11Here, for the sake of simplicity, we assume a fixed FIT (3.8 ce/kWh) and retail price (18 ce/kWh).In the DeMarket, prosumers can trade single and continuous products with respect to the 4 energy delivery periods.Table 2 provides details regarding the agents and the household devices involved in the market.Table 3 shows the market products of the participating agents, which consist of the energy in kWh and prices in ce/kWh.Note that the prices used here are arbitrary, since there are no regulations that determine how these products should be priced.

Market layer simulation results
In order to assess the efficiency of the DACO's sub-optimal solution , we also compute and compare the optimal solution   , as formulated in Eq. ( 8), by using a central MILP solver.Note that both optimal and sub-optimal solutions have the same coupling constraint of maintaining the total energy net balance across all periods in the delivery horizon at zero (see Eq. ( 5)).While the DACO's  may yield a non-zero net balance, its value must be kept as close as possible to zero.Thus, it is also important to make sure that the  solution has an acceptably small net balance.We take the target values for acceptable social welfare error to be  * = 5% and net balance violation to be 5%.We also assume that the computation time of DACO, denoted , must not be larger than the one-hour ahead time frame of the clearing horizon.Also note that it is unrealistic to compare the computation time of the DACO with that of the MILP solver, since the latter clearly outperforms any decentralized method.Both approaches, DACO and MILP, were modeled and simulated in MATLAB, and executed on a CPU with AMD PRO A6-8570 R5 @ 3.50 GHz.

DACO convergence analysis
The key variables with a direct effect on the DACO's performance and solution quality (see Section 4.4.2) are the number of ants and number of stages.By fixing the number of local ACO iterations, (here, 500 iterations), we run different simulations with different combinations of the number of ants and the number of stages.Table 4 gives insight on the convergence speed and accuracy of the final solution as a function of number of ants and number of stages.It can be noticed that at a low number of ants and high number of stages, for example 200 and 70, respectively, the DACO's has solution error of 7.30% with net balance error of 5.3%.Even though this combination is time efficient, the error margin is indeed high.As the number of ants increases and the number of stages decreases, the DACO is able to enter the feasibility region, while being compliant with the clearing horizon time frame.Specifically, the combinations of 400 and 600, and 850 and 50, for the number of ants and number of stages respectively, yield nearoptimal solutions within a solution and net balance margin error of 5%, and with computation time  below the one-hour limit of the clearing horizon.With higher number of ants and lower number of clearing stages, starting from 1000 ants and 30 stages, the DACO still manages to be within the acceptable error margins.However, the computation time becomes very expensive, which makes them inefficient.
According to the simulations presented in Table 4, we choose the best combination among them to be at 850 ants and 50 stages that yields a solution error margin of 2.10%, net balance error of 4.5%, and with computation time  = 58 minutes.This is an acceptable convergence time within the clearing horizon window of one hour ahead,   especially with the limited computation power available.In real life, it is expected that the computation power of the smart AI agents to be superior thus allowing for faster convergence in much adequate time.Also, since the developed DeTrade is an on-grid system, the trading balance within the range of ±5% boundary is considered permissible.Fig. 7 illustrates the DACO's performance in such a combination, in terms of the social welfare and the net balance, and compares them with optimal solution from the central solver.The left plot in Fig. 7 shows the evolution of the DACO method towards the ±5% boundary of the optimal solution.It can be noticed that at clearing stage number 40, the DACO enters the permissible ±5% boundary, and reaches an unchanging value of welfare of 43.57cestarting from stage 47 and up to stage 50.In the right plot of Fig. 7, the comparison between the total energy net balance of both methods is shown.Optimally, the net balance should be maintained at zero across the 4 periods of the trading horizon.However, given the decentralized and heuristic nature of the DACO, it reaches a total net balance of 0.045 kW at stage 47 and maintains the value up to stage 50.One can conclude that given this example, the market setup should have at least 40 clearing stages to ensure sufficiently good social welfare and small net balances, if we are bounded by only 850 ants.At the end of clearing horizon, all agents store their final market solution on the smart contract in the blockchain layer.Then, the smart contract validates these market solutions and outcomes the solution that all agents will adhere to during the delivery horizon (see Section 5).The resulting market solution of the smart contract is shown in Table 5.The results show the quantity allocated from every market participant, and the actual cost and revenue that the buyers and seller pay and receive respectively.These costs and revenues depend on the marginal prices of the cleared horizon, as explained by Eq. (9).With respect to the continuous products offered by agents 3 and 6, only the product of agent 3 was found feasible to be allocated.Thus, the total offered energy is activated in full in the first two periods of the market as per the constraints of the product.One can conclude that extending the temporal dimension of the DeMarket can give the prosumers more freedom to design their market products to be compliant with their own preferences and local devices constraints.

Blockchain layer simulation results
In the blockchain layer, we evaluate our smart contract by deploying it on a nation-wide university cluster (more specifications can be  found online 12 ).We establish a cluster consisting of seven instances (representing one TTP and 6 agents) that run Hyperledger Burrow on distinct compute nodes [70].Burrow is a light-weight, permissioned blockchain that is able to execute smart contracts written in Solidity.Burrow runs the Tendermint BFT consensus algorithm, which provides transaction finalization within seconds and can tolerate up to 1 3 of the network being adversarial [71].We argue that this trust assumption is realistic since it is not likely for households to collude on a large-scale in order to subvert the network.Each participating household runs a validator in the Burrow network.
For the experiment, we specify a list of actions (scenarios) that should be performed by nodes at a given time.The full experiment setup and scripts is open source and can be found in a GitHub repository. 13The experiment starts with the TTP deploying the smart contract 12 https://www.cs.vu.nl/das5/clusters.shtml 13 https://github.com/devos50/gumby/tree/blockspeed/experiments/burrow on the blockchain.The TTP first registers all 6 agents in the contract, and mints sufficient EuroTokens to each one.In the beginning of the delivery horizon, after storing all the clearing results are stored, the buying agents invoke the receivedEnergy method in the smart contract to signal the observation of incoming energy from the grid.This is repeated for the 4 delivery periods.
In Table 6 we outline the average confirmation time and gas usage of the methods invoked by the agents.Gas is a unit that indicates the computation effort to execute a specific transaction and can be considered as the fuel for the network.Each type of operation requires a fixed amount of gas.Note that on average transactions are confirmed and finalized on the blockchain within two seconds (the Tendermint consensus algorithm produces a block with transactions every second).Given the duration of the clearing and delivery horizon, this is an acceptable time for settlement.This experiment proves that Hyperledger Burrow in combination with DeTrade enables fast and secure settlement.The gas usage indicates how much computation each transaction requires the smart contract to conduct.The most computationally intensive method is storeClearingResults, requiring on average 1,249,320 gas.The high gas cost is attributed to the validation of the clearing results, which is part of the transaction that invokes the storeclearingResults method.On the public Ethereum blockchain, executing the 24 transactions outlined in Table 6 would cost $6.52,excluding transaction fees and with a price of 5.2 Gwei/gas.However, in our permissioned blockchain there no transaction fees.

Discussion
As shown in Section 6, the developed trading platform accomplished the required objectives.First, it enabled prosumers to trade energy amongst each other in a flexible arrangement that allows market bids to take inter-temporal forms.Secondly, the novel DACO method reached a near-optimal market solution to maximize the social welfare of the prosumers within the time frame of the trading horizon.Thirdly, blockchain was implemented in a practical way that offers security to the stored market results, and by leveraging the automation of smart contract, fast real-time settlements were delivered to the prosumers.It is worth noting that while the pool-structured DeMarket have several advantages over the CDA design, there is no ''one-size-fits-all'' structure for P2P markets that promises high efficiency while serving all prosumers' needs.Regulations, cultural preferences, and many other dimensions might favour other trading platforms and market designs.For example, neighbouring prosumers may prefer bilateral trading to avoid risk and privacy threats posed in a pool-structured market.In the following, we further discuss the assumptions and limitations of the considered work, along with research lines of future work.

Limitations
One of the limitations of this work arise from not considering the uncertainty related to the prosumer's commitment, as well as the intermittency of RES.Here, we assumed that all prosumers comply and commit to the market results and activation requests.In practice though, there could be violations in commitments due to forecasting errors.Even though the DeMarket is simulated on one hour ahead basis, forecasting error of the upcoming renewable energy is inevitable.Therefore, the prosumers might deviate from their bids.Commitment violations cannot be avoided even in the presence of the smart AI agents controlling all the household devices, as they take more of an advisory role and prosumers are capable of overriding their decisions.Nevertheless, in case of failure, prosumers are able buy/sell the difference from the grid utility normally, since we assume an on-grid system (see Section 6.2.1).Also, by introducing penalties in the smart contracts, unfulfilled prosumers can be eligible for some financial remuneration.
Furthermore, we only consider truthful market participants and agents in the DeMarket with no incentives to misreport their bidding quantities or prices.The general bidding strategy of such participants is to maximize their profits.In a more advanced and complex setting, further investigation can be carried out into the strategic bidding behavior of these participants.Several approaches are found in literature (see [72]); e.g., market clearing price estimation, game theory, and bidding behavior estimation based on historical bidding data.
Another limitation is in the clearing algorithm where the effect of the power flow constraints is not considered.These constraints model the physical permissible energy limits that can be transferred at all times in the grid network lines.This requires information regarding the energy flows for both participating and non-participating prosumers, which is difficult to acquire and rarely addressed literature.In addition, the complexity of level of solving the power flow problem increases drastically as the number of prosumers increase in a given network.

Future work
There are several potential directions for future work.To begin with, the DeMarket was designed to enable P2P trading with the objective of maximizing social welfare.Nevertheless, P2P markets could foster other objectives.For example, to mitigate network congestions and defer reinforcement costs through peak shaving.In this case, within local networks, P2P energy trading platform could unleash more energy flexibility opportunities among prosumers and system operators.
Another line of future work is exploring the scalability of DeTrade, especially the performance of the proposed DACO method.In general, the scalability of peer-to-peer markets and decentralized clearing algorithms are computationally challenging.The proposed DACO method performed well in a 6-player setting, with a near-optimal market solution.While it is common practice to model and simulate P2P markets with small number of players (see [19,22]), other literature that uses larger numbers of players in their simulations present less complex P2P market designs than the one presented here.Indeed, the complexity of the single and continuous products proposed adds computational burden especially with limited computation power.That being said, the DACO's versatility along with the scalable features of the blockchain layer together promise to maintain high efficiency in larger settings, which is planned to be tested in the future steps of this work.

Conclusion
In this paper, we developed a decentralized P2P energy trading platform, called DeTrade.The features of this platform, compared to the state-of-the-art, introduces new opportunities that facilitates the decentralization, decarbonization, and digitalization of energy systems.The DeTrade consists of a market layer and a blockchain layer.The market layer, called DeMarket, consists of a short-term multi-staged multi-period market with a uniform pricing mechanism.This structure can provide new opportunities in the area of P2P energy trading, yet have been overlooked before.Similar to the design of spot markets, prosumers in the DeMarket have wider and well-suited options to trade their energy products, especially those that have inter-temporal dependency.Additionally, we developed a novel decentralized market clearing method called DACO that provides a near-optimal market solution (in terms of maximum social welfare) within limited number of stages.We coupled the DeMarket with a highly secured and automated blockchain layer that ensures fast and real-time settlements.Being fully decentralized, the intermediary transaction costs are eliminated, which allows trading with more attractive prices for the prosumers.We implemented a smart contract in the Solidity programming language and deployed it on a permissioned blockchain using Hyperledger Burrow.The smart contract manages the digital Eurotokens balance, stores and verifies the market clearing results, and ensures that the digital tokens are correctly re-distributed from buyers to sellers.This generates trust among prosumers and eliminates any potential privacy violations.Finally, we simulated the operation of the DeTrade by implementing a case study using real-life data of distribution feeder.We highlighted the performance of the DACO method against a central solver by obtaining a near-optimal social welfare with a small margin of error.

Declaration of competing interest
The authors declare that they have no known competing financial interests or personal relationships that could have appeared to influence the work reported in this paper.

Fig. 3 .
Fig. 3. DeMarket Products: (a) Single product for a seller, (b) Single product for a buyer, (c) Continuous product for a seller, (d) Continuous product for a buyer.

Fig. 5 .
Fig. 5. Agent's 1 perspective in the first stage and first iteration: Ants are moving between agent's 1 space and the solution packets received from agents 2 and 3.

Table 1
The name, scope, and allowed callers of methods in the DeTrade smart contract.A method with a private scope cannot be invoked directly by a transaction.

Table 2
Description of seller and buyer agents.

Table 3
Agents offering single products.

Table 4
DACO's convergence speed and accuracy of solution as a function of number of ants and number of stages.

Table 6
The count, average confirmation times and average gas usage of transactions made by smart agents during a clearing and delivery horizon.Minimum energy quantity for prosumer  for a single market product  for block  (kW) Probability of ant  belonging to agent  in iteration  to move from edge  to  in clearing stage   ,, , Pheromones amounts deposited by ant  belonging to agent  in iteration  on the edge from  to  in clearing stage   ,, , Bidding quantity and costs shared by agent  in iteration  in clearing stage , representing the visibility of edge  to  in clearing stage   ,, , Fitness evaluation for the tour from edge  to  of ant  belonging to agent  in iteration  in clearing stage   , , Pheromones amounts assigned by agent  in their own feasibility space for edge  to  for clearing stage   , , Agent  satisfaction for edge  to  in the best tour achieved in clearing stage   Sub-optimal objective function solution achieved by the DACO   Optimal objective function solution if problem solved centrally  Objective function solution error between  and Minimum energy quantity for prosumer  for a single market product  for block  (kW) , ,,Maximum energy quantity for prosumer  for a single market product  for block  (kW)  Set of all quantity bids of market player  at trading period  for delivery period  (kW) * Upper bound acceptable margin for objective function error