Skip to content

Commit

Permalink
merged taskManager into serviceManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Gajesh2007 committed May 15, 2024
1 parent a4afec6 commit dc443d8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 130 deletions.
109 changes: 91 additions & 18 deletions contracts/src/HelloWorldServiceManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,56 @@
pragma solidity ^0.8.9;

import "@eigenlayer/contracts/libraries/BytesLib.sol";
import "./IHelloWorldTaskManager.sol";
import "@eigenlayer-middleware/src/ServiceManagerBase.sol";
import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol";
import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol";
import "@openzeppelin-upgrades/contracts/utils/cryptography/ECDSAUpgradeable.sol";
import "@eigenlayer/contracts/permissions/Pausable.sol";
import {IRegistryCoordinator} from "@eigenlayer-middleware/src/interfaces/IRegistryCoordinator.sol";
import "./IHelloWorldServiceManager.sol";

/**
* @title Primary entrypoint for procuring services from HelloWorld.
* @author Eigen Labs, Inc.
*/
contract HelloWorldServiceManager is ServiceManagerBase {
contract HelloWorldServiceManager is
ServiceManagerBase,
IHelloWorldServiceManager,
Pausable
{
using BytesLib for bytes;
using ECDSAUpgradeable for bytes32;

IHelloWorldTaskManager
public immutable helloWorldTaskManager;
/* STORAGE */
// The latest task index
uint32 public latestTaskNum;

/// @notice when applied to a function, ensures that the function is only callable by the `registryCoordinator`.
modifier onlyHelloWorldTaskManager() {
// mapping of task indices to all tasks hashes
// when a task is created, task hash is stored here,
// and responses need to pass the actual task,
// which is hashed onchain and checked against this mapping
mapping(uint32 => bytes32) public allTaskHashes;

// mapping of task indices to hash of abi.encode(taskResponse, taskResponseMetadata)
mapping(address => mapping(uint32 => bytes)) public allTaskResponses;

IRegistryCoordinator public registryCoordinator;

/* MODIFIERS */
modifier onlyOperator() {
require(
msg.sender == address(helloWorldTaskManager),
"onlyHelloWorldTaskManager: not from hello world task manager"
registryCoordinator.getOperatorStatus(msg.sender)
==
IRegistryCoordinator.OperatorStatus.REGISTERED,
"Operator must be the caller"
);
_;
}

constructor(
IAVSDirectory _avsDirectory,
IRegistryCoordinator _registryCoordinator,
IStakeRegistry _stakeRegistry,
IHelloWorldTaskManager _helloWorldTaskManager
IStakeRegistry _stakeRegistry
)
ServiceManagerBase(
_avsDirectory,
Expand All @@ -37,15 +60,65 @@ contract HelloWorldServiceManager is ServiceManagerBase {
_stakeRegistry
)
{
helloWorldTaskManager = _helloWorldTaskManager;
registryCoordinator = _registryCoordinator;
}

function initialize(
IPauserRegistry _pauserRegistry,
address initialOwner
) public initializer {
_initializePauser(_pauserRegistry, UNPAUSE_ALL);
_transferOwnership(initialOwner);
}


/* FUNCTIONS */
// NOTE: this function creates new task, assigns it a taskId
function createNewTask(
string memory name
) external {
// create a new task struct
Task memory newTask;
newTask.name = name;
newTask.taskCreatedBlock = uint32(block.number);

// store hash of task onchain, emit event, and increase taskNum
allTaskHashes[latestTaskNum] = keccak256(abi.encode(newTask));
emit NewTaskCreated(latestTaskNum, newTask);
latestTaskNum = latestTaskNum + 1;
}

/// @notice Called in the event of challenge resolution, in order to forward a call to the Slasher, which 'freezes' the `operator`.
/// @dev The Slasher contract is under active development and its interface expected to change.
/// We recommend writing slashing logic without integrating with the Slasher at this point in time.
function freezeOperator(
address operatorAddr
) external onlyHelloWorldTaskManager {

// NOTE: this function responds to existing tasks.
function respondToTask(
Task calldata task,
uint32 referenceTaskIndex,
bytes calldata signature
) external onlyOperator {
// check that the task is valid, hasn't been responsed yet, and is being responsed in time
require(
keccak256(abi.encode(task)) ==
allTaskHashes[referenceTaskIndex],
"supplied task does not match the one recorded in the contract"
);
// some logical checks
require(
allTaskResponses[msg.sender][referenceTaskIndex].length == 0,
"Operator has already responded to the task"
);

// The message that was signed
bytes32 messageHash = keccak256(abi.encodePacked("Hello, ", task.name));
bytes32 ethSignedMessageHash = messageHash.toEthSignedMessageHash();

// Recover the signer address from the signature
address signer = ethSignedMessageHash.recover(signature);

require(signer == msg.sender, "Message signer is not operator");

// updating the storage with task responsea
allTaskResponses[msg.sender][referenceTaskIndex] = signature;

// emitting event
emit TaskResponded(referenceTaskIndex, task, msg.sender);
}
}
111 changes: 0 additions & 111 deletions contracts/src/HelloWorldTaskManager.sol

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

interface IHelloWorldTaskManager {
interface IHelloWorldServiceManager {
// EVENTS
event NewTaskCreated(uint32 indexed taskIndex, Task task);

Expand Down

0 comments on commit dc443d8

Please sign in to comment.