Rust smart contracts upgrade: ensuring security and scalability

robot
Abstract generation in progress

Rust smart contracts Development Diary (9): Contract Upgrade

Smart contracts are essentially programs and are inevitably prone to defects. Even after extensive testing and auditing, vulnerabilities may still exist. Once exploited by attackers, it can lead to significant losses of user assets. Therefore, the upgradability of contracts is very necessary. This article will introduce the upgrade methods for Rust contracts.

1. The Necessity of Contract Upgrades

Smart contracts, as programs, inevitably have defects. Bug fixes and the addition of new features need to be implemented through contract upgrades.

2. Common Upgrade Methods for Solidity Contracts

Ethereum smart contracts have immutability and cannot be changed once deployed. The solution is to deploy a new contract, but it faces challenges such as address changes and state migration. Typically, a proxy contract architecture that separates data and logic is used, allowing only the logic contract to be upgraded without worrying about state migration.

3. NEAR smart contracts upgrade methods

Taking the StatusMessage project as an example to introduce the upgrading method of NEAR contracts:

3.1 Contract data structure not modified

If only the contract logic is modified without involving changes to the data structure, the new code can be redeployed directly using near deploy. The existing data can still be read normally.

3.2 The contract data structure has been modified

If the data structure is modified, redeploying directly will cause a mismatch between the old and new data structures, making it impossible to read the original data.

3.3 Upgrade using the Migrate method

NEAR provides a Migrate method to assist in contract upgrades. Add the 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)((, } }

You can complete data migration by calling the migrate method during deployment.

4. Safety Considerations for Contract Upgrades

  • Upgrade permission control, generally only developers or DAOs can upgrade.
  • It is recommended to set the contract owner as DAO, managed through proposals and voting.
  • Add #[init)ignore_state)] before the migration function
  • Delete the migration function after migration is complete
  • The new data structure is initialized during migration.

Contract upgrades are an important means to ensure contract security and functional iteration, requiring careful design and implementation.

INIT2.63%
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
FarmHoppervip
· 07-25 03:37
Quickly fix it, always staring at the loopholes in a panic.
View OriginalReply0
WenMoonvip
· 07-24 22:42
Everyone is upgrading here, isn't it a bit dangerous?
View OriginalReply0
LightningLadyvip
· 07-24 20:23
Updating the patch again, sigh.
View OriginalReply0
SandwichTradervip
· 07-22 05:36
Continue copying homework? The contract has been changed and is starting again.
View OriginalReply0
BoredRiceBallvip
· 07-22 05:34
Reliable, test before use
View OriginalReply0
SigmaValidatorvip
· 07-22 05:33
Someone attacked my smart contracts!
View OriginalReply0
BoredApeResistancevip
· 07-22 05:30
The agency has a new way to play.
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)