Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor active requests logic (#745) #818

Merged
merged 8 commits into from
Dec 16, 2024
99 changes: 1 addition & 98 deletions contracts/examples/flight/FlightOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ pragma solidity ^0.8.20;

import {IAuthorization} from "../../authorization/IAuthorization.sol";

import {ACTIVE, FULFILLED, FAILED} from "../../type/StateId.sol";
import {ACTIVE, FAILED} from "../../type/StateId.sol";
import {NftId} from "../../type/NftId.sol";
import {BasicOracle} from "../../oracle/BasicOracle.sol";
import {RequestId} from "../../type/RequestId.sol";
import {LibRequestIdSet} from "../../type/RequestIdSet.sol";
import {RiskId} from "../../type/RiskId.sol";
import {StateId} from "../../type/StateId.sol";
import {Str} from "../../type/String.sol";
Expand All @@ -33,9 +32,6 @@ contract FlightOracle is
event LogFlightOracleResponseSent(RequestId requestId, bytes1 status, int256 delay);
event LogFlightOracleRequestCancelled(RequestId requestId);

// TODO decide if this variable should be moved to instance store
// if so it need to manage active requests by requestor nft id
LibRequestIdSet.Set internal _activeRequests;


constructor(
Expand Down Expand Up @@ -99,52 +95,10 @@ contract FlightOracle is

// effects + interaction (via framework to receiving component)
_respond(requestId, responseData);

// TODO decide if the code below should be moved to GIF
_updateRequestState(requestId);
}


function updateRequestState(
RequestId requestId
)
external
restricted()
{
_updateRequestState(requestId);
}

//--- view functions ----------------------------------------------------//

// TODO decide if the code below should be moved to GIF
function activeRequests()
external
view
returns(uint256 numberOfRequests)
{
return LibRequestIdSet.size(_activeRequests);
}


// TODO decide if the code below should be moved to GIF
function getActiveRequest(uint256 idx)
external
view
returns(RequestId requestId)
{
return LibRequestIdSet.getElementAt(_activeRequests, idx);
}


function isActiveRequest(RequestId requestId)
external
view
returns(bool isActive)
{
return LibRequestIdSet.contains(_activeRequests, requestId);
}


function getRequestState(RequestId requestId)
external
view
Expand Down Expand Up @@ -172,55 +126,4 @@ contract FlightOracle is
return abi.decode(data, (FlightStatusRequest));
}

//--- internal functions ------------------------------------------------//


// TODO decide if the code below should be moved to GIF
// check callback result
function _updateRequestState(
RequestId requestId
)
internal
{
bool requestFulfilled = _getInstanceReader().getRequestState(
requestId) == FULFILLED();

// remove from active requests when successful
if (requestFulfilled && LibRequestIdSet.contains(_activeRequests, requestId)) {
LibRequestIdSet.remove(_activeRequests, requestId);
}
}


/// @dev use case specific handling of oracle requests
/// for now only log is emitted to verify that request has been received by oracle component
function _request(
RequestId requestId,
NftId requesterId,
bytes calldata requestData,
Timestamp expiryAt
)
internal
virtual override
{
FlightStatusRequest memory request = abi.decode(requestData, (FlightStatusRequest));

// TODO decide if the line below should be moved to GIF
LibRequestIdSet.add(_activeRequests, requestId);
emit LogFlightOracleRequestReceived(requestId, requesterId);
}


/// @dev use case specific handling of oracle requests
/// for now only log is emitted to verify that cancelling has been received by oracle component
function _cancel(
RequestId requestId
)
internal
virtual override
{
// TODO decide if the line below should be moved to GIF
LibRequestIdSet.remove(_activeRequests, requestId);
emit LogFlightOracleRequestCancelled(requestId);
}
}
5 changes: 2 additions & 3 deletions contracts/examples/flight/FlightOracleAuthorization.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ contract FlightOracleAuthorization
functions = _authorizeForTarget(getMainTargetName(), STATUS_PROVIDER_ROLE);
_authorize(functions, FlightOracle.respondWithFlightStatus.selector, "respondWithFlightStatus");

// authorize public role (additional authz via onlyOwner)
functions = _authorizeForTarget(getMainTargetName(), PUBLIC_ROLE());
_authorize(functions, FlightOracle.updateRequestState.selector, "updateRequestState");
// // authorize public role (additional authz via onlyOwner)
// functions = _authorizeForTarget(getMainTargetName(), PUBLIC_ROLE());
}
}

3 changes: 3 additions & 0 deletions contracts/instance/IInstance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {InstanceReader} from "./InstanceReader.sol";
import {InstanceStore} from "./InstanceStore.sol";
import {NftId} from "../type/NftId.sol";
import {ProductStore} from "./ProductStore.sol";
import {RequestSet} from "./RequestSet.sol";
import {RoleId} from "../type/RoleId.sol";
import {Seconds} from "../type/Seconds.sol";
import {UFixed} from "../type/UFixed.sol";
Expand Down Expand Up @@ -58,6 +59,7 @@ interface IInstance is
ProductStore productStore;
BundleSet bundleSet;
RiskSet riskSet;
RequestSet requestSet;
InstanceReader instanceReader;
}

Expand Down Expand Up @@ -149,6 +151,7 @@ interface IInstance is
function getInstanceReader() external view returns (InstanceReader);
function getBundleSet() external view returns (BundleSet);
function getRiskSet() external view returns (RiskSet);
function getRequestSet() external view returns (RequestSet);
function getInstanceAdmin() external view returns (InstanceAdmin);
function getInstanceStore() external view returns (InstanceStore);
function getProductStore() external view returns (ProductStore);
Expand Down
6 changes: 5 additions & 1 deletion contracts/instance/IInstanceService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface IInstanceService is IService {
error ErrorInstanceServiceMasterInstanceAdminAlreadySet();
error ErrorInstanceServiceMasterBundleSetAlreadySet();
error ErrorInstanceServiceMasterRiskSetAlreadySet();
error ErrorInstanceServiceMasterRequestSetAlreadySet();
error ErrorInstanceServiceInstanceAddressZero();

error ErrorInstanceServiceMasterInstanceReaderNotSet();
Expand All @@ -38,15 +39,18 @@ interface IInstanceService is IService {
error ErrorInstanceServiceInstanceReaderZero();
error ErrorInstanceServiceBundleSetZero();
error ErrorInstanceServiceRiskSetZero();
error ErrorInstanceServiceRequestSetZero();
error ErrorInstanceServiceInstanceStoreZero();
error ErrorInstanceServiceProductStoreZero();

error ErrorInstanceServiceInstanceAuthorityMismatch();
error ErrorInstanceServiceBundleSetAuthorityMismatch();
error ErrorInstanceServiceRiskSetAuthorityMismatch();
error ErrorInstanceServiceRequestSetAuthorityMismatch();
error ErrorInstanceServiceInstanceReaderInstanceMismatch2();
error ErrorInstanceServiceBundleSetInstanceMismatch();
error ErrorInstanceServiceRiskSetInstanceMismatch();
error ErrorInstanceServiceRequestSetInstanceMismatch();
error ErrorInstanceServiceInstanceStoreAuthorityMismatch();
error ErrorInstanceServiceProductStoreAuthorityMismatch();

Expand All @@ -59,7 +63,7 @@ interface IInstanceService is IService {
event LogInstanceServiceInstanceLocked(NftId instanceNftId, bool locked);
event LogInstanceServiceInstanceCreated(NftId instanceNftId, address instance);
event LogInstanceServiceMasterInstanceRegistered(NftId masterInstanceNftId, address masterInstance, address masterInstanceAdmin, address masterAccessManager,
address masterInstanceReader, address masterInstanceBundleSet, address masterInstanceRiskSet, address masterInstanceStore, address masterProductStore);
address masterInstanceReader, address masterInstanceBundleSet, address masterInstanceRiskSet, address masterInstanceRequestSet, address masterInstanceStore, address masterProductStore);
event LogInstanceServiceMasterInstanceReaderUpgraded(NftId instanceNfId, address newInstanceReader);
event LogInstanceServiceInstanceReaderUpgraded(NftId instanceNfId, address newInstanceReader);

Expand Down
8 changes: 8 additions & 0 deletions contracts/instance/Instance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {InstanceStore} from "./InstanceStore.sol";
import {NftId} from "../type/NftId.sol";
import {ProductStore} from "./ProductStore.sol";
import {Registerable} from "../shared/Registerable.sol";
import {RequestSet} from "./RequestSet.sol";
import {RoleId} from "../type/RoleId.sol";
import {Seconds} from "../type/Seconds.sol";
import {UFixed} from "../type/UFixed.sol";
Expand All @@ -35,6 +36,7 @@ contract Instance is
InstanceReader internal _instanceReader;
BundleSet internal _bundleSet;
RiskSet internal _riskSet;
RequestSet internal _requestSet;
InstanceStore internal _instanceStore;
ProductStore internal _productStore;
NftId [] internal _products;
Expand Down Expand Up @@ -88,13 +90,15 @@ contract Instance is
_productStore = instanceContracts.productStore;
_bundleSet = instanceContracts.bundleSet;
_riskSet = instanceContracts.riskSet;
_requestSet = instanceContracts.requestSet;
_instanceReader = instanceContracts.instanceReader;

// initialize instance supporting contracts
_instanceStore.initialize();
_productStore.initialize();
_bundleSet.initialize(instanceContracts.instanceAdmin.authority(), address(registry));
_riskSet.initialize(instanceContracts.instanceAdmin.authority(), address(registry));
_requestSet.initialize(instanceContracts.instanceAdmin.authority(), address(registry));
_instanceReader.initialize();

_componentService = IComponentService(
Expand Down Expand Up @@ -333,6 +337,10 @@ contract Instance is
return _riskSet;
}

function getRequestSet() external view returns (RequestSet) {
return _requestSet;
}

function getInstanceAdmin() external view returns (InstanceAdmin) {
return _instanceAdmin;
}
Expand Down
3 changes: 2 additions & 1 deletion contracts/instance/InstanceAdmin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {ObjectType, INSTANCE} from "../type/ObjectType.sol";
import {RoleId, ADMIN_ROLE} from "../type/RoleId.sol";
import {Str} from "../type/String.sol";
import {VersionPart} from "../type/Version.sol";
import {INSTANCE_TARGET_NAME, INSTANCE_ADMIN_TARGET_NAME, INSTANCE_STORE_TARGET_NAME, PRODUCT_STORE_TARGET_NAME, BUNDLE_SET_TARGET_NAME, RISK_SET_TARGET_NAME} from "./TargetNames.sol";
import {INSTANCE_TARGET_NAME, INSTANCE_ADMIN_TARGET_NAME, INSTANCE_STORE_TARGET_NAME, PRODUCT_STORE_TARGET_NAME, BUNDLE_SET_TARGET_NAME, RISK_SET_TARGET_NAME, REQUEST_SET_TARGET_NAME} from "./TargetNames.sol";


contract InstanceAdmin is
Expand Down Expand Up @@ -123,6 +123,7 @@ contract InstanceAdmin is
_createInstanceTarget(address(_instance.getProductStore()), PRODUCT_STORE_TARGET_NAME);
_createInstanceTarget(address(_instance.getBundleSet()), BUNDLE_SET_TARGET_NAME);
_createInstanceTarget(address(_instance.getRiskSet()), RISK_SET_TARGET_NAME);
_createInstanceTarget(address(_instance.getRequestSet()), REQUEST_SET_TARGET_NAME);
}


Expand Down
16 changes: 15 additions & 1 deletion contracts/instance/InstanceAuthorizationV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {ACCOUNTING, ORACLE, POOL, INSTANCE, COMPONENT, DISTRIBUTION, APPLICATION
import {BundleSet} from "../instance/BundleSet.sol";
import {InstanceAdmin} from "../instance/InstanceAdmin.sol";
import {InstanceStore} from "../instance/InstanceStore.sol";
import {INSTANCE_TARGET_NAME, INSTANCE_ADMIN_TARGET_NAME, INSTANCE_STORE_TARGET_NAME, PRODUCT_STORE_TARGET_NAME, BUNDLE_SET_TARGET_NAME, RISK_SET_TARGET_NAME} from "./TargetNames.sol";
import {INSTANCE_TARGET_NAME, INSTANCE_ADMIN_TARGET_NAME, INSTANCE_STORE_TARGET_NAME, PRODUCT_STORE_TARGET_NAME, BUNDLE_SET_TARGET_NAME, RISK_SET_TARGET_NAME, REQUEST_SET_TARGET_NAME} from "./TargetNames.sol";
import {ProductStore} from "../instance/ProductStore.sol";
import {ADMIN_ROLE, INSTANCE_OWNER_ROLE, PUBLIC_ROLE} from "../type/RoleId.sol";
import {RequestSet} from "../instance/RequestSet.sol";
import {RiskSet} from "../instance/RiskSet.sol";


Expand Down Expand Up @@ -71,6 +72,7 @@ contract InstanceAuthorizationV3
_addInstanceTarget(PRODUCT_STORE_TARGET_NAME);
_addInstanceTarget(BUNDLE_SET_TARGET_NAME);
_addInstanceTarget(RISK_SET_TARGET_NAME);
_addInstanceTarget(REQUEST_SET_TARGET_NAME);
}


Expand All @@ -84,6 +86,18 @@ contract InstanceAuthorizationV3
_setupProductStoreAuthorization();
_setupBundleSetAuthorization();
_setUpRiskSetAuthorization();
_setUpRequestIdSetAuthorization();
}

function _setUpRequestIdSetAuthorization()
internal
{
IAccess.FunctionInfo[] storage functions;

// authorize oracle service role
functions = _authorizeForTarget(REQUEST_SET_TARGET_NAME, getServiceRole(ORACLE()));
_authorize(functions, RequestSet.add.selector, "add");
_authorize(functions, RequestSet.remove.selector, "remove");
}


Expand Down
15 changes: 15 additions & 0 deletions contracts/instance/InstanceReader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {PolicyServiceLib} from "../product/PolicyServiceLib.sol";
import {ProductStore} from "./ProductStore.sol";
import {ReferralId, ReferralStatus, ReferralLib} from "../type/Referral.sol";
import {RequestId} from "../type/RequestId.sol";
import {RequestSet} from "./RequestSet.sol";
import {RiskId} from "../type/RiskId.sol";
import {RiskSet} from "./RiskSet.sol";
import {RoleId, INSTANCE_OWNER_ROLE} from "../type/RoleId.sol";
Expand All @@ -56,6 +57,7 @@ contract InstanceReader {
ProductStore internal _productStore;
BundleSet internal _bundleSet;
RiskSet internal _riskSet;
RequestSet internal _requestSet;
IDistributionService internal _distributionService;

/// @dev This initializer needs to be called from the instance itself.
Expand Down Expand Up @@ -85,6 +87,7 @@ contract InstanceReader {
_productStore = _instance.getProductStore();
_bundleSet = _instance.getBundleSet();
_riskSet = _instance.getRiskSet();
_requestSet = _instance.getRequestSet();
_distributionService = IDistributionService(_registry.getServiceAddress(DISTRIBUTION(), _instance.getRelease()));
}

Expand Down Expand Up @@ -366,6 +369,18 @@ contract InstanceReader {
return getState(requestId.toKey32());
}

function getActiveRequests(NftId oracleNftId) external view returns(uint256 numberOfRequests) {
return _requestSet.activeRequests(oracleNftId);
}

function getActiveRequestAt(NftId oracleNftId, uint256 idx) external view returns(RequestId requestId) {
return _requestSet.activeRequestAt(oracleNftId, idx);
}

function isRequestActive(NftId oracleNftId, RequestId requestId) external view returns(bool isActive) {
return _requestSet.contains(oracleNftId, requestId);
}

//--- pool functions -----------------------------------------------------------//

/// @dev Returns the pool info for the given pool NFT ID.
Expand Down
Loading
Loading