From a2d24d9159369ea841a2da6729ca7121b0af5b7c Mon Sep 17 00:00:00 2001 From: Marc Doerflinger Date: Tue, 17 Sep 2024 17:50:18 +0000 Subject: [PATCH] refactor state update (#660) --- contracts/instance/BaseStore.sol | 8 +++--- contracts/instance/InstanceStore.sol | 36 ++++++++---------------- contracts/instance/ProductStore.sol | 41 ++++++++++------------------ foundry.toml | 15 +++++++++- 4 files changed, 44 insertions(+), 56 deletions(-) diff --git a/contracts/instance/BaseStore.sol b/contracts/instance/BaseStore.sol index f9f1b57bf..d69521088 100644 --- a/contracts/instance/BaseStore.sol +++ b/contracts/instance/BaseStore.sol @@ -49,23 +49,23 @@ abstract contract BaseStore is StateId state ) internal - returns (Blocknumber lastUpdatedIn) + returns (Blocknumber lastUpdatedIn, StateId oldState) { if (state.eqz()) { revert ErrorBaseStoreStateZero(key32); } Metadata storage metadata = _metadata[key32]; - StateId stateOld = metadata.state; + oldState = metadata.state; lastUpdatedIn = metadata.updatedIn; - if (stateOld.eqz()) { + if (oldState.eqz()) { revert ErrorBaseStoreNotExisting(key32); } // update state if(state != KEEP_STATE()) { - checkTransition(stateOld, metadata.objectType, stateOld, state); + checkTransition(oldState, metadata.objectType, oldState, state); metadata.state = state; // solhint-disable-next-line avoid-tx-origin diff --git a/contracts/instance/InstanceStore.sol b/contracts/instance/InstanceStore.sol index eb205dbdd..d82862a3a 100644 --- a/contracts/instance/InstanceStore.sol +++ b/contracts/instance/InstanceStore.sol @@ -100,8 +100,7 @@ contract InstanceStore is { // _update(_toNftKey32(componentNftId, COMPONENT()), abi.encode(componentInfo), newState); Key32 key = _toNftKey32(componentNftId, COMPONENT()); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); _components[key] = componentInfo; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreComponentInfoUpdated(componentNftId, oldState, newState, msg.sender, tx.origin, updatedIn); @@ -129,8 +128,7 @@ contract InstanceStore is function updatePool(NftId poolNftId, IComponents.PoolInfo memory info, StateId newState) external restricted() { Key32 key = _toNftKey32(poolNftId, POOL()); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); _pools[key] = info; // solhint-disable-next-line avoid-tx-origin emit LogProductStorePoolInfoUpdated(poolNftId, oldState, newState, msg.sender, tx.origin, updatedIn); @@ -151,8 +149,7 @@ contract InstanceStore is function updateDistributorType(DistributorType distributorType, IDistribution.DistributorTypeInfo memory info, StateId newState) external restricted() { Key32 key = distributorType.toKey32(); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); _distributorTypes[key] = info; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreDistributorTypeInfoUpdated(distributorType, oldState, newState, msg.sender, tx.origin, updatedIn); @@ -160,8 +157,7 @@ contract InstanceStore is function updateDistributorTypeState(DistributorType distributorType, StateId newState) external restricted() { Key32 key = distributorType.toKey32(); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStoreDistributorTypeInfoUpdated(distributorType, oldState, newState, msg.sender, tx.origin, updatedIn); } @@ -182,8 +178,7 @@ contract InstanceStore is function updateDistributor(NftId distributorNftId, IDistribution.DistributorInfo memory info, StateId newState) external restricted() { Key32 key = _toNftKey32(distributorNftId, DISTRIBUTOR()); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); _distributors[key] = info; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreDistributorInfoUpdated(distributorNftId, oldState, newState, msg.sender, tx.origin, updatedIn); @@ -191,8 +186,7 @@ contract InstanceStore is function updateDistributorState(NftId distributorNftId, StateId newState) external restricted() { Key32 key = _toNftKey32(distributorNftId, DISTRIBUTOR()); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStoreDistributorInfoUpdated(distributorNftId, oldState, newState, msg.sender, tx.origin, updatedIn); } @@ -212,8 +206,7 @@ contract InstanceStore is function updateReferral(ReferralId referralId, IDistribution.ReferralInfo memory referralInfo, StateId newState) external restricted() { Key32 key = referralId.toKey32(); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); _referrals[key] = referralInfo; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreReferralInfoUpdated(referralId, oldState, newState, msg.sender, tx.origin, updatedIn); @@ -221,8 +214,7 @@ contract InstanceStore is function updateReferralState(ReferralId referralId, StateId newState) external restricted() { Key32 key = referralId.toKey32(); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStoreReferralInfoUpdated(referralId, oldState, newState, msg.sender, tx.origin, updatedIn); } @@ -243,8 +235,7 @@ contract InstanceStore is function updateBundle(NftId bundleNftId, IBundle.BundleInfo memory bundle, StateId newState) external restricted() { Key32 key = _toNftKey32(bundleNftId, BUNDLE()); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); _bundles[key] = bundle; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreBundleInfoUpdated(bundleNftId, oldState, newState, msg.sender, tx.origin, updatedIn); @@ -252,8 +243,7 @@ contract InstanceStore is function updateBundleState(NftId bundleNftId, StateId newState) external restricted() { Key32 key = _toNftKey32(bundleNftId, BUNDLE()); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStoreBundleInfoUpdated(bundleNftId, oldState, newState, msg.sender, tx.origin, updatedIn); } @@ -275,8 +265,7 @@ contract InstanceStore is function updateRequest(RequestId requestId, IOracle.RequestInfo memory request, StateId newState) external restricted() { Key32 key = requestId.toKey32(); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); _requests[key] = request; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreRequestInfoUpdated(requestId, oldState, newState, msg.sender, tx.origin, updatedIn); @@ -284,8 +273,7 @@ contract InstanceStore is function updateRequestState(RequestId requestId, StateId newState) external restricted() { Key32 key = requestId.toKey32(); - StateId oldState = getState(key); - Blocknumber updatedIn = _updateState(key, newState); + (Blocknumber updatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStoreRequestInfoUpdated(requestId, oldState, newState, msg.sender, tx.origin, updatedIn); } diff --git a/contracts/instance/ProductStore.sol b/contracts/instance/ProductStore.sol index ef7b95b16..271f8f0c8 100644 --- a/contracts/instance/ProductStore.sol +++ b/contracts/instance/ProductStore.sol @@ -79,8 +79,7 @@ contract ProductStore is function updateProduct(NftId productNftId, IComponents.ProductInfo memory info, StateId newState) external restricted() { Key32 key = _toNftKey32(productNftId, PRODUCT()); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); _products[key] = info; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreProductInfoUpdated(productNftId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); @@ -103,7 +102,7 @@ contract ProductStore is // Fee only has one state, so no change change possible function updateFee(NftId productNftId, IComponents.FeeInfo memory info) external restricted() { Key32 key = _toNftKey32(productNftId, FEE()); - Blocknumber lastUpdatedIn = _updateState(key, KEEP_STATE()); + (Blocknumber lastUpdatedIn,) = _updateState(key, KEEP_STATE()); _fees[key] = info; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreFeeInfoUpdated(productNftId, msg.sender, tx.origin, lastUpdatedIn); @@ -124,8 +123,7 @@ contract ProductStore is function updateRisk(RiskId riskId, IRisk.RiskInfo memory info, StateId newState) external restricted() { Key32 key = riskId.toKey32(); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); _risks[key] = info; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreRiskInfoUpdated(riskId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); @@ -134,8 +132,7 @@ contract ProductStore is function updateRiskState(RiskId riskId, StateId newState) external restricted() { // _updateState(riskId.toKey32(), newState); Key32 key = riskId.toKey32(); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStoreRiskInfoUpdated(riskId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); } @@ -157,8 +154,7 @@ contract ProductStore is function updateApplication(NftId applicationNftId, IPolicy.PolicyInfo memory policy, StateId newState) external restricted() { Key32 key = _toNftKey32(applicationNftId, POLICY()); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); _policies[key] = policy; // solhint-disable-next-line avoid-tx-origin emit LogProductStorePolicyInfoUpdated(applicationNftId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); @@ -166,8 +162,7 @@ contract ProductStore is function updateApplicationState(NftId applicationNftId, StateId newState) external restricted() { Key32 key = _toNftKey32(applicationNftId, POLICY()); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStorePolicyInfoUpdated(applicationNftId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); } @@ -176,8 +171,7 @@ contract ProductStore is function updatePolicy(NftId policyNftId, IPolicy.PolicyInfo memory policy, StateId newState) external restricted() { Key32 key = _toNftKey32(policyNftId, POLICY()); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); _policies[key] = policy; // solhint-disable-next-line avoid-tx-origin emit LogProductStorePolicyInfoUpdated(policyNftId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); @@ -185,8 +179,7 @@ contract ProductStore is function updatePolicyClaims(NftId policyNftId, IPolicy.PolicyInfo memory policy, StateId newState) external restricted() { Key32 key = _toNftKey32(policyNftId, POLICY()); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); _policies[key] = policy; // solhint-disable-next-line avoid-tx-origin emit LogProductStorePolicyInfoUpdated(policyNftId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); @@ -194,8 +187,7 @@ contract ProductStore is function updatePolicyState(NftId policyNftId, StateId newState) external restricted() { Key32 key = _toNftKey32(policyNftId, POLICY()); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStorePolicyInfoUpdated(policyNftId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); } @@ -216,8 +208,7 @@ contract ProductStore is function updatePremiumState(NftId policyNftId, StateId newState) external restricted() { Key32 key = _toNftKey32(policyNftId, PREMIUM()); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStorePremiumInfoUpdated(policyNftId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); } @@ -238,8 +229,7 @@ contract ProductStore is function updateClaim(NftId policyNftId, ClaimId claimId, IPolicy.ClaimInfo memory claim, StateId newState) external restricted() { Key32 key = _toClaimKey32(policyNftId, claimId); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); _claims[key] = claim; // solhint-disable-next-line avoid-tx-origin emit LogProductStoreClaimInfoUpdated(policyNftId, claimId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); @@ -247,8 +237,7 @@ contract ProductStore is function updateClaimState(NftId policyNftId, ClaimId claimId, StateId newState) external restricted() { Key32 key = _toClaimKey32(policyNftId, claimId); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStoreClaimInfoUpdated(policyNftId, claimId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); } @@ -269,8 +258,7 @@ contract ProductStore is function updatePayout(NftId policyNftId, PayoutId payoutId, IPolicy.PayoutInfo memory payout, StateId newState) external restricted() { Key32 key = _toPayoutKey32(policyNftId, payoutId); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); _payouts[key] = payout; // solhint-disable-next-line avoid-tx-origin emit LogProductStorePayoutInfoUpdated(policyNftId, payoutId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); @@ -278,8 +266,7 @@ contract ProductStore is function updatePayoutState(NftId policyNftId, PayoutId payoutId, StateId newState) external restricted() { Key32 key = _toPayoutKey32(policyNftId, payoutId); - StateId oldState = getState(key); - Blocknumber lastUpdatedIn = _updateState(key, newState); + (Blocknumber lastUpdatedIn, StateId oldState) = _updateState(key, newState); // solhint-disable-next-line avoid-tx-origin emit LogProductStorePayoutInfoUpdated(policyNftId, payoutId, oldState, newState, msg.sender, tx.origin, lastUpdatedIn); } diff --git a/foundry.toml b/foundry.toml index 474195a5e..cc4c419a9 100644 --- a/foundry.toml +++ b/foundry.toml @@ -8,7 +8,20 @@ cache_path = 'cache_forge' gas_limit = "18446744073709551615" # u64::MAX ffi = true -gas_reports = ["ApplicationService", "PolicyService", "PricingService", "InstanceStore", "ProductStore", "RegistryService"] +gas_reports = [ + "ApplicationService", + "BundleService", + "BundleSet", + "DistributionService", + "InstanceStore", + "PolicyService", + "PolicyServiceLib", + "PoolService", + "PricingService", + "ProductStore", + "RegistryService", + "RiskSet" +] [profile.ci]