*This article is compiled by WGB, the original author of Starcoin community, according to the live broadcast of "decentralized Stdlib upgrade - Starcoin vs ETH", click View original text.
Unlike other public chains, starcoin defines the consensus, block settings, block rewards, account definitions, Token definitions, NFT protocols, etc. in stdlib in advance for upgrading, maintenance and unified management. Stdlib is a contract library on the chain, so starcoin can realize the definition of block reward, block algorithm and consensus without hard coding. At the same time, stdlib can also be upgraded or repaired through Dao chain governance.
Smart contract: Starcoin vs ETH
- Language of smart contract
Ethereum: Ethereum contracts are mostly written using solidity. During the writing process, tokens of protocols such as ERC20 need to be written by themselves. There is no stdllib standard library.
Starcoin: Starcoin's contract is written in move language, which has the concepts of module and script. Module is mostly used to write basic code and combine calls through script. stdlib is built in starcoin, which implements ERC20, NFT, Dao and other protocols. At the same time, move language supports paradigm, which can easily create different tokens and other functions.
2. Call and storage of smart contract
Ethereum: Ethereum is divided into contract address and non contract address. The contract needs to be placed at the contract address, so the contract needs to be found through the contract address when calling the contract. If the contract is upgraded, you need to change the contract address before using the new contract without using Proxy contract and other methods.
Starcoin:Starcoin's contract and other resources (Token, NFT...) are stored in the account address, so it is necessary to call the contract through the owner address + module To find the contract. If the contract is upgraded, the address and module name of the calling contract will not be affected.
Smart contract upgrade: Starcoin vs ETH
Ethereum: When the contract on Ethereum needs to be upgraded but does not want to change the address of the call, you can use Proxy contract to access the address of the Proxy contract, and the Proxy contract provides the address of the new contract.
Starcoin: When starcoin's contract needs to be upgraded, Dao decentralized community voting and Two-phase (Two-phase submission) can be used to solve it. After the upgrade, the original address can be used to call the new contract.
Contract upgrade scheme: Starcoin VS ETH
Starcoin's stdlib contract exists in the chain and adopts Dao decentralized management. The community can decide the deployment of contract upgrade plan through voting operation.
Code submission adopts two-stage submission: submit the upgrade plan first, and then submit the update code. The whole process is divided into seven stages, as shown in the figure:
- PENDING
- ACTIVE
- AGREED
- QUEUED
- EXECTABLE
- ETRACTED
- Upgrade complete
1. PENDING
After the code is modified by the coder, an upgraded proposal txn is submitted to Dao, and the whole process enters the PENDING state. Set a period of time for the community to discuss and understand this topic before entering the next stage.
2. ACTIVE
After the end of the previous stage, enter the ACTIVE stage. In this stage, community personnel are required to vote, and move to the next stage after reaching the set specified time.
3. AGREED
After the last stage reaches the specified time, the process enters the AGREED stage. In this stage, the voting results will be counted. If the predetermined proportion is exceeded, it will be deemed that the upgrade plan is allowed by Dao community. After the publicity is initiated, the next stage can be carried out.
4.QUEUED
After counting the voting results of the previous stage, the process enters the publicity period from the initiation of publicity to the publicity period. This stage mainly displays the information of the initiator and proposal. When the publicity period passes, it enters the next stage.
5.EXECTABLE
At the end of the publicity period of the previous stage, the process enters the first stage of the Two-phase (Two-phase submission) that can upgrade the contract and submit the contract code upgrade plan. After submitting the contract upgrade plan, you can enter the next stage.
6.ETRACTED
After submitting the contract upgrade plan in the previous stage, the process enters the second stage of the Two-phase (Two-phase submission) of the upgrade contract. At this stage, the code of the repair or upgrade contract can be submitted, and the next stage can be entered after the submission is completed.
7.Upgrade complete
After the code in the previous stage is submitted, the whole contract upgrade process is completed, and then the new contract code can be used for operation.
Stdlib upgrade
1. Write the code in stdlib
Here, take DummyToken.move in stdlib as an example, and add a Mymint function and Mymint script. The function is to create 100 dummytokens for constant value.
DummyToken Module under DummyToken.move:
/// Anyone can mint DummyToken, amount should < 10000 public fun mint(_account: &signer, amount: u128) : Token<DummyToken> acquires SharedMintCapability{ assert(amount <= 10000, Errors::invalid_argument(EMINT_TOO_MUCH)); let cap = borrow_global<SharedMintCapability>(token_address()); Token::mint_with_capability(&cap.cap, amount) } // Add a new function here public fun Mymint(_account: &signer) : Token<DummyToken> acquires SharedMintCapability{ let cap = borrow_global<SharedMintCapability>(token_address()); Token::mint_with_capability(&cap.cap, 100) } /// Return the token address. public fun token_address(): address { Token::token_address<DummyToken>() }
DummyTokenScripts Module under DummyToken.move:
public(script) fun mint(sender: signer, amount: u128){ let token = DummyToken::mint(&sender, amount); let sender_addr = Signer::address_of(&sender); if(Account::is_accept_token<DummyToken>(sender_addr)){ Account::do_accept_token<DummyToken>(&sender); }; Account::deposit(sender_addr, token); } //Add a new script function here public(script) fun Mymint(sender:signer){ let token = DummyToken::Mymint(&sender); let sender_addr = Signer::address_of(&sender); if(Account::is_accept_token<DummyToken>(sender_addr)){ Account::do_accept_token<DummyToken>(&sender); }; Account::deposit(sender_addr, token); }
2. Prepare the compiled binary Module
Execute the command on the Starcoin command line to compile and package the binary Module in two steps compile:
Execution: dev compile -s 0x1 path/to/DummyToken.move result: { "ok": [ "path/to/DummyToken.mv", "path/to/DummyTokenScripts.mv" ] }
pack:
Execution: dev package -n MymintUpgrade -o storage path/to/DummyToken* result: { "ok": { "file": "path/to/storage/MymintUpgrade.blob", "package_hash": "0x6e54935144115233c9decb255d3bcd5f14c7b9d82c968c5f3a0cb1b14f18bce8" } }
3. Prepare account number and balance
Prepare account number: Two accounts are required:
- coder account: used to submit plans and codes
- Voting representative account: used to vote
You can import the key or create an account with account create Reserve balance:
- The coder account needs a certain STC as the GAS fee for proposals, upgrades and other operations.
- The voting representative account needs a large number of STC as the number of votes. Use the command line account unlock Account address Unlock account Get STC method:
Default account acquisition: dev get-coin -v 6000000STC Transfer method: account transfer -s 0x0000000000000000000000000a550c18 -r <Account number collection ID> -v 60000000000000000 -b
4. Submit proposals and enter PENDING
Submission of proposals: dev module-proposal -s <account address> -m <module path> -v <version> -e false
- -m indicates the path of the upgrade package;
- -v represents the new version number;
- -e indicates whether to skip module compatibility check and force upgrade. false indicates not to skip compatibility check. It is not skipped by default.
5. Query proposal status
View proposal id:
dev call --function 0x1::Dao::proposal_info -t 0x1::STC::STC -t 0x1::UpgradeModuleDaoProposal::UpgradeModuleV2 --arg <proposal address>
View proposal status:
dev call --function 0x1::Dao::proposal_state -t 0x1::STC::STC -t 0x1::UpgradeModuleDaoProposal::UpgradeModuleV2 --arg <proposal address> --arg <proposal_id> result: { "ok": [ <state_num> ] }
6. Wait for community discussion to enter ACTIVE
In the PENDING state, the voting period is entered after a period of time. Under dev, the blockchain time can be accelerated to the next stage through sleep. Accelerated entry:
Acceleration time: dev sleep -t 3600000 Build block (effective time): dev gen-block
View proposal status:
Execution: dev call --function 0x1::Dao::proposal_state -t 0x1::STC::STC -t 0x1::UpgradeModuleDaoProposal::UpgradeModuleV2 --arg <proposal address> --arg <proposal_id> result: { "ok": [ 2 ] } Turn into ACTIVE state
7. The community voted to enter AGREED
After voting with the voting representative account and waiting for the voting time to end, the process enters the AGREED stage
Voting: account execute-function -s <account address> --function 0x1::DaoVoteScripts::cast_vote -t 0x1::STC::STC -t 0x1::UpgradeModuleDaoProposal::UpgradeModuleV2 --arg <proposal address> --arg <proposal_id> --arg true --arg 59000000000000000u128
8. Accepted proposals are put into the update queue and entered into QUEUED
After entering AGREED, anyone can put the proposal with the status of AGREED into the update queue.
dev module-queue -s <account address> -a <proposal address> -i <proposal_id>
9. The publicity period is waiting to enter exerctable
After the waiting time of the publicity period has passed, you can enter the implementation stage. Under dev, you can speed up this stage by command Accelerated entry:
Acceleration time: dev sleep -t 3600000 Build block (effective time): dev gen-block
10. Submit the upgrade plan and enter ETRACTED
This step is the executable phase, and the upgrade plan can be executed.
dev module-plan -i <proposal_id> -a <proposal address> -s <account address>
11. Submit the code and enter Upgrade complete
In this step, the Dao process has ended and the two-stage submission process enters the last stage Code module submission:
dev module_exe -m path/to/storage/MymintUpgrade.blob -s <account address>
12. Complete the contract upgrade process
So far, the whole contract upgrade process has been completed
13. Verification of upgrade
You can execute the Mymint function in the DummyTokenScripts module to verify whether the upgrade is successful
Execute acquisition DummyToken 100 Number: account execute-function --function 0x1::DummyTokenScripts::Mymint -b -s <account address> see DummyToken: account show <account address> result: "balances": { "0x00000000000000000000000000000001::DummyToken::DummyToken": 100, "0x00000000000000000000000000000001::STC::STC": 9999645054 }