From b3855c4ba3909470af7af9001fad8db088c00cc0 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 19 Jan 2023 16:47:29 +0100 Subject: [PATCH] [style] Use _ for private and internal functions Co-authored-by: Eric Mastro --- contracts/Collateral.sol | 26 +++++++++++++------------- contracts/Marketplace.sol | 24 ++++++++++++------------ contracts/Periods.sol | 20 ++++++++++---------- contracts/Proofs.sol | 34 +++++++++++++++++----------------- contracts/StateRetrieval.sol | 10 +++++----- contracts/TestCollateral.sol | 2 +- 6 files changed, 58 insertions(+), 58 deletions(-) diff --git a/contracts/Collateral.sol b/contracts/Collateral.sol index 7f23e8c..4400ba2 100644 --- a/contracts/Collateral.sol +++ b/contracts/Collateral.sol @@ -17,44 +17,44 @@ abstract contract Collateral { return balances[account]; } - function add(address account, uint256 amount) private { + function _add(address account, uint256 amount) private { balances[account] += amount; funds.balance += amount; } - function subtract(address account, uint256 amount) private { + function _subtract(address account, uint256 amount) private { balances[account] -= amount; funds.balance -= amount; } - function transferFrom(address sender, uint256 amount) internal { + function _transferFrom(address sender, uint256 amount) internal { address receiver = address(this); require(token.transferFrom(sender, receiver, amount), "Transfer failed"); } function deposit(uint256 amount) public collateralInvariant { - transferFrom(msg.sender, amount); + _transferFrom(msg.sender, amount); funds.deposited += amount; - add(msg.sender, amount); + _add(msg.sender, amount); } - function isWithdrawAllowed() internal virtual returns (bool); + function _isWithdrawAllowed() internal virtual returns (bool); function withdraw() public collateralInvariant { - require(isWithdrawAllowed(), "Account locked"); + require(_isWithdrawAllowed(), "Account locked"); uint256 amount = balanceOf(msg.sender); funds.withdrawn += amount; - subtract(msg.sender, amount); + _subtract(msg.sender, amount); assert(token.transfer(msg.sender, amount)); } - function _slash(address account, uint256 percentage) - internal - collateralInvariant - { + function _slash( + address account, + uint256 percentage + ) internal collateralInvariant { uint256 amount = (balanceOf(account) * percentage) / 100; funds.slashed += amount; - subtract(account, amount); + _subtract(account, amount); } modifier collateralInvariant() { diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index 3957e60..f957b59 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -41,8 +41,8 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { config = configuration; } - function isWithdrawAllowed() internal view override returns (bool) { - return !hasSlots(msg.sender); + function _isWithdrawAllowed() internal view override returns (bool) { + return !_hasSlots(msg.sender); } function requestStorage( @@ -56,12 +56,12 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { requests[id] = request; requestContexts[id].endsAt = block.timestamp + request.ask.duration; - addToMyRequests(request.client, id); + _addToMyRequests(request.client, id); uint256 amount = request.price(); funds.received += amount; funds.balance += amount; - transferFrom(msg.sender, amount); + _transferFrom(msg.sender, amount); emit StorageRequested(id, request.ask); } @@ -93,7 +93,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { RequestContext storage context = requestContexts[requestId]; context.slotsFilled += 1; - addToMySlots(slot.host, slotId); + _addToMySlots(slot.host, slotId); emit SlotFilled(requestId, slotIndex, slotId); if (context.slotsFilled == request.ask.slots) { @@ -109,9 +109,9 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { SlotState state = slotState(slotId); require(state != SlotState.Paid, "Already paid"); if (state == SlotState.Finished) { - payoutSlot(slot.requestId, slotId); + _payoutSlot(slot.requestId, slotId); } else if (state == SlotState.Failed) { - removeFromMySlots(msg.sender, slotId); + _removeFromMySlots(msg.sender, slotId); } else if (state == SlotState.Filled) { _forciblyFreeSlot(slotId); } @@ -143,7 +143,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { // Slot collateral is not yet implemented as the design decision was // not finalised. - removeFromMySlots(slot.host, slotId); + _removeFromMySlots(slot.host, slotId); slot.state = SlotState.Free; slot.host = address(0); @@ -167,17 +167,17 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { } } - function payoutSlot( + function _payoutSlot( RequestId requestId, SlotId slotId ) private requestIsKnown(requestId) marketplaceInvariant { RequestContext storage context = requestContexts[requestId]; Request storage request = requests[requestId]; context.state = RequestState.Finished; - removeFromMyRequests(request.client, requestId); + _removeFromMyRequests(request.client, requestId); Slot storage slot = slots[slotId]; - removeFromMySlots(slot.host, slotId); + _removeFromMySlots(slot.host, slotId); uint256 amount = requests[requestId].pricePerSlot(); funds.sent += amount; @@ -199,7 +199,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { // Update request state to Cancelled. Handle in the withdraw transaction // as there needs to be someone to pay for the gas to update the state context.state = RequestState.Cancelled; - removeFromMyRequests(request.client, requestId); + _removeFromMyRequests(request.client, requestId); emit RequestCancelled(requestId); diff --git a/contracts/Periods.sol b/contracts/Periods.sol index 144f9f8..e380e8f 100644 --- a/contracts/Periods.sol +++ b/contracts/Periods.sol @@ -10,31 +10,31 @@ contract Periods { secondsPerPeriod = _secondsPerPeriod; } - function periodOf(uint256 timestamp) internal view returns (Period) { + function _periodOf(uint256 timestamp) internal view returns (Period) { return Period.wrap(timestamp / secondsPerPeriod); } - function blockPeriod() internal view returns (Period) { - return periodOf(block.timestamp); + function _blockPeriod() internal view returns (Period) { + return _periodOf(block.timestamp); } - function nextPeriod(Period period) internal pure returns (Period) { + function _nextPeriod(Period period) internal pure returns (Period) { return Period.wrap(Period.unwrap(period) + 1); } - function periodStart(Period period) internal view returns (uint256) { + function _periodStart(Period period) internal view returns (uint256) { return Period.unwrap(period) * secondsPerPeriod; } - function periodEnd(Period period) internal view returns (uint256) { - return periodStart(nextPeriod(period)); + function _periodEnd(Period period) internal view returns (uint256) { + return _periodStart(_nextPeriod(period)); } - function isBefore(Period a, Period b) internal pure returns (bool) { + function _isBefore(Period a, Period b) internal pure returns (bool) { return Period.unwrap(a) < Period.unwrap(b); } - function isAfter(Period a, Period b) internal pure returns (bool) { - return isBefore(b, a); + function _isAfter(Period a, Period b) internal pure returns (bool) { + return _isBefore(b, a); } } diff --git a/contracts/Proofs.sol b/contracts/Proofs.sol index 8f7339e..b13ef41 100644 --- a/contracts/Proofs.sol +++ b/contracts/Proofs.sol @@ -30,7 +30,7 @@ abstract contract Proofs is Periods { probabilities[id] = probability; } - function getPointer(SlotId id, Period period) internal view returns (uint8) { + function _getPointer(SlotId id, Period period) internal view returns (uint8) { uint256 blockNumber = block.number % 256; uint256 periodNumber = Period.unwrap(period) % 256; uint256 idOffset = uint256(SlotId.unwrap(id)) % 256; @@ -39,24 +39,24 @@ abstract contract Proofs is Periods { } function getPointer(SlotId id) public view returns (uint8) { - return getPointer(id, blockPeriod()); + return _getPointer(id, _blockPeriod()); } - function getChallenge(uint8 pointer) internal view returns (bytes32) { + function _getChallenge(uint8 pointer) internal view returns (bytes32) { bytes32 hash = blockhash(block.number - 1 - pointer); assert(uint256(hash) != 0); return keccak256(abi.encode(hash)); } - function getChallenge( + function _getChallenge( SlotId id, Period period ) internal view returns (bytes32) { - return getChallenge(getPointer(id, period)); + return _getChallenge(_getPointer(id, period)); } function getChallenge(SlotId id) public view returns (bytes32) { - return getChallenge(id, blockPeriod()); + return _getChallenge(id, _blockPeriod()); } function _getProofRequirement( @@ -64,17 +64,17 @@ abstract contract Proofs is Periods { Period period ) internal view returns (bool isRequired, uint8 pointer) { SlotState state = slotState(id); - Period start = periodOf(slotStarts[id]); - if (state != SlotState.Filled || !isAfter(period, start)) { + Period start = _periodOf(slotStarts[id]); + if (state != SlotState.Filled || !_isAfter(period, start)) { return (false, 0); } - pointer = getPointer(id, period); - bytes32 challenge = getChallenge(pointer); + pointer = _getPointer(id, period); + bytes32 challenge = _getChallenge(pointer); uint256 probability = (probabilities[id] * (256 - config.downtime)) / 256; isRequired = uint256(challenge) % probability == 0; } - function isProofRequired( + function _isProofRequired( SlotId id, Period period ) internal view returns (bool) { @@ -85,29 +85,29 @@ abstract contract Proofs is Periods { } function isProofRequired(SlotId id) public view returns (bool) { - return isProofRequired(id, blockPeriod()); + return _isProofRequired(id, _blockPeriod()); } function willProofBeRequired(SlotId id) public view returns (bool) { bool isRequired; uint8 pointer; - (isRequired, pointer) = _getProofRequirement(id, blockPeriod()); + (isRequired, pointer) = _getProofRequirement(id, _blockPeriod()); return isRequired && pointer < config.downtime; } function submitProof(SlotId id, bytes calldata proof) public { require(proof.length > 0, "Invalid proof"); // TODO: replace by actual check - require(!received[id][blockPeriod()], "Proof already submitted"); - received[id][blockPeriod()] = true; + require(!received[id][_blockPeriod()], "Proof already submitted"); + received[id][_blockPeriod()] = true; emit ProofSubmitted(id, proof); } function _markProofAsMissing(SlotId id, Period missedPeriod) internal { - uint256 end = periodEnd(missedPeriod); + uint256 end = _periodEnd(missedPeriod); require(end < block.timestamp, "Period has not ended yet"); require(block.timestamp < end + config.timeout, "Validation timed out"); require(!received[id][missedPeriod], "Proof was submitted, not missing"); - require(isProofRequired(id, missedPeriod), "Proof was not required"); + require(_isProofRequired(id, missedPeriod), "Proof was not required"); require(!missing[id][missedPeriod], "Proof already marked as missing"); missing[id][missedPeriod] = true; missed[id] += 1; diff --git a/contracts/StateRetrieval.sol b/contracts/StateRetrieval.sol index 8204e2f..34b4ab4 100644 --- a/contracts/StateRetrieval.sol +++ b/contracts/StateRetrieval.sol @@ -19,23 +19,23 @@ contract StateRetrieval { return slotsPerHost[msg.sender].values().toSlotIds(); } - function hasSlots(address host) internal view returns (bool) { + function _hasSlots(address host) internal view returns (bool) { return slotsPerHost[host].length() > 0; } - function addToMyRequests(address client, RequestId requestId) internal { + function _addToMyRequests(address client, RequestId requestId) internal { requestsPerClient[client].add(RequestId.unwrap(requestId)); } - function addToMySlots(address host, SlotId slotId) internal { + function _addToMySlots(address host, SlotId slotId) internal { slotsPerHost[host].add(SlotId.unwrap(slotId)); } - function removeFromMyRequests(address client, RequestId requestId) internal { + function _removeFromMyRequests(address client, RequestId requestId) internal { requestsPerClient[client].remove(RequestId.unwrap(requestId)); } - function removeFromMySlots(address host, SlotId slotId) internal { + function _removeFromMySlots(address host, SlotId slotId) internal { slotsPerHost[host].remove(SlotId.unwrap(slotId)); } } diff --git a/contracts/TestCollateral.sol b/contracts/TestCollateral.sol index e5c09b8..becd612 100644 --- a/contracts/TestCollateral.sol +++ b/contracts/TestCollateral.sol @@ -12,7 +12,7 @@ contract TestCollateral is Collateral { _slash(account, percentage); } - function isWithdrawAllowed() internal pure override returns (bool) { + function _isWithdrawAllowed() internal pure override returns (bool) { return true; } }