Skip to content

Commit afd0623

Browse files
committed
updated access mofifiers.
1 parent 97c17d1 commit afd0623

File tree

8 files changed

+108
-11
lines changed

8 files changed

+108
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Compiler files
22
cache/
33
out/
4+
/bin/src
45

56
# Ignores development broadcast logs
67
!/broadcast

src/AggregationAndValidation.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ contract AggregationAndValidation is BaseTask {
6868
task.isActive = false;
6969

7070
// Store the aggregated model hash on-chain
71-
trainerNodeContract.storeModelUpdate(taskId, aggregatedModelHash);
71+
// trainerNodeContract.storeModelUpdate(taskId, aggregatedModelHash);
7272

7373
emit AggregatedModelStored(taskId, aggregatedModelHash);
7474

7575
// Distribute rewards to trainers
7676
distributeRewards(taskId);
7777
}
7878

79-
modifier onlyTaskCreator(uint taskId) virtual {
79+
modifier onlyTaskCreator(uint taskId) virtual {
8080
require(tasks[taskId].creator == msg.sender, "Not the task creator");
8181
_;
8282
}

src/BaseTask.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ abstract contract BaseTask {
1111
uint totalstaked;
1212
bool isActive;
1313
address[] trainers; //we can add validators and aggregrator as well later
14+
address[] aggregrators; //we can add validators and aggregrator as well later
1415
mapping(address => uint) stakes;
1516
mapping(address => bytes) modelUpdates; //state change od model done by a trainer for the task
1617
}
1718

19+
struct TaskSummary {
20+
uint id;
21+
string description;
22+
bool isActive;
23+
address assignedTo;
24+
}
25+
1826
uint public taskCounter;
1927
mapping(uint => Task) public tasks;
2028

src/ModelSubmission.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "./BaseTask.sol";
55
// import "./Staking.sol";
66

77
contract ModelSubmission is BaseTask {
8-
function submitModelUpdate(uint taskId, bytes memory modelUpdate) public {
8+
function submitModelUpdate(uint taskId, bytes memory modelUpdate) internal {
99
require(tasks[taskId].isActive, "Task is not active");
1010
Task storage task = tasks[taskId];
1111
require(task.stakes[msg.sender] >= TRAINER_STAKE, "Must be a staked trainer");

src/RoleBasedAccess.sol

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,36 @@ contract RoleBasedAccess {
2727
_;
2828
}
2929

30+
modifier onlyCreator(address sender) {
31+
require(
32+
roles[sender] == Role.TaskCreator,
33+
"Only Task Creation role is applicable"
34+
);
35+
_;
36+
}
37+
3038
// Assign roles
3139
// function assignRole(address user, Role role) public onlyOwner {
3240
// roles[user] = role;
3341
// }
3442

35-
function assignStackedRole(address user, Role role) public {
43+
function assignStackedRole(address user, Role role) internal {
3644
roles[user] = role;
3745
}
3846

47+
modifier isRoleAssigned(address user) {
48+
Role role = roles[user];
49+
50+
if (
51+
role == Role.TaskCreator ||
52+
role == Role.Aggregator ||
53+
role == Role.TrainerNode
54+
) {
55+
revert("Role is already assigned");
56+
}
57+
_;
58+
}
59+
3960
// Get role for the caller
4061
function getMyRole() public view returns (string memory) {
4162
Role role = roles[msg.sender];
@@ -51,7 +72,7 @@ contract RoleBasedAccess {
5172
}
5273
}
5374

54-
// Check if an address is a TaskCreator
75+
// Check if an address is a TaskCreator
5576
function isTaskCreator(address user) public view returns (bool) {
5677
return roles[user] == Role.TaskCreator;
5778
}

src/Staking.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,23 @@ contract Staking is TaskManagement, RoleBasedAccess {
4545
task.totalstaked += msg.value;
4646
if (role == Role.TrainerNode) {
4747
task.trainers.push(msg.sender);
48+
assignStackedRole(msg.sender, Role.TaskCreator);
49+
} else if (role == Role.Aggregator) {
50+
task.aggregrators.push(msg.sender);
51+
assignStackedRole(msg.sender, Role.Aggregator);
52+
} else {
53+
assignStackedRole(msg.sender, Role.None);
4854
}
4955
}
5056

57+
function stakeCreatorTokens(
58+
// Role role,
59+
uint reward
60+
) public payable isRoleAssigned(msg.sender) {
61+
createTask(reward , msg.value);
62+
assignStackedRole(msg.sender, Role.TaskCreator);
63+
}
64+
5165
modifier hasToken(Role role) {
5266
require(msg.value != 0, "Stake Tokens not found");
5367
if (role == Role.TaskCreator) {

src/TaskManagement.sol

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ pragma solidity ^0.8.0;
66
import "./BaseTask.sol";
77

88
contract TaskManagement is BaseTask {
9-
function createTask(uint reward) public payable isCreatorRole(reward) {
10-
require(msg.value >= reward, "reward must be deposited");
9+
// Event to log task creation
10+
event TaskCreated(uint id, string details, address creator, uint timestamp);
11+
12+
function createTask(
13+
uint reward,
14+
uint stakeValue
15+
) internal isCreatorRole(reward, stakeValue) {
16+
require(stakeValue >= reward, "reward must be deposited");
1117

1218
Task storage newTask = tasks[taskCounter];
1319
newTask.id = taskCounter;
@@ -25,13 +31,13 @@ contract TaskManagement is BaseTask {
2531
_;
2632
}
2733

28-
modifier isCreatorRole(uint reward) {
34+
modifier isCreatorRole(uint reward, uint stakeValue) {
2935
require(
30-
msg.value >= CREATOR_STAKE,
36+
stakeValue >= CREATOR_STAKE,
3137
"Insufficient stake for task creation"
3238
);
3339
require(
34-
msg.value >= reward + CREATOR_STAKE,
40+
stakeValue >= reward + CREATOR_STAKE,
3541
"Reward and stake must be deposited, currently reward is {$reward} stake {$CREATOOR_STAKE} "
3642
);
3743
_;
@@ -42,4 +48,51 @@ contract TaskManagement is BaseTask {
4248

4349
// _;
4450
// }
51+
52+
// Get incomplete tasks
53+
function getAvailableTasks() public view returns (TaskSummary[] memory) {
54+
uint count = 0;
55+
56+
// Count the number of incomplete tasks
57+
for (uint i = 0; i < taskCounter; i++) {
58+
if (!tasks[i].isActive) {
59+
count++;
60+
}
61+
}
62+
63+
// Create a temporary array to hold TaskSummary
64+
TaskSummary[] memory availableTasks = new TaskSummary[](count);
65+
uint index = 0;
66+
67+
for (uint i = 0; i < taskCounter; i++) {
68+
if (!tasks[i].isActive) {
69+
availableTasks[index] = TaskSummary({
70+
id: tasks[i].id,
71+
description: "assigned",
72+
isActive: tasks[i].isActive,
73+
assignedTo: tasks[i].creator
74+
});
75+
index++;
76+
}
77+
}
78+
79+
return availableTasks;
80+
}
81+
// Get task details by ID
82+
// function getTaskById(uint taskId) public view returns (Task memory) {
83+
// require(taskId < nextTaskId, "Task ID does not exist");
84+
// return taskById[taskId];
85+
// }
86+
87+
// Get the last task added
88+
function getLastTask() public view returns (TaskSummary memory) {
89+
require(taskCounter > 0, "No tasks available");
90+
TaskSummary memory availableTasks = TaskSummary({
91+
id: tasks[taskCounter - 1].id,
92+
description: "assigned",
93+
isActive: tasks[taskCounter - 1].isActive,
94+
assignedTo: tasks[taskCounter - 1].creator
95+
});
96+
return availableTasks;
97+
}
4598
}

src/TrainerNode.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ contract TrainerNode is BaseTask {
2323
);
2424

2525
// Function to store a model update as an IPFS hash
26-
function storeModelUpdate(uint taskId, string memory ipfsHash) public {
26+
function storeModelUpdate(uint taskId, string memory ipfsHash) internal {
2727
require(tasks[taskId].isActive, "task not active");
2828
Task storage task = tasks[taskId];
2929
require(

0 commit comments

Comments
 (0)