From 20f04296134d2a1edff3564001b6cf336145cea6 Mon Sep 17 00:00:00 2001 From: Jack Lee <280147597@qq.com> Date: Sat, 27 Apr 2024 22:44:17 +0800 Subject: [PATCH 1/3] added sstore2 to gas-optimization --- .../gas-optimization/sstore2.md | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 docs/general/build/smart-contracts/gas-optimization/sstore2.md diff --git a/docs/general/build/smart-contracts/gas-optimization/sstore2.md b/docs/general/build/smart-contracts/gas-optimization/sstore2.md new file mode 100644 index 0000000000..5d1fd2694d --- /dev/null +++ b/docs/general/build/smart-contracts/gas-optimization/sstore2.md @@ -0,0 +1,72 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Advanced Gas Optimization in Ethereum Smart Contracts: SSTORE2 + +The cost of executing transactions on the Ethereum network can be significantly high, especially when interacting with smart contract storage using the SSTORE opcode. To mitigate these costs, developers can leverage alternative methods like SSTORE2 for more efficient data handling. + +## Understanding SSTORE + +The `SSTORE` opcode is used to store data in Ethereum's contract storage. It writes data to a specific location identified by a key. Both the key and value are 32 bytes. However, this operation is costly in terms of gas: + +- **Writing**: 22,100 gas for 32 bytes, approximately 690 gas per byte. +- **Reading**: The `SLOAD` opcode is similarly expensive. + +## Exploring SSTORE2 + +SSTORE2 optimizes data storage by leveraging the immutability of contract bytecode. Instead of using standard storage, it stores data as part of a contract's bytecode. + +**Key Features of SSTORE2:** + +- **Single Write**: Data is written once using the `CREATE` opcode instead of `SSTORE`. +- **Reading**: Uses the `EXTCODECOPY` opcode to extract data from the bytecode. + +**Demo Code** + +**Writing Data Example:** + +The following example demonstrates how to store data using SSTORE2. + +1. Modify the contract creation bytecode to include the size of the data. +2. Deploy the contract, which stores the data as bytecode. + +```solidity +// Solidity pseudo-code to demonstrate SSTORE2 usage +function deployDataStorageContract(bytes memory data) public returns (address) { + uint256 dataSize = data.length; + // Replace placeholder with data size + bytes memory bytecode = hex"61000080600a3d393df300"; + bytecode[2] = byte(uint8(dataSize)); + bytecode[3] = byte(uint8(dataSize >> 8)); + + address deployedAddress; + assembly { + deployedAddress := create(0, add(bytecode, 32), mload(bytecode)) + } + // Data is prefixed with STOP to prevent execution + return deployedAddress; +} +``` + +**Reading Data Example:** + +To read the stored data, determine the deployed address and extract the bytecode. + +```solidity +function readStoredData(address dataAddress) public view returns (bytes memory) { + uint256 codeSize; + assembly { + codeSize := extcodesize(dataAddress) + } + bytes memory data = new bytes(codeSize - 1); + assembly { + extcodecopy(dataAddress, add(data, 32), 1, sub(codeSize, 1)) + } + return data; +} +``` + +## Conclusion + +Using SSTORE2 reduce the gas costs associated with storing and retrieving large data sets in Ethereum smart contracts. These methods leverage the immutability and execution efficiency of Ethereum's contract mechanics, providing developers with powerful tools to optimize their applications. From b3b048d048a525e5c857676a7243d83f40366b13 Mon Sep 17 00:00:00 2001 From: Jack Lee <280147597@qq.com> Date: Tue, 7 May 2024 22:13:41 +0800 Subject: [PATCH 2/3] updated sstore2.md --- .../gas-optimization/sstore2.md | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/docs/general/build/smart-contracts/gas-optimization/sstore2.md b/docs/general/build/smart-contracts/gas-optimization/sstore2.md index 5d1fd2694d..4a38b061c7 100644 --- a/docs/general/build/smart-contracts/gas-optimization/sstore2.md +++ b/docs/general/build/smart-contracts/gas-optimization/sstore2.md @@ -2,30 +2,22 @@ displayed_sidebar: generalSidebar --- -# Advanced Gas Optimization in Ethereum Smart Contracts: SSTORE2 +# SSTORE2 -The cost of executing transactions on the Ethereum network can be significantly high, especially when interacting with smart contract storage using the SSTORE opcode. To mitigate these costs, developers can leverage alternative methods like SSTORE2 for more efficient data handling. - -## Understanding SSTORE +The cost of executing transactions on the Ethereum network can be very high, especially when interacting with smart contract storage using the `SSTORE` opcode. To mitigate these costs, developers can leverage alternative methods like SSTORE2 for more efficient data handling. The `SSTORE` opcode is used to store data in Ethereum's contract storage. It writes data to a specific location identified by a key. Both the key and value are 32 bytes. However, this operation is costly in terms of gas: - **Writing**: 22,100 gas for 32 bytes, approximately 690 gas per byte. - **Reading**: The `SLOAD` opcode is similarly expensive. -## Exploring SSTORE2 - -SSTORE2 optimizes data storage by leveraging the immutability of contract bytecode. Instead of using standard storage, it stores data as part of a contract's bytecode. - -**Key Features of SSTORE2:** +SSTORE2 is not a new opcode, its a more gas effective method of storing information on Ethereum. SSTORE2 optimizes data storage by leveraging the immutability of contract bytecode. Instead of using standard contract storage, it stores data as part of a contract's bytecode. The key features of SSTORE2 are as follows: - **Single Write**: Data is written once using the `CREATE` opcode instead of `SSTORE`. - **Reading**: Uses the `EXTCODECOPY` opcode to extract data from the bytecode. **Demo Code** -**Writing Data Example:** - The following example demonstrates how to store data using SSTORE2. 1. Modify the contract creation bytecode to include the size of the data. @@ -49,8 +41,6 @@ function deployDataStorageContract(bytes memory data) public returns (address) { } ``` -**Reading Data Example:** - To read the stored data, determine the deployed address and extract the bytecode. ```solidity @@ -67,6 +57,9 @@ function readStoredData(address dataAddress) public view returns (bytes memory) } ``` -## Conclusion +- You can find out more details [here](https://github.com/Vectorized/solady/blob/main/src/utils/SSTORE2.sol) +- You can read more about the gas costs savings [here](https://github.com/0xsequence/sstore2) + +Recommendations for gas optimization: -Using SSTORE2 reduce the gas costs associated with storing and retrieving large data sets in Ethereum smart contracts. These methods leverage the immutability and execution efficiency of Ethereum's contract mechanics, providing developers with powerful tools to optimize their applications. +🌟 Using SSTORE2 reduce the gas costs associated with storing and retrieving large data sets in Ethereum smart contracts. From 1e4f86d2dc7cdb77a1924269c34a34b646485a14 Mon Sep 17 00:00:00 2001 From: Jack Lee <280147597@qq.com> Date: Fri, 10 May 2024 18:48:11 +0800 Subject: [PATCH 3/3] updated sstore2.md --- docs/general/build/smart-contracts/gas-optimization/sstore2.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/general/build/smart-contracts/gas-optimization/sstore2.md b/docs/general/build/smart-contracts/gas-optimization/sstore2.md index 4a38b061c7..01fee3d980 100644 --- a/docs/general/build/smart-contracts/gas-optimization/sstore2.md +++ b/docs/general/build/smart-contracts/gas-optimization/sstore2.md @@ -6,6 +6,8 @@ displayed_sidebar: generalSidebar The cost of executing transactions on the Ethereum network can be very high, especially when interacting with smart contract storage using the `SSTORE` opcode. To mitigate these costs, developers can leverage alternative methods like SSTORE2 for more efficient data handling. +**Note:** While the following discusses the use of SSTORE2 in the context of Ethereum's standard execution environment, it's important to note that in the Conflux Core Space, the storage and pricing models differ significantly from those described below. Although the techniques described can be applied in Core Space, the cost implications may vary. + The `SSTORE` opcode is used to store data in Ethereum's contract storage. It writes data to a specific location identified by a key. Both the key and value are 32 bytes. However, this operation is costly in terms of gas: - **Writing**: 22,100 gas for 32 bytes, approximately 690 gas per byte.