0

I'm working on upgrading my Ethereum smart contract, which utilizes upgradable smart contract patterns. Currently, I have a struct named GovernorCountingSimpleStorage with existing fields, and I want to add a new field to this struct in my V2 contract. My concern is maintaining upgradeability and ensuring compatibility with existing deployments.

Here's the relevant code snippet from my V1 contract:

/// @custom:storage-location erc7201:openzeppelin.storage.GovernorCountingSimple
struct GovernorCountingSimpleStorage {
  mapping(uint256 => ProposalVote) _proposalVotes;
  // mapping to store that a user has voted at least one time
  mapping(address => bool) _hasVotedOnce;
}

// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.GovernorCountingSimple")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant GovernorCountingSimpleStorageLocation =
  0xa1cefa0f43667ef127a258e673c94202a79b656e62899531c4376d87a7f39800;

function _getGovernorCountingSimpleStorage() private pure returns (GovernorCountingSimpleStorage storage $) {
  assembly {
    $.slot := GovernorCountingSimpleStorageLocation
  }
}

Now, I want to add a new field inside GovernorCountingSimpleStorage in my V2 contract and ensure that the upgrade process is safe and doesn't disrupt existing data. Can I just add a new variable inside GovernorCountingSimpleStorage? Or do I need to create a new storage for the new variables? Should I reserve some slot inside the struct to allow future upgrades?

Is there any good resource where I can study this particular pattern used by OZ in upgradable contracts?

Edit: More about this topic that explains how it functions and the rules can be read here: https://eips.ethereum.org/EIPS/eip-7201

1 Answer 1

1

In your example, it is safe to add the new field as:

  • GovernorCountingSimpleStorage struct doesn't collide with other storage as has a custom offset
  • the new variables in GovernorCountingSimpleStorage struct added at the end don't collide with existing ones

It will be safe later on to add new variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.