Skip to content

Commit

Permalink
test(boot): break out vtransfer.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jun 6, 2024
1 parent e3772fd commit eaa7c5b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 94 deletions.
94 changes: 0 additions & 94 deletions packages/boot/test/bootstrapTests/demo-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
/* eslint-disable @jessie.js/safe-await-separator -- confused by casting 'as' */
import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import { PowerFlags } from '@agoric/vats/src/walletFlags.js';

import type { TestFn } from 'ava';

import type { BridgeHandler, ScopedBridgeManager } from '@agoric/vats';
import type {
TransferMiddleware,
TransferVat,
} from '@agoric/vats/src/vat-transfer.js';
import { BridgeId, VTRANSFER_IBC_EVENT } from '@agoric/internal';
import { keyArrayEqual, makeSwingsetTestKit } from '../../tools/supports.ts';

const { keys } = Object;
Expand Down Expand Up @@ -118,90 +111,3 @@ test('demo config meets loadgen constraint: no USDC', async t => {

// FIXME tests can pass when console shows "BOOTSTRAP FAILED"
test.todo('demo config bootstrap succeeds');

test('vtransfer', async t => {
const { buildProposal, evalProposal, getOutboundMessages, runUtils } =
t.context;
const { EV } = runUtils;

// Pull what transfer-proposal produced into local scope
const transferVat = (await EV.vat('bootstrap').consumeItem(
'transferVat',
)) as ERef<TransferVat>;
t.truthy(transferVat);
const transferMiddleware = (await EV.vat('bootstrap').consumeItem(
'transferMiddleware',
)) as TransferMiddleware;
t.truthy(transferMiddleware);
const vtransferBridgeManager = (await EV.vat('bootstrap').consumeItem(
'vtransferBridgeManager',
)) as ScopedBridgeManager<'vtransfer'>;
t.truthy(vtransferBridgeManager);

// only VTRANSFER_IBC_EVENT is supported by vtransferBridgeManager
await t.throwsAsync(
EV(vtransferBridgeManager).fromBridge({
type: 'VTRANSFER_OTHER',
}),
{
message: `Invalid inbound event type "VTRANSFER_OTHER"; expected "${VTRANSFER_IBC_EVENT}"`,
},
);

const target = 'agoric1vtransfertest';
const packet = 'thisIsPacket';

// 0 interceptors for target

// it's an error to target an address before an interceptor is registered
await t.throwsAsync(
EV(vtransferBridgeManager).fromBridge({
target,
type: VTRANSFER_IBC_EVENT,
event: 'echo',
}),
{
message:
'key "agoric1vtransfertest" not found in collection "targetToApp"',
},
);

// 1 interceptors for target

// Tap into VTRANSFER_IBC_EVENT messages
const testVtransferProposal = buildProposal(
'@agoric/builders/scripts/vats/test-vtransfer.js',
);
await evalProposal(testVtransferProposal);

// simulate a Golang upcall with arbitrary payload
// note that property order matters!
const expectedAckData = {
type: VTRANSFER_IBC_EVENT,
event: 'writeAcknowledgement',
packet,
target,
};

await EV(vtransferBridgeManager).fromBridge(expectedAckData);

// verify the ackMethod outbound
const messages = getOutboundMessages(BridgeId.VTRANSFER);
t.deepEqual(messages, [
{
target,
type: 'BRIDGE_TARGET_REGISTER',
},
{
ack: btoa(JSON.stringify(expectedAckData)),
method: 'receiveExecuted',
packet,
type: 'IBC_METHOD',
},
]);

// test adding an interceptor for the same target, which should fail
await t.throwsAsync(() => evalProposal(testVtransferProposal), {
message: /Target.*already registered/,
});
});
115 changes: 115 additions & 0 deletions packages/boot/test/bootstrapTests/vtransfer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* eslint-disable @jessie.js/safe-await-separator -- confused by casting 'as' */
import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import type { TestFn } from 'ava';

import type { ScopedBridgeManager } from '@agoric/vats';
import type {
TransferMiddleware,
TransferVat,
} from '@agoric/vats/src/vat-transfer.js';
import { BridgeId, VTRANSFER_IBC_EVENT } from '@agoric/internal';
import { makeSwingsetTestKit } from '../../tools/supports.ts';

const makeDefaultTestContext = async t => {
const swingsetTestKit = await makeSwingsetTestKit(
t.log,
'bundles/vtransfer',
{ configSpecifier: '@agoric/vm-config/decentral-demo-config.json' },
);
return swingsetTestKit;
};

type DefaultTestContext = Awaited<ReturnType<typeof makeDefaultTestContext>>;

const test: TestFn<DefaultTestContext> = anyTest;

test.before(async t => (t.context = await makeDefaultTestContext(t)));
test.after.always(t => t.context.shutdown?.());

test('vtransfer', async t => {
const { buildProposal, evalProposal, getOutboundMessages, runUtils } =
t.context;
const { EV } = runUtils;

// Pull what transfer-proposal produced into local scope
const transferVat = (await EV.vat('bootstrap').consumeItem(
'transferVat',
)) as ERef<TransferVat>;
t.truthy(transferVat);
const transferMiddleware = (await EV.vat('bootstrap').consumeItem(
'transferMiddleware',
)) as TransferMiddleware;
t.truthy(transferMiddleware);
const vtransferBridgeManager = (await EV.vat('bootstrap').consumeItem(
'vtransferBridgeManager',
)) as ScopedBridgeManager<'vtransfer'>;
t.truthy(vtransferBridgeManager);

// only VTRANSFER_IBC_EVENT is supported by vtransferBridgeManager
await t.throwsAsync(
EV(vtransferBridgeManager).fromBridge({
type: 'VTRANSFER_OTHER',
}),
{
message: `Invalid inbound event type "VTRANSFER_OTHER"; expected "${VTRANSFER_IBC_EVENT}"`,
},
);

const target = 'agoric1vtransfertest';
const packet = 'thisIsPacket';

// 0 interceptors for target

// it's an error to target an address before an interceptor is registered
await t.throwsAsync(
EV(vtransferBridgeManager).fromBridge({
target,
type: VTRANSFER_IBC_EVENT,
event: 'echo',
}),
{
message:
'key "agoric1vtransfertest" not found in collection "targetToApp"',
},
);

// 1 interceptors for target

// Tap into VTRANSFER_IBC_EVENT messages
const testVtransferProposal = buildProposal(
'@agoric/builders/scripts/vats/test-vtransfer.js',
);
await evalProposal(testVtransferProposal);

// simulate a Golang upcall with arbitrary payload
// note that property order matters!
const expectedAckData = {
event: 'writeAcknowledgement',
packet,
target,
type: VTRANSFER_IBC_EVENT,
};

await EV(vtransferBridgeManager).fromBridge(expectedAckData);

// verify the ackMethod outbound
const messages = getOutboundMessages(BridgeId.VTRANSFER);
t.deepEqual(messages, [
{
target,
type: 'BRIDGE_TARGET_REGISTER',
},
{
ack: btoa(JSON.stringify(expectedAckData)),
method: 'receiveExecuted',
packet,
type: 'IBC_METHOD',
},
]);

// test adding an interceptor for the same target, which should fail
await t.throwsAsync(() => evalProposal(testVtransferProposal), {
message: /Target.*already registered/,
});
});

0 comments on commit eaa7c5b

Please sign in to comment.