-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(orchestrator): membrane-friendly timerUtils
- Loading branch information
1 parent
52e0339
commit 92292e7
Showing
10 changed files
with
136 additions
and
94 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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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 { M } from '@endo/patterns'; | ||
|
||
/** | ||
* @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; | ||
|
||
/** | ||
* @param {Remote<TimerService>} timerService | ||
* @param {Zone} [zone] | ||
*/ | ||
|
||
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( | ||
'Time Helper', | ||
M.interface('TimeHelperI', { | ||
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]} results */ | ||
onFulfilled([currentTime, timerBrand]) { | ||
const timeout = | ||
relativeTime || | ||
TimeMath.coerceRelativeTimeRecord( | ||
SECONDS_PER_MINUTE * 5n, | ||
timerBrand, | ||
); | ||
return ( | ||
TimeMath.addAbsRel(currentTime, timeout).absValue * | ||
NANOSECONDS_PER_SECOND | ||
); | ||
}, | ||
}, | ||
); | ||
}, | ||
}, | ||
); | ||
}; | ||
|
||
/** @typedef {Awaited<ReturnType<typeof makeTimeHelper>>} TimeHelper */ |
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 was deleted.
Oops, something went wrong.
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); | ||
}); |