Rust smart contracts upgrade tips: Safe update methods for NEAR contracts

robot
Abstract generation in progress

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:

  1. Add migrate method in the new contract:

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)(), } }

  1. Call the migrate method during deployment:

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

  1. Implement access control, allowing only developers or DAO to upgrade the smart contracts.

  2. It is recommended to set the contract owner to DAO, managing it collaboratively through proposals and voting.

  3. Add #[init(ignore_state(] before the migrate function.

  4. Delete the migrate function after migration is complete to ensure it is called only once.

  5. 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,>

View Original
This page may contain third-party content, which is provided for information purposes only (not representations/warranties) and should not be considered as an endorsement of its views by Gate, nor as financial or professional advice. See Disclaimer for details.
  • Reward
  • 7
  • Share
Comment
0/400
CryptoNomicsvip
· 23h ago
*sigh* lemme explain using stochastic probability matrices why upgradeability != security
Reply0
HodlBelievervip
· 08-01 07:16
No matter how low the contract risk coefficient is, you cannot gamble.
View OriginalReply0
SnapshotLaborervip
· 08-01 07:16
Rolling out smart contracts again, silently checking the wallet balance.
View OriginalReply0
MEV_Whisperervip
· 08-01 07:11
That's how contract upgrades are. What's so great about it?
View OriginalReply0
GateUser-beba108dvip
· 08-01 07:09
Anyone can do this upgrade!
View OriginalReply0
MemeCuratorvip
· 08-01 07:07
It's too close, I don't even dare to write a contract... afraid of getting taken advantage of.
View OriginalReply0
WalletDivorcervip
· 08-01 06:54
I'm losing like a dog, even upgrading won't save me.
View OriginalReply0
Trade Crypto Anywhere Anytime
qrCode
Scan to download Gate app
Community
English
  • 简体中文
  • English
  • Tiếng Việt
  • 繁體中文
  • Español
  • Русский
  • Français (Afrique)
  • Português (Portugal)
  • Bahasa Indonesia
  • 日本語
  • بالعربية
  • Українська
  • Português (Brasil)