Skip to content

Commit

Permalink
refactor: computeRoundTiming should not allow duration === frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Mar 5, 2023
1 parent 8d641af commit 42dd965
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
12 changes: 9 additions & 3 deletions packages/inter-protocol/src/auction/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,23 @@ export const computeRoundTiming = (params, baseTime) => {
Fail`startFrequency must exceed lock period, ${freq}, ${lockPeriod}`;

startingRate > lowestRate ||
Fail`startingRate ${startingRate} must be more than ${lowestRate}`;
Fail`startingRate ${startingRate} must be more than lowest: ${lowestRate}`;
const rateChange = subtract(startingRate, lowestRate);
const requestedSteps = floorDivide(rateChange, discountStep);
requestedSteps > 0n ||
Fail`discountStep ${discountStep} too large for requested rates`;
TimeMath.compareRel(freq, clockStep) >= 0 ||
Fail`clockStep ${clockStep} must be shorter than startFrequency ${freq} to allow >1 steps `;
Fail`clockStep ${TimeMath.relValue(
clockStep,
)} must be shorter than startFrequency ${TimeMath.relValue(
freq,
)} to allow at least one step down`;

const requestedDuration = TimeMath.multiplyRelNat(clockStep, requestedSteps);
const targetDuration =
TimeMath.compareRel(requestedDuration, freq) < 0 ? requestedDuration : freq;
TimeMath.compareRel(requestedDuration, freq) < 0
? requestedDuration
: TimeMath.subtractRelRel(freq, TimeMath.toRel(1n));
const steps = TimeMath.divideRelRel(targetDuration, clockStep);
const duration = TimeMath.multiplyRelNat(clockStep, steps);

Expand Down
12 changes: 6 additions & 6 deletions packages/inter-protocol/test/auction/test-computeRoundTiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ test(
100,
{
startTime: 3600 + 300,
endTime: 3600 + 6 * 10 * 60 + 300,
steps: 6n,
endRate: 10_500n - 6n * 10n,
endTime: 3600 + 5 * 10 * 60 + 300,
steps: 5n,
endRate: 10_500n - 5n * 10n,
startDelay: 300,
clockStep: 600,
},
Expand All @@ -192,9 +192,9 @@ test(
100,
{
startTime: 3600 + 300,
endTime: 3600 + 6 * 10 * 60 + 300,
steps: 6n,
endRate: 10_500n - 6n * 350n,
endTime: 3600 + 5 * 10 * 60 + 300,
steps: 5n,
endRate: 10_500n - 5n * 350n,
startDelay: 300,
clockStep: 600,
},
Expand Down
45 changes: 37 additions & 8 deletions packages/inter-protocol/test/auction/test-scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ test('lowest >= starting', async t => {
await t.throwsAsync(
() =>
makeScheduler(fakeAuctioneer, timer, paramManager, timer.getTimerBrand()),
{ message: '-5 is negative' },
{ message: /startingRate "\[105n]" must be more than lowest: "\[110n]"/ },
);
});

Expand Down Expand Up @@ -257,7 +257,10 @@ test('zero time for auction', async t => {
await t.throwsAsync(
() =>
makeScheduler(fakeAuctioneer, timer, paramManager, timer.getTimerBrand()),
{ message: /Frequency .* must exceed duration/ },
{
message:
/clockStep "\[3n]" must be shorter than startFrequency "\[2n]" to allow at least one step down/,
},
);
});

Expand Down Expand Up @@ -475,6 +478,8 @@ test('duration = freq', async t => {
const publisherKit = makePublisherFromFakes();

let defaultParams = makeDefaultParams(fakeInvitationPayment, timerBrand);
// start hourly, request 6 steps down every 10 minutes, so duration would be
// 1 hour. Instead cut the auction short.
defaultParams = {
...defaultParams,
priceLockPeriod: 20n,
Expand All @@ -501,11 +506,35 @@ test('duration = freq', async t => {
params2,
);

await t.throwsAsync(
() =>
makeScheduler(fakeAuctioneer, timer, paramManager, timer.getTimerBrand()),
{
message: /Frequency .* must exceed duration .*/,
},
const scheduler = await makeScheduler(
fakeAuctioneer,
timer,
paramManager,
timer.getTimerBrand(),
);
let schedule = scheduler.getSchedule();
t.deepEqual(schedule.liveAuctionSchedule, undefined);
const firstSchedule = {
startTime: TimeMath.toAbs(365n, timerBrand),
endTime: TimeMath.toAbs(665n, timerBrand),
steps: 5n,
endRate: 50n,
startDelay: TimeMath.toRel(5n, timerBrand),
clockStep: TimeMath.toRel(60n, timerBrand),
};
t.deepEqual(schedule.nextAuctionSchedule, firstSchedule);

await timer.advanceTo(725n);
schedule = scheduler.getSchedule();

// start the second auction on time
const secondSchedule = {
startTime: TimeMath.toAbs(725n, timerBrand),
endTime: TimeMath.toAbs(1025n, timerBrand),
steps: 5n,
endRate: 50n,
startDelay: TimeMath.toRel(5n, timerBrand),
clockStep: TimeMath.toRel(60n, timerBrand),
};
t.deepEqual(schedule.nextAuctionSchedule, secondSchedule);
});

0 comments on commit 42dd965

Please sign in to comment.