-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! feat(orchestration): make timerUtils resumable
- Loading branch information
1 parent
99f7653
commit 18889a2
Showing
9 changed files
with
112 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,76 @@ | ||
import { RelativeTimeRecordShape, TimeMath } from '@agoric/time'; | ||
import { VowShape } from '@agoric/vow'; | ||
import { watch, allVows } from '@agoric/vow/vat.js'; | ||
import { makeHeapZone } from '@agoric/zone'; | ||
import { E } from '@endo/far'; | ||
import { TimeMath } from '@agoric/time'; | ||
import { M } from '@endo/patterns'; | ||
|
||
/** | ||
* @import {RelativeTimeRecord, TimerBrand, TimerService} from '@agoric/time'; | ||
* @import {Remote} from '@agoric/internal'; | ||
* @import {Remote} from '@agoric/internal';* | ||
* @import {RelativeTimeRecord, TimerBrand, TimerService, TimestampRecord} from '@agoric/time'; | ||
* @import {Vow} from '@agoric/vow'; | ||
* @import {Zone} from '@agoric/zone'; | ||
*/ | ||
|
||
export const SECONDS_PER_MINUTE = 60n; | ||
export const NANOSECONDS_PER_SECOND = 1_000_000_000n; | ||
|
||
/** | ||
* XXX should this be durable? resumable? | ||
* | ||
* @param {Remote<TimerService>} timer | ||
* @param {Remote<TimerService>} timerService | ||
* @param {Zone} [zone] | ||
*/ | ||
export function makeTimestampHelper(timer) { | ||
|
||
export const makeTimestampHelper = (timerService, zone = makeHeapZone()) => { | ||
/** @type {TimerBrand | undefined} */ | ||
let brandCache; | ||
const getBrand = async () => { | ||
if (brandCache) return brandCache; | ||
brandCache = await E(timer).getTimerBrand(); | ||
return brandCache; | ||
return watch(E(timerService).getTimerBrand(), { | ||
onFulfilled: timerBrand => { | ||
return timerBrand; | ||
}, | ||
}); | ||
}; | ||
|
||
return harden({ | ||
/** | ||
* XXX do this need to be resumable / use Vows? | ||
* | ||
* Takes the current time from ChainTimerService and adds a relative time to | ||
* determine a timeout timestamp in nanoseconds. Useful for | ||
* {@link MsgTransfer.timeoutTimestamp}. | ||
* | ||
* @param {RelativeTimeRecord} [relativeTime] defaults to 5 minutes | ||
* @returns {Promise<bigint>} Timeout timestamp in absolute nanoseconds | ||
* since unix epoch | ||
*/ | ||
async getTimeoutTimestampNS(relativeTime) { | ||
const currentTime = await E(timer).getCurrentTimestamp(); | ||
const timeout = | ||
relativeTime || | ||
TimeMath.coerceRelativeTimeRecord( | ||
SECONDS_PER_MINUTE * 5n, | ||
await getBrand(), | ||
return zone.exo( | ||
'Timestamp Helper', | ||
M.interface('TimeStampHelperI', { | ||
getTimeoutTimestampNS: M.call() | ||
.optional(RelativeTimeRecordShape) | ||
.returns(VowShape), | ||
}), | ||
{ | ||
/** | ||
* Takes the current time from ChainTimerService and adds a relative time | ||
* to determine a timeout timestamp in nanoseconds. Useful for | ||
* {@link MsgTransfer.timeoutTimestamp}. | ||
* | ||
* @param {RelativeTimeRecord} [relativeTime] defaults to 5 minutes | ||
* @returns {Vow<bigint>} Timeout timestamp in absolute nanoseconds since | ||
* unix epoch | ||
*/ | ||
getTimeoutTimestampNS(relativeTime) { | ||
return watch( | ||
allVows([E(timerService).getCurrentTimestamp(), getBrand()]), | ||
{ | ||
/** @param {[TimestampRecord, TimerBrand]} r */ | ||
onFulfilled([currentTime, timerBrand]) { | ||
const timeout = | ||
relativeTime || | ||
TimeMath.coerceRelativeTimeRecord( | ||
SECONDS_PER_MINUTE * 5n, | ||
timerBrand, | ||
); | ||
return ( | ||
TimeMath.addAbsRel(currentTime, timeout).absValue * | ||
NANOSECONDS_PER_SECOND | ||
); | ||
}, | ||
}, | ||
); | ||
return ( | ||
TimeMath.addAbsRel(currentTime, timeout).absValue * | ||
NANOSECONDS_PER_SECOND | ||
); | ||
}, | ||
}, | ||
}); | ||
} | ||
); | ||
}; | ||
|
||
/** @typedef {Awaited<ReturnType<typeof makeTimestampHelper>>} TimestampHelper */ | ||
|
||
/** | ||
* Convert a Date from a Cosmos message, which has millisecond precision, to a | ||
* BigInt for number of seconds since epoch, for use in a timer. | ||
* | ||
* @param {Date} date | ||
* @returns {bigint} | ||
*/ | ||
export const dateInSeconds = date => BigInt(Math.floor(date.getTime() / 1000)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js'; | ||
import { dateInSeconds } from '../../src/utils/cosmos.js'; | ||
|
||
test('dateInSeconds', t => { | ||
t.is(dateInSeconds(new Date(1)), 0n); | ||
t.is(dateInSeconds(new Date(999)), 0n); | ||
t.is(dateInSeconds(new Date(1000)), 1n); | ||
|
||
t.is(dateInSeconds(new Date('2025-12-17T12:23:45Z')), 1765974225n); | ||
}); |