Pattern Transparent Proxy e layout dello storage
Impari il pattern Transparent Proxy, come delegatecall separi la logica dallo storage e quali regole di layout dello storage seguire per aggiornare i contract in sicurezza.
Pattern Transparent Proxy e layout dello storage è una lezione Blockchain Smart Contracts with Solidity gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Blockchain Smart Contracts with Solidity, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Blockchain Smart Contracts with Solidity include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara Blockchain Smart Contracts with Solidity con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Pattern Transparent Proxy e layout dello storage» è gratuita?
Sì — il testo completo di «Pattern Transparent Proxy e layout dello storage» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Blockchain Smart Contracts with Solidity, passa a CoddyKit PRO. Il corso Blockchain Smart Contracts with Solidity include 4 lezioni in totale.
Cosa imparerò in «Pattern Transparent Proxy e layout dello storage»?
Impari il pattern Transparent Proxy, come delegatecall separi la logica dallo storage e quali regole di layout dello storage seguire per aggiornare i contract in sicurezza. Eserciti Blockchain Smart Contracts with Solidity con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Blockchain Smart Contracts with Solidity?
Non è richiesta alcuna esperienza precedente. Blockchain Smart Contracts with Solidity su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Pattern Transparent Proxy e layout dello storage»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Blockchain Smart Contracts with Solidity?
Sì. Ogni lezione Blockchain Smart Contracts with Solidity include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Perché utilizzare contratti aggiornabili?
- Implementazione del pattern proxy UUPS
- Diamond Standard (proxy multifacet)
- Pattern Transparent Proxy e layout dello storage