[style] Use _ for private and internal functions
Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
This commit is contained in:
parent
3bb077db7a
commit
b3855c4ba3
|
@ -17,44 +17,44 @@ abstract contract Collateral {
|
||||||
return balances[account];
|
return balances[account];
|
||||||
}
|
}
|
||||||
|
|
||||||
function add(address account, uint256 amount) private {
|
function _add(address account, uint256 amount) private {
|
||||||
balances[account] += amount;
|
balances[account] += amount;
|
||||||
funds.balance += amount;
|
funds.balance += amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
function subtract(address account, uint256 amount) private {
|
function _subtract(address account, uint256 amount) private {
|
||||||
balances[account] -= amount;
|
balances[account] -= amount;
|
||||||
funds.balance -= amount;
|
funds.balance -= amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
function transferFrom(address sender, uint256 amount) internal {
|
function _transferFrom(address sender, uint256 amount) internal {
|
||||||
address receiver = address(this);
|
address receiver = address(this);
|
||||||
require(token.transferFrom(sender, receiver, amount), "Transfer failed");
|
require(token.transferFrom(sender, receiver, amount), "Transfer failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
function deposit(uint256 amount) public collateralInvariant {
|
function deposit(uint256 amount) public collateralInvariant {
|
||||||
transferFrom(msg.sender, amount);
|
_transferFrom(msg.sender, amount);
|
||||||
funds.deposited += 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 {
|
function withdraw() public collateralInvariant {
|
||||||
require(isWithdrawAllowed(), "Account locked");
|
require(_isWithdrawAllowed(), "Account locked");
|
||||||
uint256 amount = balanceOf(msg.sender);
|
uint256 amount = balanceOf(msg.sender);
|
||||||
funds.withdrawn += amount;
|
funds.withdrawn += amount;
|
||||||
subtract(msg.sender, amount);
|
_subtract(msg.sender, amount);
|
||||||
assert(token.transfer(msg.sender, amount));
|
assert(token.transfer(msg.sender, amount));
|
||||||
}
|
}
|
||||||
|
|
||||||
function _slash(address account, uint256 percentage)
|
function _slash(
|
||||||
internal
|
address account,
|
||||||
collateralInvariant
|
uint256 percentage
|
||||||
{
|
) internal collateralInvariant {
|
||||||
uint256 amount = (balanceOf(account) * percentage) / 100;
|
uint256 amount = (balanceOf(account) * percentage) / 100;
|
||||||
funds.slashed += amount;
|
funds.slashed += amount;
|
||||||
subtract(account, amount);
|
_subtract(account, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
modifier collateralInvariant() {
|
modifier collateralInvariant() {
|
||||||
|
|
|
@ -41,8 +41,8 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
|
||||||
config = configuration;
|
config = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isWithdrawAllowed() internal view override returns (bool) {
|
function _isWithdrawAllowed() internal view override returns (bool) {
|
||||||
return !hasSlots(msg.sender);
|
return !_hasSlots(msg.sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
function requestStorage(
|
function requestStorage(
|
||||||
|
@ -56,12 +56,12 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
|
||||||
requests[id] = request;
|
requests[id] = request;
|
||||||
requestContexts[id].endsAt = block.timestamp + request.ask.duration;
|
requestContexts[id].endsAt = block.timestamp + request.ask.duration;
|
||||||
|
|
||||||
addToMyRequests(request.client, id);
|
_addToMyRequests(request.client, id);
|
||||||
|
|
||||||
uint256 amount = request.price();
|
uint256 amount = request.price();
|
||||||
funds.received += amount;
|
funds.received += amount;
|
||||||
funds.balance += amount;
|
funds.balance += amount;
|
||||||
transferFrom(msg.sender, amount);
|
_transferFrom(msg.sender, amount);
|
||||||
|
|
||||||
emit StorageRequested(id, request.ask);
|
emit StorageRequested(id, request.ask);
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
|
||||||
RequestContext storage context = requestContexts[requestId];
|
RequestContext storage context = requestContexts[requestId];
|
||||||
context.slotsFilled += 1;
|
context.slotsFilled += 1;
|
||||||
|
|
||||||
addToMySlots(slot.host, slotId);
|
_addToMySlots(slot.host, slotId);
|
||||||
|
|
||||||
emit SlotFilled(requestId, slotIndex, slotId);
|
emit SlotFilled(requestId, slotIndex, slotId);
|
||||||
if (context.slotsFilled == request.ask.slots) {
|
if (context.slotsFilled == request.ask.slots) {
|
||||||
|
@ -109,9 +109,9 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
|
||||||
SlotState state = slotState(slotId);
|
SlotState state = slotState(slotId);
|
||||||
require(state != SlotState.Paid, "Already paid");
|
require(state != SlotState.Paid, "Already paid");
|
||||||
if (state == SlotState.Finished) {
|
if (state == SlotState.Finished) {
|
||||||
payoutSlot(slot.requestId, slotId);
|
_payoutSlot(slot.requestId, slotId);
|
||||||
} else if (state == SlotState.Failed) {
|
} else if (state == SlotState.Failed) {
|
||||||
removeFromMySlots(msg.sender, slotId);
|
_removeFromMySlots(msg.sender, slotId);
|
||||||
} else if (state == SlotState.Filled) {
|
} else if (state == SlotState.Filled) {
|
||||||
_forciblyFreeSlot(slotId);
|
_forciblyFreeSlot(slotId);
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
|
||||||
// Slot collateral is not yet implemented as the design decision was
|
// Slot collateral is not yet implemented as the design decision was
|
||||||
// not finalised.
|
// not finalised.
|
||||||
|
|
||||||
removeFromMySlots(slot.host, slotId);
|
_removeFromMySlots(slot.host, slotId);
|
||||||
|
|
||||||
slot.state = SlotState.Free;
|
slot.state = SlotState.Free;
|
||||||
slot.host = address(0);
|
slot.host = address(0);
|
||||||
|
@ -167,17 +167,17 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function payoutSlot(
|
function _payoutSlot(
|
||||||
RequestId requestId,
|
RequestId requestId,
|
||||||
SlotId slotId
|
SlotId slotId
|
||||||
) private requestIsKnown(requestId) marketplaceInvariant {
|
) private requestIsKnown(requestId) marketplaceInvariant {
|
||||||
RequestContext storage context = requestContexts[requestId];
|
RequestContext storage context = requestContexts[requestId];
|
||||||
Request storage request = requests[requestId];
|
Request storage request = requests[requestId];
|
||||||
context.state = RequestState.Finished;
|
context.state = RequestState.Finished;
|
||||||
removeFromMyRequests(request.client, requestId);
|
_removeFromMyRequests(request.client, requestId);
|
||||||
Slot storage slot = slots[slotId];
|
Slot storage slot = slots[slotId];
|
||||||
|
|
||||||
removeFromMySlots(slot.host, slotId);
|
_removeFromMySlots(slot.host, slotId);
|
||||||
|
|
||||||
uint256 amount = requests[requestId].pricePerSlot();
|
uint256 amount = requests[requestId].pricePerSlot();
|
||||||
funds.sent += amount;
|
funds.sent += amount;
|
||||||
|
@ -199,7 +199,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
|
||||||
// Update request state to Cancelled. Handle in the withdraw transaction
|
// 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
|
// as there needs to be someone to pay for the gas to update the state
|
||||||
context.state = RequestState.Cancelled;
|
context.state = RequestState.Cancelled;
|
||||||
removeFromMyRequests(request.client, requestId);
|
_removeFromMyRequests(request.client, requestId);
|
||||||
|
|
||||||
emit RequestCancelled(requestId);
|
emit RequestCancelled(requestId);
|
||||||
|
|
||||||
|
|
|
@ -10,31 +10,31 @@ contract Periods {
|
||||||
secondsPerPeriod = _secondsPerPeriod;
|
secondsPerPeriod = _secondsPerPeriod;
|
||||||
}
|
}
|
||||||
|
|
||||||
function periodOf(uint256 timestamp) internal view returns (Period) {
|
function _periodOf(uint256 timestamp) internal view returns (Period) {
|
||||||
return Period.wrap(timestamp / secondsPerPeriod);
|
return Period.wrap(timestamp / secondsPerPeriod);
|
||||||
}
|
}
|
||||||
|
|
||||||
function blockPeriod() internal view returns (Period) {
|
function _blockPeriod() internal view returns (Period) {
|
||||||
return periodOf(block.timestamp);
|
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);
|
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;
|
return Period.unwrap(period) * secondsPerPeriod;
|
||||||
}
|
}
|
||||||
|
|
||||||
function periodEnd(Period period) internal view returns (uint256) {
|
function _periodEnd(Period period) internal view returns (uint256) {
|
||||||
return periodStart(nextPeriod(period));
|
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);
|
return Period.unwrap(a) < Period.unwrap(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAfter(Period a, Period b) internal pure returns (bool) {
|
function _isAfter(Period a, Period b) internal pure returns (bool) {
|
||||||
return isBefore(b, a);
|
return _isBefore(b, a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ abstract contract Proofs is Periods {
|
||||||
probabilities[id] = probability;
|
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 blockNumber = block.number % 256;
|
||||||
uint256 periodNumber = Period.unwrap(period) % 256;
|
uint256 periodNumber = Period.unwrap(period) % 256;
|
||||||
uint256 idOffset = uint256(SlotId.unwrap(id)) % 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) {
|
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);
|
bytes32 hash = blockhash(block.number - 1 - pointer);
|
||||||
assert(uint256(hash) != 0);
|
assert(uint256(hash) != 0);
|
||||||
return keccak256(abi.encode(hash));
|
return keccak256(abi.encode(hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getChallenge(
|
function _getChallenge(
|
||||||
SlotId id,
|
SlotId id,
|
||||||
Period period
|
Period period
|
||||||
) internal view returns (bytes32) {
|
) internal view returns (bytes32) {
|
||||||
return getChallenge(getPointer(id, period));
|
return _getChallenge(_getPointer(id, period));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getChallenge(SlotId id) public view returns (bytes32) {
|
function getChallenge(SlotId id) public view returns (bytes32) {
|
||||||
return getChallenge(id, blockPeriod());
|
return _getChallenge(id, _blockPeriod());
|
||||||
}
|
}
|
||||||
|
|
||||||
function _getProofRequirement(
|
function _getProofRequirement(
|
||||||
|
@ -64,17 +64,17 @@ abstract contract Proofs is Periods {
|
||||||
Period period
|
Period period
|
||||||
) internal view returns (bool isRequired, uint8 pointer) {
|
) internal view returns (bool isRequired, uint8 pointer) {
|
||||||
SlotState state = slotState(id);
|
SlotState state = slotState(id);
|
||||||
Period start = periodOf(slotStarts[id]);
|
Period start = _periodOf(slotStarts[id]);
|
||||||
if (state != SlotState.Filled || !isAfter(period, start)) {
|
if (state != SlotState.Filled || !_isAfter(period, start)) {
|
||||||
return (false, 0);
|
return (false, 0);
|
||||||
}
|
}
|
||||||
pointer = getPointer(id, period);
|
pointer = _getPointer(id, period);
|
||||||
bytes32 challenge = getChallenge(pointer);
|
bytes32 challenge = _getChallenge(pointer);
|
||||||
uint256 probability = (probabilities[id] * (256 - config.downtime)) / 256;
|
uint256 probability = (probabilities[id] * (256 - config.downtime)) / 256;
|
||||||
isRequired = uint256(challenge) % probability == 0;
|
isRequired = uint256(challenge) % probability == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isProofRequired(
|
function _isProofRequired(
|
||||||
SlotId id,
|
SlotId id,
|
||||||
Period period
|
Period period
|
||||||
) internal view returns (bool) {
|
) internal view returns (bool) {
|
||||||
|
@ -85,29 +85,29 @@ abstract contract Proofs is Periods {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isProofRequired(SlotId id) public view returns (bool) {
|
function isProofRequired(SlotId id) public view returns (bool) {
|
||||||
return isProofRequired(id, blockPeriod());
|
return _isProofRequired(id, _blockPeriod());
|
||||||
}
|
}
|
||||||
|
|
||||||
function willProofBeRequired(SlotId id) public view returns (bool) {
|
function willProofBeRequired(SlotId id) public view returns (bool) {
|
||||||
bool isRequired;
|
bool isRequired;
|
||||||
uint8 pointer;
|
uint8 pointer;
|
||||||
(isRequired, pointer) = _getProofRequirement(id, blockPeriod());
|
(isRequired, pointer) = _getProofRequirement(id, _blockPeriod());
|
||||||
return isRequired && pointer < config.downtime;
|
return isRequired && pointer < config.downtime;
|
||||||
}
|
}
|
||||||
|
|
||||||
function submitProof(SlotId id, bytes calldata proof) public {
|
function submitProof(SlotId id, bytes calldata proof) public {
|
||||||
require(proof.length > 0, "Invalid proof"); // TODO: replace by actual check
|
require(proof.length > 0, "Invalid proof"); // TODO: replace by actual check
|
||||||
require(!received[id][blockPeriod()], "Proof already submitted");
|
require(!received[id][_blockPeriod()], "Proof already submitted");
|
||||||
received[id][blockPeriod()] = true;
|
received[id][_blockPeriod()] = true;
|
||||||
emit ProofSubmitted(id, proof);
|
emit ProofSubmitted(id, proof);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _markProofAsMissing(SlotId id, Period missedPeriod) internal {
|
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(end < block.timestamp, "Period has not ended yet");
|
||||||
require(block.timestamp < end + config.timeout, "Validation timed out");
|
require(block.timestamp < end + config.timeout, "Validation timed out");
|
||||||
require(!received[id][missedPeriod], "Proof was submitted, not missing");
|
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");
|
require(!missing[id][missedPeriod], "Proof already marked as missing");
|
||||||
missing[id][missedPeriod] = true;
|
missing[id][missedPeriod] = true;
|
||||||
missed[id] += 1;
|
missed[id] += 1;
|
||||||
|
|
|
@ -19,23 +19,23 @@ contract StateRetrieval {
|
||||||
return slotsPerHost[msg.sender].values().toSlotIds();
|
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;
|
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));
|
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));
|
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));
|
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));
|
slotsPerHost[host].remove(SlotId.unwrap(slotId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ contract TestCollateral is Collateral {
|
||||||
_slash(account, percentage);
|
_slash(account, percentage);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isWithdrawAllowed() internal pure override returns (bool) {
|
function _isWithdrawAllowed() internal pure override returns (bool) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue