Skip to content

Commit

Permalink
refactor(auction): unset schedule is null
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert authored and turadg committed May 15, 2023
1 parent b578f74 commit e893e0e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
9 changes: 5 additions & 4 deletions packages/inter-protocol/src/auction/scheduleMath.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,16 @@ harden(computeRoundTiming);
* auction has started, then it'll be nextSchedule.startTime. Otherwise, it's
* the start of the step following the current step.
*
* @param {import('./scheduler.js').Schedule | undefined} liveSchedule
* @param {import('./scheduler.js').Schedule | undefined} nextSchedule
* @param {import('./scheduler.js').Schedule | null} liveSchedule
* @param {import('./scheduler.js').Schedule | null} nextSchedule
* @param {Timestamp} now
* @returns {Timestamp | null}
*/
export const nextDescendingStepTime = (liveSchedule, nextSchedule, now) => {
nextSchedule || Fail`nextSchedule must always be defined`;

if (!liveSchedule) {
return nextSchedule?.startTime;
return nextSchedule?.startTime || null;
}

const { startTime, endTime, clockStep } = liveSchedule;
Expand All @@ -114,7 +115,7 @@ export const nextDescendingStepTime = (liveSchedule, nextSchedule, now) => {
const expectedNext = TimeMath.addAbsRel(lastStepStart, clockStep);

if (TimeMath.compareAbs(expectedNext, endTime) > 0) {
return nextSchedule?.startTime;
return nextSchedule?.startTime || null;
}

return expectedNext;
Expand Down
24 changes: 12 additions & 12 deletions packages/inter-protocol/src/auction/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ const makeCancelToken = makeCancelTokenMaker('scheduler');
/**
* @typedef {object} ScheduleNotification
*
* @property {Timestamp | undefined} activeStartTime start time of current
* @property {Timestamp | null} activeStartTime start time of current
* auction if auction is active
* @property {Timestamp | undefined} nextStartTime start time of next auction
* @property {Timestamp | undefined} nextDescendingStepTime when the next descending step
* @property {Timestamp | null} nextStartTime start time of next auction
* @property {Timestamp | null} nextDescendingStepTime when the next descending step
* will take place
*/

Expand Down Expand Up @@ -85,11 +85,11 @@ export const makeScheduler = async (
/**
* live version is defined when an auction is active.
*
* @type {Schedule | undefined}
* @type {Schedule | null}
*/
let liveSchedule;
let liveSchedule = null;

/** @returns {Promise<{ now: Timestamp, nextSchedule: Schedule | undefined }>} */
/** @returns {Promise<{ now: Timestamp, nextSchedule: Schedule | null }>} */
const initializeNextSchedule = async () => {
return E.when(
// XXX manualTimer returns a bigint, not a timeRecord.
Expand Down Expand Up @@ -120,8 +120,8 @@ export const makeScheduler = async (
*/
const publishSchedule = () => {
const sched = harden({
activeStartTime: liveSchedule?.startTime,
nextStartTime: nextSchedule?.startTime,
activeStartTime: liveSchedule?.startTime || null,
nextStartTime: nextSchedule?.startTime || null,
nextDescendingStepTime: nextDescendingStepTime(
liveSchedule,
nextSchedule,
Expand All @@ -132,7 +132,7 @@ export const makeScheduler = async (
};

/**
* @param {Schedule | undefined} schedule
* @param {Schedule | null} schedule
*/
const clockTick = schedule => {
trace('clockTick', schedule?.startTime, now);
Expand All @@ -157,7 +157,7 @@ export const makeScheduler = async (
nextSchedule = safelyComputeRoundTiming(params, afterNow);
}

liveSchedule = undefined;
liveSchedule = null;
return E(timer).cancel(stepCancelToken);
};

Expand Down Expand Up @@ -364,6 +364,6 @@ export const makeScheduler = async (

/**
* @typedef {object} FullSchedule
* @property {Schedule | undefined} nextAuctionSchedule
* @property {Schedule | undefined} liveAuctionSchedule
* @property {Schedule | null} nextAuctionSchedule
* @property {Schedule | null} liveAuctionSchedule
*/
8 changes: 4 additions & 4 deletions packages/inter-protocol/test/auction/test-auctionContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ test.serial('onDeadline exit, with chainStorage RPC snapshot', async t => {

const scheduleTracker = await driver.getScheduleTracker();
await scheduleTracker.assertInitial({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: TimeMath.coerceTimestampRecord(170n, timerBrand),
nextStartTime: TimeMath.coerceTimestampRecord(170n, timerBrand),
});
Expand Down Expand Up @@ -1002,7 +1002,7 @@ test.serial('onDeadline exit, with chainStorage RPC snapshot', async t => {

await driver.advanceTo(185n, 'wait');
await scheduleTracker.assertChange({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: { absValue: 210n },
});
await bookTracker.assertChange({
Expand Down Expand Up @@ -1064,7 +1064,7 @@ test.serial('add assets to open auction', async t => {
});
const scheduleTracker = await driver.getScheduleTracker();
await scheduleTracker.assertInitial({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: TimeMath.coerceTimestampRecord(170n, timerBrand),
nextStartTime: TimeMath.coerceTimestampRecord(170n, timerBrand),
});
Expand Down Expand Up @@ -1142,7 +1142,7 @@ test.serial('add assets to open auction', async t => {
});
await scheduleTracker.assertChange({
nextDescendingStepTime: { absValue: 210n },
activeStartTime: undefined,
activeStartTime: null,
});

// sellers split proceeds and refund 2:1
Expand Down
26 changes: 13 additions & 13 deletions packages/inter-protocol/test/auction/test-scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ test('schedule start to finish', async t => {
subscriber,
);
const schedule = scheduler.getSchedule();
t.deepEqual(schedule.liveAuctionSchedule, undefined);
t.deepEqual(schedule.liveAuctionSchedule, null);
const firstSchedule = {
startTime: TimeMath.coerceTimestampRecord(131n, timerBrand),
endTime: TimeMath.coerceTimestampRecord(135n, timerBrand),
Expand All @@ -100,7 +100,7 @@ test('schedule start to finish', async t => {
t.true(fakeAuctioneer.getState().lockedPrices);

await scheduleTracker.assertInitial({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: TimeMath.coerceTimestampRecord(131n, timerBrand),
nextStartTime: TimeMath.coerceTimestampRecord(131n, timerBrand),
});
Expand Down Expand Up @@ -156,7 +156,7 @@ test('schedule start to finish', async t => {
// Auction finished, nothing else happens
now = await timer.advanceTo(now + 1n);
await scheduleTracker.assertChange({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: { absValue: 141n },
});
now = await timer.advanceTo(now + 1n);
Expand All @@ -168,7 +168,7 @@ test('schedule start to finish', async t => {
t.deepEqual(fakeAuctioneer.getStartRounds(), [0]);

const finalSchedule = scheduler.getSchedule();
t.deepEqual(finalSchedule.liveAuctionSchedule, undefined);
t.deepEqual(finalSchedule.liveAuctionSchedule, null);
const secondSchedule = {
startTime: TimeMath.coerceTimestampRecord(141n, timerBrand),
endTime: TimeMath.coerceTimestampRecord(145n, timerBrand),
Expand All @@ -186,7 +186,7 @@ test('schedule start to finish', async t => {
nextStartTime: { absValue: 151n },
});

t.deepEqual(finalSchedule.liveAuctionSchedule, undefined);
t.deepEqual(finalSchedule.liveAuctionSchedule, null);
t.deepEqual(finalSchedule.nextAuctionSchedule, secondSchedule);

now = await timer.advanceTo(now + 1n);
Expand Down Expand Up @@ -236,7 +236,7 @@ test('schedule start to finish', async t => {
await timer.advanceTo(now + 1n);

await scheduleTracker.assertChange({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: { absValue: 151n },
});

Expand Down Expand Up @@ -660,7 +660,7 @@ test('duration = freq', async t => {
);

let schedule = scheduler.getSchedule();
t.deepEqual(schedule.liveAuctionSchedule, undefined);
t.deepEqual(schedule.liveAuctionSchedule, null);
const firstSchedule = {
startTime: TimeMath.coerceTimestampRecord(365n, timerBrand),
endTime: TimeMath.coerceTimestampRecord(665n, timerBrand),
Expand Down Expand Up @@ -751,7 +751,7 @@ test('change Schedule', async t => {
subscriber,
);
let schedule = scheduler.getSchedule();
t.is(schedule.liveAuctionSchedule, undefined);
t.is(schedule.liveAuctionSchedule, null);

const lockTime = 345n;
const startTime = 365n;
Expand Down Expand Up @@ -888,12 +888,12 @@ test('schedule anomalies', async t => {
);
const firstStart = baseTime + delay;
await scheduleTracker.assertInitial({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: timestamp(firstStart),
nextStartTime: TimeMath.coerceTimestampRecord(baseTime + delay, timerBrand),
});
const schedule = scheduler.getSchedule();
t.deepEqual(schedule.liveAuctionSchedule, undefined);
t.deepEqual(schedule.liveAuctionSchedule, null);

t.false(fakeAuctioneer.getState().lockedPrices);
// ////////////// LOCK TIME ///////////
Expand Down Expand Up @@ -964,7 +964,7 @@ test('schedule anomalies', async t => {
await timer.advanceTo(firstStart + 2n * step);
await eventLoopIteration();
await scheduleTracker.assertChange({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: { absValue: firstStart + oneCycle },
});

Expand Down Expand Up @@ -1023,7 +1023,7 @@ test('schedule anomalies', async t => {
// ////////////// DESCENDING STEP ///////////
now = await timer.advanceTo(secondStart + 2n * step);
await scheduleTracker.assertChange({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: { absValue: thirdStart },
});

Expand Down Expand Up @@ -1083,7 +1083,7 @@ test('schedule anomalies', async t => {
// This schedule is published as a side effect of closing out the incomplete
// auction. The next one follows immediately and is correct.
await scheduleTracker.assertChange({
activeStartTime: undefined,
activeStartTime: null,
nextDescendingStepTime: { absValue: 1700017230n },
});
const veryLateActual = veryLateStart + oneCycle + delay;
Expand Down

0 comments on commit e893e0e

Please sign in to comment.