From e893e0e6f1c1a30e6192417903783991eea4584a Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Mon, 15 May 2023 11:37:23 -0700 Subject: [PATCH] refactor(auction): unset schedule is null --- .../src/auction/scheduleMath.js | 9 ++++--- .../inter-protocol/src/auction/scheduler.js | 24 ++++++++--------- .../test/auction/test-auctionContract.js | 8 +++--- .../test/auction/test-scheduler.js | 26 +++++++++---------- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/packages/inter-protocol/src/auction/scheduleMath.js b/packages/inter-protocol/src/auction/scheduleMath.js index ec0d46e132e..f8d7ea9e86f 100644 --- a/packages/inter-protocol/src/auction/scheduleMath.js +++ b/packages/inter-protocol/src/auction/scheduleMath.js @@ -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; @@ -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; diff --git a/packages/inter-protocol/src/auction/scheduler.js b/packages/inter-protocol/src/auction/scheduler.js index 31670b3c206..d98b78425be 100644 --- a/packages/inter-protocol/src/auction/scheduler.js +++ b/packages/inter-protocol/src/auction/scheduler.js @@ -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 */ @@ -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. @@ -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, @@ -132,7 +132,7 @@ export const makeScheduler = async ( }; /** - * @param {Schedule | undefined} schedule + * @param {Schedule | null} schedule */ const clockTick = schedule => { trace('clockTick', schedule?.startTime, now); @@ -157,7 +157,7 @@ export const makeScheduler = async ( nextSchedule = safelyComputeRoundTiming(params, afterNow); } - liveSchedule = undefined; + liveSchedule = null; return E(timer).cancel(stepCancelToken); }; @@ -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 */ diff --git a/packages/inter-protocol/test/auction/test-auctionContract.js b/packages/inter-protocol/test/auction/test-auctionContract.js index c51957cb078..50046c13725 100644 --- a/packages/inter-protocol/test/auction/test-auctionContract.js +++ b/packages/inter-protocol/test/auction/test-auctionContract.js @@ -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), }); @@ -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({ @@ -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), }); @@ -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 diff --git a/packages/inter-protocol/test/auction/test-scheduler.js b/packages/inter-protocol/test/auction/test-scheduler.js index 1a9c464d26f..a6be10f163f 100644 --- a/packages/inter-protocol/test/auction/test-scheduler.js +++ b/packages/inter-protocol/test/auction/test-scheduler.js @@ -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), @@ -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), }); @@ -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); @@ -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), @@ -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); @@ -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 }, }); @@ -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), @@ -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; @@ -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 /////////// @@ -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 }, }); @@ -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 }, }); @@ -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;