Method and device for resolving data conflict in transaction parallel process
By generating parallel events and determining their execution order during parallel transaction execution, the data conflict problem in parallel transaction execution is resolved, achieving higher concurrency and consistency of execution results.
Patent Information
- Application Number
- CN202211090223.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-07
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2042-09-07
AI Technical Summary
Existing technologies cannot effectively resolve data conflicts during parallel transactions, leading to inconsistent results and an inability to fully utilize the processing power of multi-core processors.
Parallel events are generated during the parallel execution of transactions, and the execution order of parallel events is determined after the transaction is completed. The values of parallel fields in the blockchain ledger are updated through parallel events to ensure the consistency of transaction results.
It enables parallel execution of multiple transactions with data conflicts, improves transaction concurrency, fully utilizes the processing power of multi-core processors, and ensures the correctness of execution results.
Smart Images

Figure CN116302378B_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of blockchain technology, and in particular relates to a solution and apparatus for data conflict problems during parallel transactions. Background Technology
[0002] Parallel processing technology can fully utilize the multi-core processing power of modern processors to execute transactions concurrently, reducing the overall time of block execution and significantly improving the overall performance of the blockchain system. However, transaction parallelism inevitably faces the problem of transaction conflicts.
[0003] For example, if multiple transactions need to write to data A during execution, then data A is a mutually exclusive resource for these transactions, and conflicts will inevitably occur when multiple transactions execute concurrently. Currently, mechanisms such as read-write sets, transaction dependency graphs, and mutual exclusion resource analysis can be used to avoid conflicts related to mutually exclusive resources during the execution of multiple transactions.
[0004] For example, in the Chang'an Chain, when generating a block, transactions can be concurrently scheduled, and the concurrency order can be recorded as a transaction dependency graph. Then, the transaction dependency graph and the transactions are packaged together into the block. Subsequently, when a node executes multiple transactions in a block, it can perform concurrent scheduling based on the transaction dependency graph.
[0005] In FISCO, when creating a transaction, potentially conflicting resources are attached to the transaction. When executing multiple transactions in a block, blockchain nodes assign transactions requiring the same resources to the same group. Transactions within the same group can be executed serially, and transactions between groups can be executed in parallel, thus avoiding parallel conflicts caused by resource contention.
[0006] However, the parallel processing techniques described above all execute transactions that do not conflict in parallel, while those that do conflict are executed sequentially. Multiple transactions with conflicting data cannot be executed in parallel. Summary of the Invention
[0007] In view of this, embodiments of this application provide a solution and apparatus for the data conflict problem during parallel transactions, so as to realize the parallel execution of multiple transactions with data conflicts, thereby improving the concurrency of transaction execution.
[0008] A first aspect of this application provides a solution to the data conflict problem during parallel transactions, applied to blockchain, the method comprising:
[0009] During the parallel execution of multiple transactions, if the parallel field of each transaction is executed, a parallel event is generated for each of the parallel fields.
[0010] After all the transactions have been completed, the execution order of the parallel events is determined.
[0011] According to the execution order, multiple parallel events are executed to update the values of each of the parallel fields in the ledger of the blockchain.
[0012] A second aspect of this application provides a device for resolving data conflict issues during parallel transactions, applied to a blockchain, the device comprising:
[0013] The event generation module is used to generate parallel events for each of the parallel fields when executing multiple transactions in parallel and the parallel field of each transaction is executed.
[0014] The sequence determination module is used to determine the execution order of the multiple parallel events after all the transactions have been executed.
[0015] An event execution module is used to execute multiple parallel events according to the execution order to update the values of each of the parallel fields in the ledger of the blockchain.
[0016] A third aspect of this application provides a computer device including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the method described in the first aspect above.
[0017] A fourth aspect of this application provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the method described in the first aspect above.
[0018] A fifth aspect of this application provides a computer program product that, when run on a computer device, causes the computer device to perform the method described in the first aspect.
[0019] Compared with the prior art, the embodiments of this application have the following advantages:
[0020] In this embodiment, during the parallel execution of multiple transactions, for each transaction, if a field within the transaction is executed, it can be determined whether that field is a parallel field that can be executed in parallel. This parallel field can be a conflicting field between multiple transactions. For a parallel field, a parallel event can be generated during transaction execution. After the transaction is completed, the execution order of the multiple parallel events obtained during transaction execution can be determined. Based on the execution order, multiple parallel events can be executed to obtain the value of the parallel field, thereby updating the value of the parallel field in the blockchain ledger. In this embodiment, for multiple transactions with data conflicts, during the parallel execution of multiple transactions, corresponding parallel events can be generated for the parallel fields with data conflicts. This allows multiple transactions to be executed in parallel first, and then the values of the parallel fields in the ledger can be updated by executing the parallel events. Previously, multiple transactions with data conflicts could only be executed serially; however, with the solution in this application, they can be executed in parallel first. During the parallel execution, the values of the conflicting fields are not updated in the ledger; instead, parallel events for the conflicting fields are generated. In other words, the embodiments of this application can allow more transactions to be executed in parallel, thereby making fuller use of the processing power of multi-core processors and improving the concurrency when executing transactions in parallel. In addition, by delaying the execution of parallel events according to the execution order, it can be ensured that the values of conflict fields in the ledger are consistent with the results of executing multiple transactions serially, thus guaranteeing the correctness of the execution results. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.
[0022] Figure 1 This is a flowchart illustrating the steps for resolving data conflict issues during parallel transactions, as provided in an embodiment of this application.
[0023] Figure 2 This is a flowchart illustrating the steps for resolving data conflict issues during parallel transactions, as provided in an embodiment of this application.
[0024] Figure 3 This is a schematic diagram of a device for resolving data conflict problems during parallel transactions, provided in an embodiment of this application.
[0025] Figure 4 This is a schematic diagram of a computer device provided in an embodiment of this application. Detailed Implementation
[0026] In the following description, specific details such as particular system architectures and techniques are set forth for illustrative purposes and not for limitation, in order to provide a thorough understanding of the embodiments of this application. However, those skilled in the art will understand that this application may also be implemented in other embodiments without these specific details. In other instances, detailed descriptions of well-known systems, apparatuses, circuits, and methods have been omitted so as not to obscure the description of this application with unnecessary detail.
[0027] In a blockchain, multiple transactions awaiting execution can be packaged into a block and then distributed to various blockchain nodes. When executing a block, a blockchain node can execute the multiple transactions within the block sequentially, according to the transaction order. Simply put, if a block contains 100 transactions, it can execute transaction number 1 first, then transaction number 2, and so on, until all transactions in the block have been executed.
[0028] During block execution, blockchain nodes can also execute multiple transactions within a block in parallel. For example, assuming a block contains 100 transactions, a blockchain node can execute transaction 1, transaction 10, transaction 50, etc., simultaneously. This means multiple transactions are executed concurrently. While allowing transactions to run in parallel, blockchain nodes must also ensure that the result of each node's block execution is consistent.
[0029] However, parallel transactions can lead to conflicts. If multiple transactions executed concurrently result in inconsistent block execution results across different nodes, these transactions are considered conflicting. Transaction conflicts typically include write-write conflicts and read-write conflicts. An example of a write-write conflict is that transactions 1 and 10 are executed concurrently. Transaction 1 writes the data `key1 = 1`, and transaction 10 writes the data `key1 = 10`. Since these two transactions are writing the same data and are executing concurrently, it's impossible to determine which transaction's result will be written to the ledger first. If the result of transaction 1 is written to the ledger first, followed by the result of transaction 10, the final result is `key1 = 10`. If the result of transaction 10 is written to the ledger first, followed by the result of transaction 1, the final result is `key1 = 1`. In other words, when a write-write conflict occurs, consistency in the execution results cannot be guaranteed across different blockchain nodes executing multiple transactions in parallel. Read-write conflicts are slightly different. Assume transactions 2 and 4 are executed concurrently, and the blockchain ledger contains the data `key2 = 2`. During transaction 2's execution, it reads `key2` from the blockchain ledger, while transaction 4 writes `key2 = 4`. Because the two transactions are executed concurrently, it's impossible to determine whether transaction 2 reads `key2` first or transaction 4 writes it first. If transaction 2 reads `key2` first, it reads the value `key2 = 2`; if transaction 4 writes it first, transaction 2 reads the value `key2 = 4`. In other words, for transaction 2, under concurrent conditions, the value of `key2` it reads is uncertain. This undoubtedly affects the contract's execution logic, leading to inconsistent execution results for the same block across different blockchain nodes.
[0030] During parallel transaction execution, if two threads compete for a shared resource, that shared resource is a mutually exclusive resource. A mutually exclusive resource can be a field with conflicting data. For example, if transactions tx1 and tx2 both need to access field field1 during execution, then field field1 is a mutually exclusive resource. As can be seen from the above, due to the existence of mutually exclusive resources, parallel execution of multiple transactions can lead to transaction conflicts, potentially preventing blockchain nodes from obtaining consistent execution results when executing the same block. In other words, parallel execution of multiple transactions with conflicting data cannot be achieved.
[0031] Based on this, this application provides a method for executing multiple transactions with conflicting data fields in parallel. The technical solution of this application is illustrated below through specific embodiments.
[0032] Reference Figure 1This document illustrates a flowchart of a solution to the data conflict problem during parallel transactions provided in an embodiment of this application. The execution subject of this embodiment is a computer device, which can be a tablet computer, wearable device, in-vehicle device, laptop computer, super mobile personal computer, server, or other computer device. The computer device can deploy a blockchain network, making it a blockchain node. Figure 1 The solution to data conflict problems during parallel transactions can specifically include the following steps:
[0033] S101: During the parallel execution of multiple transactions, if the parallel fields of each transaction are executed, then a parallel event for each parallel field is generated.
[0034] The aforementioned multiple transactions can be multiple transactions executed in parallel during the block execution process. In this embodiment, the multiple transactions can be transactions with data conflicts, for example, multiple transactions all need to operate on the same field.
[0035] During parallel transactions, blockchain nodes can leverage the multi-core processing power of processors to execute multiple transactions simultaneously. During transaction execution, the processor can process transactions according to their execution logic. This may involve operations on multiple fields, such as read operations, write operations, assignment operations, increment operations, and decrement operations.
[0036] When operations need to be performed on a field, it can be first determined whether the field is a parallel field. In this embodiment, a parallel field is a field that can be operated on in multiple transactions, but can be executed in parallel. That is, a parallel field can be a field that has data conflicts in multiple transactions, but whose execution can be postponed. In this embodiment, a field that can be postponed is a field that only affects its own field value during the execution of a transaction and is unrelated to other fields to be executed.
[0037] When a field is determined to be a parallel field, a corresponding parallel event can be generated for that field. A parallel event is an event that is executed later, meaning it can be executed after the transaction is completed, and this delay does not affect the correctness of the transaction. A parallel event can include a transaction sequence number, an event sequence number, a contract address, the field name of the operation, the operation type, and corresponding data for that operation type. Specifically, the transaction sequence number is the sequence number of the currently executing transaction; the event sequence number is the order in which the parallel event occurs within the transaction; the contract address is the address of the contract invoked when performing the operation on that parallel field; the field name of the operation is the name of the parallel field; the operation type can include the operation performed on that field, such as write, increment, decrement, or assignment; and the corresponding data for that operation type can be the data needed during the operation on that field, such as the value corresponding to an assignment operation. In one possible implementation, the parallel event can be a log event.
[0038] In one possible implementation, when determining the parallel framework, the parallel fields that can be parallelized can be identified, and then all parallel fields are placed into a parallel set. When an execution reaches a field, if the field is in this set, then the parallel field for that field can be determined.
[0039] In another possible implementation, before blockchain nodes execute multiple transactions in parallel, a temporary variable table can be established. This temporary variable table includes fields that will be read. When an operation is executed on a field, if the operation type corresponding to that field is an operation from a preset set of operations, and the field is not in the temporary variable table, then that field can be identified as a parallel field. The operations in the preset set of operations can be assignment operations, increment operations, decrement operations, etc.
[0040] S102, after multiple transactions have been completed, determine the execution order of multiple parallel events.
[0041] After all transactions have been executed in parallel, the multiple parallel events generated for a single parallel field during the parallel execution of the transactions can be identified. Then, the execution order of the multiple parallel events corresponding to a single parallel field can be determined.
[0042] When determining the execution order of parallel events, the transaction number and event number corresponding to the parallel events can be determined; then, the execution order of the parallel events can be determined based on the transaction number.
[0043] For example, during block execution, three transactions tx1, tx2, and tx3 can be executed in parallel, with transaction numbers 1, 2, and 3, respectively. tx1 may include an assignment operation on field field1: field1 = 9; tx2 may include an increment operation on field field1: field1 = field1 + 5; tx3 may include a decrement operation on field field1: field1 = field1 - 3. During the parallel execution of tx1, tx2, and tx3, when operations are performed on field1, parallel events log1, log2, and log3 can be generated, with transaction numbers 1, 2, and 3, respectively. After the parallel execution of the transactions is completed, the parallel events corresponding to field1 can be determined: log1, log2, and log3. Based on the transaction numbers corresponding to the parallel events, the execution order of the parallel events can be determined as: log1, log2, log3.
[0044] In one possible implementation, there can be multiple parallel fields during the parallel execution of multiple transactions; however, each parallel field does not necessarily need to be operated on in every transaction.
[0045] For example, consider three transactions tx1, tx2, and tx3, with transaction numbers 1, 2, and 3, executed in parallel. tx1 can assign a value to field 'field1' and increment field 'field2'; tx2 can increment field 'field1' and field 'field3'; tx2 can write to field 'field2' and decrement field 'field3'. After the transactions are executed in parallel, tx1 can generate a parallel event log1 for field 1, and a parallel event log2 for field 2; tx2 can generate a parallel event log3 for field 1, and a parallel event log4 for field 3; tx3 can generate a parallel event log5 for field 2, and a parallel event log6 for field 3. The transaction numbers corresponding to log1, log2, log3, log4, log5, and log6 are 1, 1, 2, 2, 3, and 3, respectively, and the event numbers are 1, 2, 1, 2, 1, and 2, respectively. After tx1, tx2, and tx3 are executed in parallel, it can be determined that the parallel events corresponding to field1 are log1 and log3, the parallel events corresponding to field2 are log2 and log5, and the parallel events corresponding to field3 are log4 and log6. For the field 'field1', the execution order of the parallel events is log1, log3; for the field 'field2', the execution order of the parallel events is log2, log5; and for the field 'field3', the execution order of the parallel events is log4, log6.
[0046] In one possible implementation, when determining the execution order, there may be multiple parallel events with the same transaction sequence number. In this case, the execution order of multiple parallel events with the same transaction sequence number can be determined based on the event sequence number.
[0047] For example, during block execution, transactions tx1 and tx2 can be executed in parallel, with transaction numbers 1 and 2 respectively. tx1 can include an assignment operation on field 'field1', followed by an increment operation on 'field1'; tx2 can include a decrement operation on field 'field1'. During the execution of tx1, when operations are performed on field 'field1', parallel events log1 and log2 are generated respectively; during the execution of tx2, when operations are performed on field 'field1', a parallel event log3 is generated. The transaction numbers for log1, log2, and log3 are 1, 1, and 2 respectively; the corresponding event numbers are 1, 2, and 1 respectively. After the parallel execution of transactions is complete, the parallel events corresponding to field 'field1' can be determined: log1, log2, and log3. When determining the execution order of the parallel events, the transaction numbers indicate that log3 will be executed last; since log1 and log2 have the same transaction number, the event numbers indicate that log1 will be executed first, followed by log2, meaning the final execution order is log1, log2, log3.
[0048] S103, according to the execution order, execute multiple parallel events to update the values of various parallel fields in the blockchain ledger.
[0049] For each parallel field, multiple parallel events can be executed sequentially according to the corresponding execution order, and then the final value corresponding to that parallel field can be obtained and updated in the blockchain ledger.
[0050] For the currently executing parallel event, the operation type and field of the parallel event can be determined. If the operation type is a write operation, increment operation, or decrement operation, the field value corresponding to the field can be obtained from the blockchain ledger; the operation type corresponding to the field value is performed on the field value to obtain a new field value; then, the field value in the blockchain ledger is updated with the new field value. If the operation type is an assignment operation, the value to be assigned to the field is determined, and the field value in the blockchain ledger is updated with this value.
[0051] For example, for parallel events log1, log2, and log3, log1 assigns a value to the field field 'field1': field1 = 9; log2 increments the value of field 'field1': field1 = field1 + 5; and log3 decrements the value of field 'field1': field1 = field1 - 3. When executing these parallel events, the following can be performed: field1 = 9; field1 = 9 + 5 = 14; field1 = 14 - 3 = 11. The final value of field1, 11, is written to the blockchain ledger, thus completing the execution of the block.
[0052] In one possible implementation, when multiple parallel fields exist, the parallel events corresponding to multiple fields can be executed in parallel. If multiple parallel fields exist in a transaction, the parallel events corresponding to each parallel field can be determined, and multiple parallel events corresponding to each parallel field can be executed concurrently using a multi-core processor. For example, if a computer device includes multiple parallel threads, each thread can execute the parallel events corresponding to one parallel field in its corresponding execution order. In this way, concurrency can also be achieved when executing parallel events, thereby improving concurrency. For example, for the field field 1, the execution order of the parallel events is log1, log3; for the field field 2, the execution order of the parallel events is log2, log5; and for the field field 3, the execution order of the parallel events is log4, log6. Then, when executing the parallel events in the execution order, log1, log2, and log4 can be executed simultaneously.
[0053] In this embodiment, for parallel fields with data conflicts in multiple transactions, parallel events corresponding to the parallel fields can be generated during the parallel execution of transactions. Then, after the transactions are completed, the parallel events are executed in the order of execution, thereby updating the values of the parallel fields in the blockchain. This embodiment, by delaying the execution of parallel events, eliminates the need to consider fields with data conflicts during the parallel execution of transactions, thereby enabling more transactions to be executed in parallel and improving the transaction concurrency during block execution.
[0054] Reference Figure 2 This document illustrates a flowchart of a solution to the data conflict problem during parallel transactions provided in an embodiment of this application. The execution subject of this embodiment is a computer device, which can be a tablet computer, wearable device, in-vehicle device, laptop computer, super mobile personal computer, server, or other computer device. The computer device can deploy a blockchain network, making it a blockchain node. Figure 2 The solution to data conflict problems during parallel transactions can specifically include the following steps:
[0055] S201, determine the field list corresponding to each transaction. The field list includes the fields in the transaction, and the fields have corresponding operation types and preset field types.
[0056] The method in this embodiment can be applied to scenarios where blockchain nodes execute blocks. A block can include multiple transactions, and each of these transactions can be any transaction within the block.
[0057] The execution of a transaction may include operations on variables, such as modifying the unit price of a product, determining the quantity of a product, and calculating the transaction amount. In a transaction, variables can have corresponding fields, and these fields contain corresponding operation information. The fields in the above field list are fields with specific field types and operation types. This field type can be a field defined in the smart contract. For example, a smart contract development framework can provide a special concurrency type, ParallelType, and the fields in the field list have the ParallelType field type. The above operation type can be a read operation. For example, if field A = field B, then it includes a read operation on field B.
[0058] In one possible implementation, fields that need to be operated on in multiple transactions can be defined as fields of a preset field type. Each transaction can include a list of fields, which can be used to store fields of the preset field type and operation type. For example, the field list structure can be: number of fields + field name 1 + field name 2 + ... + field name n, where the number of fields is the number of fields to be read.
[0059] In one possible implementation, a user can write contract A, which operates on field 'field1', where field1 is of type ParallelType. Contract A can include multiple contract methods, each capable of reading, assigning, incrementing, and decrementing the field. Multiple ParallelType fields and multiple contract methods can be defined within a smart contract. Contract A is then deployed on the blockchain. When constructing a transaction, if the called contract method needs to read the value of field 'field1', the field name 'field1' is recorded in the transaction body; otherwise, it is not necessary to record it in the transaction body. The field is recorded in the transaction body by encoding the field name and adding it to the transaction payload. The encoding format is: field number + field name 1 + field name 2 + ... + field name n. Here, the field number represents the number of fields to be recorded in this transaction, followed by the encoded field names. Blockchain nodes receive the transaction calling the contract and package it into a new block. After receiving the new block, the execution module can iterate through the payloads of all transactions within the block.
[0060] In actual calculations, incrementing and decrementing require retrieving the original value before adding or subtracting. However, in this embodiment, since the increment and decrement operations only affect the field itself and do not affect other fields, they can be considered a scenario where reading is not required. In this application, incrementing and decrementing can be considered write operations.
[0061] S202, record the fields in each field list into a preset temporary variable table.
[0062] For each transaction executed in parallel, each field in its field record table can be recorded in a temporary variable table. This way, the fields in the temporary variable table are those that require data conflicts and need to be read. In other words, the fields in the temporary variable table are those that need to be read in at least one of the multiple transactions during execution.
[0063] In one possible implementation, the field names recorded in each transaction payload are recorded in a temporary variable readMap. The structure of readMap can be <contract address, field name>, and each element in readMap uniquely identifies a specific field under a specific contract.
[0064] Read operations on fields require that the value of the field be determined at execution time; therefore, read operations must be performed sequentially during transaction execution. However, in this application, when implementing parallel execution of transactions with data conflicts, the execution of conflicting fields is delayed. That is, during transaction execution, the values of conflicting fields in the ledger can be left unupdated until all transactions are completed, at which point the conflicting fields are updated. In other words, in this embodiment, reading the value of a parallel field during transaction execution might result in an incorrect value. Therefore, this embodiment requires that fields with data conflicts in multiple transactions cannot be read during any transaction execution. Therefore, the aforementioned temporary variable table records fields whose execution cannot be delayed; that is, the fields in the temporary variable table have a preset field type but are not parallel fields.
[0065] S203: During the parallel execution of various transactions, if any field is executed, the operation type corresponding to that field is determined.
[0066] During transaction execution, fields can be manipulated by calling contract methods. For example, contract method 1 could include a write operation on the field `field1`. When a contract method is executed during transaction execution, the corresponding field and the operation associated with that field can be determined.
[0067] In one possible implementation, the above operation type may include read operations and non-read operations. For example, the contract method may include a flag bit. When the flag bit is 0, the operation type corresponding to the contract method is determined to be a read operation; when the flag bit is 1, the operation type corresponding to the contract method is determined to be a non-read operation.
[0068] In one possible implementation, a contract method may include multiple operations on the field, such as assigning a value to the field, incrementing it, and then decrementing it.
[0069] S204. If the operation type corresponding to the field is an operation in the preset operation set, and the field is not in the temporary variable table, then the field is determined to be a parallel field.
[0070] The operations in the aforementioned operation set can include write operations such as assignment, increment, and decrement. If the operation type for a field is an operation in the operation set, then the operation on that field can be deferred. Since the fields in the temporary variable table are also fields that cannot be deferred, if the operation type corresponding to the contract method called by the field is an operation in the operation set, and the field is not in the temporary variable table, then that field is determined to be a parallel field.
[0071] Furthermore, the operations in the aforementioned operation set can also include increment and decrement operations. Generally, an increment operation is an operation of adding one, and a decrement operation is an operation of subtracting one. An increment operation adds a value to the field itself, and this value is not necessarily 1; a decrement operation subtracts a value from the field itself, and this value is not necessarily 1. Since increment and decrement operations only include operations on themselves, they can be considered as write operations in this application.
[0072] S205 generates parallel events for parallel fields.
[0073] For parallel fields, execution can be delayed, thus generating parallel events corresponding to those fields during transaction execution. In one possible implementation, for a parallel field, a call to its corresponding contract method can correspond to one parallel event. A contract method can include one or more operations for that parallel field. For example, in transaction body tx1, if contract method 1 and contract method 2 are called for the parallel field field1, then parallel events 1 and 2 can be generated.
[0074] In one possible implementation, if the operation type corresponding to the contract method is an operation within a predefined operation set, and the field is in the temporary variable table, it indicates that the field has been read in other transactions. Since this field is not a parallel field, the operation can be performed directly on it during transaction execution. If the operation corresponding to the contract method is a read operation, and the field is in the transaction's field list, it indicates that the field is not a parallel field, and the read operation can be performed directly on it. If the operation corresponding to the contract method is a read operation, and the field is not in the transaction's field list, it indicates that the field cannot be deferred for execution but is not stored in the field list; therefore, the temporary variable table does not include this field. Other transactions cannot find this field in the temporary variable table, which may lead to deferred execution of the field, resulting in incorrect transaction execution results. Therefore, this transaction can be determined as an illegal transaction, and its execution should be stopped.
[0075] For example, when contract A is invoked to operate on field1, if field1 is incremented, decremented, or assigned a value (write operation), and field1 in contract A is not in the readMap, then the write operation is recorded as a Log event, and no further writing to the ledger is performed on this field; the transaction continues. If field1 is incremented, decremented, or assigned a value (write operation), and field1 in contract A is in the readMap, then the write operation is performed normally, no Log event is recorded, and the transaction continues. If field1 is read, and field1 in contract A is in the payload of the transaction body, the transaction continues. If field1 is read, and field1 in contract A is not in the payload of the transaction body, the transaction is considered an illegal transaction, and the transaction is stopped.
[0076] S206, after multiple transactions have been completed, determine the execution order of multiple parallel events.
[0077] After all transactions within a block are executed, all Log events generated during the transaction execution process are sorted according to their transaction sequence numbers. Log events with the same transaction sequence number are sorted according to their event sequence number.
[0078] S207, according to the execution order, execute multiple parallel events to update the values of various parallel fields in the blockchain ledger.
[0079] If the operation type recorded in the Log event is increment or decrement, then based on the contract address and field name in the event, the value of the corresponding field of the corresponding contract is obtained from the blockchain ledger, and the increment or decrement operation is performed, and then the new value is written into the blockchain ledger.
[0080] If the operation type is assignment, then the corresponding value is directly written into the blockchain ledger based on the contract address, field name, and data in the event, without needing to read the original value.
[0081] S106-S107 in this embodiment are similar to S102-S103 in the previous embodiment and can be referenced accordingly, so they will not be described in detail here.
[0082] In this embodiment, a temporary variable table records all fields from multiple transactions that cannot be delayed in execution. This ensures that each transaction, during execution, does not generate parallel events for fields in the temporary variable table, preventing execution errors caused by a transaction being unaware of other transactions' operations on those fields. For fields with data conflicts but only write or increment / decrement operations, this embodiment allows the field value in the ledger to be deferred during transaction execution instead of being updated during the transaction itself, thereby improving transaction concurrency. Furthermore, for multiple parallel fields, the multi-core capabilities of the processor can be fully utilized for parallel execution when parallel events are delayed, further improving transaction processing speed.
[0083] It should be noted that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.
[0084] To better illustrate the solution in this application, an embodiment is provided below.
[0085] Contract A contains a field `field1` of type `ParallelType`. The `method` method increments `field1` by 1, but the value of this field is not retrieved. Assuming the value of `field1` in contract A's blockchain ledger is 10, and two transactions `tx1` and `tx2` in a certain block both call contract A's `method`, while other transactions in the block are unrelated to contract A, the specific execution process of these two transactions is as follows:
[0086] Transactions tx1 and tx2 are executed concurrently. During the execution of tx1, an event log1 is generated when the increment operation on field1 is encountered. Log1 records the increment operation on field1 of contract A by tx1. Similarly, during the execution of tx2, an event log2 is generated when the increment operation on field1 is encountered. Log2 records the increment operation on field1 of contract A by tx2. After the transactions are completed, all Log events generated during execution are sorted and executed sequentially. First, the log1 event (increment operation) is executed, retrieving the value of field1 in contract A from 10, incrementing it to 11, and writing the new value to the blockchain ledger. Then, the log2 event (increment operation) is executed, retrieving the value of field1 in contract A from 11, incrementing it to 12, and writing the new value to the ledger. Once all Log events have been executed, the block execution ends.
[0087] During the above execution process, the delayed execution Log event mechanism ensures that after the block execution is completed, the field value of contract A is 12, which is consistent with the result of the serial execution of transactions within the block, and the concurrent execution of transactions tx1 and tx2 is achieved.
[0088] If the solution in this application is not adopted and tx1 and tx2 are executed concurrently, when the method is executed, tx1 and tx2 will simultaneously read the value 10 of field1 of contract A. After incrementing, the value will be 11 in tx1 and 11 in tx2. The value written to the blockchain ledger will then be 11, which is inconsistent with the result 12 of the serial execution. This is the erroneous concurrency caused by data conflict.
[0089] The specific implementation process of the solution in this application will be further described using another embodiment:
[0090] A user-written contract A is deployed on a blockchain node. Contract A can include contract methods that operate on fields field1 and field2. Contract A can include three methods: m1, m2, and m3, where:
[0091] m1: Assigns and reads the value of field1;
[0092] m2: Increments the value of field1;
[0093] m3: Decrements field2 by itself.
[0094] Suppose a block to be executed contains four transactions: A, B, C, and D. Transaction A calls m2 and m3; transaction B calls m1 and m2; transaction C calls m2; and transaction D calls m3. Then the payloads for transactions A, B, C, and D are 0, 1 + field1, 0, and 0, respectively.
[0095] Therefore, when a blockchain node executes this block, the process can be as follows:
[0096] 1. Retrieve transactions A, B, C, and D from the block; iterate through the transaction payloads and write them into the readMap to obtain <Contract A, field1>.
[0097] 2. Execute transactions A, B, C, and D.
[0098] 1) Execute transaction A:
[0099] A write operation has been performed on field1, but field1 is in readMap. At this time, a write operation can be performed on field1 normally.
[0100] A write operation was performed on field2, but field2 is not in readMap. Log1 can be generated by calling the contract method m3.
[0101] 2) Execute Transaction B:
[0102] A write operation has been performed on field1, but field1 is in readMap. At this time, a write operation can be performed on field1 normally.
[0103] A write operation was performed on field2, but field2 is not in readMap. You can call the contract method m2 to generate log2.
[0104] 3) Execute transaction C:
[0105] A write operation has been performed on field1, but field1 is in readMap. At this time, a write operation can be performed on field1 normally.
[0106] 4) Execute transaction D:
[0107] A write operation was performed on field2, but field2 is not in readMap. Log3 can be generated by calling the contract method m3.
[0108] 3. After A, B, C, and D are executed in parallel, determine the execution order of log1, log2, and log3.
[0109] 4. Execute log1, log2, and log3 in sequence to obtain the final value of field2.
[0110] 5. Write the value of field2 into the ledger, and the block execution is complete.
[0111] In this embodiment, if the previous scheme is adopted, transactions A, B, C, and D need to be executed serially; however, according to the scheme in this application, transaction D and other transactions can be executed in parallel, which obviously improves the concurrency.
[0112] To better illustrate the technical effects of the solution in this application, another embodiment is provided below:
[0113] In another possible example of this application, suppose the block contains 100 transactions, each of which requires an operation on 100 fields (excluding read operations). Following traditional transaction execution methods, these 100 transactions must be executed sequentially. Assume the execution time for each transaction is T, and the time for performing an operation on each field is t, i.e., T >= 100t. Following traditional transaction execution methods, the block execution time is 100T.
[0114] According to the method in this application, if the parallel processing capability of the computer device is sufficient, all 100 transactions can be executed in parallel, taking a time of T. After the parallel execution of the transactions is completed, 100 log events are generated for each field; for these 100 fields, the corresponding 100 log events can be executed in parallel, taking a time of 100t to complete all log events. Therefore, according to the scheme in this application, the number of block execution events is T+100t, which is much less than 100T. Obviously, the scheme in this application can fully utilize the multi-core processing capability of the processor, thereby improving the transaction execution speed.
[0115] Of course, this example uses an ideal scenario to more clearly illustrate the effect of the solution in this application. Those skilled in the art should understand that in other scenarios, although the effect of the solution in this application is not as great as that in this example, it can still improve transaction concurrency and thus reduce transaction execution time.
[0116] To further illustrate the implementation effect of this application, the solution in this application is described below with reference to an embodiment:
[0117] There are 10 transactions: x1, x2, x3, x4, x5, x6, x7, x8, x9, and x10, with transaction numbers 1-10 respectively. The computer can process 8 transactions simultaneously. Of these 10 transactions, x1 and x2 are independent of the others; x3 requires a read operation on y; x4 requires an increment operation on y; x5 requires a decrement operation on y; x6, x7, and x8 each require an increment operation on z; and x9 and x10 each require a decrement operation on z. According to the existing concurrency scheme, x6, x7, x8, x9, and x10 can be executed serially; x3, x4, and x5 can be executed serially; and x1 and x2 can be executed in parallel. That is, initially, a maximum of x1, x2, x3, and x6 can be executed in parallel; a maximum of 4 transactions can be processed simultaneously, which clearly does not fully utilize the computing power of the computer. Assuming each transaction has an execution event of T, then it would take 5T to complete the execution of 10 transactions.
[0118] However, according to the scheme in this application, the operations on z in x6, x7, x8, x9, and x10 include increment and decrement operations. Therefore, x6, x7, x8, x9, and x10 can be executed in parallel. Since x3 requires a read operation on y, x3, x4, and x5 need to be executed serially. Thus, initially, eight transactions can be executed in parallel: x1, x2, x3, x6, x7, x8, x9, and x10, fully utilizing the computing power of the computer and improving transaction concurrency. Using this method, completing 10 transactions would require 3T, clearly shortening the transaction execution time.
[0119] Reference Figure 3 The diagram illustrates a device for resolving data conflict issues during parallel transactions, as provided in an embodiment of this application. This device can be applied to blockchain and specifically includes an event generation module 31, an order determination module 32, and an event execution module 33, wherein:
[0120] The event generation module 31 is used to generate parallel events for each parallel field when executing multiple transactions in parallel, if the parallel field of each transaction is executed.
[0121] The sequence determination module 32 is used to determine the execution order of multiple parallel events after multiple transactions have been completed.
[0122] Event execution module 33 is used to execute multiple parallel events according to the execution order to update the values of each parallel field in the blockchain ledger.
[0123] In one possible implementation, the above-mentioned device further includes:
[0124] The Field List Determination Submodule is used to determine the field list corresponding to each transaction. The field list includes the fields in the transaction, and each field has a corresponding operation type and a preset field type.
[0125] The temporary variable table determination submodule is used to record the fields in each of the field lists into a preset temporary variable table.
[0126] In one possible implementation, the event generation module 31 includes:
[0127] The operation type determination submodule is used to determine the corresponding contract method for any field when executing any field during the parallel execution of various transactions. The contract method has a corresponding operation type.
[0128] The Parallel Field Determination Submodule is used to determine if a field is a parallel field if the operation type corresponding to the contract method is an operation in a preset operation set and the field is not in the temporary variable table.
[0129] The Parallel Events submodule is used to generate parallel events for parallel fields.
[0130] In one possible implementation, the above-mentioned device further includes:
[0131] The first execution module is used to perform operations on fields if the operation type corresponding to the contract method is an operation in a preset operation set and the field is in a temporary variable table.
[0132] The second execution module is used to perform a read operation on the field if the operation corresponding to the contract method is a read operation and the field is in the field list of the transaction.
[0133] The stop execution module is used to stop the execution of a transaction if the operation corresponding to the contract method is a read operation and the field is not in the field list of the transaction.
[0134] In one possible approach, the sequence determination module 32 described above includes:
[0135] The sequence number determination submodule is used to determine the transaction sequence number and event sequence number for each parallel event;
[0136] The first sorting submodule is used to sort parallel events according to the transaction sequence number;
[0137] The second sorting submodule is used to determine the execution order of multiple parallel events with the same transaction sequence number based on the event sequence number if there are multiple parallel events with the same transaction sequence number.
[0138] In one possible manner, the event execution module 33 described above includes:
[0139] The determination submodule is used to determine the operation type and fields of the currently executing parallel event;
[0140] The field value determination submodule is used to retrieve the field value corresponding to a field from the blockchain ledger;
[0141] The execution submodule is used to perform the operation corresponding to the operation type on the field value to obtain a new field value;
[0142] The update submodule is used to update the field values of fields in the blockchain ledger with the new field values.
[0143] In one possible approach, the transaction includes multiple of the parallel fields, and the aforementioned event execution module 33 includes:
[0144] A determination submodule is used to determine the parallel events corresponding to each of the parallel fields;
[0145] The parallel field concurrent execution submodule is used to concurrently execute the parallel events corresponding to multiple parallel fields.
[0146] In one possible approach, the parallel events are events that are executed later.
[0147] As the apparatus embodiments are basically similar to the method embodiments, they are described in a relatively simple manner. For relevant details, please refer to the description in the method embodiment section.
[0148] Figure 4 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. Figure 4 As shown, the computer device 4 of this embodiment includes: at least one processor 40 ( Figure 4 (Only one is shown) a processor, a memory 41, and a computer program 42 stored in the memory 41 and executable on at least one processor 40, which executes the steps in any of the above method embodiments when executing the computer program 42.
[0149] Computer device 4 can be a desktop computer, laptop, handheld computer, cloud server, or other computing device. This computer device may include, but is not limited to, processor 40 and memory 41. Those skilled in the art will understand that... Figure 4 The computer device 4 is merely an example and does not constitute a limitation on the computer device 4. It may include more or fewer components than shown, or combine certain components, or different components, such as input / output devices, network access devices, etc.
[0150] The processor 40 may be a Central Processing Unit (CPU), or it may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor.
[0151] In some embodiments, memory 41 may be an internal storage unit of the computer device 4, such as a hard disk or memory of the computer device 4. In other embodiments, memory 41 may be an external storage device of the computer device 4, such as a plug-in hard disk, smart media card (SMC), secure digital card (SD) card, flash card, etc., provided on the computer device 4. Furthermore, memory 41 may include both internal storage units and external storage devices of the computer device 4. Memory 41 is used to store operating systems, applications, bootloaders, data, and other programs, such as program code of computer programs. Memory 41 may also be used to temporarily store data that has been output or will be output.
[0152] This application also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps described in the various method embodiments above.
[0153] This application provides a computer program product that, when run on a computer device, enables the computer device to perform the steps described in the above-described method embodiments.
[0154] The above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application, and should all be included within the protection scope of this application.
Claims
1. A solution to the data conflict problem during parallel transactions, characterized in that, Applied to blockchain, the method includes: During the parallel execution of multiple transactions, if the parallel field of each transaction is executed, a parallel event is generated for each parallel field. The parallel field is a field that has data conflicts in multiple transactions but can be executed later. The parallel field is a field that only affects its own field value during the execution of the transaction and is unrelated to other fields to be executed. After all the transactions have been completed, the execution order of the parallel events is determined. According to the execution order, multiple parallel events are executed to update the values of each of the parallel fields in the ledger of the blockchain.
2. The method as described in claim 1, characterized in that, Before executing multiple transactions in parallel, the method further includes: Determine a list of fields corresponding to each transaction, the list of fields including the fields in the transaction, and the fields having corresponding operation types and preset field types; Record the fields in each of the field lists into a preset temporary variable table.
3. The method as described in claim 2, characterized in that, If the execution reaches the parallel field of each of the transactions, then a parallel event is generated for each of the parallel fields, including: During the parallel execution of each of the transactions, if any of the fields is executed, the operation type corresponding to that field is determined. If the operation type corresponding to the field is an operation in a preset operation set, and the field is not in the temporary variable table, then the field is determined to be the parallel field; Generate parallel events for the parallel field.
4. The method as described in claim 3, characterized in that, The method further includes: If the operation type corresponding to the field is an operation in a preset operation set, and the field is in the temporary variable table, then the operation is performed on the field; If the operation corresponding to the field is a read operation, and the field is in the field list of the transaction, then the read operation is performed on the field; If the operation corresponding to the field is a read operation, and the field is not in the field list of the transaction, then the execution of the transaction shall be stopped.
5. The method according to any one of claims 1-4, characterized in that, Determining the execution order of the corresponding multiple parallel events includes: Determine the transaction number and event number for each of the parallel events; The parallel events are sorted according to the transaction sequence number; If there are multiple parallel events with the same transaction sequence number, the execution order of the multiple parallel events with the same transaction sequence number is determined according to the event sequence number.
6. The method according to any one of claims 1-4, characterized in that, The execution of multiple parallel events according to the execution order includes: For the currently executing parallel event, determine the operation type and fields of the parallel event; If the operation type is an assignment operation, then the value corresponding to the assignment operation is used as the new field value of the field; If the operation type is not an assignment operation, then the field value corresponding to the field is obtained from the ledger of the blockchain; the operation corresponding to the operation type is performed on the field value to obtain a new field value; The field value of the field in the ledger of the blockchain is updated using the new field value.
7. The method according to any one of claims 1-4, characterized in that, The transaction includes multiple parallel fields, and the execution of multiple parallel events according to the execution order includes: Determine the parallel event corresponding to each of the parallel fields; The parallel events corresponding to multiple parallel fields are executed concurrently.
8. The method according to any one of claims 1-4, characterized in that, The parallel events are events that are executed later.
9. A device for resolving data conflict problems during parallel transactions, characterized in that, For use in blockchain, the device includes: The event generation module is used to generate parallel events for each of the parallel fields when executing each of the parallel fields of the transactions during the parallel execution of multiple transactions. The parallel fields are fields that have data conflicts in multiple transactions but can be delayed in execution. The parallel fields are fields that only affect their own field values during the execution of the transaction and are unrelated to other fields to be executed. The sequence determination module is used to determine the execution order of the multiple parallel events after all the transactions have been executed. An event execution module is used to execute multiple parallel events according to the execution order to update the values of each of the parallel fields in the ledger of the blockchain.
10. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the method as described in any one of claims 1-8.
11. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the method as described in any one of claims 1-8.
Citation Information
Patent Citations
Block chain transaction data processing method and device, electronic device and medium
CN109784930A
Method and device for concurrently executing transactions in block chain
CN110706101A
