Detailed Explanation of Rust Smart Contract Upgrades: Best Practices from Ethereum to NEAR

robot
Abstract generation in progress

Detailed Explanation of Rust Smart Contracts Upgrade Methods

Smart contracts, as a type of program, inevitably have defects and vulnerabilities. Even after extensive testing and auditing, problems may still arise. Once a vulnerability is exploited by an attacker, it can lead to serious consequences such as the loss of user assets. Therefore, the upgradability of contracts is very important, and this article will introduce the upgrade methods for Rust contracts.

Upgrade Methods for Ethereum Smart Contracts

Smart contracts on Ethereum are immutable and cannot be directly modified after deployment. Upgrades are usually done in the following ways:

  1. Deploy a new contract and modify the contract address in the DApp. The downside is that it requires migrating the state data of the old contract.

  2. Data and logic separation architecture. Store data in state contracts while implementing logic in another contract. When upgrading, only update the logic contract.

  3. Use proxy contracts. Proxy contracts store data and call logic contracts through delegatecall; when upgrading, only the logic contract address needs to be updated.

NEAR Contract Upgrade Method

Taking the StatusMessage project as an example, this article introduces the upgrade method for NEAR smart contracts:

1. The contract data structure has not been modified.

If only the contract logic is modified without involving changes to the data structure, you can directly use the near deploy command to redeploy the new code. The original data will be retained.

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 data properly.

3. Use the Migrate method to upgrade

NEAR provides the Migrate method to assist with upgrades:

  1. Add migrate method in the new contract
  2. Call the migrate method to perform data migration during deployment.
  3. After the migration is complete, the new contract functions can be used normally.

Smart Contracts Upgrade Security Considerations

  1. Access control - The upgrade function should be an only owner function.
  2. It is recommended to set the owner to DAO to avoid centralization risks.
  3. Use #[init(ignore_state)] to ensure that the state is not loaded before executing the migration.
  4. Delete the migration function after migration to avoid repeated calls.
  5. The new data structure is initialized during migration.

By reasonably designing the upgrade plan, the upgradability of the contract can be achieved while ensuring security, thus improving the long-term maintainability of the project.

ETH4.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
  • 6
  • Share
Comment
0/400
FlatlineTradervip
· 2h ago
The vulnerability is the wallet...
View OriginalReply0
Deconstructionistvip
· 9h ago
It is recommended to add hot reload.
View OriginalReply0
rekt_but_not_brokevip
· 9h ago
Smart contracts are actually this tricky, huh?
View OriginalReply0
LiquidityWizardvip
· 9h ago
theoretically speaking, proxy patterns r just sugar-coated state mutations w/ 73.4% more gas overhead... smh
Reply0
SchrödingersNodevip
· 9h ago
It's the old operation of changing the contract Address again.
View OriginalReply0
NestedFoxvip
· 10h ago
Rust is too much of a pitfall.
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)