รูปแบบพร็อกซีโปร่งใสและการจัดวางหน่วยความจำจัดเก็บ
เรียนรู้รูปแบบพร็อกซีโปร่งใส วิธีที่ delegatecall แยกตรรกะออกจากหน่วยความจำจัดเก็บ และกฎการจัดวางหน่วยความจำจัดเก็บที่ต้องปฏิบัติตามเพื่ออัปเกรดสัญญาอย่างปลอดภัย
รูปแบบพร็อกซีโปร่งใสและการจัดวางหน่วยความจำจัดเก็บ เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Blockchain Smart Contracts with Solidity และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Recap: Why Proxies
You have seen UUPS and the Diamond standard. Both rely on a foundational idea: a proxy holds the storage and forwards calls to a separate logic (implementation) contract. Upgrading means pointing the proxy at new logic.
How delegatecall Works
The magic behind proxies is delegatecall. It runs the logic contract's code but in the proxy's storage context, using the proxy's msg.sender and balance.
So storage lives in the proxy; behavior lives in the implementation.
A Minimal Proxy Fallback
A proxy forwards every unknown call to the implementation using assembly delegatecall in its fallback.
fallback() external payable {
address impl = _implementation();
assembly {
calldatacopy(0, 0, calldatasize())
let ok := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch ok
case 0 { revert(0, returndatasize()) }
default { return(0, returndatasize()) }
}
}The Function Clash Problem
If the proxy itself has an upgradeTo function and the logic also has a function with the same selector, calls become ambiguous. The Transparent Proxy pattern solves this by routing based on the caller.
Admin vs User Routing
In a Transparent Proxy, the admin address can call admin functions (like upgrade) but cannot reach the implementation. Everyone else is always forwarded to the implementation. This removes clashes.
modifier ifAdmin() {
if (msg.sender == _admin()) {
_;
} else {
_fallback();
}
}Storage Slot Collisions
Both proxy and logic write to the same storage. If they used slot 0 for different variables, they would corrupt each other. Proxies store admin and implementation at fixed pseudo-random slots (EIP-1967) to avoid collisions.
// EIP-1967 implementation slot
bytes32 internal constant _IMPL_SLOT =
0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;Reading the Implementation Slot
The proxy reads and writes that fixed slot directly with assembly so it never collides with logic variables.
function _implementation() internal view returns (address impl) {
bytes32 slot = _IMPL_SLOT;
assembly { impl := sload(slot) }
}Storage Layout Rules
When upgrading the logic you must preserve the order and types of existing storage variables. New variables go at the end.
- Never reorder existing variables
- Never change a variable's type
- Never insert a new variable before old ones
Storage Gaps
Inheritable base contracts reserve space for future variables with a __gap array so child contracts do not collide after an upgrade adds fields to the base.
contract Base {
uint256 public value;
// reserve 50 slots for future use
uint256[50] private __gap;
}Initializers Not Constructors
Constructors run only on the logic contract's own deployment, not through the proxy. Upgradeable contracts use an initialize function guarded so it runs exactly once.
bool private _initialized;
function initialize(uint256 v) external {
require(!_initialized, 'already init');
_initialized = true;
value = v;
}Transparent vs UUPS
Transparent proxies put the upgrade logic in the proxy (more deployment gas, simpler logic). UUPS puts it in the implementation (cheaper proxy, but you must include upgrade code in every version). Choose based on cost and team discipline.
Quick Check
Test your understanding of proxy storage.
Recap
You learned the Transparent Proxy pattern:
delegatecallruns logic in the proxy's storage- Admin-vs-user routing avoids function clashes
- EIP-1967 fixed slots and
__gaparrays prevent collisions - Use
initialize, preserve storage layout, append new variables
Follow these rules and upgrades stay safe.
เรียนรู้ Blockchain Smart Contracts with Solidity ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “รูปแบบพร็อกซีโปร่งใสและการจัดวางหน่วยความจำจัดเก็บ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รูปแบบพร็อกซีโปร่งใสและการจัดวางหน่วยความจำจัดเก็บ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบพร็อกซีโปร่งใสและการจัดวางหน่วยความจำจัดเก็บ”
เรียนรู้รูปแบบพร็อกซีโปร่งใส วิธีที่ delegatecall แยกตรรกะออกจากหน่วยความจำจัดเก็บ และกฎการจัดวางหน่วยความจำจัดเก็บที่ต้องปฏิบัติตามเพื่ออัปเกรดสัญญาอย่างปลอดภัย คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “รูปแบบพร็อกซีโปร่งใสและการจัดวางหน่วยความจำจัดเก็บ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม
ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดสัญญาจึงควรอัปเกรดได้
- การนำรูปแบบพร็อกซี UUPS มาใช้
- มาตรฐาน Diamond (พร็อกซีหลายแฟเซต)
- รูปแบบพร็อกซีโปร่งใสและการจัดวางหน่วยความจำจัดเก็บ