/** * Copyright 2017–2019, LaborX PTY * Licensed under the AGPL Version 3 license. */ pragma solidity ^0.4.25; import "./WorkflowCore.sol"; import "./WorkflowBase.sol"; import "./WorkflowBlacklistableBase.sol"; import "../labor-contract/LaborContract.sol"; import "../libs/SafeMath.sol"; import "../libs/Arrays.sol"; import "../libs/PercentCalculator.sol"; contract WorkflowEventEmitter { event TaskStateChanged( bytes32 indexed _contractKey, bytes32 contractId, uint indexed taskId, uint8 indexed newState, uint8 previousState ); } contract Workflow is WorkflowBase, WorkflowEventEmitter, InitializableOwned, WorkflowContractBlacklistableBase, WorkflowCore { using SafeMath for uint; modifier onlyContractAssignee(bytes32 _contractId, address _contract) { (,address _assignee) = LaborContract(_contract).getContractParties(_contractId); require(msg.sender == _assignee, "WORKFLOW_ASSIGNEE_SHOULD_CALL"); _; } modifier onlyContractEmployer(bytes32 _contractId, address _contract) { (address _employer,) = LaborContract(_contract).getContractParties(_contractId); require(msg.sender == _employer, "WORKFLOW_EMPLOYER_SHOULD_CALL"); _; } modifier onlyContractProxy(address _contract) { require(msg.sender == _contract, "WORKFLOW_CONTRACT_SHOULD_CALL"); _; } modifier onlyExistedContract(bytes32 _contractId, address _contract) { require( store.get(contractRegistrationsStorage, _getContractRecordKey(_contractId, _contract)), "WORKFLOW_CONTRACT_SHOULD_EXIST" ); _; } modifier onlyTaskInState(bytes32 _contractId, address _contract, uint _taskId, TaskState _state) { require(_getTaskState(_contractId, _contract, _taskId) == _state, "WORKFLOW_INVALID_STATE"); _; } constructor(Storage _storage, bytes32 _crate) public StorageAdapter(_storage, _crate) { _initCore(); } function () external payable { revert("W_ETH"); } function version() public pure returns (string) { return "0.2.0"; } function initWorkflow( address _owner, address _storage, bytes32 _crate ) public { _initOwned(_owner); _initStorageAdapter(Storage(_storage), _crate); _initCore(); } function getTaskDetails( bytes32 _contractId, address _contract, uint32 _taskId ) external view returns (uint[] _values) { _values = new uint[](TASK_DETAILS_LENGTH); uint _upfrontAmount = _getUpfrontValue(_contractId, _contract, _taskId); _values[TASK_DETAILS_BUDGET_IDX] = _getTotalValue(_contractId, _contract, _taskId).add(_upfrontAmount); _values[TASK_DETAILS_PAID_AMOUNT_IDX] = _getPaidValue(_contractId, _contract, _taskId).add(_upfrontAmount); _values[TASK_DETAILS_UPFRONT_AMOUNT_IDX] = _upfrontAmount; (, uint _assigneePercent, uint _percentPrecision) = LaborContract(_contract).getServiceSurchargePercent(_contractId); if (_values[TASK_DETAILS_PAID_AMOUNT_IDX] > 0) { _values[TASK_DETAILS_WITHDRAW_FEE_AMOUNT] = PercentCalculator.getPercent( _values[TASK_DETAILS_PAID_AMOUNT_IDX], _assigneePercent, _percentPrecision ); } } /// @notice Gets numbers of how much employer will pay for a task `_taskId` in contract `_contractId` at `_contract` /// @return /// @param _totalValue how much task costs /// @param _paymentValue how much should be paid immediately /// @param _depositValue how much should be additionally deposited function getTaskCompletionReward( bytes32 _contractId, address _contract, uint32 _taskId, uint32[] _skippedPenalties ) public view returns ( uint _totalValue, uint _paymentValue, uint _depositValue, uint _paidValue ) { _totalValue = _calculateTotalValue(_contractId, _contract, _taskId, _skippedPenalties); _paidValue = _getPaidValue(_contractId, _contract, _taskId); (_paymentValue, _depositValue) = _calculatePaymentAndDepositValuesForTaskCompletion( _contractId, _contract, _taskId, _totalValue, _paidValue ); } function getTaskCompletionRewardWithoutPenalties( bytes32 _contractId, address _contract, uint32 _taskId ) public view returns ( uint _totalValue, uint _paymentValue, uint _depositValue, uint _paidValue ) { _totalValue = _getTotalValue(_contractId, _contract, _taskId); _paidValue = _getPaidValue(_contractId, _contract, _taskId); (_paymentValue, _depositValue) = _calculatePaymentAndDepositValuesForTaskCompletion( _contractId, _contract, _taskId, _totalValue, _paidValue ); } function _calculateTotalValue( bytes32 _contractId, address _contract, uint32 _taskId, uint32[] _skippedPenalties ) private view returns (uint _totalValue) { uint _assigneePenaltyValue = _getPenaltyValue( _contractId, _contract, _taskId, _skippedPenalties, DataParser.PenaltyApplicationType.MISS_DEADLINE ); // TODO: version 2: take into account LATE_PAYMENT skipped penalties _totalValue = _getTotalValue(_contractId, _contract, _taskId); _totalValue = _totalValue.sub(SafeMath.min(_assigneePenaltyValue, _totalValue)); } function _calculatePaymentAndDepositValuesForTaskCompletion( bytes32 _contractId, address _contract, uint32 /* _taskId */, uint _totalValue, uint _paidValue ) private view returns (uint _paymentValue, uint _depositValue) { // TODO: add calculating deposit value: EP > AP then (EP.- AP) else 0 (bool _lockFullBudget, uint8 _paymentTimeline) = LaborContract(_contract).getPaymentRequirements(_contractId); if (_paymentTimeline == uint8(DataParser.PaymentTimeline.PAY_BY_TASK)) { _paymentValue = _totalValue.sub(_paidValue); if (!_lockFullBudget) { _depositValue = _paymentValue; } } } function getUnspentUpfrontAmount(bytes32 _contractId, address _contractHost) external view returns (uint) { return store.get(tasksUnspentUpfrontStorage, _getContractRecordKey(_contractId, _contractHost)); } function registerContract( bytes32 _contractId, address _contractHost ) external onlyContractProxy(_contractHost) onlyAllowedContractHost(_contractHost) returns (uint) { store.set(contractRegistrationsStorage, _getContractRecordKey(_contractId, _contractHost), true); return OK; } function precalculateWorkflowData( bytes32 _contractId, address _contractHost, bytes _data ) external view onlyExistedContract(_contractId, _contractHost) onlyContractProxy(_contractHost) returns ( uint _budget, uint _upfront ) { DataParser.TaskData[] memory _tasks = DataParser.parseTaskDataRawInput(_data); require(_isUniqueTasks(_tasks), "WORKFLOW_TASKS_NOT_UNIQUE"); (_budget, _upfront) = _iterateOverTasks(_contractId, _contractHost, _tasks, _checkTask); } function addWorkflowData( bytes32 _contractId, address _contract, bytes _data ) external onlyExistedContract(_contractId, _contract) onlyContractProxy(_contract) returns ( uint _totalBudget, uint _upfrontBudget ) { DataParser.TaskData[] memory _tasks = DataParser.parseTaskDataRawInput(_data); require(_isUniqueTasks(_tasks), "WORKFLOW_TASKS_NOT_UNIQUE"); (_totalBudget, _upfrontBudget) = _iterateOverTasks(_contractId, _contract, _tasks, _saveTaskToStorage); _incrementUnspentUpfrontAmount(_contractId, _contract, _upfrontBudget); _handleTasksPenalties(_contractId, _contract, _tasks, _data); } function _assertExistedTask( bytes32 _contractId, address _contract, DataParser.TaskData memory _task ) private view { require( !store.includes(taskIdsStorage, _getContractRecordKey(_contractId, _contract), _task.id), "WORKFLOW_TASK_ID_SHOULD_BE_UNIQUE" ); } function _assetTaskState( DataParser.TaskData memory _task ) private view { require(_task.id > 0, "WORKFLOW_INVALID_TASK_ID"); require(_task.deadline > block.timestamp, "CONTRACT_TASK_DEADLINE_IS_OUTDATED"); } function _iterateOverTasks( bytes32 _contractId, address _contract, DataParser.TaskData[] memory _tasks, function (bytes32, address, DataParser.TaskData memory, uint) internal _applyFunc ) private returns ( uint _tasksBudget, uint _upfrontBudget ) { for (uint _taskIndex = 0; _taskIndex < _tasks.length; ++_taskIndex) { DataParser.TaskData memory _task = _tasks[_taskIndex]; _assetTaskState(_task); uint _upfrontValue = 0; if (_task.isUpfront) { _upfrontValue = DataParser.getTaskUpfrontResultValue(_task, UPFRONT_PERCENT_PRECISION); } _tasksBudget = _tasksBudget.add(_task.budget); _upfrontBudget = _upfrontBudget.add(_upfrontValue); _applyFunc(_contractId, _contract, _task, _upfrontValue); } assert(_tasksBudget >= _upfrontBudget); } function _saveTaskToStorage( bytes32 _contractId, address _contract, DataParser.TaskData memory _task, uint _upfrontValue ) internal { _checkTask(_contractId, _contract, _task, _upfrontValue); bytes32 _taskRecordKey = _getTaskRecordKey(_contractId, _contract, _task.id); store.set(taskUpfrontValueStorage, _taskRecordKey, _upfrontValue); store.set(taskBudgetLeftValueStorage, _taskRecordKey, _task.budget.sub(_upfrontValue)); store.set(taskDeadlineStorage, _taskRecordKey, _task.deadline); store.add(taskIdsStorage, _getContractRecordKey(_contractId, _contract), _task.id); } function _checkTask( bytes32 _contractId, address _contract, DataParser.TaskData memory _task, uint /* _upfrontValue */ ) internal { _assertExistedTask(_contractId, _contract, _task); } function _handleTasksPenalties( bytes32 _contractId, address _contract, DataParser.TaskData[] memory _tasks, bytes _data ) private { DataParser.TaskPenaltyData[] memory _penalties = DataParser.parseTaskPenaltyDataRawInput(_data); for (uint _penaltyIdx = 0; _penaltyIdx < _penalties.length; ++_penaltyIdx) { DataParser.TaskPenaltyData memory _penalty = _penalties[_penaltyIdx]; DataParser.TaskData memory _task = Arrays.getTaskById(_tasks, _penalty.taskId); require(_task.id > 0, "WORKFLOW_LINKED_PENALTY_TASK_NOT_FOUND"); DataParser.assertTaskPenalty(_penalty); require( store.includes(taskIdsStorage, _getContractRecordKey(_contractId, _contract), _task.id), "WORKFLOW_TASK_ID_SHOULD_EXIST" ); require( !store.includes(penaltyIdsStorage, _getTaskRecordKey(_contractId, _contract, _penalty.taskId), _penalty.id), "WORKFLOW_TASK_PENALTY_ID_SHOULD_BE_UNIQUE" ); /** 1. Store penalty id in a list of penalties for the task */ store.add(penaltyIdsStorage, _getTaskRecordKey(_contractId, _contract, _penalty.taskId), _penalty.id); /** 2. Save penalty info */ bytes32 _penaltyRecordKey = _getTaskPenaltyRecordKey(_contractId, _contract, _task.id, _penalty.id); store.set(taskPenaltyApplicationTypeStorage, _penaltyRecordKey, _penalty.applicationType); store.set( taskPenaltyValueStorage, _penaltyRecordKey, DataParser.getTaskPenaltyResultValue(_penalty, _task.budget, TASK_PENALTY_PERCENT_PRECISION) ); /** 2.1 Handle applicationType == PenaltyApplicationType.LATE_PAYMENT */ if ( _penalty.applicationType == uint8(DataParser.PenaltyApplicationType.LATE_PAYMENT) && uint256(_penalty.reserved1) != 0 ) { store.set( taskPenaltyPaymentWindowValueStorage, _penaltyRecordKey, uint256(_penalty.reserved1) ); } } } function startTask( bytes32 _contractId, address _contract, uint32 _taskId ) external onlyExistedContract(_contractId, _contract) onlyContractAssignee(_contractId, _contract) onlyTaskInState(_contractId, _contract, _taskId, TaskState.NOT_STARTED) returns (uint) { return _doStartTask(_contractId, _contract, _taskId); } function _doStartTask( bytes32 _contractId, address _contract, uint32 _taskId ) private returns (uint) { bytes32 _taskRecordKey = _getTaskRecordKey(_contractId, _contract, _taskId); store.set(taskStartedAtStorage, _taskRecordKey, block.timestamp); _transitionTaskToState(_contractId, _contract, _taskId, TaskState.STARTED); LaborContract(_contract).onOperation(_contractId, "OP_TASK_START"); return OK; } // function pauseTask( // bytes32 _contractId, // address _contract, // uint32 _taskId // ) // external // onlyExistedContract(_contractId, _contract) // onlyContractAssignee(_contractId, _contract) // onlyTaskInState(_contractId, _contract, _taskId, TaskState.STARTED) // returns (uint) // { // return _doPauseTask(_contractId, _contract, _taskId); // } // function _doPauseTask( // bytes32 _contractId, // address _contract, // uint32 _taskId // ) // private // returns (uint) // { // bytes32 _taskRecordKey = _getTaskRecordKey(_contractId, _contract, _taskId); // _startTrackingPause(_taskRecordKey); // _transitionTaskToState(_contractId, _contract, _taskId, TaskState.PAUSED); // LaborContract(_contract).onOperation(_contractId, "OP_TASK_PAUSE"); // return OK; // } // function resumeTask( // bytes32 _contractId, // address _contract, // uint32 _taskId // ) // external // onlyExistedContract(_contractId, _contract) // onlyContractAssignee(_contractId, _contract) // onlyTaskInState(_contractId, _contract, _taskId, TaskState.PAUSED) // returns (uint) // { // return _doResumeTask(_contractId, _contract, _taskId); // } // function _doResumeTask( // bytes32 _contractId, // address _contract, // uint32 _taskId // ) // private // returns (uint) // { // bytes32 _taskRecordKey = _getTaskRecordKey(_contractId, _contract, _taskId); // _updatePausedInterval(_taskRecordKey); // _transitionTaskToState(_contractId, _contract, _taskId, TaskState.STARTED); // LaborContract(_contract).onOperation(_contractId, "OP_TASK_RESUME"); // return OK; // } function payPartialTaskExpenses( bytes32 _contractId, address _contract, uint32 _taskId, uint _value, uint _expireAtBlock, uint _salt, bytes _signature ) external payable onlyExistedContract(_contractId, _contract) onlyContractEmployer(_contractId, _contract) returns (uint) { require(_isTaskStarted(_contractId, _contract, _taskId), "WORKFLOW_INVALID_STATE"); _withdrawOnPartialTaskExpenses(_contractId, _contract, _taskId, _value, _expireAtBlock, _salt, _signature); return OK; } function _withdrawOnPartialTaskExpenses( bytes32 _contractId, address _contract, uint32 _taskId, uint _value, uint _expireAtBlock, uint _salt, bytes _signature ) private { uint _paidValue = _getPaidValue(_contractId, _contract, _taskId); (, uint _depositValue) = getTaskSinglePaymentReward(_contractId, _contract, _value); uint _totalValue = _getTotalValue(_contractId, _contract, _taskId); require(_totalValue >= _paidValue.add(_value), "WORKFLOW_INCORRECT_VALUE"); LaborContract(_contract).onWithdrawOperation.value(msg.value) ( _contractId, "OP_TASK_PAYMENT", _depositValue, _value, _expireAtBlock, _salt, _signature ); store.set(taskBudgetPaidValueStorage, _getTaskRecordKey(_contractId, _contract, _taskId), _paidValue.add(_value)); } function getTaskSinglePaymentReward( bytes32 _contractId, address _contract, uint _value ) public view returns (uint _paymentValue, uint _depositValue) { (bool _lockFullBudget, uint8 _paymentTimeline) = LaborContract(_contract).getPaymentRequirements(_contractId); (_paymentValue, _depositValue) = _calculatePaymentAndDepositValuesForTaskPayment( DataParser.PaymentTimeline(_paymentTimeline), _lockFullBudget, _value ); } function _calculatePaymentAndDepositValuesForTaskPayment( DataParser.PaymentTimeline _paymentTimeline, bool _lockFullBudget, uint _value ) private pure returns ( uint _paymentValue, uint _depositValue ) { if (_paymentTimeline == DataParser.PaymentTimeline.PAY_BY_TASK) { _paymentValue = _value; if (!_lockFullBudget) { _depositValue = _value; } } } function completeTaskAndPay( bytes32 _contractId, address _contract, uint32 _taskId, uint _expireAtBlock, uint _salt, bytes _signature ) external payable onlyExistedContract(_contractId, _contract) onlyContractEmployer(_contractId, _contract) returns (uint) { require(_isTaskStarted(_contractId, _contract, _taskId), "WORKFLOW_INVALID_STATE"); _withdrawOnTaskCompletionConfirmationWithoutPenalties( _contractId, _contract, _taskId, _expireAtBlock, _salt, _signature ); _setTaskFinishedTime(_contractId, _contract, _taskId); _decrementUnspentUpfrontAmountForTask(_contractId, _contract, _taskId); _transitionTaskToState(_contractId, _contract, _taskId, TaskState.COMPLETED); return OK; } function completeTask( bytes32 _contractId, address _contract, uint32 _taskId ) external onlyExistedContract(_contractId, _contract) onlyContractAssignee(_contractId, _contract) returns (uint) { require(_isTaskStarted(_contractId, _contract, _taskId), "WORKFLOW_INVALID_STATE"); return _doCompleteTask(_contractId, _contract, _taskId); } function _doCompleteTask( bytes32 _contractId, address _contract, uint32 _taskId ) private returns (uint) { bytes32 _taskRecordKey = _getTaskRecordKey(_contractId, _contract, _taskId); /** 1. Update 'pausedAt' variable to prevent invalid time track */ // _startTrackingPause(_taskRecordKey); store.set(taskCompletedAtBlockStorage, _taskRecordKey, block.number); _transitionTaskToState(_contractId, _contract, _taskId, TaskState.COMPLETION_REQUESTED); LaborContract(_contract).onOperation(_contractId, "OP_TASK_COMPLETION_REQUEST"); return OK; } function confirmTaskCompletion( bytes32 _contractId, address _contract, uint32 _taskId, uint32[] _skippedPenalties, uint _expireAtBlock, uint _salt, bytes _signature ) external payable onlyExistedContract(_contractId, _contract) onlyContractEmployer(_contractId, _contract) returns (uint) { return _confirmTaskCompletion( _contractId, _contract, _taskId, _skippedPenalties, _expireAtBlock, _salt, _signature ); } function _confirmTaskCompletion( bytes32 _contractId, address _contract, uint32 _taskId, uint32[] _skippedPenalties, uint _expireAtBlock, uint _salt, bytes _signature ) private onlyTaskInState(_contractId, _contract, _taskId, TaskState.COMPLETION_REQUESTED) returns (uint) { _withdrawOnTaskCompletionConfirmation( _contractId, _contract, _taskId, _skippedPenalties, _expireAtBlock, _salt, _signature ); _setTaskFinishedTime(_contractId, _contract, _taskId); _decrementUnspentUpfrontAmountForTask(_contractId, _contract, _taskId); _transitionTaskToState(_contractId, _contract, _taskId, TaskState.COMPLETED); // TODO: could probably clean penalties if such exist, so it could save some gas return OK; } function _withdrawOnTaskCompletionConfirmation( bytes32 _contractId, address _contract, uint32 _taskId, uint32[] _skippedPenalties, uint _expireAtBlock, uint _salt, bytes _signature ) private { (uint _totalValue,, uint _depositValue, uint _paidValue) = getTaskCompletionReward(_contractId, _contract, _taskId, _skippedPenalties); LaborContract(_contract).onWithdrawOperation.value(msg.value) ( _contractId, "OP_TASK_COMPLETION", _depositValue, _totalValue.sub(_paidValue), _expireAtBlock, _salt, _signature ); store.set(taskBudgetPaidValueStorage, _getTaskRecordKey(_contractId, _contract, _taskId), _totalValue); } function _withdrawOnTaskCompletionConfirmationWithoutPenalties( bytes32 _contractId, address _contract, uint32 _taskId, uint _expireAtBlock, uint _salt, bytes _signature ) private { (uint _totalValue,, uint _depositValue, uint _paidValue) = getTaskCompletionRewardWithoutPenalties(_contractId, _contract, _taskId); LaborContract(_contract).onWithdrawOperation.value(msg.value) ( _contractId, "OP_TASK_COMPLETION", _depositValue, _totalValue.sub(_paidValue), _expireAtBlock, _salt, _signature ); store.set(taskBudgetPaidValueStorage, _getTaskRecordKey(_contractId, _contract, _taskId), _totalValue); } function declineTaskAndSendToRework( bytes32 _contractId, address _contract, uint32 _taskId, string /* _reason */ ) external onlyExistedContract(_contractId, _contract) onlyContractEmployer(_contractId, _contract) onlyTaskInState(_contractId, _contract, _taskId, TaskState.COMPLETION_REQUESTED) returns (uint) { return _doTaskRework(_contractId, _contract, _taskId); } function _doTaskRework( bytes32 _contractId, address _contract, uint32 _taskId ) private returns (uint) { // bytes32 _taskRecordKey = _getTaskRecordKey(_contractId, _contract, _taskId); // _updatePausedInterval(_taskRecordKey); _transitionTaskToState(_contractId, _contract, _taskId, TaskState.STARTED); LaborContract(_contract).onOperation(_contractId, "OP_TASK_REWORK"); return OK; } /** PRIVATE */ function _isUniqueTasks(DataParser.TaskData[] memory _tasks) private pure returns (bool) { uint32 _findId = 0; for (uint _idx = 0; _idx < _tasks.length; _idx++) { _findId = _tasks[_idx].id; for (uint _searchIdx = _idx + 1; _searchIdx < _tasks.length; _searchIdx++) { if (_tasks[_searchIdx].id == _findId) { return false; } } } return true; } function _getPenaltyValue( bytes32 _contractId, address _contract, uint32 _taskId, uint32[] _skippedPenalties, DataParser.PenaltyApplicationType _takeOnlyApplicationType ) private view returns (uint _penaltyValue) { bytes32 _taskRecordKey = _getTaskRecordKey(_contractId, _contract, _taskId); uint _penaltiesLength = store.count(penaltyIdsStorage, _taskRecordKey); for (uint _penaltyIdx = 0; _penaltyIdx < _penaltiesLength; ++_penaltyIdx) { uint32 _penaltyId = uint32(store.get(penaltyIdsStorage, _taskRecordKey, _penaltyIdx)); bytes32 _penaltyRecordKey = _getTaskPenaltyRecordKey(_contractId, _contract, _taskId, _penaltyId); if ( DataParser.PenaltyApplicationType(store.get(taskPenaltyApplicationTypeStorage, _penaltyRecordKey)) != _takeOnlyApplicationType || Arrays.contains(_skippedPenalties, _penaltyId) ) { continue; } _penaltyValue = _penaltyValue.add(_getPenaltyValueForRecord(_taskRecordKey, _penaltyRecordKey, _takeOnlyApplicationType)); } } function _getPenaltyValueForRecord( bytes32 _taskRecordKey, bytes32 _penaltyRecordKey, DataParser.PenaltyApplicationType _takeOnlyApplicationType ) private view returns (uint _result) { if (_takeOnlyApplicationType == DataParser.PenaltyApplicationType.MISS_DEADLINE && store.get(taskDeadlineStorage, _taskRecordKey) < block.timestamp ) { _result = _result.add(store.get(taskPenaltyValueStorage, _penaltyRecordKey)); } if (_takeOnlyApplicationType == DataParser.PenaltyApplicationType.LATE_PAYMENT) { uint _lastPaymentBlock = store.get(taskCompletedAtBlockStorage, _taskRecordKey).add( store.get(taskPenaltyPaymentWindowValueStorage, _penaltyRecordKey) ); if (_lastPaymentBlock < block.number) { _result = _result.add(store.get(taskPenaltyValueStorage, _penaltyRecordKey)); } } } function _getTaskState( bytes32 _contractId, address _contract, uint _taskId ) private view returns (TaskState) { return TaskState(store.get(taskStateStorage, _getTaskRecordKey(_contractId, _contract, _taskId))); } function _incrementUnspentUpfrontAmount( bytes32 _contractId, address _contract, uint _upfrontValue ) private { uint _unspentUpfrontAmount = store.get(tasksUnspentUpfrontStorage, _getContractRecordKey(_contractId, _contract)); _unspentUpfrontAmount = _unspentUpfrontAmount.add(_upfrontValue); store.set(tasksUnspentUpfrontStorage, _getContractRecordKey(_contractId, _contract), _unspentUpfrontAmount); } function _decrementUnspentUpfrontAmountForTask( bytes32 _contractId, address _contract, uint32 _taskId ) private { uint _unspentUpfrontAmount = store.get(tasksUnspentUpfrontStorage, _getContractRecordKey(_contractId, _contract)); uint _upfrontValue = _getUpfrontValue(_contractId, _contract, _taskId); _unspentUpfrontAmount = _unspentUpfrontAmount.sub(_upfrontValue); store.set(tasksUnspentUpfrontStorage, _getContractRecordKey(_contractId, _contract), _unspentUpfrontAmount); } function _updatePausedInterval(bytes32 _taskRecordKey) private { uint pausedAt = store.get(taskPausedAtStorage, _taskRecordKey); store.set(taskPausedForStorage, _taskRecordKey, block.timestamp - pausedAt); } function _setTaskFinishedTime(bytes32 _contractId, address _contract, uint32 _taskId) private { store.set(taskFinishedStorage, _getTaskRecordKey(_contractId, _contract, _taskId), block.timestamp); } function _startTrackingPause(bytes32 _taskRecordKey) private { store.set(taskPausedAtStorage, _taskRecordKey, block.timestamp); } function _transitionTaskToState( bytes32 _contractId, address _contract, uint _taskId, TaskState _newState ) private { bytes32 _taskRecordKey = _getTaskRecordKey(_contractId, _contract, _taskId); uint8 _previousState = store.get(taskStateStorage, _taskRecordKey); store.set(taskStateStorage, _taskRecordKey, uint8(_newState)); emit TaskStateChanged(_getContractRecordKey(_contractId, _contract), _contractId, _taskId, uint8(_newState), _previousState); } function _getTaskRecordKey( bytes32 _contractId, address _contract, uint _taskId ) private pure returns (bytes32) { return keccak256(abi.encodePacked(_contractId, _contract, _taskId)); } function _getContractRecordKey( bytes32 _contractId, address _contract ) private pure returns (bytes32) { return keccak256(abi.encodePacked(_contractId, _contract)); } function _getTaskPenaltyRecordKey( bytes32 _contractId, address _contract, uint _taskId, uint _taskPenaltyId ) private pure returns (bytes32) { return keccak256(abi.encodePacked(_getTaskRecordKey(_contractId, _contract, _taskId), _taskPenaltyId)); } function _getTotalValue( bytes32 _contractId, address _contract, uint32 _taskId ) private view returns (uint _totalValue) { _totalValue = store.get(taskBudgetLeftValueStorage, _getTaskRecordKey(_contractId, _contract, _taskId)); } function _getPaidValue( bytes32 _contractId, address _contract, uint32 _taskId ) private view returns (uint _paidValue) { _paidValue = store.get(taskBudgetPaidValueStorage, _getTaskRecordKey(_contractId, _contract, _taskId)); } function _getUpfrontValue( bytes32 _contractId, address _contract, uint32 _taskId ) private view returns (uint _upfrontValue) { _upfrontValue = store.get(taskUpfrontValueStorage, _getTaskRecordKey(_contractId, _contract, _taskId)); } function _includesState(TaskState _state, TaskState[] memory _statesArray) private pure returns (bool) { for (uint _idx = 0; _idx < _statesArray.length; ++_idx) { if (_state == _statesArray[_idx]) { return true; } } return false; } function _isTaskStarted( bytes32 _contractId, address _contract, uint _taskId ) private view returns (bool) { return _isStartedState(_getTaskState(_contractId, _contract, _taskId)); } function _isStartedState(TaskState _state) private pure returns (bool) { TaskState[] memory _statesArray = __makeTwoItemArray(TaskState.NOT_STARTED, TaskState.STARTED); return _includesState(_state, _statesArray); } function __makeTwoItemArray(TaskState _state1, TaskState _state2) private pure returns (TaskState[] memory _states) { _states = new TaskState[](2); _states[0] = _state1; _states[1] = _state2; } }