Skip to content

Commit

Permalink
fix(contract): adapt for agoric-upgrade-16 (but not yet durable)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jun 20, 2024
1 parent 21e7007 commit 9db7e47
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 50 deletions.
3 changes: 2 additions & 1 deletion contract/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/
"downlevelIteration": true,
"strictNullChecks": true,
"moduleResolution": "node",
"moduleResolution": "NodeNext",
"module": "NodeNext",
},
"include": ["src/**/*.js", "test/**/*.js", "exported.js", "globals.d.ts"],
}
3 changes: 3 additions & 0 deletions contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@
"dependencies": {
"@endo/bundle-source": "^2.1.1",
"@endo/base64": "^0.2.30",
"@endo/far": "^1.1.2",
"@agoric/assert": "beta",
"@agoric/deploy-script-support": "beta",
"@agoric/ertp": "beta",
"@endo/eventual-send": "^0.14.8",
"@endo/init": "^0.5.37",
"@endo/marshal": "^0.6.9",
"@agoric/cosmic-proto": "beta",
"@agoric/notifier": "beta",
"@agoric/store": "beta",
"@agoric/zoe": "beta",
"@agoric/vow": "beta",
"@agoric/babel-standalone": "^7.17.7",
"@agoric/swingset-vat": "beta",
"@cosmjs/encoding": "^0.28.4",
Expand Down
25 changes: 18 additions & 7 deletions contract/src/contract.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check
/* global harden */
import '@agoric/zoe/exported.js';
import { Far } from '@endo/marshal';
import { Far } from '@endo/far';
import { ICS27ICAProtocol } from './ica.js';

/**
Expand All @@ -15,21 +15,32 @@ const start = () => {

const publicFacet = Far('publicFacet', {
// Public faucet for anyone to call
/**
* @param {import('@agoric/network').Port} port
* @param {object} connectionHandler
* @param {string} controllerConnectionId
* @param {string} hostConnectionId
*/
createICAAccount: (
/** @type {Port} */ port,
/** @type {object} */ connectionHandler,
/** @type {string} */ controllerConnectionId,
/** @type {string} */ hostConnectionId,
port,
connectionHandler,
controllerConnectionId,
hostConnectionId,
) =>
ICS27ICAProtocol.createICS27Account(
port,
connectionHandler,
controllerConnectionId,
hostConnectionId,
),
/**
*
* @param {[import('./types.js').Msg]} msgs
* @param {import('@agoric/network').Connection} connection
*/
sendICATxPacket: (
/** @type {[Msg]} */ msgs,
/** @type {Connection} */ connection,
msgs,
connection,
) => ICS27ICAProtocol.sendICATx(msgs, connection),
});

Expand Down
26 changes: 16 additions & 10 deletions contract/src/ica.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
// @ts-check
import { Far } from '@endo/marshal';
import { E, Far } from '@endo/far';
import { assert, details as X } from '@agoric/assert';
import { TxBody } from 'cosmjs-types/cosmos/tx/v1beta1/tx.js';
import { Any } from 'cosmjs-types/google/protobuf/any.js';
import { E } from '@endo/eventual-send';
import { TxBody } from '@agoric/cosmic-proto/cosmos/tx/v1beta1/tx.js';
import { Any } from '@agoric/cosmic-proto/google/protobuf/any.js';
import { toBase64, fromBase64 } from '@cosmjs/encoding/build/base64.js';
import { when } from '@agoric/vow/vat.js';

/**
* @import { Msg, ICAProtocol, ICS27ICAPacket } from './types.js';
* @import { PromiseVow } from '@agoric/vow';
* @import { ConnectionHandler, Connection, Port, Bytes } from '@agoric/network';
*/

/**
* Create an ICA account/channel on the connection provided
*
* @param {Port} port
* @param {object} connectionHandler
* @param {ConnectionHandler} connectionHandler
* @param {string} controllerConnectionId
* @param {string} hostConnectionId
* @returns {Promise<Connection>}
* @returns {PromiseVow<Connection>}
*/
export const createICAAccount = async (
port,
Expand All @@ -30,10 +36,10 @@ export const createICAAccount = async (
txType: 'sdk_multi_msg',
});

const connection = await E(port).connect(
const connection = await when(E(port).connect(
`/ibc-hop/${controllerConnectionId}/ibc-port/icahost/ordered/${connString}`,
connectionHandler,
);
));

return connection;
};
Expand Down Expand Up @@ -86,10 +92,10 @@ export const sendICAPacket = async (msgs, connection) => {
memo: '',
};

/** @type {Data} */
/** @type {Bytes} */
const packet = JSON.stringify(ics27);

const res = await E(connection).send(packet);
const res = await when(E(connection).send(packet));

return res;
};
Expand Down
11 changes: 7 additions & 4 deletions contract/src/types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// @ts-check

/**
* @import { PromiseVow } from '@agoric/vow';
* @import { Bytes, Connection, Port } from '@agoric/network';
*/
/**
*
* @typedef {number} Type
Expand All @@ -12,7 +15,7 @@
/**
* @typedef {object} Packet
* @property {Type} type
* @property {Data} data
* @property {Bytes} data
* @property {Memo} memo
*/

Expand All @@ -24,8 +27,8 @@

/**
* @typedef {object} ICAProtocol
* @property {(msgs: [Msg], connection: Connection) => Promise<string>} sendICATx
* @property {(port: Port, connectionHandler: object, controllerConnectionId: string, hostConnectionId: string) => Promise<Connection>} createICS27Account
* @property {(msgs: [Msg], connection: Connection) => PromiseVow<string>} sendICATx
* @property {(port: Port, connectionHandler: object, controllerConnectionId: string, hostConnectionId: string) => PromiseVow<Connection>} createICS27Account
*/

/**
Expand All @@ -37,6 +40,6 @@
/**
* @typedef {object} ICS27ICAPacket
* @property {Type} type The int32 type of the transaction (ICA only supports Type 1)
* @property {Data} data The byte encoding of a list of messages in {Type: xxx, Value: {}} format
* @property {Bytes} data The byte encoding of a list of messages in {Type: xxx, Value: {}} format
* @property {Memo} memo Optional memo for the tx. Defaults to blank ""
*/
26 changes: 22 additions & 4 deletions contract/test/prepare-test-env-ava.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
/**
* Like prepare-test-env but also sets up ses-ava and provides
* the ses-ava `test` function to be used as if it is the ava
Expand All @@ -6,10 +7,27 @@

import '@endo/init/pre-bundle-source.js';
import '@agoric/zoe/tools/prepare-test-env.js';
// eslint-disable-next-line import/no-unresolved -- https://github.com/avajs/ava/issues/2951
import rawTest from 'ava';
import { wrapTest } from '@endo/ses-ava';

// XXX wrapTest not working https://github.com/endojs/endo/issues/1235
// import { wrapTest } from '@endo/ses-ava';
import { prepareNetworkProtocol, prepareLoopbackProtocolHandler } from '@agoric/network';
import { makeHeapZone } from '@agoric/zone/heap.js';
import { vowTools } from '@agoric/vow/vat.js';

export const test = rawTest;
/** @typedef {Awaited<ReturnType<makeTestContext>>} Context */

/** @typedef {import('ava').ExecutionContext<Context>} ExecutionContext */
export const test = /** @type {import('ava').TestInterface<Context>} */ (/** @type {unknown} */ (wrapTest(rawTest)));

/**
* @param {any} _t
*/
export const makeTestContext = async _t => {
const zone = makeHeapZone();
const makeLoopbackProtocolHandler = prepareLoopbackProtocolHandler(zone, vowTools);
const makeNetworkProtocol = prepareNetworkProtocol(zone, vowTools);

const protocol = makeNetworkProtocol(makeLoopbackProtocolHandler());

return { zone, protocol };
};
51 changes: 27 additions & 24 deletions contract/test/test-contract.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
// @ts-nocheck
// @ts-check
import '@agoric/babel-standalone';
import { MsgSend } from 'cosmjs-types/cosmos/bank/v1beta1/tx.js';
import { MsgSend } from '@agoric/cosmic-proto/cosmos/bank/v1beta1/tx.js';

import { test } from './prepare-test-env-ava.js';
import { test, makeTestContext } from './prepare-test-env-ava.js';
import path from 'path';

import { E } from '@endo/eventual-send';
import { when } from '@agoric/vow/vat.js';
import { E, Far } from '@endo/far';
import { encodeBase64 } from '@endo/base64';
import {
makeNetworkProtocol,
makeLoopbackProtocolHandler,
} from '@agoric/swingset-vat/src/vats/network/index.js';
import { Far } from '@endo/marshal/src/make-far.js';
import { makePromiseKit } from '@endo/promise-kit';
import bundleSource from '@endo/bundle-source';
import { makeFakeVatAdmin } from '@agoric/zoe/tools/fakeVatAdmin.js';
import { makeZoeKit } from '@agoric/zoe';
import { makeZoeKitForTest } from '@agoric/zoe/tools/setup-zoe.js';

const filename = new URL(import.meta.url).pathname;
const dirname = path.dirname(filename);

const contractPath = `${dirname}/../src/contract.js`;

test.beforeEach(async (t) => {
t.context = await makeTestContext(t);
});

/**
* @param {import('./prepare-test-env-ava.js').ExecutionContext} t
*/
const testPublicFacet = async (t) => {
const { zoeService: zoe } = makeZoeKit(makeFakeVatAdmin().admin);
const { zoeService: zoe } = makeZoeKitForTest();

const bundle = await bundleSource(contractPath);
const installation = await E(zoe).install(bundle);
const instance = await E(zoe).startInstance(installation);

// Create a network protocol to be used for testing
const protocol = makeNetworkProtocol(makeLoopbackProtocolHandler());
const { protocol } = t.context;

/** @type {import('@endo/promise-kit').PromiseRecord<void>} */
const closed = makePromiseKit();

// Get public faucet from ICA instance
Expand All @@ -47,16 +50,16 @@ const testPublicFacet = async (t) => {
const msgBytes = MsgSend.encode(msgType).finish();

// Create first port that packet will be sent to
const port = await protocol.bind('/loopback/foo');
const port = await when(E(protocol).bindPort('/loopback/foo'));

/**
* Create the listener for the test port
*
* @type {import('../src/vats/network').ListenHandler}
* @type {import('@agoric/network').ListenHandler}
*/
const listener = Far('listener', {
async onAccept(_p, _localAddr, _remoteAddr, _listenHandler) {
return harden({
return Far('connection', {
async onReceive(c, packet, _connectionHandler) {
// Check that recieved packet is the packet we created above
console.log('Received Packet on Port 1:', packet);
Expand All @@ -65,34 +68,34 @@ const testPublicFacet = async (t) => {
});
},
});
await port.addListener(listener);
await when(E(port).addListener(listener));

// Create and send packet to first port utilizing port 2
const port2 = await protocol.bind('/loopback/bar');
await port2.connect(
port.getLocalAddress(),
const port2 = when(E(protocol).bindPort('/loopback/bar'));
await when(E(port2).connect(
await when(E(port).getLocalAddress()),
Far('opener', {
async onOpen(c, localAddr, remoteAddr, _connectionHandler) {
t.is(localAddr, '/loopback/bar/nonce/1');
t.is(remoteAddr, '/loopback/foo/nonce/2');
const pingack = await E(instance.publicFacet).sendICATxPacket(
const pingack = await when(E(instance.publicFacet).sendICATxPacket(
[
{
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
data: encodeBase64(msgBytes),
}
],
c,
);
));
t.is(pingack, 'pingack', 'expected pingack');
closed.resolve();
},
}),
);
));

await closed.promise;

await port.removeListener(listener);
await when(E(port).removeListener(listener));
};

test('raw - send interchain tx', async (t) => {
Expand Down

0 comments on commit 9db7e47

Please sign in to comment.