Skip to content

Commit

Permalink
Update MSBM w/ new features in Tangle (same branch name) (#50)
Browse files Browse the repository at this point in the history
* chore: Update MSBM w/ new features against drew/slashing-updates in

* chore: Add stub impl to contract

* chore: fmt

* feat: add tests of join/leave functionality

* feat: add HooksTests contract

* fix: services pallet address

* chore: remove artifact from slashing interface

* chore: add slash alert

* chore: udpate bytecode

* chore: update MBSM bytecode

---------

Co-authored-by: Shady Khalifa <dev+github@shadykhalifa.me>
  • Loading branch information
drewstone and shekohex authored Feb 21, 2025
1 parent 7647933 commit 8f4975b
Show file tree
Hide file tree
Showing 11 changed files with 684 additions and 60 deletions.
32 changes: 16 additions & 16 deletions bytecode/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bytecode/src/lib.rs

Large diffs are not rendered by default.

54 changes: 50 additions & 4 deletions src/BlueprintServiceManagerBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ contract BlueprintServiceManagerBase is IBlueprintServiceManager, RootChainEnabl
function onUnappliedSlash(
uint64 serviceId,
bytes calldata offender,
uint8 slashPercent,
uint256 totalPayout
uint8 slashPercent
)
external
virtual
Expand All @@ -145,8 +144,55 @@ contract BlueprintServiceManagerBase is IBlueprintServiceManager, RootChainEnabl
function onSlash(
uint64 serviceId,
bytes calldata offender,
uint8 slashPercent,
uint256 totalPayout
uint8 slashPercent
)
external
virtual
onlyFromMaster
{ }

/// @inheritdoc IBlueprintServiceManager
function canJoin(
uint64 serviceId,
ServiceOperators.OperatorPreferences calldata operator
)
external
view
virtual
onlyFromMaster
returns (bool allowed)
{
return false;
}

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

/// @inheritdoc IBlueprintServiceManager
function canLeave(
uint64 serviceId,
ServiceOperators.OperatorPreferences calldata operator
)
external
view
virtual
onlyFromMaster
returns (bool allowed)
{
return false;
}

/// @inheritdoc IBlueprintServiceManager
function onOperatorLeft(
uint64 serviceId,
ServiceOperators.OperatorPreferences calldata operator
)
external
virtual
Expand Down
41 changes: 36 additions & 5 deletions src/IBlueprintServiceManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,52 @@ interface IBlueprintServiceManager {
/// @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
uint8 slashPercent
)
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.
/// @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 onSlash(uint64 serviceId, bytes calldata offender, uint8 slashPercent, uint256 totalPayout) external;
function onSlash(uint64 serviceId, bytes calldata offender, uint8 slashPercent) external;

/// @dev Hook to check if an operator can join a service instance
/// @param serviceId The ID of the service instance
/// @param operator The operator's preferences and details
/// @return allowed Returns true if the operator is allowed to join
function canJoin(
uint64 serviceId,
ServiceOperators.OperatorPreferences calldata operator
)
external
view
returns (bool allowed);

/// @dev Hook called after an operator has joined a service instance
/// @param serviceId The ID of the service instance
/// @param operator The operator's preferences and details
function onOperatorJoined(uint64 serviceId, ServiceOperators.OperatorPreferences calldata operator) external;

/// @dev Hook to check if an operator can leave a service instance
/// @param serviceId The ID of the service instance
/// @param operator The operator's preferences and details
/// @return allowed Returns true if the operator is allowed to leave
function canLeave(
uint64 serviceId,
ServiceOperators.OperatorPreferences calldata operator
)
external
view
returns (bool allowed);

/// @dev Hook called after an operator has left a service instance
/// @param serviceId The ID of the service instance
/// @param operator The operator's preferences and details
function onOperatorLeft(uint64 serviceId, ServiceOperators.OperatorPreferences calldata operator) 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
Expand Down
88 changes: 74 additions & 14 deletions src/MasterBlueprintServiceManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,17 @@ contract MasterBlueprintServiceManager is RootChainEnabled, AccessControl, Pausa
/// @param serviceId The ID of the service.
/// @param offender The offender's details.
/// @param slashPercent The percentage of the slash.
/// @param totalPayout The total payout amount.
event UnappliedSlash(
uint64 indexed blueprintId, uint64 indexed serviceId, bytes offender, uint8 slashPercent, uint256 totalPayout
uint64 indexed blueprintId, uint64 indexed serviceId, bytes offender, uint8 slashPercent
);

/// @dev Emitted when a slash is applied to an offender.
/// @param blueprintId The unique identifier of the blueprint.
/// @param serviceId The ID of the service.
/// @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
uint64 indexed blueprintId, uint64 indexed serviceId, bytes offender, uint8 slashPercent
);

// =========== Errors ============
Expand Down Expand Up @@ -480,43 +478,39 @@ contract MasterBlueprintServiceManager is RootChainEnabled, AccessControl, Pausa
/// @param serviceId The ID of the service.
/// @param offender The offender's details.
/// @param slashPercent The percentage of the slash.
/// @param totalPayout The total payout amount.
function onUnappliedSlash(
uint64 blueprintId,
uint64 serviceId,
bytes calldata offender,
uint8 slashPercent,
uint256 totalPayout
uint8 slashPercent
)
public
onlyFromRootChain
whenNotPaused
{
address manager = blueprints.get(blueprintId);
IBlueprintServiceManager(manager).onUnappliedSlash(serviceId, offender, slashPercent, totalPayout);
emit UnappliedSlash(blueprintId, serviceId, offender, slashPercent, totalPayout);
IBlueprintServiceManager(manager).onUnappliedSlash(serviceId, offender, slashPercent);
emit UnappliedSlash(blueprintId, serviceId, offender, slashPercent);
}

/// @dev Called when a slash is applied to an offender.
/// @param blueprintId The blueprint unique identifier.
/// @param serviceId The ID of the service.
/// @param offender The offender's details.
/// @param slashPercent The percentage of the slash.
/// @param totalPayout The total payout amount.
function onSlash(
uint64 blueprintId,
uint64 serviceId,
bytes calldata offender,
uint8 slashPercent,
uint256 totalPayout
uint8 slashPercent
)
public
onlyFromRootChain
whenNotPaused
{
address manager = blueprints.get(blueprintId);
IBlueprintServiceManager(manager).onSlash(serviceId, offender, slashPercent, totalPayout);
emit Slashed(blueprintId, serviceId, offender, slashPercent, totalPayout);
IBlueprintServiceManager(manager).onSlash(serviceId, offender, slashPercent);
emit Slashed(blueprintId, serviceId, offender, slashPercent);
}

/// @dev Query the slashing origin for a service.
Expand All @@ -537,6 +531,72 @@ contract MasterBlueprintServiceManager is RootChainEnabled, AccessControl, Pausa
return IBlueprintServiceManager(manager).queryDisputeOrigin(serviceId);
}

/// @dev Hook to check if an operator can join a service instance
/// @param blueprintId The blueprint unique identifier
/// @param serviceId The ID of the service instance
/// @param operator The operator's preferences and details
/// @return allowed Returns true if the operator is allowed to join
function canJoin(
uint64 blueprintId,
uint64 serviceId,
ServiceOperators.OperatorPreferences calldata operator
)
public
view
returns (bool allowed)
{
address manager = blueprints.get(blueprintId);
return IBlueprintServiceManager(manager).canJoin(serviceId, operator);
}

/// @dev Hook called after an operator has joined a service instance
/// @param blueprintId The blueprint unique identifier
/// @param serviceId The ID of the service instance
/// @param operator The operator's preferences and details
function onOperatorJoined(
uint64 blueprintId,
uint64 serviceId,
ServiceOperators.OperatorPreferences calldata operator
)
public
{
address manager = blueprints.get(blueprintId);
IBlueprintServiceManager(manager).onOperatorJoined(serviceId, operator);
}

/// @dev Hook to check if an operator can leave a service instance
/// @param blueprintId The blueprint unique identifier
/// @param serviceId The ID of the service instance
/// @param operator The operator's preferences and details
/// @return allowed Returns true if the operator is allowed to leave
function canLeave(
uint64 blueprintId,
uint64 serviceId,
ServiceOperators.OperatorPreferences calldata operator
)
public
view
returns (bool allowed)
{
address manager = blueprints.get(blueprintId);
return IBlueprintServiceManager(manager).canLeave(serviceId, operator);
}

/// @dev Hook called after an operator has left a service instance
/// @param blueprintId The blueprint unique identifier
/// @param serviceId The ID of the service instance
/// @param operator The operator's preferences and details
function onOperatorLeft(
uint64 blueprintId,
uint64 serviceId,
ServiceOperators.OperatorPreferences calldata operator
)
public
{
address manager = blueprints.get(blueprintId);
IBlueprintServiceManager(manager).onOperatorLeft(serviceId, operator);
}

/// @dev Pause the contract.
/// @notice Only the pauser can pause the contract.
function pause() public onlyRole(PAUSER_ROLE) whenNotPaused {
Expand Down
2 changes: 1 addition & 1 deletion src/Permissions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pragma solidity ^0.8.20;
/// @notice This contract is used to restrict access of certain functions to the root chain only.
contract RootChainEnabled {
/// @notice The address of the root chain
address public constant ROOT_CHAIN = 0x1111111111111111111111111111111111111111;
address public constant ROOT_CHAIN = 0x09dF6A941ee03B1e632904E382e10862fA9cc0e3;
/// @notice The address of the rewards pallet
address public constant REWARDS_PALLET = address(0x7e87d5);

Expand Down
19 changes: 19 additions & 0 deletions src/SlashAlert.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

/// @title Slash Alert Interface
/// @notice Interface for handling slashing events in the re-staking protocol
/// @dev Implement this interface to handle slashing events for remote tokens
interface ISlashAlert {
/// @notice Called when a slashing event occurs
/// @param blueprintId The ID of the blueprint
/// @param serviceId The ID of the service
/// @param operator The address/account of the operator being slashed (32 bytes)
/// @param slashAmount The amount to slash in wei
function onSlash(
uint64 blueprintId,
uint64 serviceId,
bytes32 operator,
uint256 slashAmount
) external;
}
Loading

0 comments on commit 8f4975b

Please sign in to comment.