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

fix(vat-bank): allow numeric or string nonces #10929

Merged
merged 3 commits into from
Feb 4, 2025
Merged
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
15 changes: 7 additions & 8 deletions packages/cosmic-swingset/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { QueuedActionType } from '@agoric/internal/src/action-types.js';
import type {
CoreEval,
CoreEvalSDKType,
} from '@agoric/cosmic-proto/agoric/swingset/swingset.js';
import type { CoreEvalSDKType } from '@agoric/cosmic-proto/agoric/swingset/swingset.js';
import type { BridgeBigInt } from '@agoric/internal';

// TODO move `walletFlags.js` from @agoric/vats to @agoric/cosmic-proto
type PowerFlag = 'SMART_WALLET' | 'REMOTE_WALLET';
Expand All @@ -11,10 +8,12 @@ type PowerFlag = 'SMART_WALLET' | 'REMOTE_WALLET';
// structs or use some other shared type truth
// https://github.com/Agoric/agoric-sdk/issues/8545

export type { BridgeBigInt };

interface ActionContext<T extends Uppercase<string>> {
type: T;
blockHeight: string;
blockTime: string;
blockHeight: BridgeBigInt;
blockTime: BridgeBigInt;
}

/**
Expand All @@ -39,7 +38,7 @@ export type PleaseProvisionAction = ActionContext<'PLEASE_PROVISION'> & {
* @see VbankBalanceUpdate in vbank.go
*/
export type VbankBalanceUpdateAction = ActionContext<'VBANK_BALANCE_UPDATE'> & {
nonce: string;
nonce: BridgeBigInt;
updated: Array<{ address: string; denom: string; amount: string }>;
};

Expand Down
12 changes: 12 additions & 0 deletions packages/internal/src/typeGuards.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @jessie-check
// @ts-check

import { M } from '@endo/patterns';

Expand All @@ -11,3 +12,14 @@ export const UnguardedHelperI = M.interface(
// not exposed so sloppy okay
{ sloppy: true },
);

/**
* @typedef {number | `${bigint}`} BridgeBigInt Ensure that callees passed a
* bridge message that was serialised from a Golang int64 or uint64 accept
* either a JS number or a stringified JS bigint.
*/

/**
* @type {import('./types.js').TypedPattern<BridgeBigInt>}
*/
export const BridgeBigIntShape = M.or(M.number(), M.string());
1 change: 1 addition & 0 deletions packages/internal/test/snapshots/exports.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

[
'BridgeBigIntShape',
'BridgeId',
'CosmosInitKeyToBridgeId',
'NonNullish',
Expand Down
Binary file modified packages/internal/test/snapshots/exports.test.js.snap
Binary file not shown.
14 changes: 9 additions & 5 deletions packages/vats/src/vat-bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { E, Far } from '@endo/far';
import { M, getInterfaceGuardPayload } from '@endo/patterns';

import { AmountMath, AssetKind, BrandShape } from '@agoric/ertp';
import { deeplyFulfilledObject } from '@agoric/internal';
import { deeplyFulfilledObject, BridgeBigIntShape } from '@agoric/internal';
import { prepareGuardedAttenuator } from '@agoric/internal/src/callback.js';
import {
IterableEachTopicI,
Expand All @@ -23,8 +23,8 @@ import {

/**
* @import {Guarded} from '@endo/exo';
* @import {Passable, RemotableObject} from '@endo/pass-style';
* @import {BridgeMessage} from '@agoric/cosmic-swingset/src/types.js';
* @import {RemotableObject} from '@endo/pass-style';
* @import {BridgeMessage, BridgeBigInt} from '@agoric/cosmic-swingset/src/types.js';
*/

const { VirtualPurseControllerI } = makeVirtualPurseKitIKit();
Expand All @@ -42,12 +42,12 @@ const BridgeChannelI = M.interface('BridgeChannel', {

/**
* @typedef {Guarded<{
* update: (value: string, nonce?: string) => void;
* update: (value: string, nonce?: BridgeBigInt) => void;
* }>} BalanceUpdater
*/

const BalanceUpdaterI = M.interface('BalanceUpdater', {
update: M.call(M.string()).optional(M.string()).returns(),
update: M.call(M.string()).optional(BridgeBigIntShape).returns(),
});

/**
Expand All @@ -71,6 +71,10 @@ const prepareBalanceUpdater = zone =>
lastBalanceUpdate: -1n,
}),
{
/**
* @param {string} value
* @param {BridgeBigInt} [nonce]
*/
update(value, nonce = undefined) {
if (nonce !== undefined) {
const thisBalanceUpdate = BigInt(nonce);
Expand Down
10 changes: 9 additions & 1 deletion packages/vats/test/vat-bank.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { buildRootObject } from '../src/vat-bank.js';
/**
* @import {Remote} from '@agoric/internal';
* @import {BridgeHandler, ScopedBridgeManager} from '../src/types.js';
* @import {VbankBalanceUpdateAction} from '@agoric/cosmic-swingset/src/types.js';
*/

const { fakeVomKit } = reincarnate({ relaxDurabilityRules: false });
Expand Down Expand Up @@ -200,7 +201,14 @@ test('communication', async t => {
const notifier = E(vpurse).getCurrentAmountNotifier();
const updateRecord = await E(notifier).getUpdateSince();
const balance = { address: 'agoricfoo', denom: 'ubld', amount: '92929' };
const obj = { type: 'VBANK_BALANCE_UPDATE', updated: [balance] };
/** @type {VbankBalanceUpdateAction} */
const obj = {
type: 'VBANK_BALANCE_UPDATE',
blockHeight: 992827,
nonce: 123,
blockTime: Date.now() / 1000,
updated: [balance],
};
assert(bankHandler);
await E(bankHandler).fromBridge(obj);

Expand Down
Loading