-
Notifications
You must be signed in to change notification settings - Fork 206
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
Set of tools to sync operations that depend on-chain transactions #10171
Open
anilhelvaci
wants to merge
2
commits into
Agoric:master
Choose a base branch
from
bytepitch:anil/sync-tools
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
274 changes: 274 additions & 0 deletions
274
a3p-integration/proposals/z:acceptance/test-lib/sync-tools.js
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,274 @@ | ||
/** | ||
* @file The purpose of this file is to bring together a set of tools that | ||
* developers can use to synchronize operations they carry out in their tests. | ||
* | ||
* These operations include; | ||
* - Making sure a core-eval resulted in successfully deploying a contract | ||
* - Making sure a core-eval successfully sent zoe invitations to committee members for governance | ||
* - Making sure an account is successfully funded with vbank assets like IST, BLD etc. | ||
* - operation: query dest account's balance | ||
* - condition: dest account has a balance >= sent token | ||
* - Making sure an offer resulted successfully | ||
* | ||
*/ | ||
|
||
/** | ||
* @typedef {object} RetryOptions | ||
* @property {number} maxRetries | ||
* @property {number} retryIntervalMs | ||
* @property {(...arg0: string[]) => void} log | ||
* @property {(object) => void} [setTimeout] | ||
* @property {string} [errorMessage=Error] | ||
* | ||
* | ||
* @typedef {object} CosmosBalanceThreshold | ||
* @property {string} denom | ||
* @property {number} value | ||
*/ | ||
|
||
const ambientSetTimeout = globalThis.setTimeout; | ||
|
||
/** | ||
* From https://github.com/Agoric/agoric-sdk/blob/442f07c8f0af03281b52b90e90c27131eef6f331/multichain-testing/tools/sleep.ts#L10 | ||
* | ||
* @param {number} ms | ||
* @param {*} sleepOptions | ||
* @returns | ||
*/ | ||
const sleep = (ms, { log = () => {}, setTimeout = ambientSetTimeout }) => | ||
new Promise(resolve => { | ||
// @ts-ignore | ||
log(`Sleeping for ${ms}ms...`); | ||
setTimeout(resolve, ms); | ||
}); | ||
|
||
/** | ||
* From https://github.com/Agoric/agoric-sdk/blob/442f07c8f0af03281b52b90e90c27131eef6f331/multichain-testing/tools/sleep.ts#L24 | ||
* | ||
* @param {() => Promise} operation | ||
* @param {(result: any) => boolean} condition | ||
* @param {string} message | ||
* @param {RetryOptions} options | ||
* @returns | ||
*/ | ||
const retryUntilCondition = async ( | ||
operation, | ||
condition, | ||
message, | ||
{ maxRetries = 6, retryIntervalMs = 3500, log, setTimeout }, | ||
) => { | ||
console.log({ maxRetries, retryIntervalMs, message }); | ||
let retries = 0; | ||
|
||
while (retries < maxRetries) { | ||
try { | ||
const result = await operation(); | ||
log('RESULT', result); | ||
if (condition(result)) { | ||
return result; | ||
} | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
log(`Error: ${error.message}`); | ||
} else { | ||
log(`Unknown error: ${String(error)}`); | ||
} | ||
} | ||
|
||
retries++; | ||
console.log( | ||
`Retry ${retries}/${maxRetries} - Waiting for ${retryIntervalMs}ms for ${message}...`, | ||
); | ||
await sleep(retryIntervalMs, { log, setTimeout }); | ||
} | ||
|
||
throw Error(`${message} condition failed after ${maxRetries} retries.`); | ||
}; | ||
|
||
/** | ||
* @param {RetryOptions} options | ||
*/ | ||
const overrideDefaultOptions = options => { | ||
const defaultValues = { | ||
maxRetries: 6, | ||
retryIntervalMs: 3500, | ||
log: console.log, | ||
errorMessage: 'Error', | ||
}; | ||
|
||
return { ...defaultValues, ...options }; | ||
}; | ||
|
||
///////////// Making sure a core-eval resulted successfully deploying a contract ///////////// | ||
|
||
const makeGetInstances = follow => async () => { | ||
const instanceEntries = await follow( | ||
'-lF', | ||
`:published.agoricNames.instance`, | ||
); | ||
|
||
return Object.fromEntries(instanceEntries); | ||
}; | ||
|
||
/** | ||
* | ||
* @param {string} contractName | ||
* @param {{follow: () => object, setTimeout: (object) => void}} ambientAuthority | ||
* @param {RetryOptions} options | ||
* @returns | ||
*/ | ||
export const waitUntilContractDeployed = ( | ||
contractName, | ||
ambientAuthority, | ||
options, | ||
) => { | ||
const { follow, setTimeout } = ambientAuthority; | ||
const getInstances = makeGetInstances(follow); | ||
const { maxRetries, retryIntervalMs, errorMessage, log } = | ||
overrideDefaultOptions(options); | ||
|
||
return retryUntilCondition( | ||
getInstances, | ||
instanceObject => Object.keys(instanceObject).includes(contractName), | ||
errorMessage, | ||
{ maxRetries, retryIntervalMs, log, setTimeout }, | ||
); | ||
}; | ||
|
||
///////////// Making sure an account is successfully funded with vbank assets like IST, BLD etc. /////////////// | ||
|
||
const makeQueryCosmosBalance = queryCb => async dest => { | ||
const coins = await queryCb('bank', 'balances', dest); | ||
return coins.balances; | ||
}; | ||
|
||
/** | ||
* | ||
* @param {Array} balances | ||
* @param {CosmosBalanceThreshold} threshold | ||
* @returns {boolean} | ||
*/ | ||
const checkCosmosBalance = (balances, threshold) => { | ||
const balance = [...balances].find(({ denom }) => denom === threshold.denom); | ||
return Number(balance.amount) >= threshold.value; | ||
}; | ||
|
||
/** | ||
* @param {string} destAcct | ||
* @param {{query: () => Promise<object>, setTimeout: (object) => void}} ambientAuthority | ||
* @param {{denom: string, value: number}} threshold | ||
* @param {RetryOptions} options | ||
*/ | ||
export const waitUntilAccountFunded = ( | ||
destAcct, | ||
ambientAuthority, | ||
threshold, | ||
options, | ||
) => { | ||
const { query, setTimeout } = ambientAuthority; | ||
const queryCosmosBalance = makeQueryCosmosBalance(query); | ||
const { maxRetries, retryIntervalMs, errorMessage, log } = | ||
overrideDefaultOptions(options); | ||
|
||
return retryUntilCondition( | ||
async () => queryCosmosBalance(destAcct), | ||
balances => checkCosmosBalance(balances, threshold), | ||
errorMessage, | ||
{ maxRetries, retryIntervalMs, log, setTimeout }, | ||
); | ||
}; | ||
|
||
///////////// Making sure an offers get results ///////////// | ||
|
||
const makeQueryWallet = follow => async (/** @type {String} */ addr) => { | ||
const update = await follow('-lF', `:published.wallet.${addr}`); | ||
|
||
return update; | ||
}; | ||
|
||
/** | ||
* | ||
* @param {object} offerStatus | ||
* @param {boolean} waitForPayouts | ||
* @param {string} offerId | ||
*/ | ||
const checkOfferState = (offerStatus, waitForPayouts, offerId) => { | ||
const { updated, status } = offerStatus; | ||
|
||
if (updated !== 'offerStatus') return false; | ||
if (!status) return false; | ||
if (status.id !== offerId) return false; | ||
if (!status.numWantsSatisfied || status.numWantsSatisfied !== 1) return false; | ||
if (waitForPayouts && status.result && status.payouts) return true; | ||
if (!waitForPayouts && status.result) return true; | ||
|
||
return false; | ||
}; | ||
|
||
/** | ||
* | ||
* @param {string} addr | ||
* @param {string} offerId | ||
* @param {boolean} waitForPayouts | ||
* @param {{follow: () => object, setTimeout: (object) => void}} ambientAuthority | ||
* @param {RetryOptions} options | ||
*/ | ||
export const waitUntilOfferResult = ( | ||
addr, | ||
offerId, | ||
waitForPayouts, | ||
ambientAuthority, | ||
options, | ||
) => { | ||
const { follow, setTimeout } = ambientAuthority; | ||
const queryWallet = makeQueryWallet(follow); | ||
const { maxRetries, retryIntervalMs, errorMessage, log } = | ||
overrideDefaultOptions(options); | ||
|
||
return retryUntilCondition( | ||
async () => queryWallet(addr), | ||
status => checkOfferState(status, waitForPayouts, offerId), | ||
errorMessage, | ||
{ maxRetries, retryIntervalMs, log, setTimeout }, | ||
); | ||
}; | ||
|
||
///////////// Making sure a core-eval successfully sent zoe invitations to committee members for governance ///////////// | ||
|
||
/** | ||
* | ||
* @param {{ updated: string, currentAmount: any }} update | ||
* @returns {boolean} | ||
*/ | ||
const checkForInvitation = update => { | ||
const { updated, currentAmount } = update; | ||
|
||
if (updated !== 'balance') return false; | ||
if (!currentAmount || !currentAmount.brand) return false; | ||
|
||
return currentAmount.brand.includes('Invitation'); | ||
}; | ||
|
||
/** | ||
* | ||
* @param {string} addr | ||
* @param {{follow: () => object, setTimeout: (object) => void}} ambientAuthority | ||
* @param {RetryOptions} options | ||
*/ | ||
export const waitUntilInvitationReceived = ( | ||
addr, | ||
ambientAuthority, | ||
options, | ||
) => { | ||
const { follow, setTimeout } = ambientAuthority; | ||
const queryWallet = makeQueryWallet(follow); | ||
const { maxRetries, retryIntervalMs, errorMessage, log } = | ||
overrideDefaultOptions(options); | ||
|
||
return retryUntilCondition( | ||
async () => queryWallet(addr), | ||
checkForInvitation, | ||
errorMessage, | ||
{ maxRetries, retryIntervalMs, log, setTimeout }, | ||
); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a type or drop the line for all @returns lines. (@returns isn't required, but should be correct if present.)