Transaction grouping method in blockchain, and blockchain node
By obtaining the mapping relationship identifiers of the state variables of the same contract called by transactions in the blockchain node and grouping them, the problem of large calculation amount and storage resource occupation of transaction grouping in the existing technology is solved, and the efficiency of transaction execution is improved.
Patent Information
- Application Number
- PCT/CN2023/135042
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-04-28
- Filing Date
- 2023-11-29
- Publication Date
- 2024-10-31
AI Technical Summary
Existing blockchain technology needs to pre-execute transactions to obtain the keys of state variables when grouping transactions, resulting in a large amount of calculation and storage resource occupation, which affects the efficiency of transaction grouping.
By obtaining the mapping relationship identifiers of the state variables of multiple transactions calling the same contract, the transactions are grouped based on these identifiers to avoid pre-executing transactions to obtain the keys of the state variables.
It reduces the calculation amount and storage resource occupation of transaction grouping, and improves the sophistication and parallelism of transaction grouping.
Smart Images

Figure CN2023135042_31102024_PF_FP_ABST
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 April 28, 2023, with application number 202310493502.1 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 more particularly to a transaction grouping method in a blockchain and a blockchain node. 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] Acquire multiple transactions, where the multiple transactions call the same contract, the multiple transactions include multiple first transactions, execution of each of the first transactions includes access to a first variable of the contract, and the first variable corresponds to a mapping relationship in the contract;
[0008] Obtaining a mapping relationship identifier corresponding to a first variable to be accessed during execution of each first transaction, wherein a storage location of each first variable in the state database is determined based on the mapping relationship identifier corresponding to the first variable;
[0009] The multiple first transactions are grouped based on the mapping relationship identifier corresponding to each of the first transactions.
[0010] A second aspect of this specification provides a blockchain node, including:
[0011] an acquisition unit, configured to acquire a plurality of transactions, the plurality of transactions invoking the same contract, the plurality of transactions including a plurality of first transactions, the execution of each of the first transactions including access to a first variable of the contract, the first variable corresponding to a mapping relationship in the contract; and acquire a mapping relationship identifier corresponding to the first variable to be accessed by each of the first transactions during execution, wherein a storage location of each of the first variables in the state database is determined based on the mapping relationship identifier corresponding to the first variable;
[0012] A grouping unit is configured to group the multiple first transactions based on a mapping relationship identifier corresponding to each of the first transactions.
[0013] 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.
[0014] 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.
[0015] In the solution provided in the embodiments of this specification, multiple transactions are grouped based on the mapping relationship corresponding to the variables accessed by multiple transactions that call the same contract, thereby improving the precision of transaction grouping and further improving the parallelism of transaction execution. In addition, there is no need to obtain the key of the state variable to be accessed by each transaction by pre-executing each transaction to group each transaction, thereby reducing the computational complexity of transaction grouping and the storage resources occupied. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] 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.
[0017] FIG1 is a schematic diagram of deploying a smart contract in one embodiment;
[0018] FIG2 is a schematic diagram of calling a smart contract in one embodiment;
[0019] FIG3 is a schematic diagram of a blockchain data storage structure in one embodiment;
[0020] FIG4 is a schematic diagram of a blockchain data storage structure in one embodiment;
[0021] FIG5 is a schematic diagram of an EVM virtual machine module involved in a transaction processing process in one embodiment;
[0022] FIG6 is a schematic diagram of a slot structure in one embodiment;
[0023] FIG7 is a schematic diagram of a slot structure in one embodiment;
[0024] FIG8 is a flow chart of a method for deploying a contract in a blockchain according to an embodiment of this specification;
[0025] FIG9 is a schematic diagram of contract data in an embodiment of this specification;
[0026] FIG10 is a flow chart of a method for grouping multiple transactions according to an embodiment of the present specification;
[0027] FIG11 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] 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.
[0030] 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.
[0031] 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.
[0032] 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 deployed and can be subsequently invoked by users.
[0033] After a contract is successfully deployed, 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.
[0034] As mentioned above, the data field of a transaction that creates a smart contract may include 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.
[0035] 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 (or objects), 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.
[0036] The following is a code example of a simple smart contract written in Solidity language 1:
[0037] Code Example 1. SimpleStorage Code
[0038] In Code Example 1 above, line 2 declares a state variable called 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 containing the address of the initiator of the contract call and the string s.
[0039] As mentioned above, the state variables will eventually be stored in the database. The generated events are generally in the following form:
[0040] [topic1][topic2]...[topicn][data]
[0041] 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:
[0042] [topic1: event ID][data: msg.sender, s]
[0043] 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.
[0044] 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).
[0045] 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.
[0046] 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:
[0047] Contract account: stores the executed smart contract code and the state value in the smart contract code, and can usually only be activated by external account calls;
[0048] Externally owned account: A user's account, such as an Ethereum owner account.
[0049] 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.
[0050] 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.
[0051] Balance: The amount of Ether owned by this address.
[0052] 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.
[0053] 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.
[0054] MPT stands for Merkle Patricia Tree, a tree structure that combines the Merkle Tree (a Merkle tree) and the Patricia Tree (a compressed prefix tree, a more space-efficient Trie tree, 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.
[0055] 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.
[0056] 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.
[0057] 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.
[0058] 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.
[0059] 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.
[0060] 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 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.
[0061] 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.
[0062] For another example, consider the Leaf Node Account C in the state trie in Figure 4. This account 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, for example, uses the sha3 algorithm). The stored value can be (Account), where the account information Account is a two-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 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 nodes (i.e., Leaf Node Variable C) of the state trie.
[0063] 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.
[0064] 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.
[0065] 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 here.) 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.
[0066] 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".
[0067] 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.
[0068] 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 needs convenient access to the underlying key-value database. This access is generally 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.
[0069] 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. FIG6 is a schematic diagram of a slot structure in one embodiment. It should be noted that a total of 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.
[0070] 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.
[0071] 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:
[0072] Code Example 2. Solidity code for contract C1
[0073] Based on the contract code, for example, the following abstract syntax tree is generated: In the abstract syntax tree, information related to an object is placed in a node. These objects include, for example, state variables A and B, and mapping relationships "tokenOwner" and "ownedTokensCount". The multiple nodes corresponding to multiple objects are arranged in the order in which the objects are declared in the contract. For example, the 0th node in the abstract syntax tree corresponds to the state variable A, the 1st node in the abstract syntax tree corresponds to the state variable B, the 2nd node in the abstract syntax tree corresponds to the mapping relationship tokenOwner, and the 3rd node in the abstract syntax tree corresponds to the mapping relationship ownedTokensCount. Each node contains a number of information. Overall, the information inside a node can be called the node information of the abstract syntax tree.
[0074] For the abstract syntax tree of contract C1, the two state variables A and B in contract C1 have node positions 0 and 1 respectively in the abstract syntax tree, which correspond to the following two slots in Figure 6:
[0075] 0x0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
[0076] 0x0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001
[0077] 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.
[0078] 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.
[0079] 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.
[0080] 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.
[0081] 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.
[0082] The results of the execution of the above two contracts can be simply expressed as follows: 0x000...00:0001 (1) 0x000...01:36 (2)
[0083] 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 (i.e., the storage key of the state variable in the storage tree) can be obtained by splicing the contract address + slot position. For example, the address length of contract C1 is 20 bytes, which is 0x3321 dcaf 8911 d384 2e14 a7a4 15be 2fb1 a337 f43e. After splicing the contract address + slot position,
[0084] (1) The state key of the corresponding state variable A is:
[0085] 0x3321dcaf8911d3842e14a7a415be2fb1a337f43e0000000000000000000000000000000000000000000000000000000000
[0086] (2) The state key of the corresponding state variable B is:
[0087] 0x3321dcaf8911d3842e14a7a415be2fb1a337f43e000000000000000000000000000000000000000000000000000000000000001
[0088] 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 broken down into several segments, which are stored sequentially in the nodes of the storage trie from the root to the leaf nodes. Which segment of the state key is stored in each 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, a series of hash value changes occur. The tree construction module constructs the key-value pairs for 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.
[0089] 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.
[0090] 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.
[0091] 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.
[0092] 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.
[0093] 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. Mapping elements are state variables and will be stored in the state database. For example, tokenOwner[tokenid] = to, tokenid and to are the two input parameters of the mint function. Tokenid can also be called the mapping key for the mapping relationship tokenOwner.
[0094] As shown in Figure 7, when compiling the contract code, the name of the mapping relationship tokenOwner is assigned the slot 0x000...02 based on 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, this slot has no value. Similarly, the mapping relationship ownedTokensCount is assigned the slot with a value of 0x000...03. After this assignment, the compiler can replace the name of the mapping relationship in the contract code with its slot.
[0095] 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:
[0096] tokenowner["nft1"]=Account1;
[0097] tokenowner["nft2"]=Account2;
[0098] 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).
[0099] 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.
[0100] 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:
[0101] 0x000...05
[0102] 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:
[0103] 0x000...07
[0104] 0x000...08.
[0105] 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:
[0106] 0x5b4d ed6c c162 9f13 8186 f4b0 7950 04ad bed7 ec13 374d 15ca 04ec 96f1 4913 2460
[0107] The slot of ownedTokensCount[Account2] can be determined as:
[0108] 0x9019 1b3f 1d96 c216 c6a6 637b 9c84 98bc 25cc 907a fe24 6d61 1b3a 8bf7 27bc 081d
[0109] 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.
[0110] In the field of blockchain technology, to cope with the ever-expanding transaction volume, parallel transaction execution has been adopted to improve transaction execution efficiency. In one related technique, for multiple transactions that invoke a contract, each transaction is pre-executed to obtain a pre-execution read / write set for each transaction. The transactions are then grouped according to their pre-execution read / write sets. The pre-execution read / write sets contain 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.
[0111] To this end, embodiments of this specification provide a transaction grouping method. For multiple transactions that invoke the same contract, the method determines the mapping relationship identifiers of the state variables accessed by each transaction during execution and then groups the multiple transactions based on the mapping relationship identifiers of the state variables. Compared to the aforementioned related technologies, this method eliminates the need to pre-execute each transaction and group transactions based on the keys of the state variables they will 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.
[0112] FIG8 is a flow chart of a method for deploying a contract 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-S820.
[0113] First, in step S810, the blockchain node receives transaction Tx1 for deploying contract Contract1. Transaction Tx1 includes contract data of the contract to be deployed, and the contract data includes a read-write information table and / or a call information table.
[0114] As described above, the contract deployer can compile the Solidity code for Contract 1 using a compiler to generate bytecode that can be run on a virtual machine (e.g., the EVM). During the compilation process, if Contract 1 includes declared objects, the compiler will generate corresponding object identifiers for the declared objects according to pre-defined rules and convert the names of the declared objects into the corresponding object identifiers in the generated bytecode.
[0115] The object identifier is, for example, the slot described above. Objects declared in the contract include, for example, state variables, mapping relationships, and other objects. It is understood that the object identifier is not limited to a slot, but can be an identifier generated using other preset rules to uniquely identify the object. For example, the object identifier can be generated based on attributes such as the object's name and length. In one embodiment, if the object declared in the contract is a state variable, the object identifier can be the storage key of the state variable in the state database.
[0116] Taking slots as an example, after the compiler generates the slots for each object declared in the contract, the compiler can generate the storage location information of the state variables accessed by the contract based on the identifiers of each object.
[0117] In one case, the state variable A accessed by the contract is a fixed-length variable, and its storage location information can be the slot of the state variable included in the contract bytecode.
[0118] In another case, the state variable accessed by the contract (such as variable B) is a state variable corresponding to the mapping relationship, and its storage location information may include the parameter information associated with the mapping relationship, and the slot of the mapping relationship in the contract bytecode. The mapping relationship is associated with the sending account of the transaction, or the value of the storage object in the incoming parameters of the transaction to the contract, so that the parameter information is used to indicate the sending account of the transaction, or the incoming parameters of the transaction to the contract. For example, the contract Contract1 includes the mapping relationship a[] and includes access to the state variable a[from], wherein the state variable a[from] is obtained by mapping the sending account of the transaction that calls the contract based on the mapping relationship a[]. The storage location information of the state variable a[from] may include: "slota, {index 0}", wherein slota is the slot of the mapping relationship a, and index0 is used to indicate the from field of the transaction.
[0119] Alternatively, contract Contract1 includes mapping relation b[] and access to state variable b[C], where C is a state variable declared in the contract and b[C] is obtained by mapping state variable C based on mapping relation b[]. The storage location information of state variable b[C] may include "slotb", where slotb is the slot of mapping relation b.
[0120] In one embodiment, the state variables in the contract may correspond to multi-layer mapping relationships. For example, c[b[a[from]]], or d[c[b[C]]], where d[] and c[] are the names of the mapping relationships. For example, the multi-layer mapping relationship c[b[a[from]]] can be simplified to cba[from], that is, cba[] can be regarded as the mapping relationship name (or mapping relationship identifier) of c[b[a[from]]]. In "cba", the leftmost mapping relationship name represents the outermost mapping relationship, and the rightmost mapping relationship name represents the innermost mapping relationship. Correspondingly, when the contract includes access to the state variable c[b[a[from]]], the storage location information of the state variable c[b[a[from]]] may include "slotc, slotb, slota", where the arrangement order of each slot may indicate the layer position of each mapping relationship. For example, the leftmost "slotc" is used to indicate the outermost mapping relationship, and the rightmost "slota" is used to indicate the innermost mapping relationship. Among them, "slotc, slotb, slota" can be simplified to "cba", and this representation is used below to represent the multi-layer mapping relationship corresponding to the state variables.
[0121] When compiling contract code, the compiler not only determines the storage location information for each state variable, but also determines the access mode for that state variable within the contract based on the contract code. Examples of access modes include read mode, write mode, or both read and write mode. Consequently, the compiler can generate an access information table for the contract, which includes the access mode and storage location information for each object requested for access within the contract.
[0122] In one embodiment, a contract includes multiple functions, and the compiler can generate an access information table corresponding to each function. For example, if contract Contract1 includes functions method1 and method2, the compiler can generate an access information table corresponding to function method1 and an access information table corresponding to function method2.
[0123] When Contract 1 calls other contracts, the compiler can also determine the information of the called contracts based on the contract code of Contract 1. Specifically, the compiler can generate contract call information corresponding to each function in Contract 1. For example, if Contract 1 includes functions method1 and method2, the compiler can generate a call information table corresponding to function method1 and a call information table corresponding to function method2.
[0124] For example, the call information table for function method1 includes: the contract address of Contract2 called by function method1, the function name of function method3, the parameters passed from function method1 to function method3 in Contract2, etc. Thus, a call information table for function method1 can be generated. The call information table corresponding to function method1 includes: the contract address of contract Contract2 called by function method1, the function name of function method3 in contract Contract2, and the parameters passed from function method1 to function method3.
[0125] After performing the above operations, the compiler can generate contract data for the contract Contract1, which includes contract bytecode, and an access information table and / or a call information table. In the case where the contract Contract1 includes access objects but does not include calls to other contracts, the contract data includes contract bytecode and an access information table. In the case where the contract Contract1 does not include access objects but includes calls to other contracts, the contract data includes contract bytecode and a call information table. In the case where the contract Contract1 includes access objects and calls to other contracts, the contract data includes contract bytecode, an access information table, and a call information table.
[0126] Specifically, as shown in FIG9 , a schematic diagram of contract data in an embodiment of this specification, the contract data may include function data for each function in contract Contract1. Each function data includes the bytecode corresponding to the function, an access information table, and a call information table. As shown in FIG9 , the contract data for contract Contract1 includes function data for function method1 and function data for function method2, among others. The function data for function method1 is used as an example for description. The function data for function method1 includes the bytecode, access information table, and call information table for the function. The access information table includes multiple read-write items, each corresponding to an object requested for access in function method1. The read-write items include access mode and storage location information. The call information table includes multiple call items, each corresponding to a call to another contract in function method1. The call item includes the contract address of the contract called in function method1, the function name of the function of the contract called in the call, and the parameters passed to the function of the called contract by function method1. It is understood that the contract data in the embodiments of this specification is not limited to that shown in FIG. 9 . For example, depending on different read-write set generation methods, the read-write item may only include storage location information.
[0127] After the contract deployer generates the contract data shown in Figure 9 through the compiler, it can generate a transaction for deploying contract Contract1. The from field of this transaction is the deployer's account, the to field is empty, and the data field includes the contract data, such as shown in Figure 9. The contract deployer can then send this transaction to the blockchain so that each node in the blockchain can receive it.
[0128] At S820 , the contract data is stored in the blockchain.
[0129] After receiving the above transaction and reaching consensus on the transaction, the blockchain node can execute the transaction, thereby deploying contract Contract1 in the blockchain. Specifically, referring to the description above, the blockchain node determines the contract address of contract Contract1 and stores the contract data included in the transaction in association with the contract address in the blockchain. For example, referring to the description of Figure 4 above, the state data of contract Contract1 is created in the state tree, and the CodeHash field in the state data records the hash value of the above contract data. The contract data is then stored in association with its hash value. This allows the contract data of contract Contract1 to be read based on CodeHash, and the bytecode of each function, access information table, or call information table can be read from the contract data.
[0130] By deploying the contract as shown in FIG8 , the blockchain node can read the contract data of the contract stored in the blockchain for transactions that call the contract, and group the transactions according to the storage location information of each variable in the contract data.
[0131] Figure 10 is a flowchart of a method for grouping multiple transactions in accordance with an embodiment of this specification. It is understood that this method can be executed by a blockchain node or by an off-chain trusted device, without limitation. The following description uses the example of a blockchain node executing this method.
[0132] As shown in FIG10 , in S101 , multiple transactions that call the same contract are obtained.
[0133] The blockchain can receive multiple transactions from user clients and broadcast them in the blockchain, so that each node in the blockchain can obtain these multiple transactions and package them into blocks to execute them. These multiple transactions may include transactions that call contracts or transfer transactions. When executing these multiple transactions, the blockchain nodes can group them into groups to execute them in parallel. Based on the above description of the account data structure in the blockchain, it can be understood that there is no access conflict between transactions that call different contracts. Therefore, multiple transactions that call the same contract (for example, Contract 1) can be first selected from the multiple transactions to form a large transaction group. Different groups of transactions that call different contracts can then be executed in parallel.
[0134] For multiple transactions that call the same contract, based on the description of slots above, the contract includes a first type of state variable, whose slot is fixed during compilation, such as variable A mentioned above. The contract also includes a second type of state variable, whose value is determined based on the mapping relationship defined in the contract. The slot of this state variable cannot be determined during compilation; only the slot of the mapping relationship corresponding to the state variable is determined during compilation. State variables corresponding to the same mapping relationship in the contract may correspond to the same storage key. Therefore, two transactions accessing two state variables corresponding to the same mapping relationship may experience an access conflict. Furthermore, as mentioned above, the mapping relationship corresponding to the second type of state variable may be multi-layered. For multiple transactions calling the same contract, if the mapping relationship identifier corresponding to one transaction is at least the outermost layer of the mapping relationship identifier of another transaction, the two transactions may experience an access conflict.
[0135] In other words, the storage key of the second state variable is related to the mapping relationship corresponding to the second state variable. Therefore, for transactions that access the second state variable, the transaction can be divided into transaction groups based on the mapping relationship corresponding to each second state variable. For transactions that access both the first state variable and the second state variable, the transaction can be divided into transaction groups based on the mapping relationship corresponding to the first state variable and each second state variable.
[0136] Therefore, for multiple transactions that call the same contract, when n transactions in the multiple transactions all include access to the second state variable, the n transactions can be grouped according to the correlation between the mapping relationships corresponding to the second state variables accessed by the n transactions.
[0137] In S102 , for the n transactions mentioned above, mapping relationship identifiers corresponding to the variables to be accessed during execution of each transaction are obtained.
[0138] After obtaining the transaction data for each of the n transactions, the blockchain node reads the contract data for Contract 1 from the blockchain based on the contract address of Contract 1 invoked in the transaction. Specifically, referring to Figure 4, the blockchain node reads the state data for Contract 1 from the state tree based on the contract address of Contract 1, reads the CodeHash from the state data, and then reads the data corresponding to the CodeHash based on the CodeHash, i.e., the contract data for the CodeHash.
[0139] In one embodiment, a blockchain node can read the code of the contract function invoked by the transaction from the contract data and retrieve the slots corresponding to the various state variables accessed by the transaction from the function code. For the first type of state variable, the blockchain node retrieves the slot of the state variable itself from the function code; for the second type of state variable, the blockchain node retrieves the slot of the mapping relationship corresponding to the state variable (i.e., the mapping relationship identifier) from the function code.
[0140] In another embodiment, the contract data may include a read / write information table and / or a call information table for contract Contract 1. For example, as shown in Figure 9, the contract data may include the bytecode, access information table, and call information table for each function in contract Contract 1. The blockchain node may read the access information table and / or call information table corresponding to the functions of contract Contract 1 invoked by each of the n transactions from the contract data. The blockchain node may then obtain the storage location information of the state variables accessed by the transactions from the access information table. This storage location information includes the mapping relationship identifier corresponding to the second type of state variable. If contract Contract 1 includes calls to other contracts, the blockchain node may locate the access information table corresponding to the called contract based on the call information table corresponding to the functions of contract Contract 1, and obtain the storage location information of the state variables accessed by the other contracts from this access information table. It will be understood that for the second type of state variables in other contracts invoked in the transaction, the blockchain node may group the transactions in the same manner as for contract Contract 1. Therefore, this description uses contract Contract 1 invoked by the n transactions as an example.
[0141] In S103 , the n transactions are grouped based on the mapping relationship identifier corresponding to each of the n transactions.
[0142] In one case, for transactions Tx1 and Tx2 among n transactions, if transactions Tx1 and Tx2 correspond to the same mapping relationship identifier, it means that there is a possible access conflict between transactions Tx1 and Tx2. Therefore, transactions Tx1 and Tx2 can be grouped into one transaction group.
[0143] In another case, if the mapping relationship identifiers corresponding to transactions Tx1 and Tx2 are different, but the mapping relationship identifier corresponding to one transaction is at least one outermost mapping relationship identifier of the mapping relationship identifier corresponding to the other transaction, then there is a possibility of access conflict between transactions Tx1 and Tx2. Therefore, transactions Tx1 and Tx2 can be grouped into the same transaction group.
[0144] For example, the mapping relationship identifier corresponding to transaction Tx1 includes abcd. According to the description above, in abcd, a is the outermost mapping relationship and d is the innermost mapping relationship. The mapping relationship identifier corresponding to transaction Tx2 also includes ab. Due to the characteristics of the mapping relationship, since ab[] and abcd[] have the same outermost mapping relationship, the storage key of the state variable based on abcd[] accessed by transaction Tx1 and the state variable based on ab[] accessed by transaction Tx2 may be the same. Therefore, there is a possibility of access conflict between transactions Tx1 and Tx2. Therefore, transactions Tx1 and Tx2 are grouped together.
[0145] In one embodiment, for multiple transactions that call contract Contract1 to be grouped (including the n transactions described above), a set SW can be obtained based on the storage location information of the state variables to be accessed by each transaction. The set SW includes the storage location information of the state variables to be written by each transaction during execution. Specifically, the set SW may include slots for the first type of state variables described above, or may include slots or a combination of slots corresponding to one or more layers of mapping relationships for the second type of state variables.
[0146] Afterwards, for example, transaction Tx1 among the n transactions, a set ST1 corresponding to transaction Tx1 is set. The set ST1 is used to store storage location information of various variables used to group transaction Tx1.
[0147] For the first state variable (e.g., variable A) that transaction Tx1 will read during execution, if A is included in set SW, then A is placed in set ST1; if A is not included in set SW, then A is not placed in set ST1. The identifier of the first state variable that transaction Tx1 will write during execution is also placed in set ST1. This way, since reading the same variable by different transactions will not cause access conflicts, variables that have read-read conflicts with other transactions in the same first state variable are excluded from set ST1, resulting in more accurate and precise grouping results.
[0148] For the second state variable that transaction Tx1 will write during execution, the mapping relationship identifier corresponding to the second state variable that transaction Tx1 will write can be put into the set ST1.
[0149] For the second state variable to be read by transaction Tx1 during execution, a preset algorithm may be used to determine whether set SW includes at least one outermost layer of mapping relationship identifiers corresponding to the second state variable to be read by transaction Tx1, where the at least one outermost layer of mapping relationship identifiers is in the form of a mapping relationship identifier combination in set SW. If set SW includes multiple such mapping relationship identifier combinations, the mapping relationship identifier combination with the fewest layers is placed in set ST1.
[0150] For example, assuming that transaction Tx1 will read the variable abcd[C] during execution, it will first determine whether the set SW contains at least one of the outermost mapping relationship identifiers for the mapping relationship identifier abcd. Specifically, it can be determined whether the set SW contains at least one of a, ab, abc, and abcd. If set SW contains ab, for example, this indicates that the variable abcd[C] and ab[D] written by another transaction may correspond to the same storage key. Therefore, the outermost mapping relationship identifier (e.g., ab) can be placed in set ST1. If set SW contains a, ab, and abc, a can be placed in set ST1.
[0151] If the set SW does not include at least one outermost mapping relationship identifier of the mapping relationship identifier corresponding to the second state variable to be read by transaction Tx1 during access, it can be determined whether the mapping relationship identifier corresponding to the second state variable is at least one outermost mapping relationship identifier of any mapping relationship identifier (or combination of mapping relationship identifiers) in the set SW. If so, the mapping relationship identifier corresponding to the second state variable is placed in the set ST1; if not, the mapping relationship identifier corresponding to the second state variable is not placed in the set ST1.
[0152] Specifically, a set SR can be obtained based on the storage location information of the state variables to be accessed by each transaction. This set SR includes mapping relationship identifiers corresponding to the state variables that each transaction will read during execution. For each mapping relationship identifier in set SW, a preset algorithm is used to determine whether at least one outermost layer of mapping relationship identifiers for that mapping relationship identifier exists in set SR. If so, the determined mapping relationship identifier in set SR is added to set SR2. Subsequently, the mapping relationship identifier corresponding to the second state variable can be matched with the set identifiers in set SR2. If a matching mapping relationship identifier is found in set SR2, the mapping relationship identifier corresponding to the second state variable is added to set ST1. Otherwise, the mapping relationship identifier corresponding to the second state variable is not added to set ST1.
[0153] For example, assuming that transaction Tx1 will read variable a[C] during execution, it can be determined whether the set SW includes a mapping relationship identifier with the outermost mapping relationship identifier a, such as ab, abc, etc. If so, the mapping relationship identifier a corresponding to the variable a[C] can be placed in the set ST1. If not, the mapping relationship identifier a will not be placed in the set ST1.
[0154] The blockchain node can obtain the set STi of each transaction Txi in the same way, and then group multiple transactions based on the set STi of each transaction.
[0155] Similar to the processing of the first state variable above, through the processing of the mapping relationship of the second state variable above, the mapping relationship identifiers corresponding to the variables that have only read-read conflicts between transactions and other transactions are excluded from the set corresponding to the transaction, thereby improving the accuracy of transaction grouping by grouping each transaction based on the set STi.
[0156] Among them, when transactions are grouped based on each set, for two transactions among n transactions, such as transaction Tx1 and transaction Tx2, when, for example, the mapping relationship identifier in the set ST1 corresponding to transaction Tx1 is at least the outermost layer of the mapping relationship identifier in the set ST2 corresponding to transaction Tx2, transaction Tx1 and transaction Tx2 can be divided into one transaction group.
[0157] FIG11 is an architecture diagram of a blockchain node in an embodiment of this specification, wherein the blockchain node is configured to execute the method shown in FIG8 or FIG10 , including:
[0158] An acquisition unit 111 is configured to acquire multiple transactions, wherein the multiple transactions invoke the same contract, the multiple transactions including multiple first transactions, the execution of each of the first transactions including access to a first variable of the contract, the first variable corresponding to a mapping relationship in the contract; and acquire a mapping relationship identifier corresponding to the first variable to be accessed by each of the first transactions during execution, wherein a storage location of each of the first variables in the state database is determined based on the mapping relationship identifier corresponding to the first variable.
[0159] The grouping unit 112 is configured to group the multiple first transactions based on a mapping relationship identifier corresponding to each of the first transactions.
[0160] 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. 10 .
[0161] The embodiments of this specification also provide 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 10 is implemented.
[0162] 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.
[0163] 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, an application-specific integrated circuit (ASIC), a programmable logic controller, and an embedded microcontroller. Examples of controllers include, but are not limited to, the following microcontrollers: ARC625D, 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, an application-specific integrated circuit, a programmable logic controller, and an embedded microcontroller by logically programming the method steps. Therefore, such a controller can be considered a hardware component, and the means for implementing various functions included therein can also be considered as structures within the hardware component. Or even, the means for implementing various functions can be considered as both a software module implementing the method and a structure within the hardware component.
[0164] 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.
[0165] 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.
[0166] 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.
[0167] 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.
[0168] 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.
[0169] 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.
[0170] In a typical configuration, a computing device includes one or more processors (CPUs), input / output interfaces, network interfaces, and memory.
[0171] 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.
[0172] 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.
[0173] 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.
[0174] 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.
[0175] 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.
[0176] 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 transactions, the multiple transactions call the same contract, the multiple transactions include multiple first transactions, execution of each of the first transactions includes access to a first variable of the contract, and the first variable corresponds to a mapping relationship in the contract; Obtaining a mapping relationship identifier corresponding to a first variable to be accessed by each first transaction during execution, wherein a storage location of each first variable in the state database is determined based on the mapping relationship identifier corresponding to the first variable; The multiple first transactions are grouped based on the mapping relationship identifier corresponding to each of the first transactions.
2. The method according to claim 1, wherein the mapping relationship identifier comprises an identifier of a multi-layer mapping relationship from outside to inside, The grouping of the plurality of first transactions based on the mapping relationship identifiers corresponding to the respective first transactions includes: For two transactions among the multiple first transactions, when it is determined that a mapping relationship identifier corresponding to one transaction is at least one outermost mapping relationship identifier of a mapping relationship identifier corresponding to another transaction, the two transactions are grouped into one transaction group.
3. According to the method of claim 2, the blockchain stores contract data of the contract, the contract data includes storage location information of state variables requested to be accessed in the contract, including storage location information of the first variable, and the storage location information of the first variable includes a mapping relationship identifier corresponding to the first variable; The obtaining of the mapping relationship identifier corresponding to the first variable to be accessed by each first transaction during execution includes: Reading storage location information of the first variable from contract data of the contract according to the first transaction; A mapping relationship identifier corresponding to the first variable is obtained from the storage location information of the first variable.
4. According to the method according to claim 3, the first transaction calls the first function in the contract, the contract data includes first function data corresponding to the first function, and the first function data includes storage location information of the state variable requested to be accessed by the first function.
5. The method according to claim 2 or 3, wherein: The contract corresponds to a plurality of numbers arranged in order of size, and the mapping relationship identifier includes at least one first number among the plurality of numbers, and the at least one first number is determined at a declaration position in the contract based on at least one layer of mapping relationship corresponding to the mapping relationship identifier.
6. The method according to claim 2 or 3, wherein obtaining a mapping relationship identifier corresponding to a first variable to be accessed by each first transaction during execution comprises: Obtaining a mapping relationship identifier corresponding to the first variable read during execution and a mapping relationship identifier corresponding to the first variable written by each of the first transactions, The grouping of the plurality of first transactions based on the mapping relationship identifiers corresponding to the respective first transactions includes: Generate a first set, the first set including mapping relationship identifiers corresponding to first variables to be written by each of the first transactions during execution; For a first mapping relationship identifier corresponding to a state variable read during execution by a second transaction among the multiple first transactions, determining whether the first set includes at least one outermost layer of mapping relationship identifiers in the first mapping relationship identifiers; In a case where the first set includes the identifier of the at least one layer of mapping relationship in the outermost layer, placing the at least one layer of mapping relationship in the outermost layer into a second set corresponding to the second transaction; The first mapping relationship identifier is at least one of the outermost layers of any mapping relationship identifier in the first set. In the case of a layer mapping relationship identifier, the first mapping relationship identifier is placed in the second set; The second transactions are grouped based on the second set.
7. The method according to claim 6, wherein grouping the second transactions based on the second set comprises: For two second transactions among the multiple first transactions, when it is determined that the first mapping relationship identifier in the second set of one second transaction is at least one outermost mapping relationship of the second mapping relationship identifier in the second set of another second transaction, the two second transactions are grouped into one transaction group.
8. The method according to claim 3, further comprising: receiving a third transaction, the third transaction being used to deploy the contract, the third transaction including storage location information of a state variable requested to be accessed in the contract; According to the third transaction, contract data of the contract is stored in the blockchain, wherein the contract data includes storage location information of state variables requested to be accessed in the contract.
9. A blockchain node, comprising: an acquisition unit, configured to acquire a plurality of transactions, wherein the plurality of transactions call the same contract, the plurality of transactions include a plurality of first transactions, the execution of each of the first transactions includes access to a first variable of the contract, and the first variable corresponds to a mapping relationship in the contract; and acquire a mapping relationship identifier corresponding to a first variable to be accessed by each of the first transactions during execution, wherein a storage location of each of the first variables in a state database is determined based on the mapping relationship identifier corresponding to the first variable; A grouping unit is used to group the multiple first transactions based on the mapping relationship identifier corresponding to each of the first transactions.
10. The blockchain node according to claim 9, wherein the mapping relationship identifier includes an identifier of a multi-layer mapping relationship from outside to inside, The grouping unit is also used for: When it is determined that at least one outermost mapping relationship in two mapping relationship identifiers respectively corresponding to two transactions in the plurality of first transactions is the same, the two transactions are grouped into one transaction group.
11. The blockchain node according to claim 10, wherein the blockchain stores contract data of the contract, the contract data includes storage location information of state variables requested to be accessed in the contract, including storage location information of the first variable, and the storage location information of the first variable includes a mapping relationship identifier corresponding to the first variable; The acquisition unit is also used for: Reading storage location information of the first variable from contract data of the contract according to the first transaction; A mapping relationship identifier corresponding to the first variable is obtained from the storage location information of the first variable.
12. 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 8 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
