|
| 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