Method for grouping transactions in blockchain and blockchain node
By determining the status variable identification of transaction access in the blockchain for grouping, the problem of low transaction grouping efficiency in the existing technology is solved, and a higher degree of parallelism and resource saving are achieved.
Patent Information
- Application Number
- PCT/CN2023/135005
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-02-28
- Filing Date
- 2023-11-29
- Publication Date
- 2024-09-06
AI Technical Summary
Existing technologies cannot effectively improve the degree of parallelism when grouping transactions in the blockchain, especially when multiple transactions call the same contract, resulting in a waste of computing and storage resources.
By determining the set of identifiers of state variables that each transaction will access, and grouping multiple transactions according to these identifiers, the need for pre-execution of transactions is reduced, and the amount of calculation and storage resources are reduced.
It improves the granularity and parallelism of transaction grouping and reduces the consumption of computing resources and storage resources.
Smart Images

Figure CN2023135005_06092024_PF_FP
Abstract
Description
Transaction grouping method and blockchain nodes in blockchain
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office of China on February 28, 2023, with application number 202310183750.6 and application name “Transaction Grouping Method and Blockchain Node in Blockchain”, the entire contents of which are incorporated by reference into this application. Technical Field
[0002] The embodiments of this specification belong to the field of blockchain technology, and in particular to a transaction grouping method and blockchain node in a blockchain. Background Art
[0003] Blockchain is a novel application model for computer technologies, including distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms. In a blockchain system, data blocks are linked sequentially in chronological order to form a chain-like data structure, cryptographically guaranteeing an unalterable and unforgeable distributed ledger. Due to its decentralized, tamper-proof, and autonomous nature, blockchain is gaining increasing attention and application.
[0004] Summary of the Invention
[0005] The purpose of the present invention is to provide a transaction grouping scheme in a blockchain to improve the performance of transaction grouping in the blockchain.
[0006] In a first aspect, this specification provides a transaction grouping method in a blockchain, which is executed by a blockchain node, comprising:
[0007] Obtaining multiple first transactions from the multiple transactions to be grouped, where the multiple first transactions call the same contract;
[0008] Determine a set of identifiers corresponding to state variables of the contract to be accessed by each of the first transactions, wherein the set of identifiers includes variable identifiers of the state variables during contract execution, and the variable identifiers are used to determine keys of the state variables in a state database;
[0009] The multiple first transactions are grouped according to the identification sets of the respective first transactions.
[0010] A second aspect of this specification provides a blockchain node, including:
[0011] an acquiring unit, configured to acquire a plurality of first transactions from a plurality of transactions to be grouped, wherein the plurality of first transactions invoke a same contract;
[0012] a determining unit, configured to determine a set of identifiers corresponding to state variables of the contract to be accessed by each of the first transactions, wherein the set of identifiers includes variable identifiers of the state variables during contract execution, and the variable identifiers are used to determine keys of the state variables in a state database;
[0013] A grouping unit is configured to group the multiple first transactions according to the identification set of each first transaction.
[0014] A third aspect of this specification provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed in a computer, the computer is caused to execute the method described in the first aspect.
[0015] A fourth aspect of this specification provides a blockchain node, comprising a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code, the method described in the first aspect is implemented.
[0016] In the solution provided in the embodiments of this specification, for multiple transactions invoking the same contract, the identifiers of the state variables that each transaction will access during contract execution are determined, and then the multiple transactions are grouped according to the identifiers of the state variables that each transaction requests to access. This reduces the computational complexity and storage resources used for transaction grouping, improves the precision of transaction grouping, and further enhances the parallelization of transaction execution. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions of the embodiments of this specification, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments recorded in this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0018] FIG1 is a schematic diagram of deploying a smart contract in one embodiment;
[0019] FIG2 is a schematic diagram of calling a smart contract in one embodiment;
[0020] FIG3 is a schematic diagram of a block storage structure in one embodiment;
[0021] FIG4 is a schematic diagram of a block storage structure in one embodiment;
[0022] FIG5 is a schematic diagram of an EVM virtual machine module involved in a transaction processing process in one embodiment;
[0023] FIG6 is a schematic diagram of a slot structure in one embodiment;
[0024] FIG7 is a schematic diagram of a slot structure in one embodiment;
[0025] FIG8 is a flow chart of a method for grouping transactions in a blockchain according to an embodiment of this specification;
[0026] FIG9 is a flow chart of a method for grouping transactions according to an embodiment of this specification;
[0027] FIG10 is an architectural diagram of a blockchain node in an embodiment of this specification. DETAILED DESCRIPTION
[0028] To help those skilled in the art better understand the technical solutions in this specification, the following will provide a clear and complete description of the technical solutions in the embodiments of this specification, in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of this specification, not all of them. All other embodiments derived by those skilled in the art based on the embodiments in this specification without creative effort shall fall within the scope of protection of this specification.
[0029] To help those skilled in the art better understand the technical solutions in this specification, the following will provide a clear and complete description of the technical solutions in the embodiments of this specification, in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of this specification, not all of them. All other embodiments derived by those skilled in the art based on the embodiments in this specification without creative effort shall fall within the scope of protection of this specification.
[0030] Blockchains are generally categorized into three types: public, private, and consortium. Furthermore, various combinations exist, such as private + consortium, or consortium + public. Public blockchains offer the highest degree of decentralization. Examples include Bitcoin and Ethereum. Participants in public blockchains can access data records, participate in transactions, and compete for the right to record new blocks. Furthermore, participants (represented by nodes on the blockchain) can freely join and exit the network and perform related operations. In contrast, private blockchains have write permissions controlled by a specific organization or institution, while data read permissions are regulated by the organization. Simply put, a private blockchain can be a weakly centralized system with a small and strictly limited number of participating nodes. This type of blockchain is more suitable for internal use within a specific organization. Consortium blockchains fall somewhere between public and private blockchains, achieving "partial decentralization." Each node in the consortium chain usually has a corresponding entity or organization; participants join the network through authorization and form a stakeholder alliance to jointly maintain the operation of the blockchain.
[0031] Whether public, private, or consortium blockchains, most offer smart contract functionality. Smart contracts on the blockchain are contracts that can be triggered and executed by transactions on the blockchain system. Smart contracts can be defined in code.
[0032] Taking Ethereum as an example, supporting users to create and invoke complex logic within the Ethereum network is the biggest challenge that distinguishes Ethereum from Bitcoin blockchain technology. At the core of Ethereum, as a programmable blockchain, is the Ethereum Virtual Machine (EVM), which runs on every Ethereum node. The EVM is a Turing-complete virtual machine, meaning it can implement a wide range of complex logic. When users publish and invoke smart contracts within Ethereum, they are executing on the EVM. In reality, the virtual machine directly runs virtual machine code (virtual machine bytecode, hereinafter referred to as "bytecode"). Smart contracts deployed on the blockchain can be in bytecode form.
[0033] For example, as shown in Figure 1, after Bob sends a transaction containing information to create a smart contract to the Ethereum network, the EVM on Node 1 executes the transaction and generates a corresponding contract instance. "0x6f8ae93..." in Figure 1 represents the address of the contract. The transaction's data field may contain bytecode, and the transaction's to field is an empty account. Once consensus is reached between the nodes, the contract is successfully created and can be subsequently invoked by users.
[0034] After a contract is created, a contract account corresponding to the smart contract appears on the blockchain with a specific address. The contract code and account storage are stored in this contract account. The smart contract's behavior is controlled by the contract code, while the smart contract's account storage stores the contract's state. In other words, a smart contract creates a virtual account on the blockchain that contains the contract code and account storage.
[0035] As mentioned above, the data field of a transaction that creates a smart contract may contain the bytecode for that smart contract. Bytecode consists of a series of bytes, each of which identifies an operation. For reasons of development efficiency and readability, developers can choose to write smart contract code in a high-level language rather than directly writing bytecode. Smart contract code written in a high-level language is compiled by a compiler to generate bytecode, which can then be deployed on the blockchain. Ethereum supports many high-level languages, such as Solidity, Serpent, and LLL.
[0036] Taking the Solidity language as an example, contracts written in it are very similar to classes in object-oriented programming languages. A contract can declare multiple members, including state variables, functions, function modifiers, events, etc. State variables are values stored in the smart contract's account storage and are used to save the contract's state.
[0037] The following is a code example of a simple smart contract written in Solidity language 1:
[0038] Code Example 1. SimpleStorage Code
[0039] In the code example 1 above, line 2 declares the state variable storedData of type string, and line 3 declares an event, which stores the address of the initiator of the contract call and the string s. Lines 4-7 define the set function, which takes the string s as its input. The set function sets the input parameter to the state variable storedData and generates an event whose content includes the address of the initiator of the contract call and the string s.
[0040] As mentioned above, the state variables will eventually be stored in the database. The generated events are generally in the following form:
[0041] [topic1][topic2]...[topicn][data]
[0042] Here, the first topic, topic1, is generally the default value, such as the receipt identifier, which can be a hash value obtained by sequentially concatenating the event name, event parameter type, etc. Whether each topic exists, topic2 to topicn, depends on whether the Indexed modifier is added when defining the parameter. If so, the value of this parameter will be a topic in the receipt, and the one without the Indexed modifier will generally be placed in the data. In the example of code example 1 above, when declaring the event in line 3, the two parameters address from and s have no Indexed modifier and are generally placed in the data. The code in line 6 sets the data content [msg.sender, s] in the event through the stored() event. In this way, for the event operated on line 6, the overall form is:
[0043] [topic1: event ID][data: msg.sender, s]
[0044] Lines 8-10 define the get function. This function's operations include returning the value of the queried storedData . returns(string) indicates the type of the return value, and the constant modifier indicates that this function cannot modify the value of the state variable in the contract.
[0045] Furthermore, as shown in Figure 2, still using Ethereum as an example, after Bob sends a transaction containing information to call a smart contract to the Ethereum network, the EVM of Node 1 can execute this transaction and generate the corresponding contract instance. The from field of the transaction in Figure 2 is the address of the account initiating the smart contract call, the "0x6f8ae93..." in the to field represents the address of the smart contract being called, and the data field of the transaction stores the method and parameters for calling the smart contract. Furthermore, a value field can also be included to represent the value of Ether in the transaction. After calling the smart contract, the value of storedData may change. Subsequently, a client can view the current value of storedData through a blockchain node (such as Node 6 in Figure 2).
[0046] Smart contracts can be executed independently on each node in the blockchain network in a prescribed manner, and all execution records and data are stored on the blockchain. Therefore, when such a transaction is completed, the blockchain stores transaction credentials that cannot be tampered with or lost.
[0047] As mentioned above, the storedData in the above example is a state variable, which is stored in the smart contract's account storage. In various blockchain networks that introduce smart contracts, such as Ethereum, accounts can generally be of two types:
[0048] Contract account: stores the executed smart contract code and the value of the state in the smart contract code. It can usually only be activated by calling an external account.
[0049] Externally owned account: A user's account, such as an Ethereum owner account.
[0050] The design of external accounts and contract accounts is essentially a mapping of account addresses to account states. Account states typically include fields such as Nonce, Balance, Storage root, and CodeHash. Nonce and Balance exist in both external accounts and contract accounts. CodeHash and Storage root properties are generally only valid for contract accounts.
[0051] Nonce: A counter. For external accounts, this number can represent the number of transactions sent from the account address; for contract accounts, it can be the number of contracts created by the account.
[0052] Balance: The amount of Ether owned by this address.
[0053] Storage root: The hash of the root node of an MPT tree. This MPT tree organizes the storage of the state variables of the contract account.
[0054] CodeHash: The hash value of the smart contract code. For contract accounts, this is the hash value of the smart contract. For external accounts, since they do not include a smart contract, the CodeHash field can generally be an empty string or a string of all zeros.
[0055] MPT stands for Merkle Patricia Tree, a tree structure that combines the Merkle Tree and the Patricia Tree (a compressed prefix tree, a more space-efficient Trie tree, also known as a dictionary tree). The Merkle Tree algorithm calculates a hash value for each transaction, then hashes each transaction again, until the top-level Merkle root is reached. Ethereum uses a modified MPT tree, such as a hexadecimal tree structure, often referred to as the MPT tree.
[0056] The data structure of the Ethereum MPT tree includes a state trie. The state trie contains key-value pairs (kv or kv) representing the storage content of each account in the Ethereum network. A "key" in the state trie can be a 160-bit identifier (such as an Ethereum account address or a portion of an address's hash value, collectively referred to as an account address). This account address is distributed across the storage from the root node to the leaf nodes of the state trie. The "values" in the state trie are generated by encoding Ethereum account information using Recursive-Length Prefix encoding (RLP). As mentioned previously, for external accounts, the values include nonce and balance; for contract accounts, the values include nonce, balance, codehash, and storageroot.
[0057] A contract account is used to store the state associated with a smart contract. Once a smart contract is deployed on the blockchain, a corresponding contract account is created. This contract account typically has certain states, defined by state variables in the smart contract and generating new values when the smart contract executes. A smart contract is typically a contract defined in code within a blockchain environment that automatically executes its terms. Once an event triggers a term in the contract (meets the execution condition), the code automatically executes. In the blockchain, the state associated with a contract is stored in the storage trie. The hash value of the storage trie's root node is stored in the storageroot, locking all states of the contract to the contract account through the hash. The storage trie is also an MPT tree structure, storing a key-value mapping from state addresses to state values. Part of the information from the storage trie's root node to the leaf nodes is sequentially arranged to store the address of a state, while the leaf nodes store the state value.
[0058] In some blockchain data stores, as shown in Figure 3, each block's header includes several fields, such as the previous block's hash (prev_Hash, in the figure), a random number (in some blockchain systems, this nonce is not a random number, or in some blockchain systems, nonce in the block header is not enabled), a timestamp, a block number (Block Num), a state root hash (State_Root), a transaction root hash (Transaction_Root), and a receipt root hash (Receipt_Root). The Prev_Hash field in the block header of the next block (e.g., block N+1) points to the previous block (e.g., block N), which is the hash value of the previous block. In this way, the block header on the blockchain locks the previous block to the next block. State_Root, Transaction_Root, and Receipt_Root lock the state set, transaction set, and receipt set, respectively. The state set, transaction set, and receipt set organize the state, transactions, and receipts, respectively, in a tree format. Generally, these tree structures can be the same or different. For example, Ethereum uses the same MPT structure. In some tree structures like Ethereum, which include smart contract state collections, there are two levels of MPT structures: the leaf nodes of the upper-level MPT structure include two types: external accounts and contract accounts; each contract account includes the lower-level MPT structure, and the leaf nodes of the lower-level contain the state values of the contract account.
[0059] Figure 4 is a schematic diagram of the blockchain data storage structure. Still using Ethereum as an example, and in conjunction with Figure 3, state_root is the hash value of the root of the MPT tree, which represents the states of all accounts in the current block. This means that the state trie, which points to state_root, is an MPT-style state trie. The root node of this MPT tree is typically an extension node or a branch node, and state_root typically stores the hash value of this root node. The root node can be connected to one or more layers of extension nodes / branch nodes below it. These multiple layers of tree nodes are collectively referred to as internal nodes. A portion of the values in each node from the root to the leaf nodes of the MPT are sequentially concatenated to form an account address, which serves as the key. The account information stored in the leaf nodes is the value corresponding to this account address, thus forming a key-value pair. This key can also be a portion of the hash of the account address (using, for example, the SHA3 algorithm). The stored value can be RLP (Account), the RLP encoding of the account information. Account information is a four-tuple consisting of [nonce, balance, storageRoot, codeHash]. As mentioned earlier, external accounts typically only contain the nonce and balance fields, while the storageRoot and codeHash fields default to empty strings or all zeros. In other words, external accounts do not store contracts or state variables generated after contract execution. Contract accounts typically include Nonce, Balance, Storage Root, and CodeHash. Nonce is the transaction counter for the contract account; Balance is the account balance; Storage Root corresponds to another MPT and can be used to link to contract-related state information; CodeHash is the hash of the contract code. Account information for both external and contract accounts is generally stored in a separate leaf node. From the root node's Extension Node / Branch Node to each account's Leaf Node, there may be several branch nodes and extension nodes in between.
[0060] The state trie can be an MPT-style tree, typically a hexadecimal tree, meaning each layer can have up to 16 child nodes. Extension nodes, used to store common prefixes, typically have one child node, which can be a branch node. Branch nodes can have up to 16 child nodes, which may include extension nodes and / or leaf nodes.
[0061] For a contract account in the state trie, its storage_Root points to another MPT-formatted tree, which stores data related to state variables involved in contract execution. The MPT-formatted tree pointed to by storage_Root is the Storage Trie, which is the hash value of the Storage Trie's root node. Generally, this Storage Trie also stores key-value pairs. The key represents the address of the state variable. Its value can be the result of processing the location of the state variable declaration in the contract (a value starting at 0) using certain rules, such as sha3 (location of the state variable declaration) or sha3 (contract name + location of the state variable declaration). The value stores the value of the state variable (e.g., an RLP-encoded value). The key is formed by concatenating a portion of the data stored along the path from the root node through the intermediate nodes to the leaf nodes. The leaf nodes store the value. As mentioned earlier, the storage trie can also be an MPT-style tree, typically a hexadecimal tree. This means that a branch node can have up to 16 child nodes, which may include extension nodes and / or leaf nodes. An extension node can typically have one child node, which can be either a branch node or a leaf node.
[0062] For example, the Leaf Node Account P of the state trie in Figure 4 is a contract account, and its Storage Root locks all states in the contract storage. These states are organized into an MPT tree, and the tree structure is like the Storage trie linked to the Storage Root. In this linked Storage trie, taking Leaf Node State Variable N as an example, for example, the value of storedData in the aforementioned contract code example, its key can be, for example, but not limited to, sha3 (the declaration location of storedData), and its value is s (for the sake of simplicity, the encoding format of the value is omitted here, for example, RLP, which will be similar in the future and will not be repeated). Among them, the key values are distributed sequentially from the root node to the leaf node (i.e., Leaf Node Variable N) of the storage trie.
[0063] For another example, in the state trie of Figure 4, Leaf Node Account C is an external account, and its key is sha3(Address C), which is the hash value of the address of account C (the hash algorithm uses the sha3 algorithm, for example). The stored value value can be (Account), where the account information Account is a tuple consisting of [nonce, balance]. As mentioned above, since Account C is an external account, its account information includes two items: nonce and balance (excluding codehash and storage root, similar to the following). For example, if an external account has a nonce of 20 and a balance of 4550, then the leaf node Leaf Node State Variable C stores nonce = 20 and balance = 4550. The address of Account C is the key, and its values are distributed sequentially from the root node to the leaf node (i.e., Leaf Node Variable C) of the state trie.
[0064] As described above, after a transaction creating a smart contract is sent to the blockchain and consensus is reached on the transaction, each blockchain node can execute the transaction. A contract account corresponding to the smart contract appears on the blockchain (including, for example, the account's identity, the contract's hash value, and the contract's storage root, StorageRoot) with a specific address. The contract code and account storage are stored in the contract account's storage, as shown in Figure 5. The smart contract's behavior is controlled by the contract code, while the smart contract's account storage stores the contract's state. In other words, a smart contract creates a virtual account on the blockchain containing the contract code and storage. Contract deployment or contract update transactions generate or modify the Codehash value. Subsequently, blockchain nodes can receive transaction requests to invoke the deployed smart contract. These transaction requests may include the address of the invoked contract, the function within the invoked contract, and the input parameters. Generally, after consensus is reached on the transaction request, each blockchain node can independently execute the designated smart contract.
[0065] The left side of Figure 5 shows an example of a smart contract written in Solidity and its compilation and execution process. This smart contract is compiled by a compiler to generate bytecode. The solc command-line compiler in the figure represents Solidity. Ethereum smart contracts written in Solidity can be compiled using the solc command-line tool with parameters to generate bytecode that can be run on the EVM. After the contract deployment process described in Figure 1, a smart contract is successfully created on the blockchain. After the contract is deployed, a contract account corresponding to the smart contract is generated on the blockchain. This contract account includes information such as the contract counter Nonce, the account balance Balance, the contract bytecode hash Codehash, and the contract storage root StorageRoot. The contract has a specific address on the blockchain, known as the contract address.
[0066] In Ethereum, for example, this contract address is calculated by hashing the address of the external account that deployed the contract and its counter nonce. Specifically, an example is sha3(rlp.encode([address_sender, nonce])). (As mentioned above, rlp is an encoding format. Different blockchains can use other encoding formats, or even no re-encoding, so rlp is omitted hereafter.) sha3 is a type of hash algorithm, such as the commonly used keccak256. As mentioned above, rlp represents an encoding format. rlp.encode([address_sender, nonce]) performs rlp encoding on the contents enclosed in parentheses. The [address_sender, nonce] in the parentheses concatenates the address_sender of the external account that deployed the contract and its counter nonce. For example, using the keccak256 algorithm, a 256-bit hash value is obtained. Based on this hash value, the address of the deployed contract on the blockchain (for example, the first 20 bytes) can be obtained. 256 bits is equivalent to 32 bytes. Upon deployment, the account balance can be set to a default value of 0 or 0. The contract bytecode hash value, Codehash, can be calculated by the blockchain platform through hashing the contract bytecode. The contract storage root, StorageRoot, can be a default value or a hash value calculated from the root node of the underlying storage trie. This generally depends on whether the deployed contract performs initialization operations, such as executing the contract's constructor. If the deployed contract includes a constructor, it typically initializes state variables that will ultimately be stored in the underlying database. This initialization can be performed in the virtual machine. As described above, the initialized state variables can be used to construct an MPT tree, which can then be used to obtain the root node of the MPT tree and, in turn, the hash value of this root node. If the deployed contract does not include a constructor, the specific function may not be executed, and the blockchain platform will assign a default value to StorageRoot, such as a hash value of empty content.
[0067] After the contract is deployed, it can be called later, as described above. As shown in Figure 2, Bob initiates a transaction to the Ethereum network to call the smart contract. The contract executes, setting the state variable to the string "hello".
[0068] The execution of a contract can be specifically illustrated in Figure 5. For example, a transaction calling a contract in Figure 2 is sent to the blockchain network. After consensus is reached, each node can execute the transaction. The transaction's "to" field indicates the address of the called contract. Any node can use the contract's address to locate the contract account's storage, then read the codehash from the contract account's storage and, based on the codehash, find the corresponding contract bytecode. The node can load the contract's bytecode from storage into the virtual machine. The interpreter then interprets and executes the contract. For example, this involves parsing the called contract's bytecode (e.g., parsing Push, Add, SGET, SSTORE, Pop, etc.) to obtain the opcodes and functions. These opcodes are then stored in memory allocated by the virtual machine (alloc in the figure; the corresponding memory release operation after program execution, e.g., Free in the figure). The interpreter also obtains the jump code of the called function in the memory space. Generally, after calculating the gas required to execute the contract and ensuring that sufficient gas is available, the contract jumps to the corresponding address in memory, retrieves the OPcode of the called function, and begins execution. The data operated on by the called function's OPcode is computed (data computation), pushed onto / popped from the stack, and so on, completing the data computation. During this process, some contract context information may be required, such as the block number and information about the initiator of the contract call. This information can be obtained from the Context (Get operation). Finally, the resulting state is stored in the database storage by calling the storage interface. It should be noted that during the contract creation process, certain functions within the contract may be executed, such as the initialization function. This also involves code parsing, generating jump instructions, storing them into memory, and performing operations on the stack.
[0069] Through the above process, the virtual machine loads and executes the contract's bytecode, which may generate and / or read state, thus requiring access to the underlying database. The virtual machine requires convenient access to the underlying key-value database. This access is typically achieved using pointer-like data access capabilities. For example, in Ethereum, if you need to read the value corresponding to a key from the KV database, you must know the key of the data before accessing it.
[0070] Each contract generally has its own virtual storage space, the capacity of which can be a very large array, for example, 2 256 An array of elements, numbered from 0 to 2 256-1. Each element can occupy a certain length, for example, 32 bytes. Each element is called a slot. It should be noted that there are 2 256 The storage space of each slot is the total capacity of the virtual space, that is, unused slots do not occupy the actual storage space of the underlying database.
[0071] During the virtual machine's execution of contract bytecode, the slot locations of identical state variables in the virtual storage space must be fixed, ensuring that the keys generated during contract execution are consistent. This fixed location is typically established once the contract code is finalized. Therefore, the task of fixing the location of state variables is typically determined during the compiler compilation phase and is not directly related to the virtual machine.
[0072] The compiler compilation process can generally include the following steps: generating an abstract syntax tree, performing lexical / syntactic analysis based on the abstract syntax tree, filling symbols according to the symbol table, semantic analysis, and code generation. The abstract syntax tree includes a plurality of nodes arranged in sequence, and the plurality of nodes correspond to the plurality of objects declared in the contract. The arrangement order of the plurality of nodes corresponds to the declaration position of the plurality of objects in the contract, and the plurality of objects include the first state variable. In the link of performing lexical / syntactic analysis based on the abstract syntax tree, the slot position information of the state variable of the contract can be generated. Assume that the code of contract C1 is as follows:
[0073] Code Example 2. Solidity code for contract C1
[0074] For example, the following abstract syntax tree is generated based on the contract code:
[0075] In the AST shown above, comments are marked with / / ... Line 11 describes the slot calculation rules. In the AST above, starting from line 3, nodes, the information about the objects declared in the contract is described in the AST. Information related to each object is stored in a node. These objects include, for example, state variables A and B, and the mappings "tokenOwner" and "ownedTokensCount." Lines 5-19 are the zeroth node for A in the AST, lines 20-34 are the first node for B, lines 35-49 are the second node for tokenOwner, and lines 50-64 are the third node for ownedTokensCount. Each node contains a number of pieces of information. Collectively, the information within a node can be referred to as the node information of the AST.
[0076] Referring to the abstract syntax tree of contract C1, the two state variables A and B in contract C1 have node positions 0 and 1 in the abstract syntax tree, which correspond to the following two slots in Figure 6:
[0077] 0x0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
[0078] 0x0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001
[0079] Each of the two slots above is 256 bits, which is 32 bytes when represented by hexadecimal (0x). Four consecutive hexadecimal digits in each segment separated by spaces after 0x represent 2 bytes, so there are 16 such segments in total.
[0080] In this way, in the contract bytecode compiled by the compiler, these two positions in the slot can be used to replace the identifiers of the two state variables, such as the two 256 bits mentioned above replacing A and B respectively. In this way, when the data type is a fixed-size value, the compilation process can pre-allocate storage locations for each data to be stored based on the field sorting order, which is equivalent to specifying a fixed data pointer in advance.
[0081] During the process of loading and executing the contract bytecode in the virtual machine, the operation on A is the operation on the slot position corresponding to 0x000...00 (i.e., position 0 of the above slot, with ellipsis replacing the more 0s in the middle). Similarly, the operation on B is the operation on the slot position 0x000...01.
[0082] Figure 7 illustrates slot operations. For example, a transaction calling contract C1 invokes the setA() function within that contract with the string "0001" as an argument. During the execution of this transaction, the virtual machine stores 0001 in the slot 0x000...01. Specifically, the virtual machine pushes the 32-byte slot 0x000...01 onto the stack, followed by the corresponding value. During interpretation and execution, the virtual machine retrieves the opcode of the called function from memory and begins execution. Based on the stack's first-in, last-out (FIFO) or last-in, first-out (LIFO) nature, the value is popped from the stack, and the slot is then popped from the stack, forming a slot-value pair. The contract virtual machine then executes the opcode, writing the value to the slot. This slot acts as a serial number, uniquely identifying the contract's state variables during contract execution. In the above process, the stack generally uses 32 bytes as a storage unit, which is equal to the length of one slot. The corresponding value may be less than, equal to, or greater than 32 bytes, so the value may occupy one or more units in the stack.
[0083] As shown in Figure 7, for example, a transaction calling contract C1 calls the setB() function in the contract with an input parameter of "36". When the virtual machine executes the transaction, it stores 36 in the slot 0x000...02.
[0084] The results of the execution of the above two contracts can be simply expressed as follows:
[0085] 0x000...00:0001(1)
[0086] 0x000...01:36(2)
[0087] After the virtual machine is executed, the virtual machine or blockchain platform can convert the slots in the two slot-value pairs (1) and (2) into state keys. Specifically, the state key can be obtained by splicing the contract address + slot position. For example, the address length of contract C1 is 20 bytes, which is 0x3321dcaf 8911 d384 2e14 a7a4 15be 2fb1 a337 f43e. After splicing the contract address + slot position,
[0088] (1) The state key of the corresponding state variable A is:
[0089] 0x3321dcaf8911d3842e14a7a415be2fb1a337f43e0000000000000000000000000000000000000000000000000000000000
[0090] (2) The state key of the corresponding state variable B is:
[0091] 0x3321dcaf8911d3842e14a7a415be2fb1a337f43e000000000000000000000000000000000000000000000000000000000000001
[0092] Afterward, blockchain nodes can store these generated state key-value pairs in the underlying database. Specifically, blockchain nodes may include a tree construction module and a state database module. The tree construction module converts these keys into a storage trie, as shown in Figure 4, and constructs the value in the state key-value pair into a leaf node of this MPT tree. It should be noted that, as previously mentioned, the state key is split into several segments, which are stored sequentially in the tree nodes from the root of the storage trie to the leaf nodes. Which segment of the state key is stored in each tree node depends on whether the state key shares a prefix with other state keys in the tree. From the leaf node, through the intermediate nodes, to the root node, a series of hash value changes occur, and the tree construction module constructs the key-value pairs of these tree nodes. The tree construction module then sends these changed tree node key-value pairs to the state database module, which then stores them in the state database.
[0093] In the above process, the slots for state variables are fixed during compilation. As mentioned above, they are determined by the contract address and the slot location of the state variable. The state key can be obtained by concatenating the contract address and the slot. This way, when running the same contract and reading or writing the same state variable, the same slot is used, corresponding to the fixed state key.
[0094] It should be noted that the state variables A and B in the above example are of type uint256 and bool, respectively. uint256 is 256 bits, or 32 bytes, and bool is 1 byte. The type of these state variables determines that the data length is fixed, or fixed-length. In addition, types such as uint, uint8, and uint128 are also fixed-length. Arrays consisting of a fixed number of fixed-length elements are also fixed-length. For example, uint[2] consists of two elements, each of which is a 32-byte uint type, so uint[2] is 64 bytes in total.
[0095] In addition to fixed-length data storage, there are also variable-length data, or data whose size is unpredictable. In this case, the fixed-length approach cannot directly determine the slot location during compilation, so a different solution is used.
[0096] For example, in contract C1, mapping(uint256=>address)tokenOwner and mapping(uint256=>Counter)ownedTokensCount are defined after state variables A and B. This is a mapping relationship, which is a data type of indeterminate length. In the example of mapping(uint256=>address)tokenOwner, tokenOwner is the name of the mapping relationship.
[0097] The mint function in contract C1, for example, is used to mint NFT resources. Its input parameter, address to, is the recipient's account address of the minted NFT resource, and token id is the identifier of the minted NFT resource. Based on the previously defined mapping relationship, the mapping element tokenOwner[tokenid] = to, ownedTokensCount[to], increment() is defined in the mint function. This mapping element is a state variable and will be stored in the state database. For example, in the case of tokenOwner[tokenid] = to, tokenid and to are the two input parameters of the mint function. Tokenid can also be called the key of the mapping relationship tokenOwner.
[0098] As shown in Figure 7, when the contract code is compiled, the name of the mapping relationship tokenOwner can be assigned the slot 0x000...02 according to the position of the node corresponding to tokenOwner in the abstract syntax tree (i.e., the second position). Since tokenOwner is not a state variable, there is no value in this slot. Similarly, the mapping relationship ownedTokensCount can be assigned the slot with a position value of 0x000...03.
[0099] When the virtual machine executes contract C1, it can uniquely determine the slot of the mapping element corresponding to the key based on the mapping relationship key and the mapping relationship name slot. The slot is, for example: keccak256(key.slot), where "." is a concatenation symbol and the slot after "." is the location of the mapping relationship name slot. The number of mapping elements in the mapping relationship is uncertain, and the length of the key and value in the element is also uncertain. After contract C1 is called several times, there may be two elements in the mapping relationship tokenowner, namely:
[0100] tokenowner["nft1"]=Account1;
[0101] tokenowner["nft2"]=Account2;
[0102] The key of the first mapping element in the mapping relationship tokenOwner is nft1, and the value is Account1. The slot position of the corresponding mapping element tokenOwner[nft1] can be keccak256("nft1".0x000...02), and the value can be stored in one or more consecutive slots starting from this position (for example, the data length of the value is greater than 32 bytes). Similarly, the key of the second mapping element in the mapping relationship tokenOwner is nft2, and the value is Account2. The slot position of the corresponding mapping element tokenOwner[nft2] can be keccak256("nft2".0x000...02), and the value can be stored in one or more consecutive slots starting from this position (for example, the data length of the value is greater than 32 bytes).
[0103] For example, the length of the tokenOwner[nft1] value is less than 32 bytes, and the length of the tokenOwner[nft2] value is greater than 32 bytes and less than 64 bytes.
[0104] As shown in Figure 7, the length of the value of tokenOwner[nft1] is less than 32 bytes, and its value can be stored in one slot. The location of this slot is, for example, the value of keccak256("nft1",0x000...02), for example:
[0105] 0x000...05
[0106] Assume that the length of the tokenOwner[nft2] value is greater than 32 bytes and less than 64 bytes. Its value can be stored in two consecutive slots. The starting positions of these two slots are, for example, the values of keccak256("nft2",0x000...02). The positions of these two slots are, for example:
[0107] 0x000...07
[0108] 0x000...08.
[0109] Similarly, when executing contract C1, the virtual machine can uniquely determine the slot of each element based on the key of the mapping element ownedTokensCount[to] and the slot of the mapping relationship ownedTokensCount, that is, keccak256("to".0x000...03). For example, as shown in Figure 7, the slot of ownedTokensCount[Account1] can be determined as:
[0110] 0x5b4d ed6c c162 9f13 8186 f4b0 7950 04ad bed7 ec13 374d 15ca 04ec 96f1 4913 2460
[0111] The slot of ownedTokensCount[Account2] can be determined as:
[0112] 0x9019 1b3f 1d96 c216 c6a6 637b 9c84 98bc 25cc 907a fe24 6d61 1b3a 8bf7 27bc 081d
[0113] That is, for a contract that includes a mapping relationship, after a transaction calls the contract and defines a mapping element, the slot of the mapping element can be uniquely determined based on the key of the mapping element in the transaction and the slot of the mapping relationship.
[0114] In the blockchain technology field, parallel transaction execution is being adopted to improve transaction efficiency in response to the ever-increasing transaction volume. To enable parallel transaction execution, multiple transactions must be grouped before execution. One related technique groups transactions based on their from and to fields. However, when multiple transactions call the same contract, they are grouped together for serial execution. In practice, these multiple transactions may access different state variables within the contract without creating access conflicts.
[0115] In a second related technique, for multiple transactions that call a contract, each transaction is pre-executed to obtain a pre-execution read / write set for each transaction, and the transactions are then grouped according to their pre-execution read / write sets. The pre-execution read / write sets for each transaction include key-value pairs of state variables accessed during the pre-execution process. However, this approach, which uses pre-execution to obtain transaction read / write sets, is computationally intensive and consumes significant computing and storage resources.
[0116] To this end, embodiments of this specification provide a transaction grouping method. For multiple transactions that call the same contract, the identifiers (e.g., slot positions) of the state variables that each transaction will access during contract execution are determined, and the multiple transactions are grouped based on the identifiers of the state variables accessed by each transaction. Compared to the second related technique described above, this method eliminates the need to pre-execute each transaction and group transactions based on the keys of the state variables that each transaction will access. This reduces the computational complexity and storage resources consumed by transaction grouping, improves the precision of transaction grouping, and further enhances the parallelism of transaction execution.
[0117] FIG8 is a flow chart of a method for grouping transactions in a blockchain according to an embodiment of this specification. The method can be executed by any blockchain node in the blockchain. The method includes the following steps S810-S830.
[0118] First, in step S810, the blockchain node obtains multiple transactions that call the same contract from the transactions to be grouped.
[0119] Specifically, for N transactions to be grouped, the blockchain node can select m transactions that call the same contract (for example, contract C1) from the N transactions based on the to field of each transaction.
[0120] In step S820, the blockchain node determines the set of identifiers of the state variables to be accessed by each of the m transactions. The set of identifiers of a transaction includes the variable identifiers of the state variables to be accessed by the transaction during contract execution.
[0121] Among them, the variable identification during contract execution refers to the identification used to distinguish the various state variables in the contract during the contract execution process, rather than the key of the state variable stored in the state database. The variable identification is, for example, the slot of each state variable as described above. According to the setting of the slot of the state variable above, different state variables in a contract will be assigned different slots, and even if different transactions call the same contract, the virtual machine will still assign the same slot to the same state variable in the contract when executing the different transactions. Therefore, for multiple transactions calling the same contract, it is only necessary to determine the variable identification of the state variable accessed by each transaction during the contract execution, so that the multiple transactions can be correctly grouped, without the need to group the multiple transactions according to the state key of the state variable accessed by each transaction.
[0122] Specifically, for fixed-length state variables in a contract accessed during a transaction, the compiler replaces the identifier of the state variable with the fixed-length state variable's slot when compiling the contract code. Therefore, the virtual machine can directly retrieve the fixed-length state variable's slot from the contract code. In another embodiment, the virtual machine can determine the slot of the fixed-length state variable accessed by the transaction from a pre-stored list of fixed-length state variable slots read and / or written by various functions in the contract.
[0123] For example, as mentioned above, in the code of contract C1, the identifier of the above state variable B is its slot, that is:
[0124] 0x000000000000000000000000000000000000000000000000000000000000001,
[0125] The virtual machine can obtain the slot of the state variable B from the code of contract C1, or it can determine the slot of the fixed-length state variable of the contract read and / or written by the contract function called by the exchange based on the pre-acquired fixed-length state variable slot list of the contract function.
[0126] The slot of state variable B can be used to calculate the state key of state variable B in the state database. For example, the state key is the concatenation of the address of contract C1 and the slot of state variable B:
[0127] 0x3321dcaf8911d3842e14a7a415be2fb1a337f43e000000000000000000000000000000000000000000000000000000000000001.
[0128] It can be seen that the slot of the state variable has a much smaller data volume than the state key of the state variable. Grouping the state variables based on the variable identifiers during contract execution can save storage space (such as memory space) and save the process of generating the state key from the variable identifier. In the grouping process, the number of bytes for identifying the variable identifier is saved, thereby saving computing resources.
[0129] It can be understood that in the embodiments of this specification, the above-mentioned variable identifier is not limited to slot, but can be other types of identifiers, as long as the variable identifier can uniquely identify different state variables in the contract.
[0130] In the case of a variable-length state variable based on a mapping relationship in a transaction access contract, the virtual machine can calculate the slot of the variable-length state variable.
[0131] In one embodiment, the virtual machine can obtain transaction metadata and determine the slot of the variable-length state variable to be accessed by the transaction based on the transaction metadata. The transaction metadata is the transaction body, including the data in the from field, to field, and data field.
[0132] For example, transaction Tx1 is a transaction sent by Alice to call the mint function of contract C1. The transaction body of this transaction is shown in Table 1:
[0133] Table 1
[0134] Among them, in the Data field of transaction Tx1, "bob1" and "nft1" are the two input parameters to the mint() function.
[0135] For each contract deployed on the blockchain, blockchain nodes can predetermine and record the slots for each fixed-length state variable defined in the contract, as well as the slots for each fixed-length state variable requested to be read and / or written by each function. Variable-length state variables (such as mapping elements) cannot be determined before a transaction calls a contract and therefore need to be determined for each transaction.
[0136] Specifically, assuming the mint() function calls the ERC721 interface, the blockchain node can determine the mapping relationships included in the mint() function based on the fixed settings of the ERC721 interface. For example, in the fixed settings of the ERC721 interface, the mint() function must include tokenOwner[tokenid] = to and ownedTokensCount[to], increment(). Therefore, the blockchain node can directly obtain these two mapping relationships based on the call to the mint() function in transaction Tx1.
[0137] Afterwards, the blockchain node can determine the slot of the mapping element to be read and / or written in transaction Tx1 based on these two mapping relationships and the metadata of transaction Tx1.
[0138] Specifically, based on these two mapping relationships and the Data field of transaction Tx1, it can be determined that transaction Tx1 will write the mapping elements tokenOwner[nft1] and ownedTokensCount[bob1]. The slot of the mapping element tokenOwner[nft1] is keccak256(nft1.slot1), and the slot of the mapping element ownedTokensCount[bob1] is keccak256(bob1.slot2). Slot1 is the slot with the mapping name tokenOwner, and slot2 is the slot with the mapping name ownedTokensCount.
[0139] Through the above operations, the slot set of each state variable requested to be read by transaction Tx1 and the slot set of each state variable requested to be written by transaction Tx1 can be determined.
[0140] In another implementation, since the ERC721 interface is a convention rather than a mandatory requirement, the contract developer may introduce some special logic into the implementation of the interface. For example, the contract developer may modify the mint() function to:
[0141] function mint(address to,uint256tokenid)public{
[0142] tokenOwner[tokenid]=to;
[0143] ownedTokensCount[msg.sender],increment()}
[0144] In this case, if we still analyze the slots read and written by transaction Tx1 according to the ERC721 interface, we will obtain incorrect results. Therefore, we can read the mint() function code from the blockchain and perform static analysis on it to determine the mapping relationships included in the mint() function. Then, based on the analyzed mapping relationships, we can determine the slot set of the mapping elements read and / or written by transaction Tx1. This can determine the slot set of each state variable requested to be read by transaction Tx1 (hereinafter referred to as the slot read set) and the slot set of each state variable requested to be written by transaction Tx1 (hereinafter referred to as the slot write set).
[0145] In step S830, the blockchain node groups the multiple transactions according to the identification set of each transaction.
[0146] After determining the slot read sets and slot write sets for each of the multiple transactions as described above, the multiple transactions can be grouped based on their slot read / write sets, according to the rules of read-write mutual exclusion, write-write mutual exclusion, and read-read non-mutual exclusion. It should be understood that this grouping method is only one possible grouping method. In one embodiment, the multiple transactions can be grouped according to the slots accessed by each transaction, that is, regardless of whether the access is read or write. As long as two transactions access the same variable, the two transactions are mutually exclusive and need to be grouped together. If the two transactions do not access the same variable, the two transactions are not mutually exclusive and can be grouped together, that is, the two transactions can be executed in parallel.
[0147] FIG9 is a flow chart of a method for grouping transactions according to the slot read set and slot write set of each transaction in an embodiment of this specification.
[0148] As shown in FIG9 , in step S910 , the blockchain node traverses the slot read set and slot write set of the above m transactions to obtain a read-only slot set.
[0149] Specifically, the blockchain node traverses the slot read sets and slot write sets of the above m transactions, and puts the slots included in any slot read set but not included in all slot write sets into the read-only slot set. That is, the slots in the read-only slot set belong to one slot read set and do not belong to any slot write set.
[0150] In step S920, for transaction Tx1 among the m transactions, the blockchain node traverses the slots accessed by transaction Tx1 and determines whether all the slots accessed by transaction Tx1 are not allocated to threads for parallel execution.
[0151] Specifically, in the embodiments of this specification, a transaction queue can be set up for each execution thread used to execute transactions. When a transaction is assigned to thread Th1, for example, the sequence number of the transaction is added to the transaction queue. A determination is made as to whether the slots are recorded in the slot list. If a slot is not recorded, the slot is added to the slot list. In the slot list, each of the slots accessed by transaction Tx1 is recorded as being assigned to thread Th1, and each of the slots is recorded as being associated with transaction Tx1.
[0152] For each slot accessed by transaction Tx1, the blockchain node can search for the slot in the slot list. If the slot is not recorded in the slot list, it can be determined that the slot is not assigned to a thread. If the slot is recorded in the slot list, it can be determined that the slot has been assigned to a thread.
[0153] After traversing all slots accessed by transaction Tx1 and determining that none of them are allocated, the blockchain node executes step S930, where it adds the transaction sequence number of transaction Tx1 to the transaction queue of thread Th1. Thread Th1 is, for example, the least busy thread among multiple threads executing transactions in parallel. Specifically, the blockchain node traverses the transaction queues of each thread and determines the thread with the fewest transactions in its transaction queue as the least busy thread.
[0154] In step S940, the blockchain node assigns all slots accessed by transaction Tx1 to thread Th1.
[0155] Specifically, the blockchain node can add all the slots in the slot list and record that each slot in the slot list is assigned to thread Th1 and associated with transaction Tx1.
[0156] When it is determined in step S920 that some of the slots accessed by transaction Tx1 are already allocated, that is, not all the slots are unallocated, the blockchain node executes step S950 to determine whether all the allocated slots belong to the read-only slot set.
[0157] If all allocated slots belong to the read-only slot set, the blockchain node can execute step S930.
[0158] If at least some of all allocated slots do not belong to the read-only slot set, step S960 is executed.
[0159] In step S960 , all transactions related to all allocated slots that do not belong to the read-only slot set are determined.
[0160] In step S970, the transaction serial numbers of these transactions are added to the transaction queue of thread Th2, which is, for example, the idlest thread among multiple threads for executing transactions in parallel. Thread Th2 may be the same thread as thread Th1.
[0161] In step S980, all slots accessed by these transactions are allocated to thread Th2.
[0162] Specifically, the blockchain node may add all the slots to the slot list and record that each of the slots is assigned to thread Th2 and associated with transaction Tx1.
[0163] By executing steps S920-S980 in FIG9 for each of the m transactions, the m transactions can be assigned to the transaction queues of the respective threads according to the rules of read-read non-mutually exclusive, read-write mutually exclusive, and write-write mutually exclusive, thereby completing the grouping of the m transactions.
[0164] FIG10 is an architecture diagram of a blockchain node in an embodiment of this specification, which is used to execute the method shown in FIG8 or FIG9, including:
[0165] An acquiring unit 101 is configured to acquire a plurality of first transactions from a plurality of transactions to be grouped, wherein the plurality of first transactions invoke the same contract;
[0166] a determining unit 102, configured to determine a set of identifiers corresponding to state variables of the contract to be accessed by each first transaction, wherein the set of identifiers includes variable identifiers of the state variables during contract execution, and the variable identifiers are used to determine keys of the state variables in a state database;
[0167] The grouping unit 103 is configured to group the multiple first transactions according to the identification set of each first transaction.
[0168] The embodiments of this specification also provide a computer-readable storage medium having a computer program stored thereon. When the computer program is executed in a computer, the computer is caused to execute the method shown in FIG. 8 or FIG. 9 .
[0169] An embodiment of this specification also provides a blockchain node, including a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code, the method shown in Figure 8 or Figure 9 is implemented.
[0170] In the 1990s, technological improvements could be clearly distinguished as either hardware improvements (for example, improvements to circuit structures like diodes, transistors, and switches) or software improvements (improvements to process flows). However, with the advancement of technology, many process flow improvements today can now be considered direct improvements to hardware circuit structures. Designers almost always create the corresponding hardware circuit structure by programming the improved process flow into the hardware circuit. Therefore, it cannot be said that a process flow improvement cannot be implemented using hardware modules. For example, a programmable logic device (PLD), such as a field programmable gate array (FPGA), is an integrated circuit whose logical function is determined by user programming. Designers can "integrate" a digital system on a PLD by programming it themselves, without having to hire a chip manufacturer to design and manufacture a dedicated integrated circuit chip. Moreover, nowadays, instead of manually fabricating integrated circuit chips, this programming is mostly done using "logic compiler" software. This is similar to the software compiler used when developing programs. Before compilation, the original code must also be written in a specific programming language, called a hardware description language (HDL). There is not just one HDL, but many, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc. The most commonly used are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art will also understand that by simply programming the method flow in one of these hardware description languages and then programming it into an integrated circuit, a hardware circuit that implements the logic method flow can be easily obtained.
[0171] The controller can be implemented in any suitable manner. For example, the controller can take the form of a microprocessor or processor and a computer-readable medium storing computer-readable program code (e.g., software or firmware) executable by the (micro)processor, logic gates, switches, application-specific integrated circuits (ASICs), programmable logic controllers, and embedded microcontrollers. Examples of controllers include, but are not limited to, the following microcontrollers: ARC 625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicone Labs C8051F320. The memory controller can also be implemented as part of the control logic of the memory. Those skilled in the art will also know that in addition to implementing the controller in a purely computer-readable program code format, the controller can be implemented in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers by logically programming the method steps. Therefore, such a controller can be considered a hardware component, and the devices included therein for implementing various functions can also be considered as structures within the hardware component. Or even, the devices for implementing various functions can be considered as both software modules that implement the method and structures within the hardware component.
[0172] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a server system. Of course, this application does not exclude that with the future development of computer technology, the computer that implements the functions of the above embodiments may be, for example, a personal computer, a laptop computer, an in-vehicle human-computer interaction device, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0173] Although one or more embodiments of this specification provide method operation steps as described in the embodiments or flow charts, more or fewer operation steps may be included based on conventional or non-creative means. The order of steps listed in the embodiments is only one way of executing the order of many steps and does not represent the only execution order. When the device or terminal product in practice is executed, it can be executed in sequence or in parallel according to the method shown in the embodiments or the drawings (for example, a parallel processor or a multi-threaded processing environment, or even a distributed data processing environment). The term "comprise", "include" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, product or equipment including a series of elements includes not only those elements, but also includes other elements that are not clearly listed, or also includes elements inherent to such process, method, product or equipment. In the absence of more restrictions, it is not excluded that there are other identical or equivalent elements in the process, method, product or equipment including the elements. For example, if the words first, second, etc. are used to represent the name, they do not represent any particular order.
[0174] For the convenience of description, the above devices are described in terms of functions divided into various modules. Of course, when implementing one or more of the present specifications, the functions of each module can be implemented in the same or multiple software and / or hardware, or the module that implements the same function can be implemented by a combination of multiple sub-modules or sub-units, etc. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0175] The present invention is described with reference to the flowcharts and / or block diagrams of the methods, apparatus (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device produce a device for implementing the functions specified in one or more processes in the flowchart and / or one or more blocks in the block diagram.
[0176] These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a product including an instruction device that implements the functions specified in one or more processes in the flowchart and / or one or more boxes in the block diagram.
[0177] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, so that the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one or more processes in the flowchart and / or one or more boxes in the block diagram.
[0178] In a typical configuration, a computing device includes one or more processors (CPUs), input / output interfaces, network interfaces, and memory.
[0179] Memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.
[0180] Computer-readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic tape, magnetic disk storage, graphene storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media such as modulated data signals and carrier waves.
[0181] Those skilled in the art will appreciate that one or more embodiments of this specification may be provided as a method, system, or computer program product. Thus, one or more embodiments of this specification may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware. Furthermore, one or more embodiments of this specification may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0182] One or more embodiments of this specification may be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, and the like that perform specific tasks or implement specific abstract data types. One or more embodiments of this specification may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communications network. In distributed computing environments, program modules may be located in local and remote computer storage media, including storage devices.
[0183] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between the various embodiments can be referenced across them. Each embodiment focuses on the differences from the other embodiments. In particular, since the system embodiments are generally similar to the method embodiments, their description is relatively simple. For relevant parts, reference can be made to the description of the method embodiments. Throughout this specification, reference to the terms "one embodiment," "some embodiments," "examples," "specific examples," or "some examples" means that the specific features, structures, materials, or characteristics described in conjunction with that embodiment or example are included in at least one embodiment or example of this specification. In this specification, the schematic representations of these terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples. Furthermore, those skilled in the art may combine and integrate the different embodiments or examples, and features of different embodiments or examples, described in this specification, without conflict.
[0184] The foregoing is merely an example of one or more embodiments of this specification and is not intended to limit the one or more embodiments of this specification. It will be apparent to those skilled in the art that various modifications and variations may be made to one or more embodiments of this specification. Any modifications, equivalent substitutions, or improvements made within the spirit and principles of this specification shall be included within the scope of the claims.
Claims
1. A transaction grouping method in a blockchain, executed by a blockchain node, comprising: Acquire multiple first transactions from the multiple transactions to be grouped, where the multiple first transactions call the same contract; Determine a set of identifiers corresponding to the state variables of the contract to be accessed by each of the first transactions, wherein the set of identifiers includes variable identifiers of the state variables when the contract is executed, and the variable identifiers are used to determine the keys of the state variables in the state database; The multiple first transactions are grouped according to the identification sets of the respective first transactions.
2. The method according to claim 1, wherein: The contract corresponds to a plurality of numbers arranged in order of size, wherein the variable identifier includes one of the plurality of numbers, and the key of the state variable is generated based on the contract address of the contract and the variable identifier of the state variable.
3. The method according to claim 1 or 2, wherein the identification set includes a first set and a second set, the first set includes variable identifications of state variables of the contract read by the first transaction request, and the second set includes variable identifications of state variables of the contract written by the first transaction request, The grouping the plurality of first transactions according to the identification set of each of the first transactions comprises: According to the first set and the second set of each of the first transactions, when there is an access conflict between two first transactions, the two transactions are grouped into the same transaction group.
4. According to the method according to claim 1 or 2, the state variables include a first state variable, the first state variable is a fixed-length variable, and the first variable identifier of the first state variable is determined in advance based on the declaration position of the first state variable in the contract.
5. The method according to claim 4, wherein the first variable identifier of the first state variable is determined by the following steps: Generate an abstract syntax tree of the contract, the abstract syntax tree comprising a plurality of nodes arranged in sequence, the plurality of nodes corresponding to a plurality of objects declared in the contract, the arrangement order of the plurality of nodes corresponding to the declaration positions of the plurality of objects in the contract, the plurality of objects including the first state variable; The first variable identifier is determined according to an arrangement position of a first node corresponding to the first state variable in the abstract syntax tree.
6. The method according to claim 2, wherein the state variable includes a second state variable, the second state variable and the target parameter conform to a first mapping relationship, the first mapping relationship corresponds to a first number among the multiple numbers, the first number is determined based on a declaration position of the first mapping relationship in the contract, and the determining of the identifier set corresponding to the state variable of the contract accessed by each of the first transaction requests comprises: A second variable identifier of the second state variable is determined based on the target parameter and the first number.
7. The method according to claim 6, wherein the first number is determined by the following steps: Generate an abstract syntax tree of the contract, the abstract syntax tree comprising a plurality of nodes arranged in sequence, the plurality of nodes corresponding to a plurality of objects declared in the contract, an arrangement order of the plurality of nodes corresponding to a declaration position of the plurality of objects in the contract, and the plurality of objects including the first mapping relationship; The first number is determined according to the arrangement position of the second node corresponding to the first mapping relationship in the abstract syntax tree.
8. According to the method according to claim 6, the multiple first transactions include a second transaction, the second transaction includes access to the second state variable, and the target parameter includes any one of the following data: data in the transaction metadata of the second transaction, the value of the third state variable of the contract.
9. According to the method of claim 8, the second transaction calls the first function of the contract, and the determining the set of identifiers corresponding to the state variables of the contract accessed by each of the first transaction requests further comprises: Determine the first mapping relationship according to the first function; Obtain the first number corresponding to the first mapping relationship.
10. The method according to claim 9, wherein determining the first mapping relationship according to the first function comprises: The first mapping relationship is determined according to a pre-acquired correspondence between the first function and the first mapping relationship.
11. The method according to claim 9, wherein determining the first mapping relationship according to the first function comprises: Read the code of the first function from the blockchain; The first mapping relationship is determined according to the code of the first function.
12. The method according to claim 3, wherein grouping the plurality of first transactions according to the identification set of each first transaction comprises: Traversing the first set and the second set of each of the first transactions to obtain a read-only identification set, wherein the variable identifications included in the read-only identification set belong to one of the first sets and do not belong to any of the second sets; For a third transaction among the multiple first transactions, when it is determined that a fourth variable identifier of one or more fourth state variables requested to be accessed by the third transaction has been assigned to any thread among the multiple threads, a fifth variable identifier among the one or more fourth variable identifiers that does not belong to the read-only identifier set is determined, and the multiple threads are used to execute the multiple first transactions in parallel; determining all fourth transactions associated with all the fifth variable identifiers, wherein all the fourth transactions include the third transaction; adding the transaction sequence numbers of all the fourth transactions to the transaction queue of the first thread among the multiple threads; All state variables accessed by all the fourth transactions are assigned to the first thread.
13. A blockchain node, comprising: An acquisition unit, configured to acquire a plurality of first transactions from a plurality of transactions to be grouped, wherein the plurality of first transactions invoke a same contract; a determination unit, configured to determine a set of identifiers corresponding to the state variables of the contract to be accessed by each of the first transactions, wherein the set of identifiers includes variable identifiers of the state variables when the contract is executed, and the variable identifiers are used to determine the keys of the state variables in the state database; A grouping unit is used to group the multiple first transactions according to the identification set of each first transaction.
14. A computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to execute the method according to any one of claims 1 to 12.
15. A blockchain node, comprising a memory and a processor, wherein the memory stores an executable code, and when the processor executes the executable code, the method according to any one of claims 1 to 12 is implemented.
Citation Information
Patent Citations
State data submission method, node and block chain system
CN115129727A
Block chain parallel transaction method and device, equipment and storage medium
CN115578096A
Transaction execution method and block chain node
CN115714652A
Transaction grouping method in block chain and block chain node
CN116308347A
Transaction grouping method in block chain and block chain node
CN116450756A
