Skip to content
This repository was archived by the owner on Jan 27, 2022. It is now read-only.

Commit 354bc16

Browse files
committed
adding contract for compliency with v1.1 of the spec
1 parent 6027e71 commit 354bc16

File tree

2 files changed

+284
-0
lines changed

2 files changed

+284
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/* Copyright 2019 iExec Blockchain Tech
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
pragma solidity ^0.5.0;
17+
18+
contract WorkOrderRegistry
19+
{
20+
bytes4 constant VERSION = 0x01010000; // 1.1.0.0
21+
22+
enum WorkOrderStatus
23+
{
24+
NULL,
25+
ACTIVE,
26+
COMPLETED,
27+
FAILED
28+
}
29+
30+
struct WorkOrder
31+
{
32+
uint256 status;
33+
uint256 timestamp;
34+
bytes32 workerID;
35+
bytes32 requesterID;
36+
string request;
37+
string response;
38+
}
39+
40+
// workOrderID → workOrder
41+
mapping(bytes32 => WorkOrder) m_workorders;
42+
43+
event workOrderSubmitted(
44+
bytes32 indexed workOrderId,
45+
bytes32 indexed workerId,
46+
bytes32 indexed requesterId,
47+
string workOrderRequest,
48+
uint256 errorCode,
49+
address senderAddress,
50+
bytes4 version);
51+
52+
event workOrderCompleted(
53+
bytes32 requesterId,
54+
bytes32 workOrderId,
55+
uint256 workOrderStatus,
56+
string workOrderResponse,
57+
uint256 errorCode,
58+
bytes4 version);
59+
60+
constructor()
61+
public
62+
{}
63+
64+
function workOrderSubmit(
65+
bytes32 _workOrderID,
66+
bytes32 _workerID,
67+
bytes32 _requesterID,
68+
string memory _workOrderRequest)
69+
public returns (
70+
uint256 errorCode
71+
) {
72+
WorkOrder storage wo = m_workorders[_workOrderID];
73+
74+
errorCode = (wo.status == uint256(WorkOrderStatus.NULL)) ? 0 : 1;
75+
76+
if (errorCode == 0)
77+
{
78+
wo.status = uint256(WorkOrderStatus.ACTIVE);
79+
wo.timestamp = now;
80+
wo.workerID = _workerID;
81+
wo.requesterID = _requesterID;
82+
wo.request = _workOrderRequest;
83+
}
84+
85+
emit workOrderSubmitted(
86+
_workOrderID,
87+
_workerID,
88+
_requesterID,
89+
_workOrderRequest,
90+
errorCode,
91+
msg.sender,
92+
VERSION);
93+
94+
return errorCode;
95+
}
96+
97+
function workOrderComplete(
98+
bytes32 _workOrderID,
99+
string memory _workOrderResponse
100+
) public returns (
101+
uint256 errorCode
102+
) {
103+
WorkOrder storage wo = m_workorders[_workOrderID];
104+
105+
errorCode = (wo.status == uint256(WorkOrderStatus.ACTIVE)) ? 0 : 1;
106+
107+
if (errorCode == 0)
108+
{
109+
wo.status = uint256(WorkOrderStatus.COMPLETED);
110+
wo.response = _workOrderResponse;
111+
}
112+
113+
emit workOrderCompleted(
114+
wo.requesterID,
115+
_workOrderID,
116+
wo.status,
117+
wo.response,
118+
errorCode,
119+
VERSION);
120+
121+
return 0;
122+
}
123+
124+
function workOrderGet(
125+
bytes32 _workOrderID
126+
) public view returns (
127+
uint256 status,
128+
bytes32 workerID,
129+
string memory request,
130+
string memory response,
131+
uint256 errorCode
132+
) {
133+
WorkOrder storage wo = m_workorders[_workOrderID];
134+
return (
135+
wo.status,
136+
wo.workerID,
137+
wo.request,
138+
wo.response,
139+
0);
140+
}
141+
142+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/* Copyright 2019 iExec Blockchain Tech
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
pragma solidity ^0.5.0;
17+
18+
contract WorkerRegistry
19+
{
20+
enum WorkerStatus
21+
{
22+
NULL,
23+
ACTIVE,
24+
OFFLINE,
25+
DECOMMISSIONED,
26+
COMPROMISED
27+
}
28+
29+
struct Worker
30+
{
31+
uint256 status;
32+
uint256 workerType;
33+
bytes32 organizationId;
34+
bytes32[] appTypeIds;
35+
string details;
36+
}
37+
38+
// WorkerID → Worker
39+
mapping(bytes32 => Worker) private m_workers;
40+
41+
// workerType → organizationId → appTypeId → WorkerID[]
42+
mapping(uint256 => mapping(bytes32 => mapping(bytes32 => bytes32[]))) m_workersDB;
43+
44+
constructor()
45+
public
46+
{}
47+
48+
function workerRegister(
49+
bytes32 workerID,
50+
uint256 workerType,
51+
bytes32 organizationId,
52+
bytes32[] memory appTypeIds,
53+
string memory details
54+
) public {
55+
Worker storage worker = m_workers[workerID];
56+
57+
require(worker.status == uint256(WorkerStatus.NULL));
58+
worker.status = uint256(WorkerStatus.ACTIVE);
59+
worker.workerType = workerType;
60+
worker.organizationId = organizationId;
61+
worker.appTypeIds = appTypeIds;
62+
worker.details = details;
63+
64+
require(workerType != uint256(0));
65+
require(organizationId != bytes32(0));
66+
m_workersDB[uint256(0)][bytes32(0) ][bytes32(0)].push(workerID);
67+
m_workersDB[workerType][bytes32(0) ][bytes32(0)].push(workerID);
68+
m_workersDB[uint256(0)][organizationId][bytes32(0)].push(workerID);
69+
m_workersDB[workerType][organizationId][bytes32(0)].push(workerID);
70+
71+
for (uint256 p = 0; p < appTypeIds.length; ++p)
72+
{
73+
require(appTypeIds[p] != bytes32(0));
74+
m_workersDB[uint256(0)][bytes32(0) ][appTypeIds[p]].push(workerID);
75+
m_workersDB[workerType][bytes32(0) ][appTypeIds[p]].push(workerID);
76+
m_workersDB[uint256(0)][organizationId][appTypeIds[p]].push(workerID);
77+
m_workersDB[workerType][organizationId][appTypeIds[p]].push(workerID);
78+
}
79+
}
80+
81+
function workerUpdate(
82+
bytes32 workerID,
83+
string memory details
84+
) public {
85+
require(m_workers[workerID].status != uint256(WorkerStatus.NULL));
86+
m_workers[workerID].details = details;
87+
}
88+
89+
function workerSetStatus(
90+
bytes32 workerID,
91+
uint256 status
92+
) public {
93+
require(m_workers[workerID].status != uint256(WorkerStatus.NULL));
94+
require(status != uint256(WorkerStatus.NULL));
95+
m_workers[workerID].status = status;
96+
}
97+
98+
function workerLookUp(
99+
uint256 workerType,
100+
bytes32 organizationId,
101+
bytes32 appTypeId
102+
) public view returns(
103+
uint256 totalCount,
104+
uint256 lookupTag,
105+
bytes32[] memory ids
106+
) {
107+
return workerLookUpNext(workerType, organizationId, appTypeId, 0);
108+
}
109+
110+
function workerLookUpNext(
111+
uint256 workerType,
112+
bytes32 organizationId,
113+
bytes32 appTypeId,
114+
uint256 /*lookUpTag*/
115+
) public view returns(
116+
uint256 totalCount,
117+
uint256 newLookupTag,
118+
bytes32[] memory ids
119+
) {
120+
bytes32[] storage matchs = m_workersDB[workerType][organizationId][appTypeId];
121+
return (matchs.length, 0, matchs);
122+
}
123+
124+
function workerRetrieve(
125+
bytes32 workerID
126+
) public view returns (
127+
uint256 status,
128+
uint256 workerType,
129+
bytes32 organizationId,
130+
bytes32[] memory appTypeIds,
131+
string memory details
132+
) {
133+
Worker storage worker = m_workers[workerID];
134+
return (
135+
worker.status,
136+
worker.workerType,
137+
worker.organizationId,
138+
worker.appTypeIds,
139+
worker.details
140+
);
141+
}
142+
}

0 commit comments

Comments
 (0)