Ethereum-based contract size optimization method
By splitting the smart contract into multiple sub-business contracts and entrusting agents through index contracts, the problem of excessive size of the smart contract bytecode is solved, which improves the response speed and reduces the complexity of the smart contract code and the expansion of the architecture.
Patent Information
- Application Number
- CN202510016645.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-06
- Publication Date
- 2025-05-06
AI Technical Summary
With the popularization of Ethereum network, users' logical requirements for smart contracts are becoming more and more complex, resulting in the size of the bytecode of smart contracts becoming larger and larger, affecting the response speed, and becoming an urgent problem that developers are concerned about.
Split the smart contract into multiple sub-business contracts and commissioned through index contracts to reduce network transmission bandwidth usage, reduce the upload and deployment time of contracts, thereby improving response speed.
By splitting the smart contract into multiple sub-service contracts, it effectively reduces network transmission bandwidth usage, reduces the upload and deployment time of contracts, thereby improving response speed, and reducing the complexity of smart contract code and architecture expansion.
Smart Images

Figure CN119946128A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain technology, and in particular to a contract size optimization method based on Ethereum. Background Art
[0002] Blockchain technology is a decentralized distributed ledger technology that allows any participant to jointly maintain a growing block data ledger. These blocks are verified by cryptographic algorithms and linked to the previous block, forming an immutable blockchain. Among them, according to the degree of openness and permission management of the participants, blockchain can be divided into public chain and consortium chain. Among them, the blockchain in the former is mainly represented by Ethereum, which is a completely decentralized blockchain platform that allows developers to create and deploy Turing-complete smart contracts that can execute complex transactions and programs. However, Ethereum smart contracts introduced EIP-170 through the pseudo-dragon hard fork in 2016, limiting the contract size to 24.576KB to prevent denial of service (DOS) attacks, but with the popularity of the Ethereum network, users' logical requirements for smart contracts will become more and more complex, which will lead to the increasing size of the bytecode of smart contracts, affecting the response speed, and becoming a problem that developers are concerned about and need to be solved. Therefore, it is crucial to develop an effective method to reduce the bytecode size of smart contracts to meet the continuous needs of users. Summary of the invention
[0003] Based on the technical problems existing in the background technology, the present invention proposes a contract size optimization method based on Ethereum, which splits the smart contract into multiple sub-business contracts, effectively reduces the network transmission bandwidth occupancy, reduces the contract upload and deployment time, thereby improving the response speed, and solves the problem that the user's logical requirements for smart contracts will become more and more complex, which in turn leads to the increasing size of the bytecode of the smart contract.
[0004] The present invention provides the following technical solution: a contract size optimization method based on Ethereum, comprising the following steps:
[0005] Smart contracts are divided into two categories: index contracts and sub-business contracts. Index smart contracts are used to indirectly execute the functions of sub-business smart contracts by receiving external calls and forwarding these calls to sub-business contracts.
[0006] When a function of a sub-business smart contract is called externally, it is called through the contract address of the index smart contract. After the index smart contract receives the external request, the fallback function receives the interface data, parses the function selector from the interface data, obtains the corresponding business contract address from the implementationList, and delegates the data to the business contract for execution through the delegatecall function, and stores the storage content in the business contract in the index contract.
[0007] Preferably, all state variables in each smart contract include state variables storing the addresses of all sub-business contracts, as well as other state variables for business needs.
[0008] Preferably, except for the state variables storing the addresses of all sub-business contracts in the smart contract, other state variables are classified and sorted, with the state variables storing the addresses of all sub-business contracts placed first, and the others arranged in order.
[0009] Preferably, state variables of data types occupying a fixed size are aligned according to 32 bytes, state variables occupying multiples of 32 bytes are prioritized in front, and state variables of non-fixed size are placed behind all fixed-size state variables.
[0010] Preferably, in addition to storing the state variables of all sub-business contract addresses, the business smart contract is split into two parts according to the number of state variables, and all state variables and related functions of operating corresponding state variables are split into another sub-business smart contract until the compiled size of each sub-business smart contract is met and can be deployed on Ethereum.
[0011] The present invention provides a contract size optimization method based on Ethereum, which splits a smart contract into multiple sub-business contracts, effectively reduces the network transmission bandwidth occupation, reduces the contract upload and deployment time, and thus improves the response speed; and through indexing contracts for delegation and agency, the complexity of the smart contract code and the expansion of the architecture can be reduced; without changing the overall logic of the original business contract, the business contract is simplified and split, and the contract size is also reduced in proportion, thereby achieving the purpose of effectively optimizing the size of the smart contract. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] Figure 1 The index contract processing flow chart of the present invention;
[0013] Figure 2 This is a flow chart of the sub-business contract processing of the present invention. DETAILED DESCRIPTION
[0014] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0015] like Figure 1 and 2 As shown, the present invention provides a technical solution: a contract size optimization method based on Ethereum, comprising the following steps:
[0016] Smart contracts are divided into two categories: index contracts and sub-business contracts. The index smart contract is used to indirectly execute the functions of the sub-business smart contract. It receives external calls and forwards these calls to the sub-business contract. The index contract does not involve any business logic of the sub-business smart contract, but only receives external calls and forwards data. The sub-business smart contract is because the business smart contract is too large to be deployed on Ethereum. It is obtained by splitting the business smart contract code. Each sub-business contract code is completely different, and each sub-business contract cannot contain the same function name and parameter type. This is to ensure the uniqueness of each sub-business smart contract. Splitting the business smart contract into different sub-business smart contracts is to achieve the purpose of unlimited deployment of smart contracts on Ethereum.
[0017] All state variables in each smart contract include state variables that store the addresses of all sub-business contracts, as well as other state variables for business needs.
[0018] Except for the state variables that store the addresses of all sub-business contracts in the smart contract, other state variables are classified and sorted. The state variables that store the addresses of all sub-business contracts are placed first, and the others are arranged in sequence. Among them, the state variables of fixed-size data types are aligned according to 32 bytes. State variables that occupy multiples of 32 bytes are prioritized in front, and state variables of non-fixed size are placed after all fixed-size state variables.
[0019] In addition to storing the state variables of all sub-business contract addresses, the business smart contract is split into two parts according to the number of state variables, and all state variables and related functions corresponding to the state variables are split into another sub-business smart contract until the compiled size of each sub-business smart contract can be deployed on Ethereum. Each sub-business contract contains all state quantities in order to align the slots and prevent slot conflicts.
[0020] When a function of a sub-business smart contract is called externally, it is called through the contract address of the index smart contract. After the index smart contract receives the external request, the fallback function receives the interface data, parses the function selector from the interface data, obtains the corresponding business contract address from the implementationList, delegates the data to the business contract for execution through the delegatecall function, and stores the storage content in the business contract in the index contract.
[0021] Without changing the overall logic of the original business contract, the present invention splits the business contract into multiple sub-business contracts, and at the same time splits the entire business contract into smaller contracts in equal proportion. Theoretically, it can be split infinitely to meet the support of different business needs, thereby achieving the purpose of effectively optimizing the size of the smart contract.
[0022] Ethereum: Ethereum is an open source blockchain platform with smart contract capabilities that allows anyone to develop and deploy smart contracts.
[0023] Smart Contracts: Smart contracts are computer programs that automatically execute contract terms and are stored on the blockchain. They are automatically executed when predetermined conditions are met, without the need for intermediaries, ensuring transparency and trust. In the blockchain, smart contracts are used to automate transactions, execute agreements, and process assets, enhancing efficiency and security.
[0024] Smart contract bytecode: Smart contract bytecode is the binary representation of a smart contract running in the Ethereum Virtual Machine (EVM). It is the direct product of the compilation of the smart contract source code and contains all the logic and instructions of the smart contract.
[0025] EIP-170: EIP-170 is a number in the Ethereum Improvement Proposals (EIP), which defines a new Ethereum Virtual Machine (EVM) opcode (Opcode) for calculating the code size of a contract. This opcode allows a contract to determine its own code size at runtime, which is useful for the implementation of certain smart contract functions, such as dynamic contract size checking. The EIP-170 proposal was proposed in 2017 with the aim of solving some security issues related to contract size and optimizing the execution efficiency of contracts. By introducing this new opcode, smart contract developers can more easily write contracts that can handle codes of different sizes, while also providing additional guarantees for the security of the contract.
[0026] Function selector: The first 4 bytes of a function call data specify the function to be called. This is the first 4 bytes of the Keccak hash of a function signature.
[0027] Fallback function: delegates the call to the business contract.
[0028] Delegatecall: A low-level function calling method in Solidity that allows one contract to execute the code of another contract in the context of the caller. The main feature of delegatecall is that it maintains the storage context of the caller, which means that the storage in the called contract will be the context of the calling contract.
[0029] The above description is only a preferred specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can make equivalent replacements or changes according to the technical scheme and inventive concept of the present invention within the technical scope disclosed by the present invention, which should be covered by the protection scope of the present invention.
Claims
1. A contract size optimization method based on Ethereum, characterized by: The steps include: Smart contracts are divided into two categories: index contracts and sub-business contracts. Index smart contracts are used to indirectly execute the functions of sub-business smart contracts by receiving external calls and forwarding these calls to sub-business contracts. When a function of a sub-business smart contract is called externally, it is called through the contract address of the index smart contract. After the index smart contract receives the external request, the fallback function receives the interface data, parses the function selector from the interface data, obtains the corresponding business contract address from the implementationList, and delegates the data to the business contract for execution through the delegatecall function, and stores the storage content in the business contract in the index contract.
2. The Ethereum-based contract size optimization method according to claim 1, characterized in that: All state variables in each smart contract include state variables that store the addresses of all sub-business contracts, as well as other state variables for business needs.
3. A contract size optimization method based on Ethereum according to claim 2, characterized in that: Except for the state variables that store the addresses of all sub-business contracts in the smart contract, other state variables are classified and sorted, with the state variables that store the addresses of all sub-business contracts placed first, and the others arranged in order.
4. The method for optimizing the contract size based on Ethereum according to claim 3, characterized in that: The state variables of data types that occupy a fixed size are aligned according to 32 bytes. State variables that occupy multiples of 32 bytes are prioritized in the front, and state variables of unfixed size are placed after all fixed-size state variables.
5. The Ethereum-based contract size optimization method according to claim 4, characterized in that: In addition to storing the state variables of all sub-business contract addresses, the business smart contract is split into two parts according to the number of state variables, and all state variables and related functions that operate the corresponding state variables are split into another sub-business smart contract until the size of each sub-business smart contract compilation is sufficient to be deployed on Ethereum.