Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: restart labeled time PR #3778

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/SwingSet/src/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export { buildTimer } from './devices/timer.js';
export { buildBridge } from './devices/bridge.js'; export { buildBridge } from './devices/bridge.js';
export { default as buildCommand } from './devices/command.js'; export { default as buildCommand } from './devices/command.js';
export { buildPlugin } from './devices/plugin.js'; export { buildPlugin } from './devices/plugin.js';

export { TimeMath } from './vats/timeMath.js';
70 changes: 70 additions & 0 deletions packages/SwingSet/src/vats/timeMath.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,70 @@
// @ts-check

// TODO Move this module somewhere more pleasantly reusable

import { Nat } from '@agoric/nat';

/**
* @param {AbsoluteTimeish | Durationish} left
* @param {AbsoluteTimeish | Durationish} right
* @returns {TimerService | undefined}
*/
const sharedTimeAuthority = (left, right) => {
if (typeof left === 'bigint') {
if (typeof right === 'bigint') {
return undefined;
} else {
return right.timeAuthority;
}
} else if (typeof right === 'bigint') {
return left.timeAuthority;
} else {
const result = left.timeAuthority;
assert.equal(result, right.timeAuthority);
return result;
}
};

/**
* @type {TimeMathType}
*/
export const TimeMath = harden({
absoluteTimeValue: abs =>
(typeof abs === 'bigint' ? abs : Nat(abs.absoluteTimeValue)),
relativeTimeValue: rel =>
(typeof rel === 'bigint' ? rel : Nat(rel.relativeTimeValue)),
add: (abs, rel) => {
const absoluteTimeValue =
TimeMath.absoluteTimeValue(abs) + TimeMath.relativeTimeValue(rel);
const timeAuthority = sharedTimeAuthority(abs, rel);
if (timeAuthority) {
return harden({
timeAuthority,
absoluteTimeValue,
});
} else {
return absoluteTimeValue;
}
},
addRel: (rel1, rel2) => {
const relativeTimeValue =
TimeMath.relativeTimeValue(rel1) + TimeMath.relativeTimeValue(rel2);
const timeAuthority = sharedTimeAuthority(rel1, rel2);
if (timeAuthority) {
return harden({
timeAuthority,
relativeTimeValue,
});
} else {
return relativeTimeValue;
}
},
mod: (abs, step) => {
sharedTimeAuthority(abs, step); // just assert they're compat
return TimeMath.absoluteTimeValue(abs) % TimeMath.relativeTimeValue(step);
},
modRel: (rel, step) => {
sharedTimeAuthority(rel, step); // just assert they're compat
return TimeMath.relativeTimeValue(rel) % TimeMath.relativeTimeValue(step);
},
});
48 changes: 40 additions & 8 deletions packages/SwingSet/src/vats/types.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
* @typedef {Object} TimerService Gives the ability to get the current time, * @typedef {Object} TimerService Gives the ability to get the current time,
* schedule a single wake() call, create a repeater that will allow scheduling * schedule a single wake() call, create a repeater that will allow scheduling
* of events at regular intervals, or remove scheduled calls. * of events at regular intervals, or remove scheduled calls.
* @property {() => Timestamp} getCurrentTimestamp Retrieve the latest timestamp * @property {() => AbsoluteTimeish} getCurrentTimestamp Retrieve the latest timestamp
* @property {(baseTime: Timestamp, waker: ERef<TimerWaker>) => Timestamp} setWakeup Return * @property {(baseTime: AbsoluteTimeish, waker: ERef<TimerWaker>) => AbsoluteTimeish} setWakeup Return
* value is the time at which the call is scheduled to take place * value is the time at which the call is scheduled to take place
* @property {(waker: ERef<TimerWaker>) => Array<Timestamp>} removeWakeup Remove the waker * @property {(waker: ERef<TimerWaker>) => Array<AbsoluteTimeish>} removeWakeup Remove the waker
* from all its scheduled wakeups, whether produced by `timer.setWakeup(h)` or * from all its scheduled wakeups, whether produced by `timer.setWakeup(h)` or
* `repeater.schedule(h)`. * `repeater.schedule(h)`.
* @property {(delay: RelativeTime, interval: RelativeTime) => TimerRepeater} createRepeater * @property {(delay: Durationish, interval: Durationish) => TimerRepeater} createRepeater
* DEPRECATED: use makeRepeater instead. * DEPRECATED: use makeRepeater instead.
* @property {(delaySecs: RelativeTime, interval: RelativeTime) => TimerRepeater} makeRepeater * @property {(delaySecs: Durationish, interval: Durationish) => TimerRepeater} makeRepeater
* Create and return a repeater that will schedule `wake()` calls * Create and return a repeater that will schedule `wake()` calls
* repeatedly at times that are a multiple of interval following delay. * repeatedly at times that are a multiple of interval following delay.
* Interval is the difference between successive times at which wake will be * Interval is the difference between successive times at which wake will be
* called. When `schedule(w)` is called, `w.wake()` will be scheduled to be * called. When `schedule(w)` is called, `w.wake()` will be scheduled to be
* called after the next multiple of interval from the base. Since times can be * called after the next multiple of interval from the base. Since times can be
* coarse-grained, the actual call may occur later, but this won't change when * coarse-grained, the actual call may occur later, but this won't change when
* the next event will be called. * the next event will be called.
* @property {(delaySecs: RelativeTime, interval: RelativeTime) => Notifier<Timestamp>} makeNotifier * @property {(delaySecs: Durationish, interval: Durationish) => Notifier<AbsoluteTimeish>} makeNotifier
* Create and return a Notifier that will deliver updates repeatedly at times * Create and return a Notifier that will deliver updates repeatedly at times
* that are a multiple of interval following delay. * that are a multiple of interval following delay.
*/ */
Expand All @@ -32,18 +32,50 @@
* RelativeTime values. * RelativeTime values.
*/ */


/**
* @typedef {Object} AbsoluteTime
* @property {TimerService} timeAuthority
* @property {Timestamp} absoluteTimeValue
*/

/**
* @typedef {Object} Duration
* @property {TimerService} timeAuthority
* @property {RelativeTime} relativeTimeValue
*/

/**
* @typedef {AbsoluteTime | Timestamp} AbsoluteTimeish
* Transitional measure until all are converted to AbsoluteTime
*/

/**
* @typedef {Duration | RelativeTime} Durationish
* Transitional measure until all are converted to Duration
*/

/** /**
* @typedef {Object} TimerWaker * @typedef {Object} TimerWaker
* @property {(timestamp: Timestamp) => void} wake The timestamp passed to * @property {(timestamp: AbsoluteTimeish) => void} wake The timestamp passed to
* `wake()` is the time that the call was scheduled to occur. * `wake()` is the time that the call was scheduled to occur.
*/ */


/** /**
* @typedef {Object} TimerRepeater * @typedef {Object} TimerRepeater
* @property {(waker: ERef<TimerWaker>) => Timestamp} schedule Returns the time scheduled for * @property {(waker: ERef<TimerWaker>) => AbsoluteTimeish} schedule Returns the time scheduled for
* the first call to `E(waker).wake()`. The waker will continue to be scheduled * the first call to `E(waker).wake()`. The waker will continue to be scheduled
* every interval until the repeater is disabled. * every interval until the repeater is disabled.
* @property {() => void} disable Disable this repeater, so `schedule(w)` can't * @property {() => void} disable Disable this repeater, so `schedule(w)` can't
* be called, and wakers already scheduled with this repeater won't be * be called, and wakers already scheduled with this repeater won't be
* rescheduled again after `E(waker).wake()` is next called on them. * rescheduled again after `E(waker).wake()` is next called on them.
*/ */

/**
* @typedef TimeMathType
* @property {(abs: AbsoluteTimeish) => Timestamp} absoluteTimeValue
* @property {(rel: Durationish) => RelativeTime} relativeTimeValue
* @property {(abs: AbsoluteTimeish, rel: Durationish) => AbsoluteTimeish} add
* @property {(rel1: Durationish, rel: Durationish) => Durationish} addRel
* @property {(abs: AbsoluteTimeish, step: Durationish) => bigint} mod
* @property {(rel: Durationish, step: Durationish) => bigint} modRel
*/
2 changes: 1 addition & 1 deletion packages/governance/src/types.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
/** /**
* @typedef {Object} ClosingRule * @typedef {Object} ClosingRule
* @property {Timer} timer * @property {Timer} timer
* @property {Timestamp} deadline * @property {AbsoluteTimeish} deadline
*/ */


/** /**
Expand Down
16 changes: 8 additions & 8 deletions packages/treasury/src/types.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@
* @typedef {Object} VaultKit * @typedef {Object} VaultKit
* @property {Vault} vault * @property {Vault} vault
* @property {(ZCFSeat) => Promise<OpenLoanKit>} openLoan * @property {(ZCFSeat) => Promise<OpenLoanKit>} openLoan
* @property {(Timestamp) => Amount} accrueInterestAndAddToPool * @property {(AbsoluteTimeish) => Amount} accrueInterestAndAddToPool
* @property {ZCFSeat} vaultSeat * @property {ZCFSeat} vaultSeat
* @property {PromiseRecord<string>} liquidationPromiseKit * @property {PromiseRecord<string>} liquidationPromiseKit
* @property {ZCFSeat} liquidationZcfSeat * @property {ZCFSeat} liquidationZcfSeat
*/ */


/** /**
* @typedef {Object} LoanParams * @typedef {Object} LoanParams
* @property {RelativeTime} chargingPeriod * @property {Durationish} chargingPeriod
* @property {RelativeTime} recordingPeriod * @property {Durationish} recordingPeriod
*/ */


/** /**
Expand Down Expand Up @@ -147,21 +147,21 @@
* @param {ERef<MultipoolAutoswapPublicFacet>} autoswap * @param {ERef<MultipoolAutoswapPublicFacet>} autoswap
* @param {ERef<PriceAuthority>} priceAuthority * @param {ERef<PriceAuthority>} priceAuthority
* @param {LoanParams} loanParams * @param {LoanParams} loanParams
* @param {Timestamp} startTimeStamp * @param {AbsoluteTimeish} startTimeStamp
* @returns {VaultKit} * @returns {VaultKit}
*/ */


/** /**
* @typedef {Object} DebtStatus * @typedef {Object} DebtStatus
* @property {Timestamp} latestInterestUpdate * @property {AbsoluteTimeish} latestInterestUpdate
* @property {Amount} interest * @property {Amount} interest
* @property {Amount} newDebt * @property {Amount} newDebt
*/ */


/** /**
* @callback Calculate * @callback Calculate
* @param {DebtStatus} debtStatus * @param {DebtStatus} debtStatus
* @param {Timestamp} currentTime * @param {AbsoluteTimeish} currentTime
* @returns {DebtStatus} * @returns {DebtStatus}
*/ */


Expand All @@ -179,7 +179,7 @@
* @callback MakeInterestCalculator * @callback MakeInterestCalculator
* @param {Brand} brand * @param {Brand} brand
* @param {Ratio} rate * @param {Ratio} rate
* @param {RelativeTime} chargingPeriod * @param {Durationish} chargingPeriod
* @param {RelativeTime} recordingPeriod * @param {Durationish} recordingPeriod
* @returns {CalculatorKit} * @returns {CalculatorKit}
*/ */
2 changes: 1 addition & 1 deletion packages/zoe/src/contractSupport/priceAuthority.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const isGT = (amount, amountLimit) => !AmountMath.isGTE(amountLimit, amount);
/** /**
* @typedef {Object} OnewayPriceAuthorityOptions * @typedef {Object} OnewayPriceAuthorityOptions
* @property {Issuer} quoteIssuer * @property {Issuer} quoteIssuer
* @property {ERef<Notifier<Timestamp>>} notifier * @property {ERef<Notifier<AbsoluteTimeish>>} notifier
* @property {ERef<TimerService>} timer * @property {ERef<TimerService>} timer
* @property {PriceQuoteCreate} createQuote * @property {PriceQuoteCreate} createQuote
* @property {Brand} actualBrandIn * @property {Brand} actualBrandIn
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const addToLiened = (store, attestationElem) => {
* the expiration has only expired when the currentTime is past * the expiration has only expired when the currentTime is past
* (greater than and not equal to) the expiration. * (greater than and not equal to) the expiration.
* *
* @param {Timestamp} expiration * @param {AbsoluteTimeish} expiration
* @param {Timestamp} currentTime * @param {AbsoluteTimeish} currentTime
* @returns {boolean} * @returns {boolean}
*/ */
const hasExpired = (expiration, currentTime) => expiration < currentTime; const hasExpired = (expiration, currentTime) => expiration < currentTime;
Expand All @@ -37,7 +37,7 @@ const hasExpired = (expiration, currentTime) => expiration < currentTime;
* @param {Address} address * @param {Address} address
* @param {Amount} amountLiened - the amount of the underlying asset to put * @param {Amount} amountLiened - the amount of the underlying asset to put
* a lien on * a lien on
* @param {Timestamp} expiration * @param {AbsoluteTimeish} expiration
* @param {Handle<'attestation'>} handle - the unique handle * @param {Handle<'attestation'>} handle - the unique handle
* @returns {ExpiringAttElem} * @returns {ExpiringAttElem}
*/ */
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const { details: X } = assert;
* @param {(address: Address) => boolean} canExtend * @param {(address: Address) => boolean} canExtend
* @param {(newAttestationElem: ExpiringAttElem) => void} updateLienedAmount * @param {(newAttestationElem: ExpiringAttElem) => void} updateLienedAmount
* @param {Brand} attestationBrand * @param {Brand} attestationBrand
* @param {Timestamp} newExpiration * @param {AbsoluteTimeish} newExpiration
* @returns {void} * @returns {void}
*/ */
const extendExpiration = ( const extendExpiration = (
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { hasExpired } from './expiringHelpers.js';
* @param {Store<Address,Array<ExpiringAttElem>>} store * @param {Store<Address,Array<ExpiringAttElem>>} store
* @param {Amount} empty - an empty amount of the externalBrand * @param {Amount} empty - an empty amount of the externalBrand
* @param {Address} address * @param {Address} address
* @param {Timestamp} currentTime * @param {AbsoluteTimeish} currentTime
* @returns {Amount} amountLiened * @returns {Amount} amountLiened
*/ */
const unlienExpiredAmounts = (store, empty, address, currentTime) => { const unlienExpiredAmounts = (store, empty, address, currentTime) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/contracts/attestation/prerequisites.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const { details: X } = assert;
* Assert the cosmos-specific prerequisites * Assert the cosmos-specific prerequisites
* *
* @param {ERef<{getAccountState: (address: Address, brand: Brand) => {total: Amount, bonded: Amount, locked: Amount, * @param {ERef<{getAccountState: (address: Address, brand: Brand) => {total: Amount, bonded: Amount, locked: Amount,
* currentTime: Timestamp}}>} stakeReporter * currentTime: AbsoluteTimeish}}>} stakeReporter
* @param {StoredTime} storedTime * @param {StoredTime} storedTime
* @param {GetLiened} getLiened * @param {GetLiened} getLiened
* @param {Brand} underlyingBrand * @param {Brand} underlyingBrand
* @param {Address} address * @param {Address} address
* @param {Amount} amountToLien * @param {Amount} amountToLien
* @param {Timestamp} expiration * @param {AbsoluteTimeish} expiration
*/ */
const assertPrerequisites = async ( const assertPrerequisites = async (
stakeReporter, stakeReporter,
Expand Down
26 changes: 13 additions & 13 deletions packages/zoe/src/contracts/attestation/types.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


/** /**
* @typedef AttMaker * @typedef AttMaker
* @property {(amountToLien: Amount, expiration: Timestamp) => Promise<AttestationPair>} makeAttestations * @property {(amountToLien: Amount, expiration: AbsoluteTimeish) => Promise<AttestationPair>} makeAttestations
* @property {MakeReturnAttInvitation} makeReturnAttInvitation * @property {MakeReturnAttInvitation} makeReturnAttInvitation
* @property {MakeExtendAttInvitation} makeExtendAttInvitation * @property {MakeExtendAttInvitation} makeExtendAttInvitation
*/ */
Expand All @@ -31,7 +31,7 @@
* @callback MakeAttestationsInternal * @callback MakeAttestationsInternal
* @param {Address} address * @param {Address} address
* @param {Amount} amountToLien * @param {Amount} amountToLien
* @param {Timestamp} expiration * @param {AbsoluteTimeish} expiration
* @returns {Promise<AttestationPair>} * @returns {Promise<AttestationPair>}
*/ */


Expand All @@ -48,7 +48,7 @@
* @callback GetLienAmount * @callback GetLienAmount
* *
* @param {Address} address * @param {Address} address
* @param {Timestamp} currentTime * @param {AbsoluteTimeish} currentTime
* @returns {Amount} * @returns {Amount}
*/ */


Expand All @@ -68,7 +68,7 @@
* *
* @param {Address} address * @param {Address} address
* @param {Amount} amount * @param {Amount} amount
* @param {Timestamp} expiration * @param {AbsoluteTimeish} expiration
* @returns {Promise<Payment>} * @returns {Promise<Payment>}
*/ */


Expand All @@ -88,7 +88,7 @@
* @typedef {Object} ExpiringAttElem * @typedef {Object} ExpiringAttElem
* @property {Address} address * @property {Address} address
* @property {Amount} amountLiened * @property {Amount} amountLiened
* @property {Timestamp} expiration * @property {AbsoluteTimeish} expiration
* @property {Handle<'attestation'>} handle * @property {Handle<'attestation'>} handle
*/ */


Expand Down Expand Up @@ -128,7 +128,7 @@
* handle). * handle).
* *
* @param {ZCFSeat} seat * @param {ZCFSeat} seat
* @param {Timestamp} newExpiration * @param {AbsoluteTimeish} newExpiration
* @returns {void} * @returns {void}
*/ */


Expand All @@ -138,7 +138,7 @@
* On slashing, we disallow any extensions. We do not reduce the liens. * On slashing, we disallow any extensions. We do not reduce the liens.
* *
* @param {Array<Address>} addresses * @param {Array<Address>} addresses
* @param {Timestamp} currentTime * @param {AbsoluteTimeish} currentTime
* @returns {void} * @returns {void}
*/ */


Expand All @@ -157,8 +157,8 @@
* attestation. Internal because the currentTime must be passed in, * attestation. Internal because the currentTime must be passed in,
* and that can only come from a trusted source. * and that can only come from a trusted source.
* *
* @param {Timestamp} newExpiration * @param {AbsoluteTimeish} newExpiration
* @param {Timestamp} currentTime * @param {AbsoluteTimeish} currentTime
* @returns {Promise<Invitation>} * @returns {Promise<Invitation>}
*/ */


Expand All @@ -168,7 +168,7 @@
* Make an invitation for extending the expiration date of an expiring * Make an invitation for extending the expiration date of an expiring
* attestation. * attestation.
* *
* @param {Timestamp} newExpiration * @param {AbsoluteTimeish} newExpiration
* @returns {Promise<Invitation>} * @returns {Promise<Invitation>}
*/ */


Expand All @@ -177,12 +177,12 @@
* Get the amount currently liened for the address and brand. * Get the amount currently liened for the address and brand.
* *
* @param {Address} addresses * @param {Address} addresses
* @param {Timestamp} currentTime * @param {AbsoluteTimeish} currentTime
* @param {Brand} brand * @param {Brand} brand
* @returns {Amount} * @returns {Amount}
*/ */


/** /**
* @typedef {{getTime: () => Timestamp, updateTime: (currentTime: * @typedef {{getTime: () => AbsoluteTimeish, updateTime: (currentTime:
* Timestamp) => void}} StoredTime * AbsoluteTimeish) => void}} StoredTime
*/ */
Loading