From 4789f760192d51688c06fe97218c3f3a4f2c8b6b Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Tue, 8 Oct 2019 16:02:04 +0200 Subject: [PATCH 1/2] adding contract for compliency with v1.1 of the spec Signed-off-by: Hadrien Croubois --- .../proxy-model/WorkOrderRegistry.sol | 142 ++++++++++++++++++ .../contracts/proxy-model/WorkerRegistry.sol | 142 ++++++++++++++++++ 2 files changed, 284 insertions(+) create mode 100644 examples/common/python/connectors/ethereum/contracts/proxy-model/WorkOrderRegistry.sol create mode 100644 examples/common/python/connectors/ethereum/contracts/proxy-model/WorkerRegistry.sol diff --git a/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkOrderRegistry.sol b/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkOrderRegistry.sol new file mode 100644 index 000000000..828a3a05e --- /dev/null +++ b/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkOrderRegistry.sol @@ -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 0; + } + + 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); + } + +} diff --git a/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkerRegistry.sol b/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkerRegistry.sol new file mode 100644 index 000000000..3b06331c2 --- /dev/null +++ b/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkerRegistry.sol @@ -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[] + 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 + ); + } +} From e216da67f11c77cd3c357b9f3a5c617db31029e8 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Tue, 8 Oct 2019 16:20:02 +0200 Subject: [PATCH 2/2] minor debug Signed-off-by: Hadrien Croubois --- .../ethereum/contracts/proxy-model/WorkOrderRegistry.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkOrderRegistry.sol b/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkOrderRegistry.sol index 828a3a05e..e57775be8 100644 --- a/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkOrderRegistry.sol +++ b/examples/common/python/connectors/ethereum/contracts/proxy-model/WorkOrderRegistry.sol @@ -118,7 +118,7 @@ contract WorkOrderRegistry errorCode, VERSION); - return 0; + return errorCode; } function workOrderGet(