Skip to content

Commit

Permalink
refactor state update (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Sep 17, 2024
1 parent 639b4d6 commit a2d24d9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 56 deletions.
8 changes: 4 additions & 4 deletions contracts/instance/BaseStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 12 additions & 24 deletions contracts/instance/InstanceStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -151,17 +149,15 @@ 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);
}

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);
}
Expand All @@ -182,17 +178,15 @@ 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);
}

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);
}
Expand All @@ -212,17 +206,15 @@ 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);
}

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);
}
Expand All @@ -243,17 +235,15 @@ 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);
}

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);
}
Expand All @@ -275,17 +265,15 @@ 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);
}

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);
}
Expand Down
41 changes: 14 additions & 27 deletions contracts/instance/ProductStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -157,17 +154,15 @@ 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);
}

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);
}
Expand All @@ -176,26 +171,23 @@ 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);
}

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);
}

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);
}
Expand All @@ -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);
}
Expand All @@ -238,17 +229,15 @@ 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);
}

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);
}
Expand All @@ -269,17 +258,15 @@ 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);
}

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);
}
Expand Down
15 changes: 14 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit a2d24d9

Please sign in to comment.