🎉 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 tips: Safe update methods for NEAR contracts
Rust smart contracts upgrade techniques
Smart contracts, as a form of program code, inevitably have flaws and vulnerabilities. Even after extensive testing and auditing, security issues may still arise. Once a contract vulnerability is exploited by an attacker, it can lead to severe consequences such as the loss of user assets. Therefore, contract upgrades are crucial for fixing vulnerabilities and adding new features. This article will introduce the upgrade methods for Rust smart contracts.
NEAR Contract Upgrade Common Methods
Taking the StatusMessage project as an example, this article introduces common upgrade methods for NEAR smart contracts:
rust #[near_bindgen] #[derive(BorshDeserialize, BorshSerialize)] pub struct StatusMessage { records: LookupMap<string, string="">, }
impl Default for StatusMessage { Self { Self { records: LookupMap::new(b'r'.to_vec)((, } } }
#[near_bindgen] impl StatusMessage { pub fn set_status)&mut self, message: String) { let account_id = env::signer_account_id(); self.records.insert(&account_id, &message); }
Option<string> { return self.records.get(&account_id); } }
( contract data structure not modified
If only new functions are added and there is no modification to the data structure, you can directly use the near deploy command to redeploy the new code. The original data can be read normally.
) The contract data structure has been modified.
If the data structure is modified, redeploying directly will cause the new and old data structures to be incompatible, making it impossible to read data normally. In this case, the Migrate method needs to be used for the upgrade:
rust #( #[init)ignore_state###] Self { let old_state: OldStatusMessage = env::state_read###[private].expect('failed'); Self { taglines: old_state.records, bios: LookupMap::new(b'b'.to_vec)(), } }
near deploy
--wasmFile target/wasm32-unknown-unknown/release/status_message.wasm
--initFunction 'migrate'
--initArgs '{}' \ --accountId statusmessage.example.testnet
This allows for the migration of old data into the new data structure.
Contract Upgrade Security Considerations
Implement access control, allowing only developers or DAO to upgrade the smart contracts.
It is recommended to set the contract owner to DAO, managing it collaboratively through proposals and voting.
Add #[init(ignore_state(] before the migrate function.
Delete the migrate function after migration is complete to ensure it is called only once.
The new data structure is initialized during migration.
By using the above methods, the upgrade of Rust smart contracts can be completed safely and efficiently.
![])https://img-cdn.gateio.im/webp-social/moments-af3fe22c1999da5db0e2853b8a271276.webp)</string,>