Skip to content

Commit

Permalink
fixup! feat(orchestration): make timerUtils resumable
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Jun 21, 2024
1 parent e4df2b6 commit 33a31fa
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/orchestration/src/examples/stakeBld.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { deeplyFulfilled } from '@endo/marshal';
import { M } from '@endo/patterns';
import { prepareLocalOrchestrationAccountKit } from '../exos/local-orchestration-account.js';
import { makeChainHub } from '../exos/chain-hub.js';
import { makeTimestampHelper } from '../exos/time.js';
import { makeTimeHelper } from '../exos/time-helper.js';

/**
* @import {NameHub} from '@agoric/vats';
Expand Down Expand Up @@ -50,7 +50,7 @@ export const start = async (zcf, privateArgs, baggage) => {
privateArgs.timerService,
vowTools,
makeChainHub(privateArgs.agoricNames),
makeTimestampHelper(privateArgs.timerService),
makeTimeHelper(privateArgs.timerService),
);

// ----------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { orchestrationAccountMethods } from '../utils/orchestrationAccount.js';
* @import {PromiseVow, VowTools} from '@agoric/vow';
* @import {TypedJson} from '@agoric/cosmic-proto';
* @import {ChainHub} from './chain-hub.js';
* @import {TimestampHelper} from './time.js';
* @import {TimeHelper} from './time-helper.js';
*/

const trace = makeTracer('LOA');
Expand Down Expand Up @@ -66,7 +66,7 @@ const PUBLIC_TOPICS = {
* @param {Remote<TimerService>} timerService
* @param {VowTools} vowTools
* @param {ChainHub} chainHub
* @param {TimestampHelper} timestampHelper
* @param {TimeHelper} timeHelper
*/
export const prepareLocalOrchestrationAccountKit = (
zone,
Expand All @@ -75,7 +75,7 @@ export const prepareLocalOrchestrationAccountKit = (
timerService,
{ watch, when, allVows },
chainHub,
timestampHelper,
timeHelper,
) =>
/** Make an object wrapping an LCA with Zoe interfaces. */
zone.exoClassKit(
Expand Down Expand Up @@ -403,7 +403,7 @@ export const prepareLocalOrchestrationAccountKit = (
// TODO #9324 what's a reasonable default? currently 5 minutes
// FIXME: do not call `getTimeoutTimestampNS` if `opts.timeoutTimestamp` or `opts.timeoutHeight` is provided
const timeoutTimestampV = watch(
timestampHelper.getTimeoutTimestampNS(),
timeHelper.getTimeoutTimestampNS(),
this.facets.getTimeoutTimestampWatcher,
{ opts },
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ export const NANOSECONDS_PER_SECOND = 1_000_000_000n;
* @param {Zone} [zone]
*/

export const makeTimestampHelper = (timerService, zone = makeHeapZone()) => {
export const makeTimeHelper = (timerService, zone = makeHeapZone()) => {
/** @type {TimerBrand | undefined} */
let brandCache;
const getBrand = () => {
if (brandCache) return brandCache;
return watch(E(timerService).getTimerBrand(), {
onFulfilled: timerBrand => {
brandCache = timerBrand;
return timerBrand;
},
});
};

return zone.exo(
'Timestamp Helper',
M.interface('TimeStampHelperI', {
'Time Helper',
M.interface('TimeHelperI', {
getTimeoutTimestampNS: M.call()
.optional(RelativeTimeRecordShape)
.returns(VowShape),
Expand All @@ -53,7 +54,7 @@ export const makeTimestampHelper = (timerService, zone = makeHeapZone()) => {
return watch(
allVows([E(timerService).getCurrentTimestamp(), getBrand()]),
{
/** @param {[TimestampRecord, TimerBrand]} r */
/** @param {[TimestampRecord, TimerBrand]} results */
onFulfilled([currentTime, timerBrand]) {
const timeout =
relativeTime ||
Expand All @@ -73,4 +74,4 @@ export const makeTimestampHelper = (timerService, zone = makeHeapZone()) => {
);
};

/** @typedef {Awaited<ReturnType<typeof makeTimestampHelper>>} TimestampHelper */
/** @typedef {Awaited<ReturnType<typeof makeTimeHelper>>} TimeHelper */
6 changes: 3 additions & 3 deletions packages/orchestration/src/utils/start-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { makeChainHub } from '../exos/chain-hub.js';
import { prepareRemoteChainFacade } from '../exos/remote-chain-facade.js';
import { prepareCosmosOrchestrationAccount } from '../exos/cosmos-orchestration-account.js';
import { prepareLocalChainFacade } from '../exos/local-chain-facade.js';
import { makeTimestampHelper } from '../exos/time.js';
import { makeTimeHelper } from '../exos/time-helper.js';

/**
* @import {PromiseKit} from '@endo/promise-kit'
Expand Down Expand Up @@ -48,7 +48,7 @@ export const provideOrchestration = (
const zone = makeDurableZone(baggage);

const chainHub = makeChainHub(remotePowers.agoricNames);
const timestampHelper = makeTimestampHelper(remotePowers.timerService);
const timeHelper = makeTimeHelper(remotePowers.timerService);

const vowTools = prepareVowTools(zone.subZone('vows'));

Expand All @@ -60,7 +60,7 @@ export const provideOrchestration = (
remotePowers.timerService,
vowTools,
chainHub,
timestampHelper,
timeHelper,
);

const asyncFlowTools = prepareAsyncFlowTools(zone.subZone('asyncFlow'), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { prepareLocalOrchestrationAccountKit } from '../../src/exos/local-orches
import { ChainAddress } from '../../src/orchestration-api.js';
import { makeChainHub } from '../../src/exos/chain-hub.js';
import {
makeTimestampHelper,
makeTimeHelper,
NANOSECONDS_PER_SECOND,
} from '../../src/exos/time.js';
} from '../../src/exos/time-helper.js';
import { commonSetup } from '../supports.js';

test('deposit, withdraw', async t => {
Expand All @@ -37,7 +37,7 @@ test('deposit, withdraw', async t => {
timer,
vowTools,
makeChainHub(bootstrap.agoricNames),
makeTimestampHelper(timer),
makeTimeHelper(timer),
);

t.log('request account from vat-localchain');
Expand Down Expand Up @@ -109,7 +109,7 @@ test('delegate, undelegate', async t => {
timer,
vowTools,
makeChainHub(bootstrap.agoricNames),
makeTimestampHelper(timer),
makeTimeHelper(timer),
);

t.log('request account from vat-localchain');
Expand Down Expand Up @@ -162,7 +162,7 @@ test('transfer', async t => {
timer,
vowTools,
makeChainHub(bootstrap.agoricNames),
makeTimestampHelper(timer),
makeTimeHelper(timer),
);

t.log('request account from vat-localchain');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import { V } from '@agoric/vow/vat.js';
import { buildZoeManualTimer } from '@agoric/zoe/tools/manualTimer.js';
import { TimeMath } from '@agoric/time';
import {
makeTimestampHelper,
makeTimeHelper,
NANOSECONDS_PER_SECOND,
SECONDS_PER_MINUTE,
} from '../../src/exos/time.js';
} from '../../src/exos/time-helper.js';

test('makeTimestampHelper - getCurrentTimestamp', async t => {
test('makeTimeHelper - getCurrentTimestamp', async t => {
const timer = buildZoeManualTimer(t.log);
const timerBrand = timer.getTimerBrand();
t.is(timer.getCurrentTimestamp().absValue, 0n, 'current time is 0n');

const timestampHelper = makeTimestampHelper(timer);
const timeHelper = makeTimeHelper(timer);
t.is(
await V.when(timestampHelper.getTimeoutTimestampNS()),
await V.when(timeHelper.getTimeoutTimestampNS()),
5n * SECONDS_PER_MINUTE * NANOSECONDS_PER_SECOND,
'default timestamp is 5 minutes from current time, in nanoseconds',
);

t.is(
await V.when(
timestampHelper.getTimeoutTimestampNS(
timeHelper.getTimeoutTimestampNS(
TimeMath.coerceRelativeTimeRecord(1n, timerBrand),
),
),
Expand All @@ -35,7 +35,7 @@ test('makeTimestampHelper - getCurrentTimestamp', async t => {
await timer.tickN(3);
t.is(
await V.when(
timestampHelper.getTimeoutTimestampNS(
timeHelper.getTimeoutTimestampNS(
TimeMath.coerceRelativeTimeRecord(1n, timerBrand),
),
),
Expand Down

0 comments on commit 33a31fa

Please sign in to comment.