UUPS 代理模式实现
学习使用 OpenZeppelin 实现通用可升级代理标准(UUPS),以安全升级合约。
UUPS 代理模式实现 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Blockchain Smart Contracts with Solidity 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Understanding UUPS
The Universal Upgradeable Proxy Standard (UUPS) is a popular design pattern for creating upgradeable smart contracts. It allows you to change a contract's logic without changing its address on the blockchain.
This is crucial for fixing bugs, adding new features, or adapting to changing requirements without losing user data or contract state.
How UUPS Works
UUPS uses a proxy contract that holds the contract's state (data) and a separate implementation contract that holds the contract's logic (functions).
- Proxy Contract: A simple, unchanging contract that forwards calls to the current implementation.
- Implementation Contract: Contains the actual business logic. This is the contract that gets upgraded.
The key difference in UUPS is that the upgrade logic resides within the implementation contract, not the proxy.
Trusting OpenZeppelin
Implementing upgradeable contracts correctly is complex and prone to errors. That's why we rely on battle-tested libraries like OpenZeppelin Contracts.
OpenZeppelin provides secure and audited implementations of proxy patterns, including UUPS, making it much safer and easier to build upgradeable contracts.
Building UUPS Contracts
To make your contract UUPS-compatible, it needs to inherit from OpenZeppelin's UUPSUpgradeable contract. This base contract provides the necessary functions and modifiers for upgradeability.
Remember, upgradeable contracts use an initializer function instead of a constructor, as the proxy never calls the implementation's constructor directly.
Our First Upgradeable Contract
Here's a basic contract, MyContractV1, ready for UUPS upgrades. Notice it inherits from OpenZeppelin's UUPSUpgradeable. You'll typically install OpenZeppelin contracts via npm and import them.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
contract MyContractV1 is UUPSUpgradeable {
// This contract will contain our initial logic
// We'll add an initializer in the next scene
}Initializing UUPS Contracts
In upgradeable contracts, you cannot use a regular constructor. The proxy contract is deployed once, and its constructor is called. The implementation contract's constructor is never called when the proxy is deployed or upgraded.
Instead, you use an initializer function. This function is called once, typically right after the proxy is deployed, to set up the initial state of your logic contract.
Initializing State
Let's add an initializer to our MyContractV1. We use __UUPSUpgradeable_init() and __Ownable_init() to properly initialize base contracts, and Reinitializer(1) to prevent multiple initializations.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
contract MyContractV1 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
string public message;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
function initialize(string memory initialMessage) public initializer {
__UUPSUpgradeable_init();
__Ownable_init(); // Initialize Ownable for access control
message = initialMessage;
}
function setMessage(string memory newMessage) public onlyOwner {
message = newMessage;
}
// Required for UUPS, defines who can upgrade
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
}Deploying Your UUPS Contract
Deploying UUPS contracts involves using an upgrades plugin (e.g., for Hardhat or Truffle). This plugin handles creating the proxy and linking it to your initial implementation contract.
- It deploys your implementation contract.
- It deploys the proxy contract, pointing to your implementation.
- It calls the
initializefunction on the proxy.
This process ensures your contract is correctly set up for future upgrades.
Performing an Upgrade
When you want to upgrade your contract, you deploy a new version of your implementation contract (e.g., MyContractV2). Then, you call an upgrade function on the existing proxy contract, telling it to point to the new implementation.
The _authorizeUpgrade function in your implementation contract controls who has permission to perform this upgrade, often restricted to the contract owner.
Upgrading Your Logic
Let's imagine we want to add a new feature to MyContractV1. We create MyContractV2, inheriting from MyContractV1 to carry over its state and existing logic. We then add our new function.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "./MyContractV1.sol"; // Assuming MyContractV1 is in the same directory
contract MyContractV2 is MyContractV1 {
uint public counter; // New state variable
// We don't need a new initializer unless we have new base contracts
// to initialize, or specific V2 setup.
function incrementCounter() public onlyOwner {
counter++;
}
// Existing functions like setMessage and message getter are still available
// via the proxy, using the logic from MyContractV2.
}UUPS Key Concepts
Which of the following statements are TRUE regarding the Universal Upgradeable Proxy Standard (UUPS) implemented with OpenZeppelin?
UUPS: Secure Upgrades
In this lesson, you learned about the Universal Upgradeable Proxy Standard (UUPS) and how to implement it using OpenZeppelin Contracts.
- UUPS separates state (proxy) from logic (implementation).
- You use
UUPSUpgradeableas a base contract. initializefunctions replace constructors for setup.- OpenZeppelin's upgrade plugins simplify deployment and upgrades.
UUPS is a powerful pattern for building future-proof smart contracts!
常见问题解答
「UUPS 代理模式实现」课时是免费的吗?
是的 — 「UUPS 代理模式实现」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。
「UUPS 代理模式实现」这节课中我会学到什么?
学习使用 OpenZeppelin 实现通用可升级代理标准(UUPS),以安全升级合约。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Blockchain Smart Contracts with Solidity 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Blockchain Smart Contracts with Solidity 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「UUPS 代理模式实现」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Blockchain Smart Contracts with Solidity 课中编写并运行代码吗?
能。每节 Blockchain Smart Contracts with Solidity 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 为什么需要可升级合约
- UUPS 代理模式实现
- Diamond 标准(多 facet 代理)
- 透明代理模式与存储布局