Skip to content

Commit

Permalink
Start private vars with leading underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Nov 22, 2023
1 parent 674b29d commit fb0e42b
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 80 deletions.
63 changes: 33 additions & 30 deletions contracts/DateTimeUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,77 +35,77 @@ type Timestamp is uint256;

using DateTimeUtils for Timestamp global;
using {
monthsLess as <,
monthsEqual as ==,
monthsGreater as >,
monthsAdd as +
_monthsLess as <,
_monthsEqual as ==,
_monthsGreater as >,
_monthsAdd as +
} for Months global;
using {
timestampLess as <,
timestampLessOrEqual as <=,
timestampEqual as ==
_timestampLess as <,
_timestampLessOrEqual as <=,
_timestampEqual as ==
} for Timestamp global;

function monthsLess(Months left, Months right) pure returns (bool result) {
function _monthsLess(Months left, Months right) pure returns (bool result) {
return Months.unwrap(left) < Months.unwrap(right);
}

function monthsEqual(Months a, Months b) pure returns (bool result) {
function _monthsEqual(Months a, Months b) pure returns (bool result) {
return !(a < b) && !(b < a);
}

function monthsGreater(Months left, Months right) pure returns (bool result) {
function _monthsGreater(Months left, Months right) pure returns (bool result) {
return !(left < right) && !(left == right);
}

function monthsAdd(Months a, Months b) pure returns (Months sum) {
function _monthsAdd(Months a, Months b) pure returns (Months sum) {
sum = Months.wrap(Months.unwrap(a) + Months.unwrap(b));
}

function timestampLessOrEqual(Timestamp left, Timestamp right) pure returns (bool result) {
function _timestampLessOrEqual(Timestamp left, Timestamp right) pure returns (bool result) {
return Timestamp.unwrap(left) <= Timestamp.unwrap(right);
}

function timestampLess(Timestamp left, Timestamp right) pure returns (bool result) {
function _timestampLess(Timestamp left, Timestamp right) pure returns (bool result) {
return Timestamp.unwrap(left) < Timestamp.unwrap(right);
}

function timestampEqual(Timestamp left, Timestamp right) pure returns (bool result) {
function _timestampEqual(Timestamp left, Timestamp right) pure returns (bool result) {
return Timestamp.unwrap(left) == Timestamp.unwrap(right);
}

library DateTimeUtils {
function timestamp() internal view returns (Timestamp timestampValue) {
function timestamp() public view returns (Timestamp timestampValue) {
return Timestamp.wrap(block.timestamp);
}

// Conversion functions

function day(uint256 untypedDay) internal pure returns (Day dayValue) {
function day(uint256 untypedDay) public pure returns (Day dayValue) {
return Day.wrap(untypedDay);
}

function months(uint256 untypedMonths) internal pure returns (Months monthsValue) {
function months(uint256 untypedMonths) public pure returns (Months monthsValue) {
return Months.wrap(untypedMonths);
}

// Untyped functions wrappers

function timestampToDate(Timestamp timestampValue) internal pure returns (Year _year, Month _month, Day dayValue) {
function timestampToDate(Timestamp timestampValue) public pure returns (Year _year, Month _month, Day dayValue) {
(uint256 untypedYear, uint256 untypedMonth, uint256 untypedDay) =
UntypedDateTime.timestampToDate(Timestamp.unwrap(timestampValue));
return (Year.wrap(untypedYear), Month.wrap(untypedMonth), Day.wrap(untypedDay));
}

function timestampFromDate(Year year, Month month, Day dayValue) internal pure returns (Timestamp timestampValue) {
function timestampFromDate(Year year, Month month, Day dayValue) public pure returns (Timestamp timestampValue) {
timestampValue = Timestamp.wrap(UntypedDateTime.timestampFromDate(
Year.unwrap(year),
Month.unwrap(month),
Day.unwrap(dayValue)
));
}

function addMonths(Timestamp timestampValue, Months monthsValue) internal pure returns (Timestamp newTimestamp) {
function addMonths(Timestamp timestampValue, Months monthsValue) public pure returns (Timestamp newTimestamp) {
newTimestamp = Timestamp.wrap(
UntypedDateTime.addMonths(
Timestamp.unwrap(timestampValue),
Expand All @@ -114,7 +114,7 @@ library DateTimeUtils {
);
}

function subSeconds(Timestamp timestampValue, Seconds secondsValue) internal pure returns (Timestamp newTimestamp) {
function subSeconds(Timestamp timestampValue, Seconds secondsValue) public pure returns (Timestamp newTimestamp) {
newTimestamp = Timestamp.wrap(
UntypedDateTime.subSeconds(
Timestamp.unwrap(timestampValue),
Expand All @@ -125,30 +125,33 @@ library DateTimeUtils {

// Operations

function add(Timestamp timestampValue, Months monthsValue) internal pure returns (Timestamp newTimestamp) {
newTimestamp = timestampValue.addMonths(monthsValue);
function add(Timestamp timestampValue, Months monthsValue) public pure returns (Timestamp newTimestamp) {
newTimestamp = addMonths(timestampValue, monthsValue);
}

function sub(Timestamp timestampValue, Seconds secondsValue) internal pure returns (Timestamp newTimestamp) {
newTimestamp = timestampValue.subSeconds(secondsValue);
function sub(Timestamp timestampValue, Seconds secondsValue) public pure returns (Timestamp newTimestamp) {
newTimestamp = subSeconds(timestampValue, secondsValue);
}

function duration(Timestamp from, Timestamp to) internal pure returns (Seconds difference) {
function duration(Timestamp from, Timestamp to) public pure returns (Seconds difference) {
difference = Seconds.wrap(Timestamp.unwrap(to) - Timestamp.unwrap(from));
}

// Auxiliary functions

function firstDayOfMonth(Timestamp timestampValue) internal pure returns (Timestamp newTimestamp) {
function firstDayOfMonth(Timestamp timestampValue) public pure returns (Timestamp newTimestamp) {
(Year year, Month month, ) = timestampToDate(timestampValue);
return timestampFromDate(year, month, day(1));
}

function nextMonth(Timestamp timestampValue) internal pure returns (Timestamp newTimestamp) {
return timestampValue.firstDayOfMonth().add(months(1));
function nextMonth(Timestamp timestampValue) public pure returns (Timestamp newTimestamp) {
return add(
firstDayOfMonth(timestampValue),
months(1)
);
}

function min(Timestamp a, Timestamp b) internal pure returns (Timestamp minimum) {
function min(Timestamp a, Timestamp b) public pure returns (Timestamp minimum) {
minimum = Timestamp.wrap(Math.min(Timestamp.unwrap(a), Timestamp.unwrap(b)));
}
}
20 changes: 10 additions & 10 deletions contracts/Sequence.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ library SequenceLibrary {
Timestamp nextTimestamp;
}

function add(Sequence storage sequence, Timestamp timestamp, uint256 value) internal {
function add(Sequence storage sequence, Timestamp timestamp, uint256 value) public {
uint256 length = sequence.ids.length();
if (length > 0) {
if (timestamp <= _getNodeByIndex(sequence, length - 1).timestamp) {
Expand All @@ -72,7 +72,7 @@ library SequenceLibrary {
Sequence storage sequence,
Timestamp timestamp
)
internal
public
view
returns (Iterator memory iterator)
{
Expand Down Expand Up @@ -115,7 +115,7 @@ library SequenceLibrary {
});
}

function getValue(Sequence storage sequence, Iterator memory iterator) internal view returns (uint256 value) {
function getValue(Sequence storage sequence, Iterator memory iterator) public view returns (uint256 value) {
if(iterator.idIndex == _EMPTY_ITERATOR_INDEX) {
return 0;
}
Expand All @@ -125,7 +125,7 @@ library SequenceLibrary {
return _getNodeByIndex(sequence, iterator.idIndex).value;
}

function getLastValue(Sequence storage sequence) internal view returns (uint256 lastValue) {
function getLastValue(Sequence storage sequence) public view returns (uint256 lastValue) {
uint256 length = sequence.ids.length();
if (length > 0) {
return _getNodeByIndex(sequence, length - 1).value;
Expand All @@ -134,16 +134,16 @@ library SequenceLibrary {
}
}

function step(Iterator memory iterator) internal pure returns (bool success) {
function step(Iterator memory iterator) public pure returns (bool success) {
success = hasNext(iterator);
iterator.idIndex += 1;
}

function hasNext(Iterator memory iterator) internal pure returns (bool exist) {
function hasNext(Iterator memory iterator) public pure returns (bool exist) {
return iterator.idIndex + 1 < iterator.sequenceSize;
}

function clear(Sequence storage sequence) internal {
function clear(Sequence storage sequence) public {
uint256 length = sequence.ids.length();
for (uint256 i = 0; i < length; ++i) {
Node storage node = _getNodeByIndex(sequence, i);
Expand All @@ -153,7 +153,7 @@ library SequenceLibrary {
sequence.freeNodeId = NodeId.wrap(0);
}

function clear(Sequence storage sequence, Timestamp before) internal {
function clear(Sequence storage sequence, Timestamp before) public {
// It's important to store the most right value
for (uint256 nodesAmount = sequence.ids.length(); nodesAmount > 1; --nodesAmount) {
if (before <= _getNodeByIndex(sequence, 0).timestamp) {
Expand All @@ -168,15 +168,15 @@ library SequenceLibrary {
// because current version fails on
// SequenceLibrary.NodeId.unwrap(value)
// TODO: remove the function after slither fix the issue
function unwrapNodeId(NodeId value) internal pure returns (uint256 unwrappedValue) {
function unwrapNodeId(NodeId value) public pure returns (uint256 unwrappedValue) {
return NodeId.unwrap(value);
}

// This function is a workaround to allow slither to analyze the code
// because current version fails on
// SequenceLibrary.NodeId.wrap(value)
// TODO: remove the function after slither fix the issue
function wrapNodeId(uint256 unwrappedValue) internal pure returns (NodeId wrappedValue) {
function wrapNodeId(uint256 unwrappedValue) public pure returns (NodeId wrappedValue) {
return NodeId.wrap(unwrappedValue);
}

Expand Down
12 changes: 6 additions & 6 deletions contracts/Timeline.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ library TimelineLibrary {
uint256 value;
}

function process(Timeline storage timeline, Timestamp until) internal {
function process(Timeline storage timeline, Timestamp until) public {
if (until <= timeline.processedUntil) {
return;
}
Expand All @@ -89,7 +89,7 @@ library TimelineLibrary {
timeline.processedUntil = until;
}

function getSum(Timeline storage timeline, Timestamp from, Timestamp to) internal view returns (uint256 sum) {
function getSum(Timeline storage timeline, Timestamp from, Timestamp to) public view returns (uint256 sum) {
_validateTimeInterval(timeline, from , to, true);
if (timeline.valuesQueue.empty()) {
return 0;
Expand All @@ -116,7 +116,7 @@ library TimelineLibrary {
}
}

function add(Timeline storage timeline, Timestamp from, Timestamp to, uint256 value) internal {
function add(Timeline storage timeline, Timestamp from, Timestamp to, uint256 value) public {
_validateTimeInterval(timeline, from , to, false);
Seconds duration = DateTimeUtils.duration(from, to);
uint256 rate = value / Seconds.unwrap(duration);
Expand All @@ -128,7 +128,7 @@ library TimelineLibrary {
_addChange(timeline, to, 0, rate + reminder);
}

function clear(Timeline storage timeline, Timestamp before) internal {
function clear(Timeline storage timeline, Timestamp before) public {
if (timeline.processedUntil < before) {
revert ClearUnprocessed();
}
Expand All @@ -146,15 +146,15 @@ library TimelineLibrary {
// because current version fails on
// TimelineLibrary.ValueId.unwrap(value)
// TODO: remove the function after slither fix the issue
function unwrapValueId(ValueId value) internal pure returns (bytes32 unwrappedValue) {
function unwrapValueId(ValueId value) public pure returns (bytes32 unwrappedValue) {
return ValueId.unwrap(value);
}

// This function is a workaround to allow slither to analyze the code
// because current version fails on
// TimelineLibrary.ValueId.wrap(value)
// TODO: remove the function after slither fix the issue
function wrapValueId(bytes32 unwrappedValue) internal pure returns (ValueId wrappedValue) {
function wrapValueId(bytes32 unwrappedValue) public pure returns (ValueId wrappedValue) {
return ValueId.wrap(unwrappedValue);
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/structs/Heap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ library HeapLibrary {
uint256[] values;
}

function add(Heap storage heap, uint256 value) internal {
function add(Heap storage heap, uint256 value) public {
if(heap.values.length == 0) {
heap.values.push(0);
}
Expand All @@ -44,15 +44,15 @@ library HeapLibrary {
_fixUp(heap, NodeId.wrap(heap.values.length - 1), value);
}

function get(Heap storage heap) internal view returns (uint256 minimum) {
function get(Heap storage heap) public view returns (uint256 minimum) {
if (heap.size > 0) {
return _getValue(heap, _ROOT);
} else {
revert AccessToEmptyHeap();
}
}

function pop(Heap storage heap) internal {
function pop(Heap storage heap) public {
if (heap.size > 0) {
uint256 lastValue = _getValue(heap, _getLastNode(heap));
--heap.size;
Expand Down
12 changes: 6 additions & 6 deletions contracts/structs/PriorityQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ library PriorityQueueLibrary {
mapping (uint256 => Value[]) values;
}

function push(PriorityQueue storage queue, uint256 priority, Value value) internal {
function push(PriorityQueue storage queue, uint256 priority, Value value) public {
if (queue.values[priority].length == 0) {
queue.priorities.add(priority);
}
queue.values[priority].push(value);
}

function empty(PriorityQueue storage queue) internal view returns (bool result) {
function empty(PriorityQueue storage queue) public view returns (bool result) {
return queue.priorities.size == 0;
}

function front(PriorityQueue storage queue) internal view returns (Value value) {
function front(PriorityQueue storage queue) public view returns (Value value) {
if (empty(queue)) {
revert AccessToEmptyPriorityQueue();
}
uint256 priority = queue.priorities.get();
return queue.values[priority][queue.values[priority].length - 1];
}

function pop(PriorityQueue storage queue) internal {
function pop(PriorityQueue storage queue) public {
if (empty(queue)) {
revert AccessToEmptyPriorityQueue();
}
Expand All @@ -71,15 +71,15 @@ library PriorityQueueLibrary {
// because current version fails on
// PriorityQueueLibrary.Value.unwrap(value)
// TODO: remove the function after slither fix the issue
function unwrapValue(Value value) internal pure returns (uint256 unwrappedValue) {
function unwrapValue(Value value) public pure returns (uint256 unwrappedValue) {
return Value.unwrap(value);
}

// This function is a workaround to allow slither to analyze the code
// because current version fails on
// PriorityQueueLibrary.Value.wrap(value)
// TODO: remove the function after slither fix the issue
function wrapValue(uint256 unwrappedValue) internal pure returns (Value wrappedValue) {
function wrapValue(uint256 unwrappedValue) public pure returns (Value wrappedValue) {
return Value.wrap(unwrappedValue);
}
}
Loading

0 comments on commit fb0e42b

Please sign in to comment.