ERC-1504 Upgradeable contract (Upgradable Smart Contract)
Stagnant · Other · created 2018-10-17
An upgrade standard that separates a Handler contract holding logic from a Data contract holding data, allowing the contract logic to be replaced while the data is preserved.
Why it is needed
Smart contracts cannot change their code after deployment. So historically, when bugs or security vulnerabilities surfaced, the only option was to abandon the existing contract, deploy a new one, and migrate the data over. The original text cites the 2016 DAO attack as a case where this situation led to major financial losses and an Ethereum hard fork. For example, even if a vulnerability is found in an ERC-20 token contract, without an upgrade mechanism, an attacker can keep exploiting the vulnerability and compounding losses while a safe new contract is deployed and data is migrated. ERC-1504 tries to reduce this problem by separating logic (Handler) from data (Data), so that only the logic is replaced while the data stays in place.
What to implement
function initialize (string _str, uint256 _int, uint16 [] _array) public;: Initializes the Data contract by setting the initial values of exmStr, exmInt, and exmArray.function registerUpgrader (address _upgraderAddr) external;: Registers the Upgrader contract's address with the Handler.function isUpgraderRegistered () external view returns(bool);: Checks whether an Upgrader contract is registered.function done() external;: Self-destructs the existing Handler contract once the upgrade is complete.function live() external pure returns(bool): Proves that this contract is a live, operating Handler contract, and always returns true.function initialize (string _str, uint256 _int, uint16 [] _array) external onlyHandler returns(bool);: An initialization function callable only by the Handler contract, which sets the initial values of the Data contract.function setHandler (address _handlerAddr, address _originalHandlerAddr) external returns(bool);: The owner or a registered Upgrader contract sets or replaces the Handler contract address.function startUpgrading (address _upgraderAddr) public returns(bool);: The owner registers a new Upgrader contract to begin the upgrade procedure.function getProposalPeriod () public view isReady allowedAddress returns(uint256);: Queries the proposal's validity period (proposalPeriod).function setProposalPeriod (uint256 _proposalPeriod) public isReady returns(bool);: The owner sets the proposal's validity period.function canBeUpgraded (address _originalHandlerAddr) external view isReady returns(UpgradingStatus);: Informs the Upgrader contract of the current Handler's upgrade status (UpgradingStatus).function live () external view returns(bool);: Checks whether the Data contract has been initialized.function startProposal () external;: Checks the state of the Data contract and the existing and new Handler contracts, then starts the vote.function getResolution() external returns(UpgraderStatus);: Checks whether voters have reached consensus; if so, replaces the Handler, and if the period has elapsed, expires the proposal.function done() external;: The owner self-destructs the Upgrader contract.
In depth
The design that separates Data and Handler
A service can be divided into resources, meaning the data, and operations, the logic that handles that data. Since it is usually the logic that needs upgrading, ERC-1504 splits the contract into two parts. The Data contract, which stores the data, persists permanently, while the Handler contract, which holds the operations, can be replaced as needed. The Handler reads and writes data only through the getter and setter functions provided by the Data contract.
Handler interface (IHandler)
Since Handler contract functions vary with the business logic, a common interface, IHandler, is defined so that external services always work. initialize() sets the initial values of the Data contract's exmStr, exmInt, and exmArray, and registerUpgrader() registers the Upgrader contract's address. isUpgraderRegistered() returns whether registration has occurred, and done() self-destructs the existing Handler once the upgrade is complete. live() always returns true to prove that this contract is a live, operating Handler. Because live() has a default implementation, IHandler is designed as an abstract contract rather than a pure interface.
Handler deployment procedure
Deploying a Handler contract is a four step process. First, deploy the Data contract, then deploy the Handler contract at the address specified by the Data contract. Next, either call the Data contract's setHandler() directly or register the Handler address through an Upgrader contract (to use an Upgrader, the Data contract must already be initialized). Finally, initialize the Data contract if it has not already been initialized.
The Data contract's three kinds of data and UpgradingStatus
The Data contract holds three kinds of data. Administrator Data holds the owner address, the Handler contract address, and whether initialization has occurred. Upgrader Data holds the Upgrader contract address, the block number at which the proposal was submitted (proposalBlockNumber), and the proposal validity period (proposalPeriod). Resource Data is all remaining data the service needs. The Handler's upgrade status is represented by the UpgradingStatus enum, which has five values: Done (upgrade possible), InProgress (in progress), Blocked (another proposal in progress), Expired (expired), and Error (the original Handler errored). Three modifiers, onlyHandler, allowedAddress, and ready, check call permissions and initialization state.
The Upgrader contract and voting procedure
The Upgrader is an optional contract that replaces the Handler based on votes from voters pre-registered by the owner. The procedure checks that the Data contract and both the existing and new Handler are deployed, deploys the Upgrader with the three addresses, and then registers the Upgrader address, in order, with the new Handler, the existing Handler, and the Data contract. Next, startProposal() begins the vote, and getResolution() is called before expiration to check whether consensus was reached. If consensus is reached, the upgrade succeeds; otherwise the proposal expires. The notExpired modifier checks the proposal state on every call and switches it to Expired if the state reported by the Data contract is not InProgress. done() can be called at any time to self-destruct the Upgrader.
Limitations (Caveats) of the voting mechanism
The original text notes that because the Upgrader's voting mechanism is simple, it also has limitations. Only the owner of the Data and Handler contracts can be the administrator, and only the owner has the authority to add voters or start a proposal. In addition, for an upgrade to succeed, voters must remain active and engaged. Votes are only valid within a set period, and if sufficient approval is not gathered within that period, the proposal expires.
Gas cost and utility
When the owner replaces the Handler directly, it costs only about as much gas as a normal contract call, far less than creating and deploying an entirely new contract. Using an Upgrader adds extra gas cost for the voting process, but the original text explains that this is less painful than leaving a vulnerable contract as is or performing a hard fork (as was done in response to the DAO attack). Because the owner only needs to replace the part that caused the problem, the Data contract's extensibility and flexibility can be improved without data loss or blockchain inconsistency.
Proposal status
This ERC is in Stagnant status, a proposal that is no longer being actively discussed or advanced. It is currently unlikely to change or be adopted.