Implementation method of fine-grained transaction partial rollback mechanism for stack type EVM (Error Vector Magnitude)
By using the persistent stack structure pStack in smart contract transactions, fine-grained division and operation of the stack are realized, which solves the high overhead problem caused by read-after-write conflicts in smart contract transactions, and improves the efficiency of transaction execution and resource utilization.
Patent Information
- Application Number
- CN202410899429.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-05
- Publication Date
- 2024-10-15
AI Technical Summary
During the concurrent execution of smart contract transactions, transaction rollback caused by read-write conflicts requires re-executing all operations, resulting in high overhead. The existing fine-grained rollback mechanism has limited efficiency, especially in a stacked EVM environment. Restoring the previous stack state is a challenge.
Adopt pStack, a persistent stack structure based on the stack's native stack, to simulate a standard stack, support fine-grained stack operation primitives, divide the stack into memory segments, allow multiple snapshots to share unmodified memory segments, realize partial rollback of transactions, and only restart Perform operations affected by the conflict.
It reduces the rollback and re-execution overhead caused by read-after-write conflicts in smart contract transactions, improves execution efficiency, saves computing resources, and retains non-conflicted computing logic and reading and writing.
Smart Images

Figure CN118780796A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of blockchain, and in particular to a method for implementing a fine-grained transaction partial rollback mechanism for a stack-type EVM. Background Art
[0002] Blockchain is a distributed digital ledger with the characteristics of decentralization, data immutability, and traceability. In order to expand the application scope of the blockchain system and enable it to handle logical operations with richer semantics than simple transfers, Ethereum first implemented the concept of smart contracts. On the Ethereum platform, a smart contract is an automatically executed program. Users can write contracts in the quasi-Turing-complete high-level language Solidity and use the Solidity compiler to compile the contract code into bytecode. Users can then deploy the bytecode to the contract account on the chain, and other accounts call the contract in the form of transactions.
[0003] However, during the concurrent execution of smart contract transactions, when a read-after-write conflict occurs, the aborted transaction will trigger the revocation process, rolling back its execution context and corresponding state modifications. Existing transaction execution schemes usually roll back the transaction status to the initial state before the transaction execution, and then reallocate the aborted transaction to an idle worker thread for re-execution. Although this transaction-granularity-based rollback scheme can obviously ensure the correctness of execution, if a transaction faces a conflict that only affects a small part of its execution, all operations need to be re-executed, which brings high overhead. In addition, compared with ordinary transfer transactions, smart contract transactions are more complex, involving business code logic and a stack-based instruction execution environment. Therefore, the use of coarse-grained transaction rollback will not only cause a significant waste of CPU computing resources, but also bring expensive re-execution overhead.
[0004] Ethereum Virtual Machine (EVM) is the mainstream standard for smart contract execution environment, and most execution environments are EVM compatible. Each EVM instance contains a program counter (PC), a runtime stack (Stack), and a temporary table structure (Memory); the code logic written in the contract will be compiled into EVM bytecode containing instructions to be executed, and EVM manipulates the runtime stack by executing the instructions in the bytecode pointed to by PC. When implementing a fine-grained transaction partial rollback mechanism, both the program counter and the table structure can be easily stored and restored. However, how to efficiently restore the previous stack state is a challenging problem. The existing fine-grained transaction rollback mechanism introduces a register-based method to convert stack-based EVM instructions into register-based intermediate representations. However, this method requires a complete execution of the stack-based runtime environment once before instruction conversion can be performed, and it cannot handle unconverted branch logic, so its usage scenarios are limited.
[0005] Compared with the solution of converting stack instructions into intermediate representation, stack-based native rollback support is a more general solution. However, stack-based virtual machines manage operands in the stack without explicitly knowing the addresses of the operands. In order to support a partial rollback mechanism, it is necessary to record all stack states that need to be rolled back in the future. A preliminary solution is to take a complete snapshot of all elements in the stack, copy them and store them when a checkpoint needs to be created. However, this method is extremely inefficient and brings significant memory consumption and maintenance overhead. Summary of the invention
[0006] The purpose of the present invention is to provide a method for implementing a fine-grained transaction partial rollback mechanism for stack-based EVM, so that smart contract transactions that are aborted due to read-after-write conflicts only need to be rolled back to a state recorded before the conflict, rather than rolling back to the initial state before the transaction was executed. In order to support the partial rollback of aborted transactions to a specific snapshot, the mechanism adopts a stack-native rollback solution, which simulates the standard EVM stack by designing a persistent stack structure pStack, and supports general stack operation primitives. pStack divides the stack area into fine-grained sections, so that multiple snapshots can share unmodified memory segments instead of copying the entire stack, thereby reducing the memory consumption and maintenance overhead required for snapshot recording. Using the stack snapshots recorded by the pStack structure, the fine-grained transaction partial rollback mechanism can retain non-conflicting computing logic and read-write, saving a lot of computing resources, thereby effectively improving execution efficiency.
[0007] The specific technical solution for achieving the purpose of the present invention is:
[0008] A method for implementing a fine-grained transaction partial rollback mechanism for stack-based EVM, using a persistent stack structure pStack, to implement a fine-grained transaction partial rollback mechanism in an EVM smart contract execution environment based on a stack-based virtual machine; wherein,
[0009] The pStack is a structure based on a persistent stack, which is used to record the states of all stacks that may be rolled back in the future. The structure simulates a standard stack, supports general stack operation primitives, and can effectively restore and operate snapshots of multiple stacks; the structure divides the stack into several memory segments in a fine-grained manner, each memory segment records m consecutive elements in the stack, so that multiple snapshots can share unmodified memory segments without copying the entire stack; the structure supports accessing all stack elements using indexes, and tracking previously recorded stack snapshots; the structure uses stack frames to identify stack snapshots, and the pStack stack frame consists of three parts: a pointer list pointing to the memory segment it maintains; a variable that records the stack height and a Boolean reference tag list to track whether the current pStack stack frame shares a memory segment with other pStack stack frames;
[0010] The fine-grained transaction partial rollback mechanism is a conflict management solution during the concurrent execution of transactions. When a transaction T encounters a read-after-write conflict and needs to be aborted, the mechanism uses the stack snapshot recorded by the pStack structure to roll back the transaction T to a state recorded before the conflict, rather than rolling back to the initial state before the transaction was executed; at the same time, the mechanism allows the aborted transaction to only re-execute the operations affected by the conflict, while other executed logic and read-write operations that are not related to the conflict will be retained and reused by the next re-execution, thereby greatly saving the re-execution overhead of transaction T.
[0011] The present invention combines the pStack structure with the EVM standard to implement a fine-grained transaction partial rollback mechanism, including the following steps:
[0012] Step A1: Check in the EVM execution environment whether transaction T reads state data outside the smart contract execution environment; if it reads external state, create a checkpoint, copy the stack height and pointer list of the previous stack frame to fork and build a new pStack stack frame;
[0013] Step A2: Execute transaction T; according to the logical operation in transaction T, update the pointer list of the current pStack stack frame and the memory segments involved in the operation;
[0014] Step A3: Detect read-after-write conflicts of smart contract transactions; when smart contract transaction T is terminated due to a conflict, roll back transaction T to the pStack stack frame state recorded in the most recent checkpoint before the conflict, and delete all stack frames created after this pStack stack frame as well as read dependencies and write version information that are invalid due to the conflict;
[0015] Step A4: Re-execute the transaction in the execution context of the pStack frame recorded in the most recent checkpoint rolled back to, and insert the local write-set version of transaction T newly written after the most recent abort into the shared storage during the tentative commit;
[0016] Further, the step A1 comprises the following steps:
[0017] Step A11: Check in the EVM execution environment whether the state to be read by the smart contract transaction T exists in the local read-write set of transaction T;
[0018] Step A12: If the state does not exist, create a new checkpoint;
[0019] Step A13: Fork a new pStack stack frame to capture the current EVM stack state snapshot and return a globally unique snapshot identification number;
[0020] Step A14: Build and initialize the new stack frame, copy the current stack height and the pointer list pointing to the pStack stack frame before the new stack frame, and mark all memory segments with reference marks with a value of true, indicating that these segments are in a shared state.
[0021] Further, the step A2 comprises the following steps:
[0022] Step A21: Execute the logical operation in transaction T, and find the target memory segment to be updated of the element involved in the operation through the element index on the current pStack frame;
[0023] Step A22: If the reference flag of the target memory segment to be updated is false, the value of the element will be directly written into the corresponding position of the target segment of the current pStack stack frame;
[0024] Step A23: If the reference mark of the target memory segment to be updated is true, the target segment is cloned to the newly allocated memory segment of the current pStack stack frame, and the value of the element is written into the new segment; after that, the reference mark of the segment is set to false, indicating that the new segment is owned solely by the current pStack stack frame.
[0025] Further, the step A3 comprises the following steps:
[0026] Step A31: When a read-after-write conflict occurs in a transaction, the conflicting key that causes the transaction to be aborted is recorded in the conflicting key set of the aborted transaction;
[0027] Step A32: When transaction T detects that it is aborted, it atomically obtains the conflicting key from the conflicting key set and finds the conflicting key that occurs earliest in the running logic;
[0028] Step A32: Transaction T restores the state of the EVM stack to the corresponding pStack stack frame through the snapshot identification number, and removes all stack frames created after the snapshot identification number; if the reference mark in any stack frame is false, the memory allocation of the segment is reclaimed;
[0029] Step A34: Transaction T cleans up the read dependencies and write versions affected by the read-after-write conflict.
[0030] Further, the step A4 comprises the following steps:
[0031] Step A41: Transaction T is restored to the previous execution state and re-executed according to the execution context information recorded in the pStack stack frame corresponding to the most recent checkpoint;
[0032] Step A42: Since the partial rollback has preserved the non-conflicting read and write states, during the tentative commit, only the version of transaction T newly written to the local write set after the most recent abort needs to be inserted into the shared storage.
[0033] The beneficial effects of the present invention include:
[0034] The present invention aims to reduce the high rollback and re-execution overhead caused by read-after-write conflicts in smart contract transactions, and focuses on developing an implementation method for a fine-grained transaction partial rollback mechanism for stack-based EVM. The present invention fully considers the stack-based, big-endian alignment characteristics of EVM, and uses a persistent stack structure pStack to achieve efficient fine-grained transaction partial rollback in the EVM smart contract execution environment. Specifically, the present invention simulates a standard stack through pStack and supports general stack operation primitives. pStack divides the stack into several memory segments, each of which records m consecutive elements in the stack. Such fine-grained division allows multiple snapshots to share unmodified memory segments instead of copying the entire stack to achieve efficient snapshot recording and management. At the same time, pStack can use indexes to access all stack elements and track previously recorded stack snapshots. Based on this structure, aborted transactions can be partially rolled back instead of rolling back to the initial state before the transaction was executed. Compared with the complete rollback solution, the transaction rollback at the instruction level adopted in the present invention can retain the non-conflicting computing logic and reading and writing, save a lot of computing resources, and thus effectively improve the execution efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Figure 1 This is an example diagram of a read-after-write conflict in a smart contract transaction proposed by the present invention;
[0036] Figure 2 This is a comparative example diagram of the fine-grained transaction partial rollback and the transaction full rollback proposed by the present invention;
[0037] Figure 3 It is a schematic diagram of the implementation of the fine-grained transaction partial rollback mechanism based on pStack proposed in the present invention. DETAILED DESCRIPTION
[0038] The present invention is further described in detail with reference to the following specific examples and drawings. The process, conditions, experimental methods, etc. for implementing the present invention, except for the contents specifically mentioned below, are all common knowledge and common common sense in the art and are not particularly limited by the present invention.
[0039] The present invention discloses a method for implementing a fine-grained transaction partial rollback mechanism for stack-based EVM, aiming to reduce the high rollback and re-execution overhead caused by read-after-write conflicts in smart contract transactions. This mechanism proposes a stack-native rollback method for the mainstream standard EVM in the current smart contract execution environment. By designing a persistent stack structure pStack, the stack area is finely divided so that multiple snapshots can share part of the stack state (unmodified memory segments), thereby efficiently restoring and operating snapshots of multiple stacks. Based on this structure, transactions that are aborted due to read-after-write conflicts can be partially rolled back, and only the operations affected by the conflict need to be re-executed. The present invention adopts instruction-level transaction rollback to retain non-conflicting computing logic and read-write, saving a large amount of computing resources, and effectively improving execution efficiency.
[0040] Figure 1 An example of a read-after-write conflict in a smart contract transaction proposed by the present invention is shown. In Example 1, since the read of state a during the execution of transaction T2 omitted the write of transaction T1, T1 detected a read-after-write conflict between T1 and T2 during the tentative submission phase and terminated the execution of T2. In Example 2, transaction T4 is executed before T3, reads state b and writes it to the transaction local storage, and transaction T3 also needs to write to state b. At the same time, after T4 is tentatively submitted, T5 reads T4's write of state c. After that, T3 finds that there is a read-after-write conflict with T4, so it terminates T4, and causes T4 to cascade terminate T5.
[0041] Figure 2 A comparative example of the fine-grained partial transaction rollback and the full transaction rollback proposed by the present invention is shown. Full transaction rollback will restore the transaction status to the initial state before the transaction execution, after which the terminated transaction will be reallocated to an idle worker thread for re-execution. Although the adoption of this transaction granularity-based rollback scheme can obviously ensure correctness, if a transaction faces a conflict that only affects a small part of its execution, all operations need to be re-executed, which brings higher overhead. In addition, compared with ordinary transfer transactions, smart contract transactions are more complex, involving business code logic and a stack-based instruction execution environment. Still using coarse-grained transaction rollbacks will not only cause a significant waste of CPU computing resources, but also bring expensive re-execution overhead.
[0042] In contrast, fine-grained transaction partial rollback allows the aborted transaction to re-execute only the operations affected by the conflict. At the same time, other executed logic and reads and writes that are not related to the conflict will be retained and reused by the next re-execution, thus greatly saving the re-execution overhead. The insight of fine-grained transaction partial rollback is that the transaction partial order relationship implied by the non-conflicting logic meets the established serialization order requirements, so retaining these partial order relationships helps to accelerate the execution and align it with the established order, thereby achieving strict determinism. In general, in fine-grained transaction partial rollback, when a transaction T encounters a read-after-write conflict and needs to be aborted, T only needs to roll back to a state recorded before the conflict, rather than rolling back to the initial state before the transaction was executed. During the re-execution process, T can continue to execute based on the execution context recorded by the marked state, and continue to read or write the new version of the state until the execution is completed.
[0043] Figure 3 The implementation method of the fine-grained transaction partial rollback mechanism based on pStack proposed in the present invention is shown. pStack can effectively restore and operate snapshots of multiple stacks. Conceptually, pStack simulates a standard stack and supports general stack operation primitives. At the same time, pStack can access all stack elements using indexes and track previously recorded stack snapshots. Overall, pStack divides the stack into several memory segments, each of which records m consecutive elements in the stack. Such fine-grained division allows multiple snapshots to share partial stack states (unmodified segments) instead of copying the entire stack, so as to achieve efficient snapshot recording and management.
[0044] A stack snapshot is identified by a stack frame, which consists of three parts: a pointer list pointing to the memory segment it maintains; a variable that records the stack height and a Boolean reference tag list to track whether the current stack frame shares the memory segment with the previous stack frame, implementing a write-time copy mechanism at the segment granularity.
[0045] In order to combine pStack with the EVM standard, the present invention replaces the runtime stack in EVM with pStack, and also makes the table structure (Memory) multi-versioned. The specific process is as follows:
[0046] In step ①, check whether transaction T reads state data outside the smart contract execution environment; if it needs to read external state, create a checkpoint and fork to build a new pStack stack frame. In step ②, execute transaction T; according to the logical operation in transaction T, update the pointer list of the current pStack stack frame and the memory segments involved in the operation. In step ③, detect the read-after-write conflict of the smart contract transaction; when the smart contract transaction T is terminated due to a conflict, roll back transaction T to the pStack stack frame state recorded in the most recent checkpoint before the conflict, and delete all stack frames created after this pStack stack frame and the read dependency and write version information that are invalid due to the conflict. In step ④, re-execute the transaction from the execution context of the pStack stack frame recorded in the most recent checkpoint rolled back, and insert the local write set version of transaction T newly written after the latest abort into the shared storage when tentatively committing.
[0047] pStack supports the following three operation primitives:
[0048] ·write(elementIndex,value): This primitive updates the current stack frame while retaining the previous data. First, the target segment of the element to be updated is found on the stack frame by the element index (elementIndex). If the reference mark of the segment is false, the value of the element (value) will be written directly to the corresponding position of the target segment. Otherwise, the primitive will clone the target segment to the newly allocated memory segment and write the value of the element to the new segment. After that, the reference mark of the segment is set to false, indicating that the new segment is owned by a separate stack frame. Standard stack operations, such as pushing and popping elements, can be implemented by this primitive.
[0049] fork(): This primitive creates a new stack frame as a snapshot of the current stack and returns a globally unique snapshot identifier. When a new stack frame is created, the primitive copies the current stack height and the list of pointers to the previous stack frame, and marks all segments with a reference flag with a value of true, indicating that these segments are in a shared state.
[0050] rollback(snapshotID): This primitive restores the stack state to the stack frame identified by snapshotID. This operation is done by simply removing all stack frames created after snapshotID and reclaiming the memory allocation of the corresponding segment if any reference in the stack frame is marked as false.
[0051] Algorithm 1 describes in detail how to integrate the implementation of partial rollback into tentative transaction execution: Lines 1 to 11 describe additional EVM primitives used to support partial rollback, including creating checkpoints, rolling back to a checkpoint, and transaction primitives used to find conflict locations. In the design of the present invention, a transaction will create a checkpoint before reading the external state. Since the EVM reads state data through the SLOAD instruction, transaction T calls the EVM makeCheckpoint(·) primitive to create a checkpoint before executing each SLOAD instruction (and the state to be read is not in the transaction local read-write set). When a read-after-write conflict occurs, the conflict key (the key that causes the transaction to be aborted) will be recorded in the conflict key set (conflictKeySet) of the aborted transaction. If transaction T detects that it is aborted, transaction T calls the localAbort(·) primitive to trigger the rollback process. Transaction T first atomically obtains the conflict key from the conflict key set and finds the conflict key that occurs earliest in the running logic. Then, transaction T calls revertStateTo(·) to partially roll back to the most recent checkpoint before the conflict (lines 12 to 15). After that, transaction T needs to clean up the read dependencies and write versions affected by the conflict (lines 16 to 22). It should be noted that after the rollback is completed, transaction T can be re-executed according to the execution context recorded in the checkpoint to which it was rolled back. Since the partial rollback has retained the non-conflicting state reads and writes, when tentatively committing, it is only necessary to insert into the shared storage the version of transaction T that was newly written to the local write set after the most recent abort (lines 23 to 27). Compared with the full rollback solution, the use of instruction-level transaction rollback can retain non-conflicting computing logic and reads and writes, saving a lot of computing resources, thereby effectively improving execution efficiency.
[0052]
[0053] The protection content of the present invention is not limited to the above embodiments. Without departing from the spirit and scope of the inventive concept, changes and advantages that can be thought of by those skilled in the art are included in the present invention and are protected by the attached claims.
Claims
1. A method for implementing a fine-grained transaction partial rollback mechanism for stacked EVM, characterized in that: Using a persistent stack structure pStack, a fine-grained transaction partial rollback mechanism is implemented in the EVM smart contract execution environment based on the stack virtual machine. The specific steps include: Step A1: Check in the EVM execution environment whether transaction T reads state data outside the smart contract execution environment; if it reads external state, create a checkpoint, copy the stack height and pointer list of the previous stack frame to fork and build a new pStack stack frame; Step A2: Execute transaction T; according to the logical operation in transaction T, update the pointer list of the current pStack stack frame and the memory segments involved in the operation; Step A3: Detect read-after-write conflicts of smart contract transactions; when smart contract transaction T is terminated due to a conflict, roll back transaction T to the pStack stack frame state recorded in the most recent checkpoint before the conflict, and delete all stack frames created after this pStack stack frame as well as read dependencies and write version information that are invalid due to the conflict; Step A4: Re-execute the transaction in the execution context of the pStack frame recorded in the most recent checkpoint rolled back to, and insert the local write-set version of transaction T newly written after the most recent abort into the shared storage during the tentative commit; Among them: the pStack is a structure based on a persistent stack, which is used to record the states of all stacks to be rolled back in the future; the structure simulates a standard stack, supports general stack operation primitives, and can effectively restore and operate snapshots of multiple stacks; the structure divides the stack into several memory segments in a fine-grained manner, each memory segment records m consecutive elements in the stack, so that multiple snapshots can share unmodified memory segments without copying the entire stack; the structure supports accessing all stack elements using indexes, and tracking recorded stack snapshots; the structure uses stack frames to identify stack snapshots, and the stack frame consists of three parts: a pointer list pointing to the memory segment it maintains; a variable that records the stack height and a Boolean reference tag list to track whether the current stack frame shares a memory segment with other stack frames.
2. The implementation method according to claim 1, characterized in that: The step A1 comprises the following steps: Step A11: Check in the EVM execution environment whether the state to be read by the smart contract transaction T exists in the local read-write set of transaction T; Step A12: If the state does not exist, create a new checkpoint; Step A13: Fork a new pStack stack frame to capture the current EVM stack state snapshot and return a globally unique snapshot identification number; Step A14: Build and initialize the new stack frame, copy the current stack height and the pointer list pointing to the pStack stack frame before the new stack frame, and mark all memory segments with reference marks with a value of true, indicating that these segments are in a shared state.
3. The implementation method according to claim 1, characterized in that: The step A2 comprises the following steps: Step A21: Execute the logical operation in transaction T, and find the target memory segment to be updated of the element involved in the operation through the element index on the current pStack stack frame; Step A22: If the reference flag of the target memory segment to be updated is false, the value of the element will be directly written into the corresponding position of the target segment of the current pStack stack frame; Step A23: If the reference flag of the target memory segment of the element to be updated is true, the target segment is cloned to the newly allocated memory segment of the current pStack stack frame, and the value of the element is written into the new segment; After this, the reference mark of the segment is set to false, indicating that the new segment is solely owned by the current pStack stack frame.
4. The implementation method according to claim 1, characterized in that: The step A3 comprises the following steps: Step A31: When a read-after-write conflict occurs in a transaction, the conflicting key that causes the transaction to be aborted is recorded in the conflicting key set of the aborted transaction; Step A32: When transaction T detects that it is aborted, it atomically obtains the conflicting key from the conflicting key set and finds the conflicting key that occurs earliest in the running logic; Step A32: Transaction T restores the state of the EVM stack to the corresponding pStack stack frame through the snapshot identification number, and removes all stack frames created after the snapshot identification number; if the reference mark in any stack frame is false, the memory allocation of the segment is reclaimed; Step A34: Transaction T cleans up the read dependencies and write versions affected by the read-after-write conflict.
5. The implementation method according to claim 1, characterized in that: The step A4 comprises the following steps: Step A41: Transaction T is restored to the previous execution state and re-executed according to the execution context information recorded in the pStack stack frame corresponding to the most recent checkpoint; Step A42: Since the partial rollback has preserved the non-conflicting read and write states, during the tentative commit, only the version of transaction T newly written to the local write set after the most recent abort needs to be inserted into the shared storage.
