Rust smart contracts upgrade methods and security considerations

robot
Abstract generation in progress

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:

  • Fix discovered security vulnerabilities
  • Add new features
  • Optimize contract logic to enhance performance

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:

  • Strictly control upgrade permissions, allowing only the contract owner or DAO to perform upgrades.
  • It is recommended to set the contract owner to DAO, managing upgrades through proposals and voting.
  • Add #[init(ignore_state)] before the migration function to ensure that the state is not loaded before execution.
  • Delete the migration function after the migration is complete to avoid repeated calls.
  • The new data structure is initialized during migration.

Reasonable design and execution of upgrade plans can maximize the security of contracts and user assets while ensuring the upgradability of the contracts.

INIT-0.88%
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
  • 5
  • Share
Comment
0/400
NFTArchaeologistvip
· 3h ago
Upgrading is the hard truth.
View OriginalReply0
HappyToBeDumpedvip
· 21h ago
Upgrading is equivalent to fixing vulnerabilities.
View OriginalReply0
NftBankruptcyClubvip
· 21h ago
No more talk, let's get to work.
View OriginalReply0
NFTHoardervip
· 21h ago
Indeed, safety is the most important.
View OriginalReply0
FundingMartyrvip
· 21h ago
I choose to stick to the fee.
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)