🎉 Gate Square Growth Points Summer Lucky Draw Round 1️⃣ 2️⃣ Is Live!
🎁 Prize pool over $10,000! Win Huawei Mate Tri-fold Phone, F1 Red Bull Racing Car Model, exclusive Gate merch, popular tokens & more!
Try your luck now 👉 https://www.gate.com/activities/pointprize?now_period=12
How to earn Growth Points fast?
1️⃣ Go to [Square], tap the icon next to your avatar to enter [Community Center]
2️⃣ Complete daily tasks like posting, commenting, liking, and chatting to earn points
100% chance to win — prizes guaranteed! Come and draw now!
Event ends: August 9, 16:00 UTC
More details: https://www
Rust smart contracts upgrade methods and security considerations
Discussion on Rust Smart Contracts Upgrade Methods
Contract upgrades are an important aspect of smart contract development. Due to the immutable nature of blockchain, once a smart contract is deployed on-chain, it cannot be directly modified. However, in practical applications, contracts often need to fix vulnerabilities or add new features, which requires upgrades to achieve. This article will introduce common upgrade methods for Rust smart contracts.
1. The Necessity of Contract Upgrades
Smart contracts, as program code, inevitably have vulnerabilities. Even after extensive testing and auditing, there may still be undiscovered issues. Once a vulnerability is exploited maliciously, it can lead to significant asset losses. Therefore, having upgradeability is very important for smart contracts, mainly used for:
2. NEAR Contract Upgrade Method
The following uses the StatusMessage project as an example to introduce the upgrade method of NEAR smart contracts:
2.1 Contract data structure has not been modified
If only the contract logic is modified and there are no changes to the data structure, you can directly use the near deploy command to redeploy the new code. Example:
bash near deploy
--accountId statusmessage.testnet
--wasmFile target/wasm32-unknown-unknown/release/status_message.wasm
In this case, the data in the original contract will be retained.
2.2 The contract data structure has been modified.
If the data structure of the contract is modified, redeploying it directly will result in an error due to the mismatch between the old and new data structures. In this case, a migration method is needed for the upgrade.
Add migrate method in the new contract:
rust #[private] #[init(ignore_state)] Self { let old_state: OldStatusMessage = env::state_read().expect('failed'); Self { taglines: old_state.records, bios: LookupMap::new(b'b'.to_vec)((, } }
Then call the migrate method during deployment:
bash near deploy
--wasmFile target/wasm32-unknown-unknown/release/status_message.wasm
--initFunction 'migrate'
--initArgs '{}' \ --accountId statusmessage.testnet
This allows for the migration of old data into the new data structure.
![])https://img-cdn.gateio.im/webp-social/moments-73f5e5195fa71f1f25f5d35ba1e8b8ec.webp)
3. Security Considerations for Smart Contract Upgrades
When upgrading contracts, the following points need to be noted:
Reasonable design and execution of upgrade plans can maximize the security of contracts and user assets while ensuring the upgradability of the contracts.