Skip to content

Commit

Permalink
Merge pull request #176 from skalenetwork/hotfix/SKALE-2223-skip-time
Browse files Browse the repository at this point in the history
Hotfix/skale 2223 skip time
  • Loading branch information
DimaStebaev authored Apr 22, 2020
2 parents 986ec39 + 9618e7b commit 148e99e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 17 deletions.
22 changes: 11 additions & 11 deletions contracts/delegation/TimeHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract TimeHelpers {

uint constant ZERO_YEAR = 2020;

function calculateProofOfUseLockEndTime(uint month, uint lockUpPeriodDays) external pure returns (uint timestamp) {
function calculateProofOfUseLockEndTime(uint month, uint lockUpPeriodDays) external view returns (uint timestamp) {
timestamp = BokkyPooBahsDateTimeLibrary.addDays(monthToTimestamp(month), lockUpPeriodDays);
}

Expand All @@ -41,16 +41,7 @@ contract TimeHelpers {
return timestampToMonth(now);
}

function monthToTimestamp(uint _month) public pure returns (uint timestamp) {
uint year = ZERO_YEAR;
uint month = _month;
year = year.add(month.div(12));
month = month.mod(12);
month = month.add(1);
return BokkyPooBahsDateTimeLibrary.timestampFromDate(year, month, 1);
}

function timestampToMonth(uint timestamp) public pure returns (uint) {
function timestampToMonth(uint timestamp) public view returns (uint) {
uint year;
uint month;
(year, month, ) = BokkyPooBahsDateTimeLibrary.timestampToDate(timestamp);
Expand All @@ -59,4 +50,13 @@ contract TimeHelpers {
require(month > 0, "Timestamp is too far in the past");
return month;
}

function monthToTimestamp(uint _month) public view returns (uint timestamp) {
uint year = ZERO_YEAR;
uint month = _month;
year = year.add(month.div(12));
month = month.mod(12);
month = month.add(1);
return BokkyPooBahsDateTimeLibrary.timestampFromDate(year, month, 1);
}
}
82 changes: 76 additions & 6 deletions contracts/test/TimeHelpersWithDebug.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,89 @@ import "../delegation/TimeHelpers.sol";


contract TimeHelpersWithDebug is TimeHelpers, Ownable {
uint timeShift;

function getCurrentMonth() external view returns (uint) {
return timestampToMonth(now.add(timeShift));
struct TimeShift {
uint pointInTime;
uint shift;
}

TimeShift[] timeShift;

function skipTime(uint sec) external onlyOwner {
timeShift = timeShift.add(sec);
if (timeShift.length > 0) {
timeShift.push(TimeShift({pointInTime: now, shift: timeShift[timeShift.length - 1].shift.add(sec)}));
} else {
timeShift.push(TimeShift({pointInTime: now, shift: sec}));
}
}

function initialize() external initializer {
Ownable.initialize(msg.sender);
timeShift = 0;
}

function timestampToMonth(uint timestamp) public view returns (uint) {
return super.timestampToMonth(timestamp.add(getTimeShift(timestamp)));
}

function monthToTimestamp(uint _month) public view returns (uint) {
uint shiftedTimestamp = super.monthToTimestamp(_month);
if (timeShift.length > 0) {
uint lastTimeShiftIndex = timeShift.length - 1;
if (timeShift[lastTimeShiftIndex].pointInTime.add(timeShift[lastTimeShiftIndex].shift) < shiftedTimestamp) {
if (timeShift[0].pointInTime.add(timeShift[0].shift) < shiftedTimestamp) {
if (shiftedTimestamp < timeShift[0].pointInTime) {
return shiftedTimestamp;
} else {
return timeShift[0].pointInTime;
}
} else {
uint left = 0;
uint right = lastTimeShiftIndex;
while (left + 1 < right) {
uint middle = left.add(right).div(2);
if (timeShift[middle].pointInTime.add(timeShift[middle].shift) < shiftedTimestamp) {
right = middle;
} else {
left = middle;
}
}
if (shiftedTimestamp < timeShift[right].pointInTime + timeShift[left].shift) {
return shiftedTimestamp.sub(timeShift[left].shift);
} else {
return timeShift[right].pointInTime;
}
}
} else {
return shiftedTimestamp.sub(timeShift[lastTimeShiftIndex].shift);
}
} else {
return shiftedTimestamp;
}
}

// private

function getTimeShift(uint timestamp) internal view returns (uint) {
if (timeShift.length > 0) {
if (timestamp < timeShift[0].pointInTime) {
return 0;
} else if (timestamp >= timeShift[timeShift.length - 1].pointInTime) {
return timeShift[timeShift.length - 1].shift;
} else {
uint left = 0;
uint right = timeShift.length - 1;
while (left + 1 < right) {
uint middle = left.add(right).div(2);
if (timestamp < timeShift[middle].pointInTime) {
right = middle;
} else {
left = middle;
}
}
return timeShift[left].shift;
}
} else {
return 0;
}
}

}
6 changes: 6 additions & 0 deletions test/delegation/DelegationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ contract("DelegationController", ([owner, holder1, holder2, validator, validator

await timeHelpersWithDebug.skipTime(month * delegationPeriod);
(await delegationController.getState(delegationId)).toNumber().should.be.equal(State.COMPLETED);

// skipTime should now affect new delegations
const { logs } = await delegationController.delegate(
validatorId, amount, delegationPeriod, info, {from: holder1});
delegationId = logs[0].args.delegationId;
(await delegationController.getState(delegationId)).toNumber().should.be.equal(State.PROPOSED);
});

describe("when delegation is accepted", async () => {
Expand Down

0 comments on commit 148e99e

Please sign in to comment.