Replies: 1 comment
-
How to upgrade the system smart contractAuthors: Galaxy(XDC Protocol Team), Dan Liu 1. ContextIn some cases, we need to upgrade the contract, such as fix bug or add feature. The system smart contract is deployed at a specific address and performs special functions. It is impossible to upgrade the system smart contract through conventional methods. But how can we achieve it when needed? This article introduces how to upgrade a system contracts using XDCValiator as an example. 2. MethodologyAfter research, we find the function advantages:
shortcoming:
3. Detailed OperationsThe current version of XDCValiator is v1 and the new version is v2. We propose the upgrade plan with the following steps:
4. Work scheduleWe will work from the latest commit of dev-upgrade branch. Each PR will also be merged into the In terms of role, Dan Liu and Gerui Wang and Liam will develop the code. Galaxy will provide test smart contracts. The protocol team will also provide infra support.
The expected total development time will be 8-11 weeks. 5. Test Strategy
6. Future Work |
Beta Was this translation helpful? Give feedback.
-
Upgrade contract XDCValidator in place
This document explains how to upgrade the contract's code in place while keeping the contract's data untouched. We take the system contract XDCValidator as an example.
1. download the solidity compiler
The code of contract XDCValidator uses solidity
^0.4.21
, but the below explorer pages show us that the contract XDCValidator is using solidity v0.4.24.So I choose solidity v0.4.26 for the new contract. This is the latest version in branch v0.4.x. It can be downloaded from https://github.com/ethereum/solidity/releases/.
2. prepare the new contract
Make a new file
contracts/validator/contract/XDCValidatorV2.sol
based on the existing contract XDCValidator, such as:Notice: the contract name in the file is still XDCValidator, not XDCValidatorV2
3. update bindings
Use abigen and solc-static-linux to update the bindings.
4. get the code of new contract
The variable
XDCValidatorBin
in the file validator.go is the bytecode to deploy contract. It is not the runtime bytecode of the contract. And it can not be used to upgrade the contract's code. There are many ways to get the real bytecode. Here we get it by genesis.The puppeth will extract the runtime bytecode of XDCValidatorV2 and save to the genesis. We can get it by the key
alloc.0000000000000000000000000000000000000088.code
in the genesis file, then paste it into the filecommon/constants.go
:5. modify golang code
Modify the file
consensus/XDPoS/XDPoS.go
:Notice: the above code works fine in my private network, but it is not good for production system.
6. upgrade nodes
Recompile the code
Restart all nodes with new binary XDC.
7. test the code
7.1 test function getAge
I use curl and eth_call to do tests.
"0x967e6e65" is the selector of function
getAge()
. According to my test code, the above command:7.1.1 before block number 30
returns "execution reverted" because the old code has no function
getAge
.7.1.2 between block number 30 and 60
returns "0x0000000000000000000000000000000000000000000000000000000000000000" because the new code has function getAge() and the value in slot 16(age) is 0.
7.1.3 after block number 60
returns "0x0000000000000000000000000000000000000000000000000000000000000002" because the new code has function getAge() and the value in slot 16(age) was set to 2.
7.2 test function setAge
Use Remix and MetaMask to call the function
setAge
. Then use remix to call the functiongetAge
and verify the value of age.Beta Was this translation helpful? Give feedback.
All reactions