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

feat: ERC20 and AssetId support #43

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ verbosity = 4
[fmt]
bracket_spacing = true
int_types = "long"
line_length = 132
line_length = 120
multiline_func_header = "all"
number_underscore = "thousands"
quote_style = "double"
Expand All @@ -27,7 +27,7 @@ wrap_comments = true
[invariant]
fail_on_revert = false
runs = 256
depth = 100
depth = 120

[rpc_endpoints]
# Uncomment to enable the RPC server
Expand Down
24 changes: 12 additions & 12 deletions src/BlueprintServiceManagerBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,16 @@ contract BlueprintServiceManagerBase is IBlueprintServiceManager, RootChainEnabl
function onUnregister(ServiceOperators.OperatorPreferences calldata operator) external virtual onlyFromMaster { }

/// @inheritdoc IBlueprintServiceManager
function onUpdatePriceTargets(ServiceOperators.OperatorPreferences calldata operator) external payable virtual onlyFromMaster { }

/// @inheritdoc IBlueprintServiceManager
function onRequest(
uint64 requestId,
address requester,
ServiceOperators.OperatorPreferences[] calldata operators,
bytes calldata requestInputs,
address[] calldata permittedCallers,
uint64 ttl
)
function onUpdatePriceTargets(ServiceOperators.OperatorPreferences calldata operator)
external
payable
virtual
onlyFromMaster
{ }

/// @inheritdoc IBlueprintServiceManager
function onRequest(ServiceOperators.RequestParams calldata params) external payable virtual onlyFromMaster { }

/// @inheritdoc IBlueprintServiceManager
function onApprove(
ServiceOperators.OperatorPreferences calldata operator,
Expand All @@ -71,7 +64,14 @@ contract BlueprintServiceManagerBase is IBlueprintServiceManager, RootChainEnabl
{ }

/// @inheritdoc IBlueprintServiceManager
function onReject(ServiceOperators.OperatorPreferences calldata operator, uint64 requestId) external virtual onlyFromMaster { }
function onReject(
ServiceOperators.OperatorPreferences calldata operator,
uint64 requestId
)
external
virtual
onlyFromMaster
{ }

/// @inheritdoc IBlueprintServiceManager
function onServiceInitialized(
Expand Down
80 changes: 58 additions & 22 deletions src/IBlueprintServiceManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,50 @@ library ServiceOperators {
/// @notice The NVMe storage price target.
uint64 storage_nvme;
}

/// @dev Represents different types of assets that can be used in the system
enum AssetKind {
/// @notice Custom Asset Id
Custom,
/// @notice Standard ERC20 token asset type
Erc20
}

/// @dev Represents an asset with its type and associated data
struct Asset {
/// @notice The kind/type of the asset (Custom or ERC20)
AssetKind kind;
/// @notice The data associated with the asset, encoded as bytes32
/// @dev The data is encoded as follows:
/// - For Custom assets: The asset ID is stored as uint256
/// - For ERC20 assets: The token address is stored as address
bytes32 data;
}

/// @dev Represents parameters for a service request
struct RequestParams {
/// @notice Unique identifier for the request
uint64 requestId;
/// @notice Address of the requester
address requester;
/// @notice Array of operator preferences containing public keys and price targets
OperatorPreferences[] operators;
/// @notice Input parameters for the request encoded as bytes
bytes requestInputs;
/// @notice Array of addresses that are permitted to call the service
address[] permittedCallers;
/// @notice Time-to-live value indicating how long the service is valid
uint64 ttl;
/// @notice Asset to be used for payment
Asset paymentAsset;
/// @notice Amount of payment asset to be used
uint256 amount;
}
}

/// @title IBlueprintServiceManager
/// @dev Interface for the BlueprintServiceManager contract, which acts as a manager for the lifecycle of a Blueprint Instance,
/// @dev Interface for the BlueprintServiceManager contract, which acts as a manager for the lifecycle of a Blueprint
/// Instance,
/// facilitating various stages such as registration, service requests, job execution, and job result handling.
interface IBlueprintServiceManager {
/// @dev Hook to handle blueprint creation. Gets called by the root chain when a new blueprint is created.
Expand Down Expand Up @@ -66,22 +106,8 @@ interface IBlueprintServiceManager {
/// instance from the blueprint but this does not mean the service is initiated yet.
/// To get notified when the service is initiated, implement the `onServiceInitialized` hook.
///
/// @param requestId The ID of the request.
/// @param requester The address of the service requester.
/// @param operators The list of operators to be considered for the service.
/// @param requestInputs The inputs required for the service request in bytes format.
/// @param permittedCallers The list of permitted callers for the service.
/// @param ttl The time-to-live for the service.
function onRequest(
uint64 requestId,
address requester,
ServiceOperators.OperatorPreferences[] calldata operators,
bytes calldata requestInputs,
address[] calldata permittedCallers,
uint64 ttl
)
external
payable;
/// @param params The parameters for the service request.
function onRequest(ServiceOperators.RequestParams calldata params) external payable;

/// @dev Hook for service request approval. Called when a service request is approved by an operator.
/// @param operator The operator's details.
Expand Down Expand Up @@ -149,12 +175,19 @@ interface IBlueprintServiceManager {
/// @param owner The owner of the service.
function onServiceTermination(uint64 serviceId, address owner) external;

/// @dev Hook for handling unapplied slashes. Called when a slash is queued and still not yet applied to an offender.
/// @dev Hook for handling unapplied slashes. Called when a slash is queued and still not yet applied to an
/// offender.
/// @param serviceId The ID of the service related to the slash.
/// @param offender The offender's details in bytes format.
/// @param slashPercent The percentage of the slash.
/// @param totalPayout The total payout amount in wei.
function onUnappliedSlash(uint64 serviceId, bytes calldata offender, uint8 slashPercent, uint256 totalPayout) external;
function onUnappliedSlash(
uint64 serviceId,
bytes calldata offender,
uint8 slashPercent,
uint256 totalPayout
)
external;

/// @dev Hook for handling applied slashes. Called when a slash is applied to an offender.
/// @param serviceId The ID of the service related to the slash.
Expand All @@ -164,21 +197,24 @@ interface IBlueprintServiceManager {
function onSlash(uint64 serviceId, bytes calldata offender, uint8 slashPercent, uint256 totalPayout) external;

/// @dev Query the slashing origin for a service. This mainly used by the runtime to determine the allowed account
/// that can slash a service. by default, the service manager is the only account that can slash a service. override this
/// that can slash a service. by default, the service manager is the only account that can slash a service. override
/// this
/// function to allow other accounts to slash a service.
/// @param serviceId The ID of the service.
/// @return slashingOrigin The account that can slash the offender.
function querySlashingOrigin(uint64 serviceId) external view returns (address slashingOrigin);

/// @dev Query the dispute origin for a service. This mainly used by the runtime to determine the allowed account
/// that can dispute an unapplied slash and remove it. by default, the service manager is the only account that can dispute a
/// that can dispute an unapplied slash and remove it. by default, the service manager is the only account that can
/// dispute a
/// service. override this
/// function to allow other accounts to dispute a service.
/// @param serviceId The ID of the service.
/// @return disputeOrigin The account that can dispute the unapplied slash for that service
function queryDisputeOrigin(uint64 serviceId) external view returns (address disputeOrigin);

/// @dev Query the developer payment address for a service. This mainly used by the runtime or the Master Blueprint Service
/// @dev Query the developer payment address for a service. This mainly used by the runtime or the Master Blueprint
/// Service
/// Manager
/// to determine the developer payment address for a service.
/// @notice This function should be implemented by the Blueprint Service Manager contract.
Expand Down
54 changes: 35 additions & 19 deletions src/MasterBlueprintServiceManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,36 @@ contract MasterBlueprintServiceManager is RootChainEnabled, AccessControl, Pausa
/// @param requestId The ID of the request.
/// @param requester The address of the service requester.
/// @param ttl The time-to-live for the service.
event ServiceRequested(uint64 indexed blueprintId, uint64 indexed requestId, address indexed requester, uint64 ttl);
/// @param asset The asset used for payment for the service.
/// @param amount The amount of the payment asset.
event ServiceRequested(
uint64 indexed blueprintId,
uint64 indexed requestId,
address indexed requester,
uint64 ttl,
ServiceOperators.Asset asset,
uint256 amount
);

/// @dev Emitted when a service request is approved by an operator.
/// @param blueprintId The unique identifier of the blueprint.
/// @param requestId The ID of the request.
/// @param operator The operator's preferences.
/// @param restakingPercent The percentage of the restaking amount.
event RequestApproved(
uint64 indexed blueprintId, uint64 indexed requestId, ServiceOperators.OperatorPreferences operator, uint8 restakingPercent
uint64 indexed blueprintId,
uint64 indexed requestId,
ServiceOperators.OperatorPreferences operator,
uint8 restakingPercent
);

/// @dev Emitted when a service request is rejected by an operator.
/// @param blueprintId The unique identifier of the blueprint.
/// @param requestId The ID of the request.
/// @param operator The operator's preferences.
event RequestRejected(uint64 indexed blueprintId, uint64 indexed requestId, ServiceOperators.OperatorPreferences operator);
event RequestRejected(
uint64 indexed blueprintId, uint64 indexed requestId, ServiceOperators.OperatorPreferences operator
);

/// @dev Emitted when a service is initialized.
/// @param blueprintId The unique identifier of the blueprint.
Expand Down Expand Up @@ -138,7 +152,9 @@ contract MasterBlueprintServiceManager is RootChainEnabled, AccessControl, Pausa
/// @param offender The offender's details.
/// @param slashPercent The percentage of the slash.
/// @param totalPayout The total payout amount.
event Slashed(uint64 indexed blueprintId, uint64 indexed serviceId, bytes offender, uint8 slashPercent, uint256 totalPayout);
event Slashed(
uint64 indexed blueprintId, uint64 indexed serviceId, bytes offender, uint8 slashPercent, uint256 totalPayout
);

// ============ Storage ============

Expand Down Expand Up @@ -229,29 +245,21 @@ contract MasterBlueprintServiceManager is RootChainEnabled, AccessControl, Pausa

/// @dev Called when a user requests a service instance from the blueprint.
/// @param blueprintId The blueprint unique identifier.
/// @param requestId The ID of the request.
/// @param requester The address of the service requester.
/// @param operators The list of operators to be considered for the service.
/// @param requestInputs The inputs required for the service request.
/// @param permittedCallers The list of permitted callers for the service.
/// @param ttl The time-to-live for the service.
/// @param params The request parameters.
function onRequest(
uint64 blueprintId,
uint64 requestId,
address requester,
ServiceOperators.OperatorPreferences[] calldata operators,
bytes calldata requestInputs,
address[] calldata permittedCallers,
uint64 ttl
ServiceOperators.RequestParams calldata params
)
public
payable
onlyFromRootChain
whenNotPaused
{
address manager = blueprints.get(blueprintId);
IBlueprintServiceManager(manager).onRequest(requestId, requester, operators, requestInputs, permittedCallers, ttl);
emit ServiceRequested(blueprintId, requestId, requester, ttl);
IBlueprintServiceManager(manager).onRequest(params);
emit ServiceRequested(
blueprintId, params.requestId, params.requester, params.ttl, params.paymentAsset, params.amount
);
}

/// @dev Called when a service request is approved by an operator.
Expand Down Expand Up @@ -371,7 +379,15 @@ contract MasterBlueprintServiceManager is RootChainEnabled, AccessControl, Pausa
/// @param blueprintId The blueprint unique identifier.
/// @param serviceId The ID of the service.
/// @param owner The owner of the service.
function onServiceTermination(uint64 blueprintId, uint64 serviceId, address owner) public onlyFromRootChain whenNotPaused {
function onServiceTermination(
uint64 blueprintId,
uint64 serviceId,
address owner
)
public
onlyFromRootChain
whenNotPaused
{
address manager = blueprints.get(blueprintId);
IBlueprintServiceManager(manager).onServiceTermination(serviceId, owner);
emit ServiceTerminated(blueprintId, serviceId, owner);
Expand Down
Loading
Loading