Skip to content

Commit

Permalink
fixup! feat(multichain/tools): executeOfferTx does not wait for compl…
Browse files Browse the repository at this point in the history
…etion
  • Loading branch information
dckc committed Jan 21, 2025
1 parent 60fc0b9 commit dd2d28b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
59 changes: 59 additions & 0 deletions multichain-testing/tools/block-iter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { makeIntervalIterable } from '@agoric/client-utils/src/clock-timer.js';

const { freeze } = Object;

/**
* @import {IntervalIO} from '@agoric/client-utils/src/clock-timer.js';
* @import {makeQueryClient} from './query.js';
*/

/**
* @param {ReturnType<typeof makeQueryClient>} api
* @param {number} [delta]
*/
const recentBlockRate = async (api, delta = 2) => {
/** @param {{ height: number, time: string }} header */
const relevant = ({ height, time }) => ({ height, time });
const { block: latest } = await api.queryBlock();
const heightRecent = Number(latest.header.height) - delta;
const { block: recent } = await api.queryBlock(heightRecent);
const t0 = Date.parse(recent.header.time);
const t1 = Date.parse(latest.header.time);
return {
delta,
latest: { ...relevant(latest.header) },
recent: { ...relevant(recent.header) },
elapsed: t1 - t0,
period: (t1 - t0) / delta,
};
};

/**
* Make an iterator for observing each block.
*
* We measure the block rate by observing the latest block
* and one earlier (by `delta`) block.
*
* Then we poll at 2x that rate (Nyquist frequency) and
* fire the iterator when the height changes.
*
* @param {IntervalIO & { api: ReturnType<typeof makeQueryClient>, delta?: number }} io
*/
export const makeBlocksIterable = ({ api, delta = 2, ...io }) => {
return freeze({
async *[Symbol.asyncIterator]() {
const { period } = await recentBlockRate(api, delta);
const nyquist = period / 2;
let prev;
const ticks = makeIntervalIterable(nyquist, io);
for await (const tick of ticks) {
const { block } = await api.queryBlock();
const current = Number(block.header.height);
if (current === prev) continue;
prev = current;
const { time } = block.header;
yield freeze({ tick, height: current, time });
}
},
});
};
11 changes: 11 additions & 0 deletions multichain-testing/tools/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import type { QueryDenomHashResponseSDKType } from '@agoric/cosmic-proto/ibc/app
import type { QueryChannelResponseSDKType } from '@agoric/cosmic-proto/ibc/core/channel/v1/query.js';
import type { QueryChannelsResponseSDKType } from '@agoric/cosmic-proto/ibc/core/channel/v1/query.js';

// TODO: export tendermint Block from @agoric/cosmic-proto
type BlockHeaderPartial = {
height: number;
time: string;
};

// TODO use telescope generated query client from @agoric/cosmic-proto
// https://github.com/Agoric/agoric-sdk/issues/9200
// TODO: inject fetch
export function makeQueryClient(apiUrl: string) {
const query = async <T>(path: string): Promise<T> => {
try {
Expand All @@ -26,6 +33,10 @@ export function makeQueryClient(apiUrl: string) {

return {
query,
queryBlock: (height?: number) =>
query<{ block: { header: BlockHeaderPartial } }>(
`/cosmos/base/tendermint/v1beta1/blocks/${height || 'latest'}`,
),
queryBalances: (address: string) =>
query<QueryAllBalancesResponseSDKType>(
`/cosmos/bank/v1beta1/balances/${address}`,
Expand Down

0 comments on commit dd2d28b

Please sign in to comment.