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

adding contract for compliency with v1.1 of the spec #123

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* Copyright 2019 iExec Blockchain Tech
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pragma solidity ^0.5.0;

contract WorkOrderRegistry
{
bytes4 constant VERSION = 0x01010000; // 1.1.0.0

enum WorkOrderStatus
{
NULL,
ACTIVE,
COMPLETED,
FAILED
}

struct WorkOrder
{
uint256 status;
uint256 timestamp;
bytes32 workerID;
bytes32 requesterID;
string request;
string response;
}

// workOrderID → workOrder
mapping(bytes32 => WorkOrder) m_workorders;

event workOrderSubmitted(
bytes32 indexed workOrderId,
bytes32 indexed workerId,
bytes32 indexed requesterId,
string workOrderRequest,
uint256 errorCode,
address senderAddress,
bytes4 version);

event workOrderCompleted(
bytes32 requesterId,
bytes32 workOrderId,
uint256 workOrderStatus,
string workOrderResponse,
uint256 errorCode,
bytes4 version);

constructor()
public
{}

function workOrderSubmit(
bytes32 _workOrderID,
bytes32 _workerID,
bytes32 _requesterID,
string memory _workOrderRequest)
public returns (
uint256 errorCode
) {
WorkOrder storage wo = m_workorders[_workOrderID];

errorCode = (wo.status == uint256(WorkOrderStatus.NULL)) ? 0 : 1;

if (errorCode == 0)
{
wo.status = uint256(WorkOrderStatus.ACTIVE);
wo.timestamp = now;
wo.workerID = _workerID;
wo.requesterID = _requesterID;
wo.request = _workOrderRequest;
}

emit workOrderSubmitted(
_workOrderID,
_workerID,
_requesterID,
_workOrderRequest,
errorCode,
msg.sender,
VERSION);

return errorCode;
}

function workOrderComplete(
bytes32 _workOrderID,
string memory _workOrderResponse
) public returns (
uint256 errorCode
) {
WorkOrder storage wo = m_workorders[_workOrderID];

errorCode = (wo.status == uint256(WorkOrderStatus.ACTIVE)) ? 0 : 1;

if (errorCode == 0)
{
wo.status = uint256(WorkOrderStatus.COMPLETED);
wo.response = _workOrderResponse;
}

emit workOrderCompleted(
wo.requesterID,
_workOrderID,
wo.status,
wo.response,
errorCode,
VERSION);

return errorCode;
}

function workOrderGet(
bytes32 _workOrderID
) public view returns (
uint256 status,
bytes32 workerID,
string memory request,
string memory response,
uint256 errorCode
) {
WorkOrder storage wo = m_workorders[_workOrderID];
return (
wo.status,
wo.workerID,
wo.request,
wo.response,
0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* Copyright 2019 iExec Blockchain Tech
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pragma solidity ^0.5.0;

contract WorkerRegistry
{
enum WorkerStatus
{
NULL,
ACTIVE,
OFFLINE,
DECOMMISSIONED,
COMPROMISED
}

struct Worker
{
uint256 status;
uint256 workerType;
bytes32 organizationId;
bytes32[] appTypeIds;
string details;
}

// WorkerID → Worker
mapping(bytes32 => Worker) private m_workers;

// workerType → organizationId → appTypeId → WorkerID[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From #72:
s/appTypeId/appTypeIds/

(I see no appTypeId here, only appTypeIds)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appTypeIds are arrays of appTypeId
to make worker searcheable by invididual appTypeId, we have to split the set of supported appTypeIds and enter as many entries in this multidimensional mapping.

workerRegister's code should show that clearly

mapping(uint256 => mapping(bytes32 => mapping(bytes32 => bytes32[]))) m_workersDB;

constructor()
public
{}

function workerRegister(
bytes32 workerID,
uint256 workerType,
bytes32 organizationId,
bytes32[] memory appTypeIds,
string memory details
) public {
Worker storage worker = m_workers[workerID];

require(worker.status == uint256(WorkerStatus.NULL));
worker.status = uint256(WorkerStatus.ACTIVE);
worker.workerType = workerType;
worker.organizationId = organizationId;
worker.appTypeIds = appTypeIds;
worker.details = details;

require(workerType != uint256(0));
require(organizationId != bytes32(0));
m_workersDB[uint256(0)][bytes32(0) ][bytes32(0)].push(workerID);
m_workersDB[workerType][bytes32(0) ][bytes32(0)].push(workerID);
m_workersDB[uint256(0)][organizationId][bytes32(0)].push(workerID);
m_workersDB[workerType][organizationId][bytes32(0)].push(workerID);

for (uint256 p = 0; p < appTypeIds.length; ++p)
{
require(appTypeIds[p] != bytes32(0));
m_workersDB[uint256(0)][bytes32(0) ][appTypeIds[p]].push(workerID);
m_workersDB[workerType][bytes32(0) ][appTypeIds[p]].push(workerID);
m_workersDB[uint256(0)][organizationId][appTypeIds[p]].push(workerID);
m_workersDB[workerType][organizationId][appTypeIds[p]].push(workerID);
}
}

function workerUpdate(
bytes32 workerID,
string memory details
) public {
require(m_workers[workerID].status != uint256(WorkerStatus.NULL));
m_workers[workerID].details = details;
}

function workerSetStatus(
bytes32 workerID,
uint256 status
) public {
require(m_workers[workerID].status != uint256(WorkerStatus.NULL));
require(status != uint256(WorkerStatus.NULL));
m_workers[workerID].status = status;
}

function workerLookUp(
uint256 workerType,
bytes32 organizationId,
bytes32 appTypeId
) public view returns(
uint256 totalCount,
uint256 lookupTag,
bytes32[] memory ids
) {
return workerLookUpNext(workerType, organizationId, appTypeId, 0);
}

function workerLookUpNext(
uint256 workerType,
bytes32 organizationId,
bytes32 appTypeId,
uint256 /*lookUpTag*/
) public view returns(
uint256 totalCount,
uint256 newLookupTag,
bytes32[] memory ids
) {
bytes32[] storage matchs = m_workersDB[workerType][organizationId][appTypeId];
return (matchs.length, 0, matchs);
}

function workerRetrieve(
bytes32 workerID
) public view returns (
uint256 status,
uint256 workerType,
bytes32 organizationId,
bytes32[] memory appTypeIds,
string memory details
) {
Worker storage worker = m_workers[workerID];
return (
worker.status,
worker.workerType,
worker.organizationId,
worker.appTypeIds,
worker.details
);
}
}