当前位置:网站首页>Precautions for scalable contract solution based on openzeppelin
Precautions for scalable contract solution based on openzeppelin
2022-07-02 11:35:00 【Ni xiansen_】
be based on Openzeppelin Considerations for scalable contract solutions
matters needing attention
Constructors
Please Do not use Constructors contructor()
, We know that the runtime logic of scalable contracts is separated from data , Contract data is stored in Agency contract
in , The contract we wrote is Logical contract
, When the contract is deployed , Logical contract
call contructor()
The initialization data is Logical contract
Of , Agency contract
The data in is not initialized , So it's invalid .
Include Global variables
Assign initial value when declaring , Because this is equivalent to the constructor contructor()
Set these values in .
Parent contract initialization
If MyContract Inherited from contract BaseContract, that BaseContract Contract initialization function initialize() Of modifier( Decorators ) You have to use onlyInitializing, such as :
contract BaseContract is Initializable {
uint256 public y;
function initialize() public onlyInitializing {
y = 42;
}
}
// BaseContract Inherited from Initializable, There is no need to repeat explicit inheritance Initializable
contract MyContract is BaseContract {
int storageValue;
// modifier( Decorators ) initializer To ensure that initialize Will only be called once
function initialize(int initValue) public initializer {
BaseContract.initialize();
storageValue = initValue;
}
......
}
Declare state variables
- When declaring state variables , It cannot be assigned Initial value
It's wrong to do like this
contract MyContract is Initializable {
int storageValue = 666; // Invalid initial value
function initialize() public initializer {
}
......
}
Because this is equivalent to the constructor contructor()
Set these values in , Therefore, it does not apply to scalable conventions .
- Defining constant state variables can still
It's OK like this
contract MyContract is Initializable {
// constant Constant keyword
int public constant storageValue = 666; // It works
function initialize() public initializer {
}
......
}
Create a new contract instance in the contract code
- Do not create new contract instances in the contract , The contract created is not scalable
like this , Even if MyContract It's upgradeable , but ERC20 Instances cannot be upgraded
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyContract is Initializable {
ERC20 public token;
function initialize() public initializer {
token = new ERC20("Test", "TST"); // This contract cannot be upgraded
}
......
}
- If you want to MyContract Quote other upgradeable contracts , Then inject the deployed scalable contract instance into MyContract in , It's a good solution
like this
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
contract MyContract is Initializable {
IERC20Upgradeable public token;
function initialize(IERC20Upgradeable _token) public initializer {
token = _token;
}
......
}
Declare the state variable when upgrading the contract
When writing a new version of the contract , Whether due to new features or bug Repair , There is an additional limitation to be observed : The declaration order of contract state variables cannot be changed , Nor can you change its type .
For example, the current version (V1) Contract state variable layout
contract MyContractV1 {
uint256 private x;
string private y;
}
When writing the new version of the contract , Please avoid the wrong operation :
- Change the type of variable
This is wrong
contract MyContractV2 {
string private x;
string private y;
}
- Change the order in which they are declared
This is wrong
contract MyContractV2 {
string private y;
uint256 private x;
}
- Introduce a new variable before the existing variable
This is wrong
contract MyContractV2 {
bytes private a;
uint256 private x;
string private y;
}
That's right
contract MyContractV2 {
uint256 private x;
string private y;
bytes private a;
}
- Delete existing variables
This is wrong
contract MyContractV2 {
string private y;
}
Reference documents :
Openzeppelin Write upgradeable contracts :https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable
边栏推荐
- Implementation of six singleton modes
- 原生方法合并word
- 微信小程序利用百度api达成植物识别
- TIPC introduction 1
- Never forget, there will be echoes | hanging mirror sincerely invites you to participate in the opensca user award research
- Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
- Redis exceeds the maximum memory error oom command not allowed when used memory & gt; ' maxmemory'
- Functional interfaces and method references
- Some things configured from ros1 to ros2
- 从攻击面视角,看信创零信任方案实践
猜你喜欢
RPA advanced (II) uipath application practice
TIPC Service and Topology Tracking4
一.STM32的开发环境,keil5/MDK5.14安装教程(附下载链接)
Compilation errors and printout garbled problems caused by Chinese content in vs2019 code
念念不忘,必有回响 | 悬镜诚邀您参与OpenSCA用户有奖调研
Openmldb meetup No.4 meeting minutes
I STM32 development environment, keil5/mdk5.14 installation tutorial (with download link)
从攻击面视角,看信创零信任方案实践
Map set assignment to database
What are the software product management systems? Inventory of 12 best product management tools
随机推荐
bedtools使用教程
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
Is it safe to open a stock account through the QR code of the securities manager? Or is it safe to open an account in a securities company?
Wechat applet uses Baidu API to achieve plant recognition
The first white paper on agile practice in Chinese enterprises was released | complete download is attached
2022年4月17日五心红娘团队收获双份喜报
MTK full dump grab
TIPC Getting Started6
ROS lacks xacro package
JS -- take a number randomly from the array every call, and it cannot be the same as the last time
The difference between self and static in PHP in methods
TIPC addressing 2
念念不忘,必有回响 | 悬镜诚邀您参与OpenSCA用户有奖调研
Jenkins installation
Supermarket (heap overload
Eight sorting summaries
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
CTF record
map集合赋值到数据库