Skip to content

Commit c0f9c5d

Browse files
committed
unify storage locations
1 parent 59f1d41 commit c0f9c5d

19 files changed

Lines changed: 344 additions & 198 deletions

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ hh run scripts/find_missing_virtual_methods.ts.ts
170170

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

173+
### find upgradeable contracts storage locations
174+
175+
```bash
176+
grep -o -h -E '([A-Z0-9_]+_LOCATION_V[0-9]+_[0-9]+\s*=\s*0x[A-Ha-h0-9]+)' -r contracts
177+
```
178+
179+
Prints addresses for each storage struct of each release of each upgradeable contract
180+
173181
## Forge
174182

175183
### Commands

contracts/distribution/Distribution.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ abstract contract Distribution is
2222
InstanceLinkedComponent,
2323
IDistributionComponent
2424
{
25-
// keccak256(abi.encode(uint256(keccak256("etherisc.storage.Distribution")) - 1)) & ~bytes32(uint256(0xff));
26-
bytes32 public constant DISTRIBUTION_STORAGE_LOCATION_V1 = 0xaab7c5ea03d290056d6c060e0833d3ebcbe647f7694616a2ec52738a64b2f900;
25+
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.Distribution@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
26+
bytes32 public constant DISTRIBUTION_STORAGE_LOCATION_V3_0 = 0xf05be9ae88f1a13816beae3bf4177e164907f5d055d6c38ea0a1fabc08c6c500;
2727

2828
struct DistributionStorage {
2929
IComponentService _componentService;
@@ -241,7 +241,7 @@ abstract contract Distribution is
241241

242242
function _getDistributionStorage() private pure returns (DistributionStorage storage $) {
243243
assembly {
244-
$.slot := DISTRIBUTION_STORAGE_LOCATION_V1
244+
$.slot := DISTRIBUTION_STORAGE_LOCATION_V3_0
245245
}
246246
}
247247
}

contracts/oracle/Oracle.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ abstract contract Oracle is
1818
InstanceLinkedComponent,
1919
IOracleComponent
2020
{
21-
// keccak256(abi.encode(uint256(keccak256("etherisc.storage.Oracle")) - 1)) & ~bytes32(uint256(0xff));
22-
bytes32 public constant ORACLE_STORAGE_LOCATION_V1 = 0xaab7c0ea03d290e56d6c060e0733d3ebcbe647f7694616a2ec52738a64b2f900;
21+
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.Oracle@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
22+
bytes32 public constant ORACLE_STORAGE_LOCATION_V3_0 = 0x189f66bb26b02fde8f3e50c7c928f45e0131f04edebf4d58daf0aee756df0e00;
2323

2424
struct OracleStorage {
2525
IComponentService _componentService;
@@ -148,7 +148,7 @@ abstract contract Oracle is
148148

149149
function _getOracleStorage() private pure returns (OracleStorage storage $) {
150150
assembly {
151-
$.slot := ORACLE_STORAGE_LOCATION_V1
151+
$.slot := ORACLE_STORAGE_LOCATION_V3_0
152152
}
153153
}
154154
}

contracts/pool/BundleService.sol

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@ contract BundleService is
3030

3131
string public constant NAME = "BundleService";
3232

33-
address private _registryAddress;
34-
IRegistryService private _registryService;
35-
IAccountingService private _accountingService;
36-
IComponentService private _componentService;
33+
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.BundleService@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
34+
bytes32 public constant BUNDLE_SERVICE_STORAGE_LOCATION_V3_0 = 0x46b8ce3500c11673bb6f380e5e46ed88536ecfaa352cb594644cb469c5a14d00;
35+
36+
struct BundleServiceStorage {
37+
address _registryAddress;
38+
IRegistryService _registryService;
39+
IAccountingService _accountingService;
40+
IComponentService _componentService;
41+
}
3742

3843
function _initialize(
3944
address owner,
@@ -50,9 +55,10 @@ contract BundleService is
5055

5156
__Service_init(authority, registry, owner);
5257

53-
_registryService = IRegistryService(_getServiceAddress(REGISTRY()));
54-
_accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
55-
_componentService = IComponentService(_getServiceAddress(COMPONENT()));
58+
BundleServiceStorage storage $ = _getBundleServiceStorage();
59+
$._registryService = IRegistryService(_getServiceAddress(REGISTRY()));
60+
$._accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
61+
$._componentService = IComponentService(_getServiceAddress(COMPONENT()));
5662

5763
_registerInterface(type(IBundleService).interfaceId);
5864
}
@@ -100,7 +106,8 @@ contract BundleService is
100106
(NftId poolNftId, IInstance instance) = PoolLib.getAndVerifyActivePool(getRegistry(), msg.sender);
101107

102108
// register bundle with registry
103-
bundleNftId = _registryService.registerBundle(
109+
BundleServiceStorage storage $ = _getBundleServiceStorage();
110+
bundleNftId = $._registryService.registerBundle(
104111
IRegistry.ObjectInfo(
105112
NftIdLib.zero(),
106113
poolNftId,
@@ -261,7 +268,8 @@ contract BundleService is
261268
Amount balanceAmountWithFees = instanceReader.getBalanceAmount(bundleNftId);
262269
feeAmount = instanceReader.getFeeAmount(bundleNftId);
263270
unstakedAmount = balanceAmountWithFees - feeAmount;
264-
_accountingService.decreaseBundleBalance(instanceStore, bundleNftId, unstakedAmount, feeAmount);
271+
BundleServiceStorage storage $ = _getBundleServiceStorage();
272+
$._accountingService.decreaseBundleBalance(instanceStore, bundleNftId, unstakedAmount, feeAmount);
265273
}
266274

267275
emit LogBundleServiceBundleClosed(bundleNftId);
@@ -291,7 +299,8 @@ contract BundleService is
291299
}
292300

293301
// effects
294-
_accountingService.increaseBundleBalance(
302+
BundleServiceStorage storage $ = _getBundleServiceStorage();
303+
$._accountingService.increaseBundleBalance(
295304
instanceStore,
296305
bundleNftId,
297306
amount,
@@ -334,7 +343,8 @@ contract BundleService is
334343
}
335344

336345
// effects
337-
_accountingService.decreaseBundleBalance(
346+
BundleServiceStorage storage $ = _getBundleServiceStorage();
347+
$._accountingService.decreaseBundleBalance(
338348
instanceStore,
339349
bundleNftId,
340350
unstakedAmount,
@@ -399,6 +409,11 @@ contract BundleService is
399409
emit LogBundleServiceCollateralReleased(bundleNftId, policyNftId, collateralAmount);
400410
}
401411

412+
function _getBundleServiceStorage() private pure returns (BundleServiceStorage storage $) {
413+
assembly {
414+
$.slot := BUNDLE_SERVICE_STORAGE_LOCATION_V3_0
415+
}
416+
}
402417

403418
function _getDomain() internal pure override returns(ObjectType) {
404419
return BUNDLE();

contracts/pool/Pool.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ abstract contract Pool is
2323
InstanceLinkedComponent,
2424
IPoolComponent
2525
{
26-
// keccak256(abi.encode(uint256(keccak256("etherisc.storage.Pool")) - 1)) & ~bytes32(uint256(0xff));
27-
bytes32 public constant POOL_STORAGE_LOCATION_V1 = 0x25e3e51823fbfffb988e0a2744bb93722d9f3e906c07cc0a9e77884c46c58300;
26+
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.Pool@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
27+
bytes32 public constant POOL_STORAGE_LOCATION_V3_0 = 0xceba0dc57a322ad9ac8622c7bbee3a223850780710333daffeacd4b6d53d6e00;
2828

2929
struct PoolStorage {
3030
IComponents.PoolInfo _poolInfo;
@@ -337,7 +337,7 @@ abstract contract Pool is
337337

338338
function _getPoolStorage() private pure returns (PoolStorage storage $) {
339339
assembly {
340-
$.slot := POOL_STORAGE_LOCATION_V1
340+
$.slot := POOL_STORAGE_LOCATION_V3_0
341341
}
342342
}
343343
}

contracts/pool/PoolService.sol

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ contract PoolService is
3131
Service,
3232
IPoolService
3333
{
34-
IAccountingService private _accountingService;
35-
IBundleService internal _bundleService;
36-
IComponentService internal _componentService;
37-
IStaking private _staking;
34+
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.PoolService@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
35+
bytes32 public constant POOL_SERVICE_STORAGE_LOCATION_V3_0 = 0x99b25f02200e1434f4531a215abb8fd8c59ad15f6abb6b06ffa8223eb7162700;
36+
37+
struct PoolServiceStorage {
38+
IAccountingService _accountingService;
39+
IBundleService _bundleService;
40+
IComponentService _componentService;
41+
IStaking _staking;
42+
}
3843

3944
function _initialize(
4045
address owner,
@@ -51,10 +56,11 @@ contract PoolService is
5156

5257
__Service_init(authority, registry, owner);
5358

54-
_accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
55-
_bundleService = IBundleService(_getServiceAddress(BUNDLE()));
56-
_componentService = IComponentService(_getServiceAddress(COMPONENT()));
57-
_staking = IStaking(getRegistry().getStakingAddress());
59+
PoolServiceStorage storage $ = _getPoolServiceStorage();
60+
$._accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
61+
$._bundleService = IBundleService(_getServiceAddress(BUNDLE()));
62+
$._componentService = IComponentService(_getServiceAddress(COMPONENT()));
63+
$._staking = IStaking(getRegistry().getStakingAddress());
5864

5965
_registerInterface(type(IPoolService).interfaceId);
6066
}
@@ -90,9 +96,10 @@ contract PoolService is
9096
// TODO get performance fee for pool (#477)
9197

9298
// releasing collateral in bundle
93-
(Amount unstakedAmount, Amount feeAmount) = _bundleService.close(instance, bundleNftId);
99+
PoolServiceStorage storage $ = _getPoolServiceStorage();
100+
(Amount unstakedAmount, Amount feeAmount) = $._bundleService.close(instance, bundleNftId);
94101

95-
_accountingService.decreasePoolBalance(
102+
$._accountingService.decreasePoolBalance(
96103
instance.getInstanceStore(),
97104
poolNftId,
98105
unstakedAmount + feeAmount,
@@ -205,13 +212,14 @@ contract PoolService is
205212
amount);
206213

207214
// do all the book keeping
208-
_accountingService.increasePoolBalance(
215+
PoolServiceStorage storage $ = _getPoolServiceStorage();
216+
$._accountingService.increasePoolBalance(
209217
instanceStore,
210218
poolNftId,
211219
netAmount,
212220
feeAmount);
213221

214-
_bundleService.stake(instanceReader, instanceStore, bundleNftId, netAmount);
222+
$._bundleService.stake(instanceReader, instanceStore, bundleNftId, netAmount);
215223

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

@@ -244,16 +252,17 @@ contract PoolService is
244252
) = PoolLib.checkAndGetPoolInfo(getRegistry(), msg.sender, bundleNftId);
245253

246254
// call bundle service for bookkeeping and additional checks
247-
Amount unstakedAmount = _bundleService.unstake(instanceStore, bundleNftId, amount);
255+
PoolServiceStorage storage $ = _getPoolServiceStorage();
256+
Amount unstakedAmount = $._bundleService.unstake(instanceStore, bundleNftId, amount);
248257

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

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

255-
// update pool bookkeeping - performance fees stay in the pool, but as fees
256-
_accountingService.decreasePoolBalance(
264+
// update pool bookkeeping - performance fees stay in the pool, but as fees
265+
$._accountingService.decreasePoolBalance(
257266
instanceStore,
258267
poolNftId,
259268
unstakedAmount,
@@ -350,13 +359,14 @@ contract PoolService is
350359
Amount bundleNetAmount = premium.netPremiumAmount;
351360

352361
InstanceStore instanceStore = instance.getInstanceStore();
353-
_accountingService.increasePoolBalance(
362+
PoolServiceStorage storage $ = _getPoolServiceStorage();
363+
$._accountingService.increasePoolBalance(
354364
instanceStore,
355365
poolNftId,
356366
bundleNetAmount + bundleFeeAmount,
357367
poolFeeAmount);
358368

359-
_accountingService.increaseBundleBalanceForPool(
369+
$._accountingService.increaseBundleBalanceForPool(
360370
instanceStore,
361371
bundleNftId,
362372
bundleNetAmount,
@@ -399,14 +409,15 @@ contract PoolService is
399409
sumInsuredAmount);
400410

401411
// lock collateral amount from involved bundle
402-
_bundleService.lockCollateral(
412+
PoolServiceStorage storage $ = _getPoolServiceStorage();
413+
$._bundleService.lockCollateral(
403414
instance,
404415
applicationNftId,
405416
bundleNftId,
406417
localCollateralAmount);
407418

408419
// update value locked with staking service
409-
_staking.increaseTotalValueLocked(
420+
$._staking.increaseTotalValueLocked(
410421
instance.getNftId(),
411422
token,
412423
totalCollateralAmount);
@@ -455,20 +466,21 @@ contract PoolService is
455466

456467
// effects
457468
NftId poolNftId = getRegistry().getParentNftId(bundleNftId);
469+
PoolServiceStorage storage $ = _getPoolServiceStorage();
458470

459-
_accountingService.decreasePoolBalance(
471+
$._accountingService.decreasePoolBalance(
460472
instanceStore,
461473
poolNftId,
462474
payoutAmount,
463475
AmountLib.zero());
464476

465-
_accountingService.decreaseBundleBalanceForPool(
477+
$._accountingService.decreaseBundleBalanceForPool(
466478
instanceStore,
467479
bundleNftId,
468480
payoutAmount,
469481
AmountLib.zero());
470482

471-
_bundleService.releaseCollateral(
483+
$._bundleService.releaseCollateral(
472484
instanceStore,
473485
policyNftId,
474486
bundleNftId,
@@ -477,7 +489,7 @@ contract PoolService is
477489
// update value locked with staking service
478490
TokenHandler poolTokenHandler = instanceReader.getTokenHandler(poolNftId);
479491

480-
_staking.decreaseTotalValueLocked(
492+
$._staking.decreaseTotalValueLocked(
481493
instanceReader.getInstanceNftId(),
482494
address(poolTokenHandler.TOKEN()),
483495
payoutAmount);
@@ -494,7 +506,7 @@ contract PoolService is
494506
payoutBeneficiary);
495507

496508
if (processingFeeAmount.gtz()) {
497-
_accountingService.increaseProductFeesForPool(
509+
$._accountingService.increaseProductFeesForPool(
498510
instanceStore,
499511
productNftId,
500512
processingFeeAmount);
@@ -541,10 +553,11 @@ contract PoolService is
541553
// decrease fee counters by withdrawnAmount
542554
{
543555
InstanceStore store = instance.getInstanceStore();
556+
PoolServiceStorage storage $ = _getPoolServiceStorage();
544557
// decrease fee amount of the bundle
545-
_accountingService.decreaseBundleBalanceForPool(store, bundleNftId, AmountLib.zero(), withdrawnAmount);
558+
$._accountingService.decreaseBundleBalanceForPool(store, bundleNftId, AmountLib.zero(), withdrawnAmount);
546559
// decrease pool balance
547-
_accountingService.decreasePoolBalance(store, poolNftId, withdrawnAmount, AmountLib.zero());
560+
$._accountingService.decreasePoolBalance(store, poolNftId, withdrawnAmount, AmountLib.zero());
548561
}
549562

550563
// interactions
@@ -575,15 +588,17 @@ contract PoolService is
575588

576589
Amount remainingCollateralAmount = policyInfo.sumInsuredAmount - policyInfo.claimAmount;
577590

578-
_bundleService.releaseCollateral(
591+
// effects
592+
PoolServiceStorage storage $ = _getPoolServiceStorage();
593+
$._bundleService.releaseCollateral(
579594
instance.getInstanceStore(),
580595
policyNftId,
581596
policyInfo.bundleNftId,
582597
remainingCollateralAmount);
583598

584599
// update value locked with staking service
585600
InstanceReader instanceReader = instance.getInstanceReader();
586-
_staking.decreaseTotalValueLocked(
601+
$._staking.decreaseTotalValueLocked(
587602
instanceReader.getInstanceNftId(),
588603
address(instanceReader.getToken(policyInfo.productNftId)),
589604
remainingCollateralAmount);
@@ -607,6 +622,12 @@ contract PoolService is
607622
return PoolLib.getAndVerifyActivePool(getRegistry(), msg.sender);
608623
}
609624

625+
function _getPoolServiceStorage() private pure returns (PoolServiceStorage storage $) {
626+
assembly {
627+
$.slot := POOL_SERVICE_STORAGE_LOCATION_V3_0
628+
}
629+
}
630+
610631

611632
function _getDomain() internal pure override returns(ObjectType) {
612633
return POOL();

0 commit comments

Comments
 (0)