diff --git a/i18n/ar/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md b/i18n/ar/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md new file mode 100644 index 0000000000..1879426c25 --- /dev/null +++ b/i18n/ar/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md @@ -0,0 +1,120 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Using Assembly for Math Operations + +When optimizing gas usage in Ethereum smart contracts, common mathematical operations can be made more efficient using assembly. While Solidity provides high-level math operations, using assembly implementations can lead to significant gas savings. + +### Standard vs Assembly Math Operations + +Here's a comparison showing both approaches: + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +contract MathOperations { + // Standard implementation + function standardMax(uint256 x, uint256 y) public pure returns (uint256) { + return x > y ? x : y; + } + + // Assembly optimized implementation + function assemblyMax(uint256 x, uint256 y) public pure returns (uint256 z) { + /// @solidity memory-safe-assembly + assembly { + z := xor(x, mul(xor(x, y), gt(y, x))) + } + } + + // Standard implementation + function standardMin(uint256 x, uint256 y) public pure returns (uint256) { + return x < y ? x : y; + } + + // Assembly optimized implementation + function assemblyMin(uint256 x, uint256 y) public pure returns (uint256 z) { + /// @solidity memory-safe-assembly + assembly { + z := xor(x, mul(xor(x, y), lt(y, x))) + } + } +} +``` + +### Gas Comparison + +| Operation | Standard Implementation | Assembly Implementation | Potential Savings | +| --------- | ------------------------ | ------------------------ | ------------------------ | +| Max | ~300 gas | ~200 gas | ~100 gas | +| Min | ~300 gas | ~200 gas | ~100 gas | + +### Common Assembly Math Operations + +1. **Maximum Value** + +```solidity +function max(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := xor(x, mul(xor(x, y), gt(y, x))) + } +} +``` + +2. **Minimum Value** + +```solidity +function min(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := xor(x, mul(xor(x, y), lt(y, x))) + } +} +``` + +3. **Average (with rounding up)** + +```solidity +function average(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := add(div(add(x, y), 2), and(and(x, y), 1)) + } +} +``` + +### Why Assembly is More Efficient + +1. **Fewer Operations**: Assembly implementations often use fewer EVM operations than their high-level counterparts. + +2. **No Conditional Jumps**: Assembly implementations can avoid conditional jumps (JUMPI operations) which are gas-intensive. + +3. **Direct Memory Access**: Assembly allows direct manipulation of values without additional overhead. + +### When to Use Assembly Math + +โœ… **Recommended for:** + +- High-frequency mathematical operations +- Gas-critical contracts +- Simple mathematical functions +- When maximum efficiency is required + +โŒ **Not recommended for:** + +- Complex mathematical operations +- When code readability is crucial +- When maintaining type safety is important +- Inexperienced developers + +### Additional Resources + +For more optimized math operations, consider exploring: + +- [Solady Library](https://github.com/Vectorized/solady) +- [PRBMath Library](https://github.com/PaulRBerg/prb-math) + +**Warning**: Assembly code bypasses Solidity's safety features. Ensure thorough testing and auditing before deployment. + +#### Recommendations for gas optimization: + +๐ŸŒŸ Consider using these assembly implementations for frequently called math operations in your smart contracts. diff --git a/i18n/ar/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md b/i18n/ar/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md new file mode 100644 index 0000000000..377884bbdf --- /dev/null +++ b/i18n/ar/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md @@ -0,0 +1,117 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Using SUB or XOR Instead of ISZERO(EQ) + +When optimizing gas usage in Ethereum smart contracts, comparison operations in assembly can be optimized by using SUB or XOR instead of ISZERO(EQ). This optimization can lead to gas savings in specific scenarios, particularly for equality checks. + +### Standard EQ vs SUB/XOR Comparison + +Here's a comparison showing different approaches: + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +contract ComparisonExample { + address public owner; + + constructor() { + owner = msg.sender; + } + + // Standard EQ approach + function checkOwnerEQ() external view { + assembly { + if iszero(eq(caller(), sload(owner.slot))) { + revert(0, 0) + } + } + } + + // Optimized SUB approach + function checkOwnerSUB() external view { + assembly { + if sub(caller(), sload(owner.slot)) { + revert(0, 0) + } + } + } + + // Alternative XOR approach + function checkOwnerXOR() external view { + assembly { + if xor(caller(), sload(owner.slot)) { + revert(0, 0) + } + } + } +} +``` + +### Gas Comparison + +| Implementation | Gas Cost (Approximate) | +| ----------------------------- | ----------------------------------------- | +| ISZERO(EQ) | ~22 gas | +| SUB | ~20 gas | +| XOR | ~20 gas | +| Potential Savings | ~2 gas per check | + +### When to Use Each Approach + +#### ISZERO(EQ) + +- Standard approach for equality checks +- Clear and straightforward +- Slightly higher gas cost compared to SUB and XOR + +#### SUB (Subtraction) + +- Best for comparing numerical values or addresses +- Clearer intention in code +- No risk of bit-flip issues +- Slightly more gas efficient than ISZERO(EQ) + +```solidity +assembly { + // Reverts if caller is not owner + if sub(caller(), sload(owner.slot)) { + revert(0, 0) + } +} +``` + +#### XOR (Exclusive OR) + +- Slightly more efficient for bitwise operations +- Must be used carefully due to bit-flip vulnerability +- Not recommended for security-critical comparisons +- Slightly more gas efficient than ISZERO(EQ) + +```solidity +assembly { + if xor(caller(), sload(owner.slot)) { + revert(0, 0) + } +} +``` + +### When to Use This Optimization + +โœ… **Recommended for:** + +- High-frequency equality checks +- Gas-critical loops or functions +- Simple numerical or address comparisons + +โŒ **Not recommended for:** + +- Security-critical comparisons (when using XOR) +- Complex comparison logic +- When code clarity is paramount + +#### Gas Optimization Tips + +๐ŸŒŸ Use SUB for equality checks to save gas, avoid XOR unless necessary for bitwise operations, and ensure to document your optimization decisions clearly. diff --git a/i18n/ar/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md b/i18n/ar/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md index 01b3737b9c..beea9ef159 100644 --- a/i18n/ar/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md +++ b/i18n/ar/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md @@ -101,7 +101,10 @@ Enabling the fullstate mode for dual spaces. enable_single_mpt_storage=true ``` -> The Conflux Foundation currently does not provide snapshot data containing the full historical state; users need to synchronize the data themselves. +> The Conflux Foundation currently does not provide snapshot data containing the full historical state; users need to synchronize the data themselves. To run a **fullstate node**, which enables querying blockchain state on any block height, you need to use a specially compiled Conflux client program. Currently, the official binary release does not enable this feature, and you need to compile it yourself. The compilation command is: +```bash +cargo build --release --features u64-mpt-db-key +``` ### PivotHint diff --git a/i18n/es/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md b/i18n/es/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md new file mode 100644 index 0000000000..264fbdda39 --- /dev/null +++ b/i18n/es/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md @@ -0,0 +1,120 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Using Assembly for Math Operations + +When optimizing gas usage in Ethereum smart contracts, common mathematical operations can be made more efficient using assembly. While Solidity provides high-level math operations, using assembly implementations can lead to significant gas savings. + +### Standard vs Assembly Math Operations + +Here's a comparison showing both approaches: + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +contract MathOperations { + // Standard implementation + function standardMax(uint256 x, uint256 y) public pure returns (uint256) { + return x > y ? x : y; + } + + // Assembly optimized implementation + function assemblyMax(uint256 x, uint256 y) public pure returns (uint256 z) { + /// @solidity memory-safe-assembly + assembly { + z := xor(x, mul(xor(x, y), gt(y, x))) + } + } + + // Standard implementation + function standardMin(uint256 x, uint256 y) public pure returns (uint256) { + return x < y ? x : y; + } + + // Assembly optimized implementation + function assemblyMin(uint256 x, uint256 y) public pure returns (uint256 z) { + /// @solidity memory-safe-assembly + assembly { + z := xor(x, mul(xor(x, y), lt(y, x))) + } + } +} +``` + +### Gas Comparison + +| Operation | Standard Implementation | Assembly Implementation | Potential Savings | +| --------- | ------------------------ | ------------------------ | ------------------------ | +| Max | ~300 gas | ~200 gas | ~100 gas | +| Min | ~300 gas | ~200 gas | ~100 gas | + +### Common Assembly Math Operations + +1. **Maximum Value** + +```solidity +function max(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := xor(x, mul(xor(x, y), gt(y, x))) + } +} +``` + +2. **Minimum Value** + +```solidity +function min(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := xor(x, mul(xor(x, y), lt(y, x))) + } +} +``` + +3. **Average (with rounding up)** + +```solidity +function average(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := add(div(add(x, y), 2), and(and(x, y), 1)) + } +} +``` + +### Why Assembly is More Efficient + +1. **Fewer Operations**: Assembly implementations often use fewer EVM operations than their high-level counterparts. + +2. **No Conditional Jumps**: Assembly implementations can avoid conditional jumps (JUMPI operations) which are gas-intensive. + +3. **Direct Memory Access**: Assembly allows direct manipulation of values without additional overhead. + +### When to Use Assembly Math + +โœ… **Recommended for:** + +- High-frequency mathematical operations +- Gas-critical contracts +- Simple mathematical functions +- When maximum efficiency is required + +โŒ **Not recommended for:** + +- Complex mathematical operations +- When code readability is crucial +- When maintaining type safety is important +- Inexperienced developers + +### Recursos Adicionales + +For more optimized math operations, consider exploring: + +- [Solady Library](https://github.com/Vectorized/solady) +- [PRBMath Library](https://github.com/PaulRBerg/prb-math) + +**Warning**: Assembly code bypasses Solidity's safety features. Ensure thorough testing and auditing before deployment. + +#### Recommendations for gas optimization: + +๐ŸŒŸ Consider using these assembly implementations for frequently called math operations in your smart contracts. diff --git a/i18n/es/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md b/i18n/es/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md new file mode 100644 index 0000000000..377884bbdf --- /dev/null +++ b/i18n/es/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md @@ -0,0 +1,117 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Using SUB or XOR Instead of ISZERO(EQ) + +When optimizing gas usage in Ethereum smart contracts, comparison operations in assembly can be optimized by using SUB or XOR instead of ISZERO(EQ). This optimization can lead to gas savings in specific scenarios, particularly for equality checks. + +### Standard EQ vs SUB/XOR Comparison + +Here's a comparison showing different approaches: + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +contract ComparisonExample { + address public owner; + + constructor() { + owner = msg.sender; + } + + // Standard EQ approach + function checkOwnerEQ() external view { + assembly { + if iszero(eq(caller(), sload(owner.slot))) { + revert(0, 0) + } + } + } + + // Optimized SUB approach + function checkOwnerSUB() external view { + assembly { + if sub(caller(), sload(owner.slot)) { + revert(0, 0) + } + } + } + + // Alternative XOR approach + function checkOwnerXOR() external view { + assembly { + if xor(caller(), sload(owner.slot)) { + revert(0, 0) + } + } + } +} +``` + +### Gas Comparison + +| Implementation | Gas Cost (Approximate) | +| ----------------------------- | ----------------------------------------- | +| ISZERO(EQ) | ~22 gas | +| SUB | ~20 gas | +| XOR | ~20 gas | +| Potential Savings | ~2 gas per check | + +### When to Use Each Approach + +#### ISZERO(EQ) + +- Standard approach for equality checks +- Clear and straightforward +- Slightly higher gas cost compared to SUB and XOR + +#### SUB (Subtraction) + +- Best for comparing numerical values or addresses +- Clearer intention in code +- No risk of bit-flip issues +- Slightly more gas efficient than ISZERO(EQ) + +```solidity +assembly { + // Reverts if caller is not owner + if sub(caller(), sload(owner.slot)) { + revert(0, 0) + } +} +``` + +#### XOR (Exclusive OR) + +- Slightly more efficient for bitwise operations +- Must be used carefully due to bit-flip vulnerability +- Not recommended for security-critical comparisons +- Slightly more gas efficient than ISZERO(EQ) + +```solidity +assembly { + if xor(caller(), sload(owner.slot)) { + revert(0, 0) + } +} +``` + +### When to Use This Optimization + +โœ… **Recommended for:** + +- High-frequency equality checks +- Gas-critical loops or functions +- Simple numerical or address comparisons + +โŒ **Not recommended for:** + +- Security-critical comparisons (when using XOR) +- Complex comparison logic +- When code clarity is paramount + +#### Gas Optimization Tips + +๐ŸŒŸ Use SUB for equality checks to save gas, avoid XOR unless necessary for bitwise operations, and ensure to document your optimization decisions clearly. diff --git a/i18n/es/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md b/i18n/es/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md index 01b3737b9c..beea9ef159 100644 --- a/i18n/es/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md +++ b/i18n/es/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md @@ -101,7 +101,10 @@ Enabling the fullstate mode for dual spaces. enable_single_mpt_storage=true ``` -> The Conflux Foundation currently does not provide snapshot data containing the full historical state; users need to synchronize the data themselves. +> The Conflux Foundation currently does not provide snapshot data containing the full historical state; users need to synchronize the data themselves. To run a **fullstate node**, which enables querying blockchain state on any block height, you need to use a specially compiled Conflux client program. Currently, the official binary release does not enable this feature, and you need to compile it yourself. The compilation command is: +```bash +cargo build --release --features u64-mpt-db-key +``` ### PivotHint diff --git a/i18n/ja/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md b/i18n/ja/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md new file mode 100644 index 0000000000..1879426c25 --- /dev/null +++ b/i18n/ja/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md @@ -0,0 +1,120 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Using Assembly for Math Operations + +When optimizing gas usage in Ethereum smart contracts, common mathematical operations can be made more efficient using assembly. While Solidity provides high-level math operations, using assembly implementations can lead to significant gas savings. + +### Standard vs Assembly Math Operations + +Here's a comparison showing both approaches: + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +contract MathOperations { + // Standard implementation + function standardMax(uint256 x, uint256 y) public pure returns (uint256) { + return x > y ? x : y; + } + + // Assembly optimized implementation + function assemblyMax(uint256 x, uint256 y) public pure returns (uint256 z) { + /// @solidity memory-safe-assembly + assembly { + z := xor(x, mul(xor(x, y), gt(y, x))) + } + } + + // Standard implementation + function standardMin(uint256 x, uint256 y) public pure returns (uint256) { + return x < y ? x : y; + } + + // Assembly optimized implementation + function assemblyMin(uint256 x, uint256 y) public pure returns (uint256 z) { + /// @solidity memory-safe-assembly + assembly { + z := xor(x, mul(xor(x, y), lt(y, x))) + } + } +} +``` + +### Gas Comparison + +| Operation | Standard Implementation | Assembly Implementation | Potential Savings | +| --------- | ------------------------ | ------------------------ | ------------------------ | +| Max | ~300 gas | ~200 gas | ~100 gas | +| Min | ~300 gas | ~200 gas | ~100 gas | + +### Common Assembly Math Operations + +1. **Maximum Value** + +```solidity +function max(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := xor(x, mul(xor(x, y), gt(y, x))) + } +} +``` + +2. **Minimum Value** + +```solidity +function min(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := xor(x, mul(xor(x, y), lt(y, x))) + } +} +``` + +3. **Average (with rounding up)** + +```solidity +function average(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := add(div(add(x, y), 2), and(and(x, y), 1)) + } +} +``` + +### Why Assembly is More Efficient + +1. **Fewer Operations**: Assembly implementations often use fewer EVM operations than their high-level counterparts. + +2. **No Conditional Jumps**: Assembly implementations can avoid conditional jumps (JUMPI operations) which are gas-intensive. + +3. **Direct Memory Access**: Assembly allows direct manipulation of values without additional overhead. + +### When to Use Assembly Math + +โœ… **Recommended for:** + +- High-frequency mathematical operations +- Gas-critical contracts +- Simple mathematical functions +- When maximum efficiency is required + +โŒ **Not recommended for:** + +- Complex mathematical operations +- When code readability is crucial +- When maintaining type safety is important +- Inexperienced developers + +### Additional Resources + +For more optimized math operations, consider exploring: + +- [Solady Library](https://github.com/Vectorized/solady) +- [PRBMath Library](https://github.com/PaulRBerg/prb-math) + +**Warning**: Assembly code bypasses Solidity's safety features. Ensure thorough testing and auditing before deployment. + +#### Recommendations for gas optimization: + +๐ŸŒŸ Consider using these assembly implementations for frequently called math operations in your smart contracts. diff --git a/i18n/ja/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md b/i18n/ja/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md new file mode 100644 index 0000000000..377884bbdf --- /dev/null +++ b/i18n/ja/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md @@ -0,0 +1,117 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Using SUB or XOR Instead of ISZERO(EQ) + +When optimizing gas usage in Ethereum smart contracts, comparison operations in assembly can be optimized by using SUB or XOR instead of ISZERO(EQ). This optimization can lead to gas savings in specific scenarios, particularly for equality checks. + +### Standard EQ vs SUB/XOR Comparison + +Here's a comparison showing different approaches: + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +contract ComparisonExample { + address public owner; + + constructor() { + owner = msg.sender; + } + + // Standard EQ approach + function checkOwnerEQ() external view { + assembly { + if iszero(eq(caller(), sload(owner.slot))) { + revert(0, 0) + } + } + } + + // Optimized SUB approach + function checkOwnerSUB() external view { + assembly { + if sub(caller(), sload(owner.slot)) { + revert(0, 0) + } + } + } + + // Alternative XOR approach + function checkOwnerXOR() external view { + assembly { + if xor(caller(), sload(owner.slot)) { + revert(0, 0) + } + } + } +} +``` + +### Gas Comparison + +| Implementation | Gas Cost (Approximate) | +| ----------------------------- | ----------------------------------------- | +| ISZERO(EQ) | ~22 gas | +| SUB | ~20 gas | +| XOR | ~20 gas | +| Potential Savings | ~2 gas per check | + +### When to Use Each Approach + +#### ISZERO(EQ) + +- Standard approach for equality checks +- Clear and straightforward +- Slightly higher gas cost compared to SUB and XOR + +#### SUB (Subtraction) + +- Best for comparing numerical values or addresses +- Clearer intention in code +- No risk of bit-flip issues +- Slightly more gas efficient than ISZERO(EQ) + +```solidity +assembly { + // Reverts if caller is not owner + if sub(caller(), sload(owner.slot)) { + revert(0, 0) + } +} +``` + +#### XOR (Exclusive OR) + +- Slightly more efficient for bitwise operations +- Must be used carefully due to bit-flip vulnerability +- Not recommended for security-critical comparisons +- Slightly more gas efficient than ISZERO(EQ) + +```solidity +assembly { + if xor(caller(), sload(owner.slot)) { + revert(0, 0) + } +} +``` + +### When to Use This Optimization + +โœ… **Recommended for:** + +- High-frequency equality checks +- Gas-critical loops or functions +- Simple numerical or address comparisons + +โŒ **Not recommended for:** + +- Security-critical comparisons (when using XOR) +- Complex comparison logic +- When code clarity is paramount + +#### Gas Optimization Tips + +๐ŸŒŸ Use SUB for equality checks to save gas, avoid XOR unless necessary for bitwise operations, and ensure to document your optimization decisions clearly. diff --git a/i18n/ja/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md b/i18n/ja/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md index 01b3737b9c..beea9ef159 100644 --- a/i18n/ja/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md +++ b/i18n/ja/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md @@ -101,7 +101,10 @@ Enabling the fullstate mode for dual spaces. enable_single_mpt_storage=true ``` -> The Conflux Foundation currently does not provide snapshot data containing the full historical state; users need to synchronize the data themselves. +> The Conflux Foundation currently does not provide snapshot data containing the full historical state; users need to synchronize the data themselves. To run a **fullstate node**, which enables querying blockchain state on any block height, you need to use a specially compiled Conflux client program. Currently, the official binary release does not enable this feature, and you need to compile it yourself. The compilation command is: +```bash +cargo build --release --features u64-mpt-db-key +``` ### PivotHint diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md b/i18n/zh/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md new file mode 100644 index 0000000000..8d696b03f5 --- /dev/null +++ b/i18n/zh/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/assemblyMathOperations.md @@ -0,0 +1,120 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Using Assembly for Math Operations + +When optimizing gas usage in Ethereum smart contracts, common mathematical operations can be made more efficient using assembly. While Solidity provides high-level math operations, using assembly implementations can lead to significant gas savings. + +### Standard vs Assembly Math Operations + +Here's a comparison showing both approaches: + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +contract MathOperations { + // Standard implementation + function standardMax(uint256 x, uint256 y) public pure returns (uint256) { + return x > y ? x : y; + } + + // Assembly optimized implementation + function assemblyMax(uint256 x, uint256 y) public pure returns (uint256 z) { + /// @solidity memory-safe-assembly + assembly { + z := xor(x, mul(xor(x, y), gt(y, x))) + } + } + + // Standard implementation + function standardMin(uint256 x, uint256 y) public pure returns (uint256) { + return x < y ? x : y; + } + + // Assembly optimized implementation + function assemblyMin(uint256 x, uint256 y) public pure returns (uint256 z) { + /// @solidity memory-safe-assembly + assembly { + z := xor(x, mul(xor(x, y), lt(y, x))) + } + } +} +``` + +### Gas Comparison + +| Operation | Standard Implementation | Assembly Implementation | Potential Savings | +| --------- | ------------------------ | ------------------------ | ------------------------ | +| Max | ~300 gas | ~200 gas | ~100 gas | +| Min | ~300 gas | ~200 gas | ~100 gas | + +### Common Assembly Math Operations + +1. **Maximum Value** + +```solidity +function max(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := xor(x, mul(xor(x, y), gt(y, x))) + } +} +``` + +2. **Minimum Value** + +```solidity +function min(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := xor(x, mul(xor(x, y), lt(y, x))) + } +} +``` + +3. **Average (with rounding up)** + +```solidity +function average(uint256 x, uint256 y) public pure returns (uint256 z) { + assembly { + z := add(div(add(x, y), 2), and(and(x, y), 1)) + } +} +``` + +### Why Assembly is More Efficient + +1. **Fewer Operations**: Assembly implementations often use fewer EVM operations than their high-level counterparts. + +2. **No Conditional Jumps**: Assembly implementations can avoid conditional jumps (JUMPI operations) which are gas-intensive. + +3. **Direct Memory Access**: Assembly allows direct manipulation of values without additional overhead. + +### When to Use Assembly Math + +โœ… **Recommended for:** + +- High-frequency mathematical operations +- Gas-critical contracts +- Simple mathematical functions +- When maximum efficiency is required + +โŒ **Not recommended for:** + +- Complex mathematical operations +- When code readability is crucial +- When maintaining type safety is important +- Inexperienced developers + +### ๅ…ถไป–่ต„ๆบ + +For more optimized math operations, consider exploring: + +- [Solady Library](https://github.com/Vectorized/solady) +- [PRBMath Library](https://github.com/PaulRBerg/prb-math) + +**Warning**: Assembly code bypasses Solidity's safety features. Ensure thorough testing and auditing before deployment. + +#### ๅ…ณไบŽ gas ไผ˜ๅŒ–็š„ๅปบ่ฎฎ๏ผš + +๐ŸŒŸ Consider using these assembly implementations for frequently called math operations in your smart contracts. diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md b/i18n/zh/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md new file mode 100644 index 0000000000..377884bbdf --- /dev/null +++ b/i18n/zh/docusaurus-plugin-content-docs/current/general/build/smart-contracts/gas-optimization/useSUBorXOR.md @@ -0,0 +1,117 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Using SUB or XOR Instead of ISZERO(EQ) + +When optimizing gas usage in Ethereum smart contracts, comparison operations in assembly can be optimized by using SUB or XOR instead of ISZERO(EQ). This optimization can lead to gas savings in specific scenarios, particularly for equality checks. + +### Standard EQ vs SUB/XOR Comparison + +Here's a comparison showing different approaches: + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +contract ComparisonExample { + address public owner; + + constructor() { + owner = msg.sender; + } + + // Standard EQ approach + function checkOwnerEQ() external view { + assembly { + if iszero(eq(caller(), sload(owner.slot))) { + revert(0, 0) + } + } + } + + // Optimized SUB approach + function checkOwnerSUB() external view { + assembly { + if sub(caller(), sload(owner.slot)) { + revert(0, 0) + } + } + } + + // Alternative XOR approach + function checkOwnerXOR() external view { + assembly { + if xor(caller(), sload(owner.slot)) { + revert(0, 0) + } + } + } +} +``` + +### Gas Comparison + +| Implementation | Gas Cost (Approximate) | +| ----------------------------- | ----------------------------------------- | +| ISZERO(EQ) | ~22 gas | +| SUB | ~20 gas | +| XOR | ~20 gas | +| Potential Savings | ~2 gas per check | + +### When to Use Each Approach + +#### ISZERO(EQ) + +- Standard approach for equality checks +- Clear and straightforward +- Slightly higher gas cost compared to SUB and XOR + +#### SUB (Subtraction) + +- Best for comparing numerical values or addresses +- Clearer intention in code +- No risk of bit-flip issues +- Slightly more gas efficient than ISZERO(EQ) + +```solidity +assembly { + // Reverts if caller is not owner + if sub(caller(), sload(owner.slot)) { + revert(0, 0) + } +} +``` + +#### XOR (Exclusive OR) + +- Slightly more efficient for bitwise operations +- Must be used carefully due to bit-flip vulnerability +- Not recommended for security-critical comparisons +- Slightly more gas efficient than ISZERO(EQ) + +```solidity +assembly { + if xor(caller(), sload(owner.slot)) { + revert(0, 0) + } +} +``` + +### When to Use This Optimization + +โœ… **Recommended for:** + +- High-frequency equality checks +- Gas-critical loops or functions +- Simple numerical or address comparisons + +โŒ **Not recommended for:** + +- Security-critical comparisons (when using XOR) +- Complex comparison logic +- When code clarity is paramount + +#### Gas Optimization Tips + +๐ŸŒŸ Use SUB for equality checks to save gas, avoid XOR unless necessary for bitwise operations, and ensure to document your optimization decisions clearly. diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md b/i18n/zh/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md index b7c2d61b6c..e2ef7db84b 100644 --- a/i18n/zh/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md +++ b/i18n/zh/docusaurus-plugin-content-docs/current/general/run-a-node/advanced-topics/node-configuration.md @@ -101,7 +101,10 @@ single_mpt_space = "evm" # ๆ ธๅฟƒ็ฉบ้—ดไฝฟ็”จ"native" enable_single_mpt_storage=true ``` -> The Conflux Foundation currently does not provide snapshot data containing the full historical state; users need to synchronize the data themselves. +> The Conflux Foundation currently does not provide snapshot data containing the full historical state; users need to synchronize the data themselves. To run a **fullstate node**, which enables querying blockchain state on any block height, you need to use a specially compiled Conflux client program. Currently, the official binary release does not enable this feature, and you need to compile it yourself. The compilation command is: +```bash +cargo build --release --features u64-mpt-db-key +``` ### PivotHint