Skip to content

Commit

Permalink
unify storage locations
Browse files Browse the repository at this point in the history
  • Loading branch information
rapidddenis committed Sep 3, 2024
1 parent bc7a257 commit 36bc878
Show file tree
Hide file tree
Showing 19 changed files with 285 additions and 151 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ hh run scripts/find_missing_virtual_methods.ts.ts

Checks all public/external methods in services and components for methods that are not marked as `virtual` but should be.

### find upgradeable contracts storage locations

```bash
grep -o -h -E '([A-Z0-9_]+_LOCATION_V[0-9]+_[0-9]+\s*=\s*0x[A-Ha-h0-9]+)' -r contracts
```

Prints addresses for each storage struct of each release of each upgradeable contract

## Forge

### Commands
Expand Down
6 changes: 3 additions & 3 deletions contracts/distribution/Distribution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ abstract contract Distribution is
InstanceLinkedComponent,
IDistributionComponent
{
// keccak256(abi.encode(uint256(keccak256("etherisc.storage.Distribution")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant DISTRIBUTION_STORAGE_LOCATION_V1 = 0xaab7c5ea03d290056d6c060e0833d3ebcbe647f7694616a2ec52738a64b2f900;
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.Distribution@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant DISTRIBUTION_STORAGE_LOCATION_V3_0 = 0xf05be9ae88f1a13816beae3bf4177e164907f5d055d6c38ea0a1fabc08c6c500;

struct DistributionStorage {
IComponentService _componentService;
Expand Down Expand Up @@ -238,7 +238,7 @@ abstract contract Distribution is

function _getDistributionStorage() private pure returns (DistributionStorage storage $) {
assembly {
$.slot := DISTRIBUTION_STORAGE_LOCATION_V1
$.slot := DISTRIBUTION_STORAGE_LOCATION_V3_0
}
}
}
6 changes: 3 additions & 3 deletions contracts/oracle/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ abstract contract Oracle is
InstanceLinkedComponent,
IOracleComponent
{
// keccak256(abi.encode(uint256(keccak256("etherisc.storage.Oracle")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant ORACLE_STORAGE_LOCATION_V1 = 0xaab7c0ea03d290e56d6c060e0733d3ebcbe647f7694616a2ec52738a64b2f900;
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.Oracle@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant ORACLE_STORAGE_LOCATION_V3_0 = 0x189f66bb26b02fde8f3e50c7c928f45e0131f04edebf4d58daf0aee756df0e00;

struct OracleStorage {
IComponentService _componentService;
Expand Down Expand Up @@ -148,7 +148,7 @@ abstract contract Oracle is

function _getOracleStorage() private pure returns (OracleStorage storage $) {
assembly {
$.slot := ORACLE_STORAGE_LOCATION_V1
$.slot := ORACLE_STORAGE_LOCATION_V3_0
}
}
}
37 changes: 26 additions & 11 deletions contracts/pool/BundleService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ contract BundleService is

string public constant NAME = "BundleService";

address private _registryAddress;
IRegistryService private _registryService;
IAccountingService private _accountingService;
IComponentService private _componentService;
// keccak256(abi.encode(uint256(keccak256("[email protected]")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant BUNDLE_SERVICE_STORAGE_LOCATION_V3_0 = 0x46b8ce3500c11673bb6f380e5e46ed88536ecfaa352cb594644cb469c5a14d00;

struct BundleServiceStorage {
address _registryAddress;
IRegistryService _registryService;
IAccountingService _accountingService;
IComponentService _componentService;
}

function _initialize(
address owner,
Expand All @@ -51,9 +56,10 @@ contract BundleService is

__Service_init(authority, registry, owner);

_registryService = IRegistryService(_getServiceAddress(REGISTRY()));
_accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
_componentService = IComponentService(_getServiceAddress(COMPONENT()));
BundleServiceStorage storage $ = _getBundleServiceStorage();
$._registryService = IRegistryService(_getServiceAddress(REGISTRY()));
$._accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
$._componentService = IComponentService(_getServiceAddress(COMPONENT()));

_registerInterface(type(IBundleService).interfaceId);
}
Expand Down Expand Up @@ -99,7 +105,8 @@ contract BundleService is
(NftId poolNftId,, IInstance instance) = _getAndVerifyActiveComponent(POOL());

// register bundle with registry
bundleNftId = _registryService.registerBundle(
BundleServiceStorage storage $ = _getBundleServiceStorage();
bundleNftId = $._registryService.registerBundle(
IRegistry.ObjectInfo(
NftIdLib.zero(),
poolNftId,
Expand Down Expand Up @@ -258,7 +265,8 @@ contract BundleService is
Amount balanceAmountWithFees = instanceReader.getBalanceAmount(bundleNftId);
feeAmount = instanceReader.getFeeAmount(bundleNftId);
unstakedAmount = balanceAmountWithFees - feeAmount;
_accountingService.decreaseBundleBalance(instanceStore, bundleNftId, unstakedAmount, feeAmount);
BundleServiceStorage storage $ = _getBundleServiceStorage();
$._accountingService.decreaseBundleBalance(instanceStore, bundleNftId, unstakedAmount, feeAmount);
}
}

Expand Down Expand Up @@ -286,7 +294,8 @@ contract BundleService is
}

// effects
_accountingService.increaseBundleBalance(
BundleServiceStorage storage $ = _getBundleServiceStorage();
$._accountingService.increaseBundleBalance(
instanceStore,
bundleNftId,
amount,
Expand Down Expand Up @@ -327,7 +336,8 @@ contract BundleService is
}

// effects
_accountingService.decreaseBundleBalance(
BundleServiceStorage storage $ = _getBundleServiceStorage();
$._accountingService.decreaseBundleBalance(
instanceStore,
bundleNftId,
unstakedAmount,
Expand Down Expand Up @@ -388,6 +398,11 @@ contract BundleService is
instanceStore.decreaseLocked(bundleNftId, collateralAmount);
}

function _getBundleServiceStorage() private pure returns (BundleServiceStorage storage $) {
assembly {
$.slot := BUNDLE_SERVICE_STORAGE_LOCATION_V3_0
}
}

function _getDomain() internal pure override returns(ObjectType) {
return BUNDLE();
Expand Down
6 changes: 3 additions & 3 deletions contracts/pool/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ abstract contract Pool is
InstanceLinkedComponent,
IPoolComponent
{
// keccak256(abi.encode(uint256(keccak256("etherisc.storage.Pool")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant POOL_STORAGE_LOCATION_V1 = 0x25e3e51823fbfffb988e0a2744bb93722d9f3e906c07cc0a9e77884c46c58300;
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.Pool@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant POOL_STORAGE_LOCATION_V3_0 = 0xceba0dc57a322ad9ac8622c7bbee3a223850780710333daffeacd4b6d53d6e00;

struct PoolStorage {
IComponents.PoolInfo _poolInfo;
Expand Down Expand Up @@ -337,7 +337,7 @@ abstract contract Pool is

function _getPoolStorage() private pure returns (PoolStorage storage $) {
assembly {
$.slot := POOL_STORAGE_LOCATION_V1
$.slot := POOL_STORAGE_LOCATION_V3_0
}
}
}
75 changes: 48 additions & 27 deletions contracts/pool/PoolService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ contract PoolService is
Service,
IPoolService
{
IAccountingService private _accountingService;
IBundleService internal _bundleService;
IComponentService internal _componentService;
IStaking private _staking;
// keccak256(abi.encode(uint256(keccak256("[email protected]")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant POOL_SERVICE_STORAGE_LOCATION_V3_0 = 0x99b25f02200e1434f4531a215abb8fd8c59ad15f6abb6b06ffa8223eb7162700;

struct PoolServiceStorage {
IAccountingService _accountingService;
IBundleService _bundleService;
IComponentService _componentService;
IStaking _staking;
}

function _initialize(
address owner,
Expand All @@ -56,10 +61,11 @@ contract PoolService is

__Service_init(authority, registry, owner);

_accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
_bundleService = IBundleService(_getServiceAddress(BUNDLE()));
_componentService = IComponentService(_getServiceAddress(COMPONENT()));
_staking = IStaking(getRegistry().getStakingAddress());
PoolServiceStorage storage $ = _getPoolServiceStorage();
$._accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
$._bundleService = IBundleService(_getServiceAddress(BUNDLE()));
$._componentService = IComponentService(_getServiceAddress(COMPONENT()));
$._staking = IStaking(getRegistry().getStakingAddress());

_registerInterface(type(IPoolService).interfaceId);
}
Expand Down Expand Up @@ -95,9 +101,10 @@ contract PoolService is
// TODO get performance fee for pool (#477)

// releasing collateral in bundle
(Amount unstakedAmount, Amount feeAmount) = _bundleService.close(instance, bundleNftId);
PoolServiceStorage storage $ = _getPoolServiceStorage();
(Amount unstakedAmount, Amount feeAmount) = $._bundleService.close(instance, bundleNftId);

_accountingService.decreasePoolBalance(
$._accountingService.decreasePoolBalance(
instance.getInstanceStore(),
poolNftId,
unstakedAmount + feeAmount,
Expand Down Expand Up @@ -210,13 +217,14 @@ contract PoolService is
amount);

// do all the book keeping
_accountingService.increasePoolBalance(
PoolServiceStorage storage $ = _getPoolServiceStorage();
$._accountingService.increasePoolBalance(
instanceStore,
poolNftId,
netAmount,
feeAmount);

_bundleService.stake(instanceReader, instanceStore, bundleNftId, netAmount);
$._bundleService.stake(instanceReader, instanceStore, bundleNftId, netAmount);

emit LogPoolServiceBundleStaked(instanceNftId, poolNftId, bundleNftId, amount, netAmount);

Expand Down Expand Up @@ -249,16 +257,17 @@ contract PoolService is
) = PoolLib.checkAndGetPoolInfo(getRegistry(), msg.sender, bundleNftId);

// call bundle service for bookkeeping and additional checks
Amount unstakedAmount = _bundleService.unstake(instanceStore, bundleNftId, amount);
PoolServiceStorage storage $ = _getPoolServiceStorage();
Amount unstakedAmount = $._bundleService.unstake(instanceStore, bundleNftId, amount);

// Important: from now on work only with unstakedAmount as it is the only reliable amount.
// if amount was max, this was set to the available amount

// TODO: handle performance fees (issue #477)
netAmount = unstakedAmount;

// update pool bookkeeping - performance fees stay in the pool, but as fees
_accountingService.decreasePoolBalance(
// update pool bookkeeping - performance fees stay in the pool, but as fees
$._accountingService.decreasePoolBalance(
instanceStore,
poolNftId,
unstakedAmount,
Expand Down Expand Up @@ -355,13 +364,14 @@ contract PoolService is
Amount bundleNetAmount = premium.netPremiumAmount;

InstanceStore instanceStore = instance.getInstanceStore();
_accountingService.increasePoolBalance(
PoolServiceStorage storage $ = _getPoolServiceStorage();
$._accountingService.increasePoolBalance(
instanceStore,
poolNftId,
bundleNetAmount + bundleFeeAmount,
poolFeeAmount);

_accountingService.increaseBundleBalanceForPool(
$._accountingService.increaseBundleBalanceForPool(
instanceStore,
bundleNftId,
bundleNetAmount,
Expand Down Expand Up @@ -402,14 +412,15 @@ contract PoolService is
sumInsuredAmount);

// lock collateral amount from involved bundle
_bundleService.lockCollateral(
PoolServiceStorage storage $ = _getPoolServiceStorage();
$._bundleService.lockCollateral(
instance,
applicationNftId,
bundleNftId,
localCollateralAmount);

// update value locked with staking service
_staking.increaseTotalValueLocked(
$._staking.increaseTotalValueLocked(
instance.getNftId(),
token,
totalCollateralAmount);
Expand Down Expand Up @@ -449,20 +460,21 @@ contract PoolService is

// effects
NftId poolNftId = getRegistry().getParentNftId(bundleNftId);
PoolServiceStorage storage $ = _getPoolServiceStorage();

_accountingService.decreasePoolBalance(
$._accountingService.decreasePoolBalance(
instanceStore,
poolNftId,
payoutAmount,
AmountLib.zero());

_accountingService.decreaseBundleBalanceForPool(
$._accountingService.decreaseBundleBalanceForPool(
instanceStore,
bundleNftId,
payoutAmount,
AmountLib.zero());

_bundleService.releaseCollateral(
$._bundleService.releaseCollateral(
instanceStore,
policyNftId,
bundleNftId,
Expand All @@ -471,7 +483,7 @@ contract PoolService is
// update value locked with staking service
TokenHandler poolTokenHandler = instanceReader.getTokenHandler(poolNftId);

_staking.decreaseTotalValueLocked(
$._staking.decreaseTotalValueLocked(
instanceReader.getInstanceNftId(),
address(poolTokenHandler.TOKEN()),
payoutAmount);
Expand Down Expand Up @@ -519,10 +531,11 @@ contract PoolService is
// decrease fee counters by withdrawnAmount
{
InstanceStore store = instance.getInstanceStore();
PoolServiceStorage storage $ = _getPoolServiceStorage();
// decrease fee amount of the bundle
_accountingService.decreaseBundleBalanceForPool(store, bundleNftId, AmountLib.zero(), withdrawnAmount);
$._accountingService.decreaseBundleBalanceForPool(store, bundleNftId, AmountLib.zero(), withdrawnAmount);
// decrease pool balance
_accountingService.decreasePoolBalance(store, poolNftId, withdrawnAmount, AmountLib.zero());
$._accountingService.decreasePoolBalance(store, poolNftId, withdrawnAmount, AmountLib.zero());
}

// interactions
Expand Down Expand Up @@ -553,15 +566,17 @@ contract PoolService is

Amount remainingCollateralAmount = policyInfo.sumInsuredAmount - policyInfo.claimAmount;

_bundleService.releaseCollateral(
// effects
PoolServiceStorage storage $ = _getPoolServiceStorage();
$._bundleService.releaseCollateral(
instance.getInstanceStore(),
policyNftId,
policyInfo.bundleNftId,
remainingCollateralAmount);

// update value locked with staking service
InstanceReader instanceReader = instance.getInstanceReader();
_staking.decreaseTotalValueLocked(
$._staking.decreaseTotalValueLocked(
instanceReader.getInstanceNftId(),
address(instanceReader.getToken(policyInfo.productNftId)),
remainingCollateralAmount);
Expand All @@ -580,6 +595,12 @@ contract PoolService is
return PoolLib.getAndVerifyActivePool(getRegistry(), msg.sender);
}

function _getPoolServiceStorage() private pure returns (PoolServiceStorage storage $) {
assembly {
$.slot := POOL_SERVICE_STORAGE_LOCATION_V3_0
}
}


function _getDomain() internal pure override returns(ObjectType) {
return POOL();
Expand Down
Loading

0 comments on commit 36bc878

Please sign in to comment.