A peer-to-peer network power transaction settlement method and system based on a smart contract
By using Merklegen decomposition based on a power grid oracle and batch processing steps controlled by a processing counter, the problems of single transactions exceeding the gas limit and insufficient security of step-by-step transactions in power trading settlement are solved, thereby improving the reliability and security of power trading.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SHAANXI HYDROPOWER DEVELOPMENT GROUP CO LTD
- Filing Date
- 2026-02-06
- Publication Date
- 2026-06-02
AI Technical Summary
In blockchain-based power trading settlement, the massive amount of delivery data can cause a single transaction to exceed the gas limit. However, adopting step-by-step independent transactions cannot guarantee the atomicity and security of settlement. Existing solutions struggle to strike a balance between process controllability and security against re-entry.
By using the Merkle root provided by the power grid oracle as an on-chain trust anchor, the power delivery details dataset is decomposed into ordered data shard vectors. Then, using a processing counter and a controlled reentrancy process, it is decomposed into multiple batch processing steps. Combined with Merkle proof verification and off-chain log recording, the atomicity and security of settlement are ensured.
It solves the problem of single transactions exceeding the gas limit, realizes the reliability and security of large-scale power transaction settlement, reduces the security threat of reentrancy attacks, and improves processing capacity.
Smart Images

Figure CN121660684B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of transaction settlement, and particularly relates to a method and system for settlement of peer-to-peer power transactions based on smart contracts. Background Technology
[0002] Peer-to-peer (P2P) electricity trading, as a new decentralized energy trading model, allows electricity producers and consumers to trade directly. The core objective of this model is to reduce intermediary costs through a decentralized architecture while ensuring transparency and traceability in the transaction process. Blockchain technology, with its immutable and decentralized trust characteristics, becomes the core support for achieving this goal. By encoding core elements such as trading rules, electricity price agreements, and delivery conditions into smart contracts, it can automate transaction matching, electricity delivery confirmation, and fund settlement, significantly reducing human intervention and trust costs.
[0003] However, electricity transaction settlement requires verification based on detailed electricity delivery data within a given period. This data typically contains a massive number of entries, such as hourly electricity consumption / generation records for hundreds of users in a region. Each record needs to be associated with multiple dimensions of data, including timestamps, electricity consumption values, metering device identifiers, and information on both parties involved in the transaction. If all the original detailed data were directly uploaded to the blockchain and verification, electricity accumulation, and fee calculation were completed in a single smart contract call, it would result in extremely high computational complexity and a high frequency of state changes during contract execution. This could easily exceed the blockchain network's block gas limit, directly causing transaction failure and preventing settlement from being completed.
[0004] To avoid gas limits in a single transaction, existing solutions attempt to break down complex settlement tasks into multiple independent transactions for step-by-step execution. However, this approach introduces the risk of lacking atomicity. Atomicity requires that a sequence of operations either all execute successfully or none execute at all, allowing no intermediate states. In a settlement model with multiple independent transactions, if any transaction fails due to network congestion, node failure, or other reasons, the entire settlement process will be interrupted, resulting in some ledgers being updated while others are not, causing inconsistencies and risks to fund security.
[0005] Furthermore, implementing step-by-step execution logic presents a conflict between security and functionality. Malicious external reentrancy is a dangerous security vulnerability. Traditional solutions often employ a check-enable-interaction model or reentrancy lock mechanism to strictly limit external contracts' callbacks to the original contract, thus preventing state tampering. However, in the complex scenario of power transaction settlement, batch tasks need to be executed sequentially through multiple rounds of calls. Completely prohibiting reentrancy would lead to process interruption; if reentrancy is allowed, it is necessary to prevent malicious external contracts from exploiting callback vulnerabilities to tamper with the settlement state. Existing solutions struggle to strike a balance between process controllability and secure reentrancy prevention. Either excessive restrictions on reentrancy prevent step-by-step settlement, or insufficient reentrancy control creates security vulnerabilities.
[0006] Therefore, how to handle the verification of large-scale off-chain data on-chain while ensuring the atomicity and security of settlement, and how to solve the performance bottleneck of complex on-chain settlement tasks, have become urgent problems to be solved in the current power trading field. Summary of the Invention
[0007] Therefore, the purpose of this invention is to propose a peer-to-peer power trading settlement method and system based on smart contracts, in order to solve the technical problem in blockchain-based power trading settlement where massive amounts of settlement data cause a single transaction to exceed the gas limit, and step-by-step independent transactions cannot guarantee the atomicity and security of settlement.
[0008] The technical solution of the peer-to-peer power trading settlement method based on smart contracts proposed in this invention is as follows:
[0009] The peer-to-peer power trading settlement method based on smart contracts includes the following steps:
[0010] Merkle root, which is a periodic aggregated power delivery proof provided by the grid oracle, is used to obtain the settlement function of the settlement smart contract when the Merkle root is detected to match the transaction commitment hash pre-stored in the settlement smart contract.
[0011] Within the settlement function, the power delivery details dataset to be settled is decomposed into an ordered data fragment vector according to a preset batch processing rule. The total number of fragments contained in the data fragment vector is defined as the processing depth. A processing counter is initialized. When the settlement function is executed for the first time, the initial settlement state is recorded, and the first data fragment is carried in, and its own batch processing function is called.
[0012] Within the batch processing function, the Merkel proof contained in the current data shard and the Merkel root stored in the settlement smart contract are used for verification to check the validity of the current data shard and update the cumulative settlement electricity. It is determined whether the current value of the processing counter is less than the processing depth minus 1. If so, the processing counter is incremented and the next data shard in the data shard vector is used as a parameter to call the batch processing function again, while triggering the corresponding processing log event.
[0013] After the callback process ends, the final cumulative settlement electricity and total cost are obtained based on the storage state of the settlement smart contract. After the verification process has been fully executed, the total cost is transferred to the power seller's contract address in one go according to the final result, and the total ledger status and transaction completion marker of both the power buyer and seller are atomically updated.
[0014] Furthermore, the decomposition method for the power delivery details dataset is as follows:
[0015] Set a processing depth N, where N is a positive integer; decompose the power delivery detail dataset into N data slices, each data slice containing multiple delivery details and their corresponding Merkel proofs, ensuring that these N data slices cover all power delivery detail data to be settled.
[0016] Furthermore, the process of recording the initial settlement state includes:
[0017] In the storage space of the settlement smart contract, a mapping with transaction ID as the key is created. The value corresponding to the mapping is a structure containing the following fields: electricity seller address, electricity buyer address, declared value of total electricity to be settled, Merkle root, a processing counter of type uint256 initialized to 0, and a verified electricity or fee variable for accumulation.
[0018] Furthermore, the process of continuously processing the data fragments in the data fragmentation vector is as follows:
[0019] Within the batch processing function of the settlement smart contract, after completing the verification logic for the current data shard, controlled reentrancy is achieved by verifying the call source and transaction stage through internal logic. The internally stored processing counter associated with the transaction ID is incremented, and the next data shard in the data shard vector indexed by the new counter value is used as a parameter to recursively or iteratively call itself.
[0020] Furthermore, triggering the corresponding processing log event includes:
[0021] Each time a data shard is successfully processed, an on-chain event is triggered, and a data structure containing the transaction ID, the current processing counter value, the electricity and fee of the currently verified shard, and the timestamp of the current block operation is recorded in the blockchain log for off-chain indexing and querying.
[0022] Furthermore, the final cumulative electricity consumption and total cost are obtained as follows:
[0023] After the final step of the callback process is completed, the structure associated with the specified transaction ID is read from the global state storage of the settlement smart contract to obtain the final accumulated verified electricity, and the total fee is calculated accordingly.
[0024] Furthermore, the verification process includes:
[0025] Check if the final processing counter value is equal to the set processing depth N; verify the final cumulative settlement electricity recorded in the settlement smart contract with the initially declared total electricity to be settled to ensure that the value of the final cumulative settlement electricity is equal to the value of the initially declared total electricity to be settled.
[0026] Furthermore, the method for transferring the total cost to the electricity seller's contract address in a single transaction is as follows:
[0027] Call the token transfer function that conforms to the ERC-223 standard, set the amount to the total fee that has been finally verified, set the recipient address to the electricity seller's contract address, and encode the final settlement summary information in the data field.
[0028] The technical solution of the peer-to-peer power trading and settlement system based on smart contracts proposed in this invention is as follows:
[0029] The peer-to-peer power trading and settlement system based on smart contracts includes the following modules:
[0030] The triggering module is used to obtain the Merkle root of the periodic aggregated power delivery proof provided by the power grid oracle. When the Merkle root is detected to match the transaction commitment hash pre-stored in the settlement smart contract, the power seller node triggers the settlement function of the settlement smart contract.
[0031] The decomposition module is used to decompose the power delivery details dataset to be settled into an ordered data fragment vector according to a preset batch processing rule within the settlement function. The total number of fragments contained in the data fragment vector is defined as the processing depth. A processing counter is initialized, and when the settlement function is executed for the first time, the initial settlement state is recorded, and the first data fragment is carried in, and its own batch processing function is called.
[0032] The recording module is used to verify the validity of the current data shard and update the cumulative settlement electricity within the batch processing function by using the Merkle proof contained in the current data shard and the Merkle root stored in the settlement smart contract. It also determines whether the current value of the processing counter is less than the processing depth minus 1. If so, it increments the processing counter and calls the batch processing function again with the next data shard in the data shard vector as a parameter, while triggering the corresponding processing log event.
[0033] The update module is used to obtain the final cumulative settlement electricity and total cost based on the storage state of the settlement smart contract after the callback process ends. After the verification process has been fully executed, the total cost is transferred to the power seller's contract address in one go according to the final result, and the total ledger status and transaction completion mark of both the power buyer and seller are atomically updated.
[0034] Furthermore, the decomposition module decomposes the power delivery details dataset as follows:
[0035] Set a processing depth N, where N is a positive integer; decompose the power delivery detail dataset into N data slices, each data slice containing multiple delivery details and their corresponding Merkel proofs, ensuring that these N data slices cover all power delivery detail data to be settled.
[0036] Furthermore, the process of recording the initial settlement state includes:
[0037] In the storage space of the settlement smart contract, a mapping with transaction ID as the key is created. The value corresponding to the mapping is a structure containing the following fields: electricity seller address, electricity buyer address, declared value of total electricity to be settled, Merkle root, a processing counter of type uint256 initialized to 0, and a verified electricity or fee variable for accumulation.
[0038] Furthermore, the process of continuously processing the data fragments in the data fragmentation vector is as follows:
[0039] Within the batch processing function of the settlement smart contract, after completing the verification logic for the current data shard, controlled reentrancy is achieved by verifying the call source and transaction stage through internal logic. The internally stored processing counter associated with the transaction ID is incremented, and the next data shard in the data shard vector indexed by the new counter value is used as a parameter to recursively or iteratively call itself.
[0040] Furthermore, triggering the corresponding processing log event includes:
[0041] Each time a data shard is successfully processed, an on-chain event is triggered, and a data structure containing the transaction ID, the current processing counter value, the electricity and fee of the currently verified shard, and the timestamp of the current block operation is recorded in the blockchain log for off-chain indexing and querying.
[0042] Furthermore, the final cumulative electricity consumption and total cost are obtained as follows:
[0043] After the final step of the callback process is completed, the structure associated with the specified transaction ID is read from the global state storage of the settlement smart contract to obtain the final accumulated verified electricity, and the total fee is calculated accordingly.
[0044] Furthermore, the verification process includes:
[0045] Check if the final processing counter value is equal to the set processing depth N; verify the final cumulative settlement electricity recorded in the settlement smart contract with the initially declared total electricity to be settled to ensure that the value of the final cumulative settlement electricity is equal to the value of the initially declared total electricity to be settled.
[0046] Furthermore, the method for transferring the total cost to the electricity seller's contract address in a single transaction is as follows:
[0047] Call the token transfer function that conforms to the ERC-223 standard, set the amount to the total fee that has been finally verified, set the recipient address to the electricity seller's contract address, and encode the final settlement summary information in the data field.
[0048] The beneficial effects of this invention are as follows: This invention uses the Merkle root provided by the power grid oracle as an on-chain trust anchor and decomposes the complex settlement verification task based on Merkle proofs into multiple batch processing sub-steps that can be called consecutively through multiple transactions. This resolves the contradiction in existing technologies between single transaction execution exceeding the blockchain gas limit and the inability to guarantee the atomicity of settlement for multiple independent transactions. This invention utilizes ordered data sharding vectors and processing counters to control the controlled reentrancy process, ensuring the determinism and orderliness of the multi-component settlement process. By adopting a strategy of first recording intermediate processing logs, obtaining the final result from the global state storage of the settlement smart contract after all batch processing callbacks are completed, and then performing a one-time atomic fund transfer and ledger update after verifying the processing counter and accumulated electricity, this invention not only guarantees the atomicity and fund security of complex settlement but also reduces the security threat posed by reentrancy attacks by completely isolating the final state change and fund transfer operations from the intermediate data verification steps, thereby improving the reliability, security, and processing capacity of large-scale on-chain electricity transaction settlement. Attached Figure Description
[0049] Figure 1This is a flowchart illustrating the steps of the peer-to-peer power trading settlement method based on smart contracts according to the present invention. Detailed Implementation
[0050] The technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention.
[0051] Specific embodiments of the peer-to-peer power trading settlement method based on smart contracts proposed in this invention are as follows:
[0052] like Figure 1 As shown, the peer-to-peer power trading settlement method based on smart contracts includes the following steps:
[0053] S1. Obtain the Merkle root of the periodic aggregated power delivery proof provided by the power grid oracle. When the Merkle root is detected to match the transaction commitment hash pre-stored in the settlement smart contract, the power seller node triggers the settlement function of the settlement smart contract.
[0054] The purpose of this step is to establish a trust connection between on-chain and off-chain data and to establish the legitimate conditions for initiating settlement. The power grid oracle is an off-chain service that periodically collects smart meter readings, for example, every 15 minutes, and summarizes and calculates all relevant electricity delivery details within a given settlement period. The oracle uses each delivery detail record, including timestamps, electricity amounts, and participating parties, as leaf nodes to construct a Merkle tree, and submits the final Merkle root to the blockchain, publishing it to the settlement smart contract. In a more specific embodiment, the power grid oracle includes at least a data acquisition unit that interfaces with the physical power grid metering system, and a unit that constructs the collected electricity delivery detail data into a Merkle tree and generates the cryptographic proof of the Merkle root. The power grid oracle can be an oracle found in blockchains; the difference is that this oracle is applied within a power blockchain.
[0055] The electricity seller's backend service node continuously monitors the settlement smart contract and reads the newly released Merkle root. Simultaneously, the electricity seller node compares this Merkle root with the transaction commitment hash, which was jointly confirmed by both the buyer and seller and pre-stored in the settlement smart contract before the start of the transaction cycle. The transaction commitment hash is a summary of key terms such as the transaction participants, electricity price, and declared total delivery volume. A successful comparison proves that the authoritative summary of the on-chain data to be settled is consistent with the off-chain agreement. The electricity seller node then calls the settlement function of the settlement smart contract, passing in the corresponding transaction ID, and initiates the entire settlement process. This step prevents malicious nodes from arbitrarily initiating settlements or using tampered data to initiate settlements, ensuring that the data source of the settlement process is consensus-driven and authentic.
[0056] S2, within the settlement function, the power delivery details dataset to be settled is decomposed into an ordered data fragment vector according to the preset batch processing rules. The total number of fragments contained in the data fragment vector is defined as the processing depth. A processing counter is initialized. When the settlement function is executed for the first time, the initial settlement state is recorded, and the first data fragment is carried in, and its own batch processing function is called.
[0057] The purpose of this step is to break through the gas limit of a single blockchain block by decomposing large-scale computational tasks. Specifically, a processing depth of N is set, where N is a positive integer; the electricity delivery details dataset is decomposed into N data shards, each containing multiple delivery details and their corresponding Merkel proofs, ensuring that these N data shards cover all unsettled electricity delivery details data.
[0058] For example, if there are 1000 records in the electricity delivery details to be settled, the electricity seller node, according to a preset batch processing rule (e.g., processing 100 records per batch), decomposes the 1000 records along with their respective Merkel proofs into an ordered vector containing 10 data shards. The processing depth N is then 10. Each data shard contains the corresponding number of delivery details and their Merkel proofs, and all data shards cover the 1000 electricity delivery detail data without overlap.
[0059] It should be noted that in actual calculations, the power delivery details in each data shard are not necessarily equal. For example, if there are 1003 records in the power delivery details to be settled, perform integer division and remainder operations on them. The base shard size is the quotient of 1003 divided by 10, which is 100 records, with a remainder of 3. To distribute the remainder evenly, the size of the first 3 data shards is set to 101 records, and the size of the remaining 7 shards remains 100 records.
[0060] The electricity seller node calls the `startSettlement` function of the settlement smart contract, passing in the transaction ID, the Merkle root previously published by the oracle, and the declared total electricity to be settled. The settlement smart contract records the initial settlement state upon its first execution of this function. In the settlement smart contract's storage space, a mapping with the transaction ID as the key is created. The value corresponding to this mapping is a structure containing the following fields: seller address, buyer address, declared total electricity to be settled, Merkle root, a `uint256` type processing counter initialized to 0, and a variable for accumulating verified electricity or fees. Simultaneously, a processing counter is initialized to 0, and an accumulated verified electricity is set to 0. To process the first data shard, the electricity seller node calls the `processBatch` function of the settlement smart contract, passing in the first data shard—the first 100 settlement details—and its Merkle proof as parameters.
[0061] This step enables the settlement smart contract to process massive amounts of electricity transaction data without causing transaction failures due to excessive computational load, while laying the foundation for subsequent orderly execution.
[0062] S3, within the batch processing function, the Merkle proof contained in the current data shard and the Merkle root stored in the settlement smart contract are used for verification to check the validity of the current data shard and update the cumulative settlement electricity. It is determined whether the current value of the processing counter is less than the processing depth minus 1. If so, the processing counter is incremented and the next data shard in the data shard vector is used as a parameter to call the batch processing function again, while triggering the corresponding processing log event.
[0063] After receiving the first data shard, the `processBatch` function of the settlement smart contract performs the following operations: Iterates through each settlement detail in the data shard, verifying it using its accompanying Merkle proof and the Merkle root stored in the smart contract to ensure that all data has not been tampered with and belongs to this settlement. If the verification passes, the electricity amounts of all details in that data shard are accumulated, and the result is added to the accumulated verified electricity status variable associated with that transaction ID in the settlement smart contract.
[0064] Each time the processBatch function successfully processes a data shard, it triggers an on-chain event using the Solidity emit keyword. This event records a data structure containing the transaction ID, the current processing counter value, the electricity and fees of the currently verified shard, and the timestamp of the block being operated on, into the blockchain log for off-chain indexing and querying.
[0065] Within the batch processing function of the settlement smart contract, after completing the verification logic for the current data shard, controlled reentrancy is achieved by verifying the call source and transaction stage through internal logic. The internally stored processing counter associated with the transaction ID is incremented, and the next shard in the data shard vector indexed by the new counter value is used as a parameter to recursively or iteratively call itself.
[0066] After the first transaction calling the `processBatch` function succeeds, the electricity seller node, for example by confirming through log events, prepares the second data shard, containing details and proofs for records 101-200, and initiates the second transaction, calling the `processBatch` function again. Inside the `processBatch` function, security checks are performed first, such as checking if `msg.sender` matches the electricity seller address corresponding to the transaction ID in the record, and checking the current processing counter value to prevent out-of-order or duplicate submissions. After successful checks, the smart contract processes the data and increments the internally stored processing counter associated with the transaction ID. This call-confirmation-re-call loop is driven by off-chain nodes until all data shards have been processed.
[0067] Of course, in other embodiments, after the settlement smart contract completes the verification logic of the current data shard and triggers the processing log event, the current transaction ends; the electricity seller node listens to the processing log event to confirm that the current data shard has been successfully processed, and constructs a new transaction containing the next data shard indexed by the incremented processing counter in the data shard vector; the electricity seller node initiates the new transaction to call the batch processing function of the settlement smart contract again, repeating the above process until the processing counter reaches the set processing depth. Specifically, after the processBatch function is executed, the corresponding transaction ends. After the off-chain node of the electricity seller listens to the event, it confirms that the first batch has been successfully processed, constructs a new transaction, calls the processBatch function again, and passes in the second data shard. This process is repeated until all data shards have been processed.
[0068] S4. After the callback process ends, the final cumulative settlement electricity and total cost are obtained based on the storage state of the settlement smart contract. After the verification process has been fully executed, the total cost is transferred to the power seller's contract address in one go according to the final result, and the total ledger status and transaction completion mark of both the power buyer and seller are atomically updated.
[0069] This step ensures the atomicity of the transaction, meaning either the entire transaction succeeds or no funds are transferred. It eliminates the risk of unbalanced ledgers due to partial success or failure that can occur in traditional step-by-step transactions. Even if a data shard fails to verify at some point, both parties' funds remain safe because the fund transfer step is not triggered, guaranteeing financial-grade transaction security.
[0070] After the final step of the callback process completes, the structure associated with the specified transaction ID is read from the global state storage of the settlement smart contract to obtain the final accumulated verified electricity, and the total fee is calculated accordingly. Specifically, during the execution of the `finalizeSettlement` function, which is the final settlement function, when the final accumulated settlement electricity needs to be obtained, the settlement smart contract reads the `verifiedAmount` variable from the structure associated with the transaction ID in its state storage. Because this variable has been atomically updated in each previous `processBatch` call, its value is the final accumulated verified electricity after all batches are processed.
[0071] The verification process is as follows: First, check if the final processing counter value is equal to the set processing depth N. Second, verify the final cumulative settlement electricity recorded in the settlement smart contract against the initially declared total electricity to be settled, ensuring that the final cumulative settlement electricity value is equal to the initially declared total electricity to be settled. Specifically, in the `finalizeSettlement` function, the `processedCount` (processing counter state variable) associated with the transaction ID is read, and its value is checked to be strictly equal to the preset processing depth N, where N is 10. Then, the `verifiedAmount` (final cumulative verified electricity state variable) is read and compared with the `declaredTotalAmount` (declared total electricity) stored when the transaction ID was initially recorded. Verification is only successful when both `processedCount == 10` and `verifiedAmount == declaredTotalAmount` are true, and the subsequent payment process will then proceed.
[0072] The token transfer function conforming to the ERC-223 standard is invoked, setting the amount to the final verified total fee, the recipient address to the electricity retailer's contract address, and encoding the final settlement summary information in the data field. Compared to ERC-20, the ERC-223 standard handles contract-to-contract transfers better, preventing token lock-up. By using the settlement summary carried in the ERC-223 standard's unique data field, the electricity retailer's contract knows which transaction the money corresponds to upon receiving the tokens, achieving synchronous flow of payment and business information without additional communication overhead.
[0073] Specifically, after verification, the settlement smart contract calculates the total cost based on the pre-stored electricity price. For example, if the total electricity to be settled is 10,000 kWh and the unit price is 1 yuan / kWh, the total cost is 10,000 kWh × 1 yuan / kWh = 10,000 yuan. The token contract's transfer function is then called to transfer the total cost of 10,000 yuan to the electricity seller's contract address in one go. The internal ledger status is updated, marking the transaction ID as completed. The entire final settlement and payment process is completed within the same blockchain transaction, ensuring the atomicity of the operation.
[0074] Specific embodiments of the peer-to-peer power trading and settlement system based on smart contracts proposed in this invention are as follows:
[0075] The peer-to-peer power trading and settlement system based on smart contracts includes the following modules:
[0076] The triggering module is used to obtain the Merkle root of the periodic aggregated power delivery proof provided by the power grid oracle. When the Merkle root is detected to match the transaction commitment hash pre-stored in the settlement smart contract, the power seller node triggers the settlement function of the settlement smart contract.
[0077] The decomposition module is used to decompose the power delivery details dataset to be settled into an ordered data fragment vector according to a preset batch processing rule within the settlement function. The total number of fragments contained in the data fragment vector is defined as the processing depth. A processing counter is initialized, and when the settlement function is executed for the first time, the initial settlement state is recorded, and the first data fragment is carried in, and its own batch processing function is called.
[0078] The recording module is used to verify the validity of the current data shard and update the cumulative settlement electricity within the batch processing function by using the Merkle proof contained in the current data shard and the Merkle root stored in the settlement smart contract. It also determines whether the current value of the processing counter is less than the processing depth minus 1. If so, it increments the processing counter and calls the batch processing function again with the next data shard in the data shard vector as a parameter, while triggering the corresponding processing log event.
[0079] The update module is used to obtain the final cumulative settlement electricity and total cost based on the storage state of the settlement smart contract after the callback process ends. After the verification process has been fully executed, the total cost is transferred to the power seller's contract address in one go according to the final result, and the total ledger status and transaction completion mark of both the power buyer and seller are atomically updated.
[0080] It should be noted that the processing procedures of the triggering module, decomposition module, recording module, and update module are the same as those of steps S1, S2, S3, and S4 in the various embodiments of the smart contract-based peer-to-peer power trading settlement method described above, and will not be elaborated further here.
[0081] While various embodiments of the invention have been shown and described in this specification, it will be apparent to those skilled in the art that such embodiments are provided by way of example only. Many modifications, alterations, and alternatives will occur to those skilled in the art without departing from the spirit and essence of the invention.
Claims
1. A peer-to-peer network electricity transaction settlement method based on a smart contract, characterized in that, The method comprises the following steps: obtaining a Merkle root of a periodic aggregated power delivery proof provided by a power grid oracle, and triggering a settlement function of a settlement smart contract by a power seller node when it is monitored that the Merkle root matches a transaction commitment hash pre-stored in the settlement smart contract; in the settlement function, the power delivery detail data set to be settled is decomposed into an ordered data shard vector according to a preset batch processing rule, the total number of data shards contained in the data shard vector is defined as the processing depth, a processing counter is initialized, the initial settlement state is recorded when the settlement function is executed for the first time, and the first data shard is carried to call the batch processing function of itself; in the batch processing function, the Merkle proof contained in the current data shard and the Merkle root stored in the settlement smart contract are used for verification to verify the validity of the current data shard and update the cumulative settlement power, and it is judged whether the current value of the processing counter is less than the processing depth minus 1, if yes, the processing counter is incremented, the next data shard in the data shard vector is taken as a parameter, and the batch processing function is called again, and a corresponding processing log event is triggered; after the callback process is completed, the final cumulative settlement power and total cost are obtained based on the storage state of the settlement smart contract, after it is verified that the processing flow has been completely executed, the total cost is transferred to the power seller contract address at one time according to the final result, and the total ledger state and transaction completion flag of the power buyer and seller are atomically updated; the process of continuously processing the data shards in the data shard vector is that, after the verification logic of the current data shard is completed in the batch processing function of the settlement smart contract, the controlled re-entry is realized through internal logic verification and transaction stage, the processing counter associated with the transaction ID stored in the internal storage is incremented, the next data shard in the data shard vector indexed by the new counter value is taken as a parameter, and the batch processing function is recursively or iteratively called; the verification processing flow comprises: checking whether the final processing counter value is equal to the set processing depth N; 2.The smart contract-based peer-to-peer network power transaction settlement method of claim 1, wherein, verifying the final cumulative settlement power recorded in the settlement smart contract with the initial declared total power to be settled to ensure that the value of the final cumulative settlement power is equal to the value of the initial declared total power to be settled. The decomposition method of the power delivery detail data set is: 3.The smart contract-based peer-to-peer network power transaction settlement method of claim 2, wherein, setting the processing depth N, N is a positive integer; the power delivery detail data set is decomposed into N data shards, each data shard containing multiple delivery details and their corresponding Merkle proofs, ensuring that the N data shards cover all the power delivery detail data to be settled. The process of recording the initial settlement state comprises: 4.The smart contract-based peer-to-peer network power transaction settlement method of claim 1, wherein, in the storage space of the settlement smart contract, a mapping with a transaction ID as a key value is created, the value corresponding to the mapping is a structure, and the structure comprises the following fields: a power seller address, a power buyer address, a declared value of total power to be settled, a Merkle root, and a processing counter of uint256 type initialized to 0 and a verified power or fee variable for accumulation. The corresponding processing log event is triggered, which comprises: At each successful processing of a data slice, a data structure containing the transaction ID, the value of the current processing counter, the current slice's verified electricity and fee, and the current block's timestamp is recorded into the blockchain's log by triggering an on-chain event, for off-chain indexing and querying. 5.The smart contract-based peer-to-peer network power transaction settlement method of claim 4, wherein, The final cumulative settlement electricity and total fee are obtained in the following way: After the last step of the callback process is executed, the structure associated with the specified transaction ID is read from the global state storage of the settlement smart contract to obtain the final cumulative verified electricity, and the total fee is calculated accordingly. 6.The smart contract-based peer-to-peer network power transaction settlement method of claim 5, wherein, The method of transferring the total fee to the electricity seller's contract address in one go is as follows: The token transfer function conforming to the ERC-223 standard is called, the amount is set to the final verified total fee, the recipient address is set to the electricity seller's contract address, and the final settlement summary information is encoded in the data field.
7. The system applied to the peer-to-peer network power transaction settlement method based on the smart contract according to any one of claims 1-6, characterized in that, The following modules are included: A triggering module is used to obtain the periodic aggregated power delivery proof's Merkle root provided by the grid oracle, and when the Merkle root matches the pre-stored transaction commitment hash in the settlement smart contract, the settlement function of the settlement smart contract is triggered by the electricity seller node; A decomposition module is used to decompose the power delivery detail dataset to be settled into an ordered data slice vector according to the preset batch processing rule within the settlement function, and the total number of slices included in the data slice vector is defined as the processing depth; a processing counter is initialized, and the initial settlement state is recorded when the settlement function is first executed, and the first data slice is carried to call the batch processing function of itself; A recording module is used to verify the validity of the current data slice and update the cumulative settlement electricity by using the Merkle proof contained in the current data slice and the Merkle root stored in the settlement smart contract within the batch processing function, and to determine whether the current value of the processing counter is less than the processing depth minus 1, if so, the processing counter is incremented and the next data slice in the data slice vector is used as a parameter to call the batch processing function again, and a corresponding processing log event is triggered; An updating module is used to obtain the final cumulative settlement electricity and total fee based on the storage state of the settlement smart contract after the callback process is completed, and to transfer the total fee to the electricity seller's contract address in one go and atomically update the total ledger state and transaction completion flag of the electricity buyer and seller based on the final result. 8.The smart contract-based peer-to-peer network power transaction settlement system of claim 7, wherein, The decomposition method of the decomposition module for the power delivery detail dataset is as follows: Set the processing depth N, N is a positive integer; the power delivery detail dataset is decomposed into N data slices, each data slice contains multiple delivery details and their corresponding Merkle proofs, ensuring that the N data slices cover all the power delivery detail data to be settled.