Skip to content

Commit

Permalink
update contract unit tests to reflect new storage slot segment setter
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-goldman committed Feb 16, 2023
1 parent ea49829 commit 736b859
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 72 deletions.
7 changes: 7 additions & 0 deletions .changeset/wet-owls-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@chugsplash/contracts': patch
'@chugsplash/plugins': patch
'@chugsplash/core': patch
---

Update contract unit tests to reflect new storage slot segment setter
5 changes: 4 additions & 1 deletion packages/contracts/contracts/ChugSplashManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,10 @@ contract ChugSplashManager is OwnableUpgradeable, ReentrancyGuardUpgradeable {
if (_action.actionType == ChugSplashActionType.DEPLOY_IMPLEMENTATION) {
_deployImplementation(_action.referenceName, _action.data);
} else if (_action.actionType == ChugSplashActionType.SET_STORAGE) {
(bytes32 key, uint8 offset, bytes memory val) = abi.decode(_action.data, (bytes32, uint8, bytes));
(bytes32 key, uint8 offset, bytes memory val) = abi.decode(
_action.data,
(bytes32, uint8, bytes)
);
_setProxyStorage(proxy, adapter, key, offset, val);
} else {
revert("ChugSplashManager: attemped setImplementation action in wrong function");
Expand Down
9 changes: 7 additions & 2 deletions packages/contracts/contracts/adapters/DefaultAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ contract DefaultAdapter is IProxyAdapter {
/**
* @inheritdoc IProxyAdapter
*/
function setStorage(address payable _proxy, bytes32 _key, bytes32 _value) external {
IProxyUpdater(_proxy).setStorage(_key, _value);
function setStorage(
address payable _proxy,
bytes32 _key,
uint8 _offset,
bytes memory _segment
) external {
IProxyUpdater(_proxy).setStorage(_key, _offset, _segment);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ contract OZTransparentAdapter is IProxyAdapter {
/**
* @inheritdoc IProxyAdapter
*/
function setStorage(address payable _proxy, bytes32 _key, bytes32 _value) external {
function setStorage(
address payable _proxy,
bytes32 _key,
uint8 _offset,
bytes memory _segment
) external {
// We perform a low-level call here to avoid OpenZeppelin's `TransparentUpgradeableProxy`
// reverting on successful calls, which is likely occurring because its `upgradeToAndCall`
// function doesn't return any data.
Expand All @@ -38,7 +43,7 @@ contract OZTransparentAdapter is IProxyAdapter {
Proxy.upgradeToAndCall,
(
Proxy(_proxy).implementation(),
abi.encodeCall(IProxyUpdater.setStorage, (_key, _value))
abi.encodeCall(IProxyUpdater.setStorage, (_key, _offset, _segment))
)
)
);
Expand Down
9 changes: 7 additions & 2 deletions packages/contracts/contracts/adapters/OZUUPSAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ contract OZUUPSAdapter is IProxyAdapter {
/**
* @inheritdoc IProxyAdapter
*/
function setStorage(address payable _proxy, bytes32 _key, bytes32 _value) external {
OZUUPSUpdater(_proxy).setStorage(_key, _value);
function setStorage(
address payable _proxy,
bytes32 _key,
uint8 _offset,
bytes memory _segment
) external {
OZUUPSUpdater(_proxy).setStorage(_key, _offset, _segment);
}

/**
Expand Down
17 changes: 12 additions & 5 deletions packages/contracts/contracts/interfaces/IProxyAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ interface IProxyAdapter {
function completeExecution(address payable _proxy, address _implementation) external;

/**
* @notice Modifies some storage slot within the proxy contract. Gives us a lot of power to
* perform upgrades in a more transparent way.
* @notice Replaces a segment of a proxy's storage slot value at a given key and offset. The
* storage value outside of this segment remains the same.
*
* @param _key Storage key to modify.
* @param _value New value for the storage key.
* @param _proxy Address of the proxy to modify.
* @param _key Storage key to modify.
* @param _offset Bytes offset of the new segment from the right side of the storage slot.
* @param _segment New value for the segment of the storage slot.
*/
function setStorage(address payable _proxy, bytes32 _key, bytes32 _value) external;
function setStorage(
address payable _proxy,
bytes32 _key,
uint8 _offset,
bytes memory _segment
) external;

/**
* @notice Changes the admin of the proxy.
Expand Down
11 changes: 6 additions & 5 deletions packages/contracts/contracts/interfaces/IProxyUpdater.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ pragma solidity ^0.8.9;
*/
interface IProxyUpdater {
/**
* @notice Modifies some storage slot within the proxy contract. Gives us a lot of power to
* perform upgrades in a more transparent way.
* @notice Replaces a segment of a proxy's storage slot value at a given key and offset. The
* storage value outside of this segment remains the same.
*
* @param _key Storage key to modify.
* @param _value New value for the storage key.
* @param _key Storage key to modify.
* @param _offset Bytes offset of the new segment from the right side of the storage slot.
* @param _segment New value for the segment of the storage slot.
*/
function setStorage(bytes32 _key, bytes32 _value) external;
function setStorage(bytes32 _key, uint8 _offset, bytes memory _segment) external;

receive() external payable;

Expand Down
17 changes: 2 additions & 15 deletions packages/contracts/contracts/updaters/DefaultUpdater.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import { IProxyUpdater } from "../interfaces/IProxyUpdater.sol";
import { ProxyUpdater } from "./ProxyUpdater.sol";

/**
* @title DefaultUpdater
Expand All @@ -10,7 +10,7 @@ import { IProxyUpdater } from "../interfaces/IProxyUpdater.sol";
* transparent proxy pattern, see:
* https://docs.openzeppelin.com/contracts/4.x/api/proxy#transparent_proxy
*/
contract DefaultUpdater is IProxyUpdater {
contract DefaultUpdater is ProxyUpdater {
/**
* @notice The storage slot that holds the address of the owner.
* bytes32(uint256(keccak256('eip1967.proxy.admin')) - 1)
Expand Down Expand Up @@ -45,19 +45,6 @@ contract DefaultUpdater is IProxyUpdater {
}
}

/**
* @notice Modifies some storage slot within the proxy contract. Gives us a lot of power to
* perform upgrades in a more transparent way.
*
* @param _key Storage key to modify.
* @param _value New value for the storage key.
*/
function setStorage(bytes32 _key, bytes32 _value) external ifAdmin {
assembly {
sstore(_key, _value)
}
}

receive() external payable {
revert("DefaultUpdater: caller is not an admin");
}
Expand Down
17 changes: 2 additions & 15 deletions packages/contracts/contracts/updaters/OZUUPSUpdater.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import { IProxyUpdater } from "../interfaces/IProxyUpdater.sol";
import { ProxyUpdater } from "./ProxyUpdater.sol";

/**
* @title DefaultUpdater
Expand All @@ -10,7 +10,7 @@ import { IProxyUpdater } from "../interfaces/IProxyUpdater.sol";
* transparent proxy pattern, see:
* https://docs.openzeppelin.com/contracts/4.x/api/proxy#transparent_proxy
*/
contract OZUUPSUpdater is IProxyUpdater {
contract OZUUPSUpdater is ProxyUpdater {
/**
* @notice The storage slot that holds the address of the implementation.
* bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1)
Expand Down Expand Up @@ -97,19 +97,6 @@ contract OZUUPSUpdater is IProxyUpdater {
}
}

/**
* @notice Modifies some storage slot within the proxy contract. Gives us a lot of power to
* perform upgrades in a more transparent way.
*
* @param _key Storage key to modify.
* @param _value New value for the storage key.
*/
function setStorage(bytes32 _key, bytes32 _value) external ifChugSplashAdmin {
assembly {
sstore(_key, _value)
}
}

/**
* @notice Sets up the proxy updater when execution is being initiated.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import { IProxyUpdater } from "../interfaces/IProxyUpdater.sol";

/**
* @title ProxyUpdater
* @notice The ProxyUpdater contains the logic that sets storage slots within the proxy contract
* when an action is executed in the ChugSplashManager. When execution is being initiated,
* the ChugSplashManager sets each proxy to have a ProxyUpdater as its implementation. Then,
* during execution, the ChugSplashManager triggers `setStorage` actions on the proxy by
* calling the proxy, which then delegatecalls into this contract.
* @notice An abstract contract that contains the logic which sets storage slots within the proxy
* contract when an action is executed in the ChugSplashManager. When execution is being
* initiated, the ChugSplashManager sets each proxy to have a ProxyUpdater as its
* implementation. Then, during execution, the ChugSplashManager triggers `setStorage`
* actions on the proxy by calling the proxy, which then delegatecalls into a ProxyUpdater
* contract.
*/
contract ProxyUpdater {
abstract contract ProxyUpdater is IProxyUpdater {
/**
* @notice Replaces a segment of a proxy's storage slot value at a given key and offset. The
* storage value outside of this segment remains the same. Note that it's crucial for
Expand Down Expand Up @@ -40,7 +43,7 @@ contract ProxyUpdater {
* 0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC22222222CCCC
*
* @param _key Storage key to modify.
* @param _offset Offset of the new storage slot value to write. Denominated in bytes.
* @param _offset Bytes offset of the new segment from the right side of the storage slot.
* @param _segment New value for the segment of the storage slot. The length of this value is a
* bytesN value, where N is in the range [1, 32] (inclusive).
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/ifaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const DefaultAdapterArtifact = require('../artifacts/contracts/adapters/D
export const OZUUPSAdapterArtifact = require('../artifacts/contracts/adapters/OZUUPSAdapter.sol/OZUUPSAdapter.json')
export const OZTransparentAdapterArtifact = require('../artifacts/contracts/adapters/OZTransparentAdapter.sol/OZTransparentAdapter.json')

export const buildInfo = require('../artifacts/build-info/f9ca85351e713894739c9cbe065460c5.json')
export const buildInfo = require('../artifacts/build-info/9f866ea0881c755cccaca99645a22754.json')

export const ChugSplashRegistryABI = ChugSplashRegistryArtifact.abi
export const ChugSplashBootLoaderABI = ChugSplashBootLoaderArtifact.abi
Expand Down
14 changes: 8 additions & 6 deletions packages/contracts/test/ChugSplashManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,15 @@ contract ChugSplashManager_Test is Test {
address payable proxyAddress = manager.getDefaultProxyAddress(
bundle.actions[0].action.referenceName
);
(bytes32 storageKey, uint8 offset, bytes memory segment) = abi.decode(
action.data,
(bytes32, uint8, bytes)
);

vm.expectCall(
address(defaultUpdater),
abi.encodeCall(defaultUpdater.setStorage, (storageKey, offset, segment))
);
vm.expectCall(
address(registry),
abi.encodeCall(
Expand All @@ -527,11 +535,6 @@ contract ChugSplashManager_Test is Test {
ChugSplashBundleState memory bundleState = manager.bundles(bundleId);
vm.prank(address(manager));
address implementationAddress = Proxy(proxyAddress).implementation();
(bytes32 storageKey, bytes32 expectedStorageValue) = abi.decode(
action.data,
(bytes32, bytes32)
);
bytes32 storageValue = vm.load(proxyAddress, storageKey);
uint256 executionGasUsed = 67190;
uint256 estExecutorPayment = (tx.gasprice *
executionGasUsed *
Expand All @@ -540,7 +543,6 @@ contract ChugSplashManager_Test is Test {
assertEq(bundleState.actionsExecuted, 1);
assertTrue(bundleState.executions[actionIndex]);
assertEq(implementationAddress, address(defaultUpdater));
assertEq(storageValue, expectedStorageValue);
assertGt(finalTotalDebt, estExecutorPayment + initialTotalDebt);
assertGt(finalExecutorDebt, estExecutorPayment + initialExecutorDebt);
}
Expand Down
56 changes: 45 additions & 11 deletions packages/contracts/test/bundle-info.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,65 @@
{
"configUri": "ipfs://QmS35tha8uE8iKYG6rw7TeTyhGB9xqHoard6RXxHBJ5V94",
"bundle": {
"root": "0x2d4ea96aa61410f91a144fb57d64c28fca2e099404eec43fc3830b8311be7ccb",
"root": "0xc69c5f209aecccd63577bed37340ccf7d78ca0c5ca726a821e5dd4885a9b3fa2",
"actions": [
{
"action": {
"actionType": 0,
"referenceName": "MyFirstContract",
"data": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111110101"
"data": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000141111111111111111111111111111111111111111000000000000000000000000"
},
"proof": {
"actionIndex": 0,
"siblings": [
"0xc1272d5368cbc24015c12646296c2fa723ff43c03caccd84330981b4108251e1",
"0xb35b26b12430cff2ddd5c24b513cfd2dffa12ae2788b150d3cc238ba29c337b9"
"0xff50c585051b08b3734683722a269e5cc8f04b3b865cd804ce61cd42a2d36036",
"0xdc482ddbf2a646ba153cfcf3239bab9ca02c50e4221a0d645fafe9d8d1233fd2",
"0x8958b5734025beaf6508e2a8a531057783d0376c82302d3c1a92fd370053c08b"
]
}
},
{
"action": {
"actionType": 0,
"referenceName": "MyFirstContract",
"data": "0x0000000000000000000000000000000000000000000000000000000000000001466972737400000000000000000000000000000000000000000000000000000a"
"data": "0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020466972737400000000000000000000000000000000000000000000000000000a"
},
"proof": {
"actionIndex": 1,
"siblings": [
"0x3df7764438164b7d855a8b9e12e6755b7cd0d2aaca87cf789a248753462af93a",
"0xb35b26b12430cff2ddd5c24b513cfd2dffa12ae2788b150d3cc238ba29c337b9"
"0xf83892dbb16a290da8af4b450e2b0026eb33e82229b454b1636da7b006bdd5e9",
"0xdc482ddbf2a646ba153cfcf3239bab9ca02c50e4221a0d645fafe9d8d1233fd2",
"0x8958b5734025beaf6508e2a8a531057783d0376c82302d3c1a92fd370053c08b"
]
}
},
{
"action": {
"actionType": 0,
"referenceName": "MyFirstContract",
"data": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000010100000000000000000000000000000000000000000000000000000000000000"
},
"proof": {
"actionIndex": 2,
"siblings": [
"0x537d8edb399d3f74715e2f665cc3b436b4e3b243fea3f1fb430542a9fb617b33",
"0xcd1f2ad02a0255738fef982026f6b5542befcb4adafec3d89f5095034c642622",
"0x8958b5734025beaf6508e2a8a531057783d0376c82302d3c1a92fd370053c08b"
]
}
},
{
"action": {
"actionType": 0,
"referenceName": "MyFirstContract",
"data": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000010100000000000000000000000000000000000000000000000000000000000000"
},
"proof": {
"actionIndex": 3,
"siblings": [
"0xb3615c1ccce6fc1e98a503ae91a7e55db3766e43f9ac69341c221401a2db5d48",
"0xcd1f2ad02a0255738fef982026f6b5542befcb4adafec3d89f5095034c642622",
"0x8958b5734025beaf6508e2a8a531057783d0376c82302d3c1a92fd370053c08b"
]
}
},
Expand All @@ -38,10 +70,11 @@
"data": "0x608060405234801561001057600080fd5b506103bb806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80638381f58a14610051578063a9b6d3e01461006f578063d73c277c1461008d578063e582dd31146100ab575b600080fd5b6100596100c9565b60405161006691906101bd565b60405180910390f35b6100776100da565b6040516100849190610219565b60405180910390f35b610095610100565b6040516100a291906102cd565b60405180910390f35b6100b361018e565b6040516100c0919061030a565b60405180910390f35b60008054906101000a900460ff1681565b600060029054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6001805461010d90610354565b80601f016020809104026020016040519081016040528092919081815260200182805461013990610354565b80156101865780601f1061015b57610100808354040283529160200191610186565b820191906000526020600020905b81548152906001019060200180831161016957829003601f168201915b505050505081565b600060019054906101000a900460ff1681565b600060ff82169050919050565b6101b7816101a1565b82525050565b60006020820190506101d260008301846101ae565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610203826101d8565b9050919050565b610213816101f8565b82525050565b600060208201905061022e600083018461020a565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561026e578082015181840152602081019050610253565b8381111561027d576000848401525b50505050565b6000601f19601f8301169050919050565b600061029f82610234565b6102a9818561023f565b93506102b9818560208601610250565b6102c281610283565b840191505092915050565b600060208201905081810360008301526102e78184610294565b905092915050565b60008115159050919050565b610304816102ef565b82525050565b600060208201905061031f60008301846102fb565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061036c57607f821691505b60208210810361037f5761037e610325565b5b5091905056fea264697066735822122083241dfc84b846b5a2b4dcfc20dddc5eee3586203f311ffc3b03ed1ba3008b2c64736f6c634300080f0033"
},
"proof": {
"actionIndex": 2,
"actionIndex": 4,
"siblings": [
"0x0ea232209cb3ebb639882cb06e58700268be1699d804aba267612ee905c479a9",
"0x50a72b081d949d902fa7e44958b58501a0a446cbab7128206271eec9ca0f1d59"
"0x633dc4d7da7256660a892f8f1604a44b5432649cc8ec5cb3ced4c4e6ac94dd1d",
"0xa72a556f16ec0d646b73e04e90d037c8478cbb8cec900ff33fa970e5797ccdf7"
]
}
},
Expand All @@ -52,10 +85,11 @@
"data": "0x"
},
"proof": {
"actionIndex": 3,
"actionIndex": 5,
"siblings": [
"0xd8cf1c8a60763c89f182afbe29f535bcdccb7915eff3bf14770fb09525e04d00",
"0x50a72b081d949d902fa7e44958b58501a0a446cbab7128206271eec9ca0f1d59"
"0x633dc4d7da7256660a892f8f1604a44b5432649cc8ec5cb3ced4c4e6ac94dd1d",
"0xa72a556f16ec0d646b73e04e90d037c8478cbb8cec900ff33fa970e5797ccdf7"
]
}
}
Expand Down

0 comments on commit 736b859

Please sign in to comment.