Skip to content

Commit

Permalink
feat(types): export explicit types
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Apr 30, 2024
1 parent 56d0778 commit 048d344
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 22 deletions.
4 changes: 4 additions & 0 deletions packages/network/src/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import { Fail } from '@agoric/assert';
import { encodeBase64, decodeBase64 } from '@endo/base64';

/**
* @import {Bytes} from './types.js';
*/

/** @typedef {Bytes | Buffer | Uint8Array | Iterable<number>} ByteSource */

/**
Expand Down
1 change: 1 addition & 0 deletions packages/network/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './shapes.js';
export { prepareRouter, prepareRouterProtocol } from './router.js';
export * from './multiaddr.js';
export * from './bytes.js';
export * from './types.js';
47 changes: 28 additions & 19 deletions packages/network/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { Shape } from './shapes.js';

import '@agoric/store/exported.js';
/// <reference path="./types.js" />
/**
* @import {AttemptDescription, Bytes, Closable, CloseReason, Connection, ConnectionHandler, Endpoint, ListenHandler, Port, Protocol, ProtocolHandler, ProtocolImpl} from './types.js';
*/

/**
* Compatibility note: this must match what our peers use, so don't change it
Expand Down Expand Up @@ -409,7 +412,7 @@ const preparePort = (zone, powers) => {
* @param {MapStore<Port, SetStore<Closable>>} opts.currentConnections
* @param {MapStore<string, Port>} opts.boundPorts
* @param {import('@agoric/vow').Remote<ProtocolHandler>} opts.protocolHandler
* @param {Remote<ProtocolImpl>} opts.protocolImpl
* @param {import('@agoric/vow').Remote<ProtocolImpl>} opts.protocolImpl
*/
const initPort = ({
localAddr,
Expand Down Expand Up @@ -454,15 +457,19 @@ const preparePort = (zone, powers) => {
}
listening.set(localAddr, [
this.facets.port,
/** @type {Remote<Required<ListenHandler>>} */ (listenHandler),
/** @type {import('@agoric/vow').Remote<Required<ListenHandler>>} */ (
listenHandler
),
]);
E(lhandler).onRemove(lport, lhandler).catch(rethrowUnlessMissing);
} else {
listening.init(
localAddr,
harden([
this.facets.port,
/** @type {Remote<Required<ListenHandler>>} */ (listenHandler),
/** @type {import('@agoric/vow').Remote<Required<ListenHandler>>} */ (
listenHandler
),
]),
);
}
Expand All @@ -481,7 +488,7 @@ const preparePort = (zone, powers) => {
);
return watch(innerVow, this.facets.rethrowUnlessMissingWatcher);
},
/** @param {Remote<ListenHandler>} listenHandler */
/** @param {import('@agoric/vow').Remote<ListenHandler>} listenHandler */
async removeListener(listenHandler) {
const { listening, localAddr, protocolHandler } = this.state;
listening.has(localAddr) || Fail`Port ${localAddr} is not listening`;
Expand All @@ -503,11 +510,11 @@ const preparePort = (zone, powers) => {
},
/**
* @param {Endpoint} remotePort
* @param {Remote<ConnectionHandler>} [connectionHandler]
* @param {import('@agoric/vow').Remote<ConnectionHandler>} [connectionHandler]
*/
async connect(
remotePort,
connectionHandler = /** @type {Remote<ConnectionHandler>} */ (
connectionHandler = /** @type {import('@agoric/vow').Remote<ConnectionHandler>} */ (
makeIncapable()
),
) {
Expand Down Expand Up @@ -724,8 +731,8 @@ const prepareBinder = (zone, powers) => {
* @param {object} opts
* @param {MapStore<Port, SetStore<Closable>>} opts.currentConnections
* @param {MapStore<string, Port>} opts.boundPorts
* @param {MapStore<Endpoint, [Port, Remote<Required<ListenHandler>>]>} opts.listening
* @param {Remote<ProtocolHandler>} opts.protocolHandler
* @param {MapStore<Endpoint, [Port, import('@agoric/vow').Remote<Required<ListenHandler>>]>} opts.listening
* @param {import('@agoric/vow').Remote<ProtocolHandler>} opts.protocolHandler
*/
({ currentConnections, boundPorts, listening, protocolHandler }) => {
/** @type {SetStore<Connection>} */
Expand Down Expand Up @@ -771,7 +778,7 @@ const prepareBinder = (zone, powers) => {

const innerVow = watch(
E(
/** @type {Remote<Required<ProtocolHandler>>} */ (
/** @type {import('@agoric/vow').Remote<Required<ProtocolHandler>>} */ (
protocolHandler
),
).onInstantiate(
Expand Down Expand Up @@ -813,7 +820,7 @@ const prepareBinder = (zone, powers) => {
// Allocate a local address.
const instantiateInnerVow = watch(
E(
/** @type {Remote<Required<ProtocolHandler>>} */ (
/** @type {import('@agoric/vow').Remote<Required<ProtocolHandler>>} */ (
protocolHandler
),
).onInstantiate(port, localAddr, remoteAddr, protocolHandler),
Expand Down Expand Up @@ -939,7 +946,7 @@ const prepareBinder = (zone, powers) => {

const innerVow = watch(
E(
/** @type {Remote<Required<ProtocolHandler>>} */ (
/** @type {import('@agoric/vow').Remote<Required<ProtocolHandler>>} */ (
protocolHandler
),
).onInstantiate(
Expand Down Expand Up @@ -1169,7 +1176,7 @@ export const prepareNetworkProtocol = (zone, powers) => {
const makeBinderKit = prepareBinder(zone, powers);

/**
* @param {Remote<ProtocolHandler>} protocolHandler
* @param {import('@agoric/vow').Remote<ProtocolHandler>} protocolHandler
* @returns {Protocol}
*/
const makeNetworkProtocol = protocolHandler => {
Expand All @@ -1181,7 +1188,7 @@ export const prepareNetworkProtocol = (zone, powers) => {
/** @type {MapStore<string, Port>} */
const boundPorts = detached.mapStore('addrToPort');

/** @type {MapStore<Endpoint, [Port, Remote<Required<ListenHandler>>]>} */
/** @type {MapStore<Endpoint, [Port, import('@agoric/vow').Remote<Required<ListenHandler>>]>} */
const listening = detached.mapStore('listening');

const { binder, protocolImpl } = makeBinderKit({
Expand Down Expand Up @@ -1293,7 +1300,7 @@ export function prepareLoopbackProtocolHandler(zone, { watch, allVows }) {

/** @param {string} [instancePrefix] */
const initHandler = (instancePrefix = 'nonce/') => {
/** @type {MapStore<string, [Remote<Port>, Remote<Required<ListenHandler>>]>} */
/** @type {MapStore<string, [import('@agoric/vow').Remote<Port>, import('@agoric/vow').Remote<Required<ListenHandler>>]>} */
const listeners = detached.mapStore('localAddr');

return {
Expand Down Expand Up @@ -1325,7 +1332,7 @@ export function prepareLoopbackProtocolHandler(zone, { watch, allVows }) {
* @param {*} _port
* @param {Endpoint} localAddr
* @param {Endpoint} remoteAddr
* @returns {PromiseVow<AttemptDescription>}}
* @returns {import('@agoric/vow').PromiseVow<AttemptDescription>}}
*/
async onConnect(_port, localAddr, remoteAddr) {
const { listeners } = this.state;
Expand Down Expand Up @@ -1372,7 +1379,7 @@ export function prepareLoopbackProtocolHandler(zone, { watch, allVows }) {
localAddr,
harden([
port,
/** @type {Remote<Required<ListenHandler>>} */ (
/** @type {import('@agoric/vow').Remote<Required<ListenHandler>>} */ (
listenHandler
),
]),
Expand All @@ -1383,15 +1390,17 @@ export function prepareLoopbackProtocolHandler(zone, { watch, allVows }) {
localAddr,
harden([
port,
/** @type {Remote<Required<ListenHandler>>} */ (listenHandler),
/** @type {import('@agoric/vow').Remote<Required<ListenHandler>>} */ (
listenHandler
),
]),
);
}
},
/**
* @param {Remote<Port>} port
* @param {import('@agoric/vow').Remote<Port>} port
* @param {Endpoint} localAddr
* @param {Remote<ListenHandler>} listenHandler
* @param {import('@agoric/vow').Remote<ListenHandler>} listenHandler
* @param {*} _protocolHandler
*/
async onListenRemove(port, localAddr, listenHandler, _protocolHandler) {
Expand Down
4 changes: 4 additions & 0 deletions packages/network/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { Shape } from './shapes.js';
// XXX ambient types runtime imports until https://github.com/Agoric/agoric-sdk/issues/6512
import '@agoric/store/exported.js';
/// <reference path="./types.js" />
/**
* @import {AttemptDescription, Bytes, Closable, CloseReason, Connection, ConnectionHandler, Endpoint, ListenHandler, Port, Protocol, ProtocolHandler, ProtocolImpl} from './types.js';
* @import {PromiseVow, Remote, VowKit, VowResolver, VowTools} from '@agoric/vow';
*/

/**
* @template T
Expand Down
3 changes: 3 additions & 0 deletions packages/network/src/types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// @ts-check

// Ensure this is a module.
export {};

/** @import {PromiseVow, Remote} from '@agoric/vow' */

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/network/test/test-network-misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
preparePortAllocator,
} from '../src/index.js';

import '../src/types.js';
/**
* @import {ListenHandler} from '../src/types.js';
*/

// eslint-disable-next-line no-constant-condition
const log = false ? console.log : () => {};
Expand Down
5 changes: 3 additions & 2 deletions packages/orchestration/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import { makeICAConnectionAddress, parseAddress } from './utils/address.js';
import { makeTxPacket, parsePacketAck } from './utils/tx.js';

/**
* @import { ChainAccount, ChainAddress } from './types.js';
* @import {Connection, Port, PortAllocator} from '@agoric/network';
* @import {Remote} from '@agoric/vow';
* @import { IBCConnectionID } from '@agoric/vats';
* @import { Zone } from '@agoric/base-zone';
* @import { TxBody } from '@agoric/cosmic-proto/cosmos/tx/v1beta1/tx.js';
*
* @import { AttenuatedNetwork, ChainAccount, ChainAddress } from './types.js';
*/

const { Fail, bare } = assert;
Expand Down
5 changes: 5 additions & 0 deletions packages/vats/src/proposals/network-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { makeScalarBigMapStore } from '@agoric/vat-data';
// bootstrap vat can't yet be upgraded.
import { when } from '@agoric/vat-data/vow.js';

/**
* @import {ProtocolHandler} from '@agoric/network';
* @import {Remote} from '@agoric/vow';
*/

const NUM_IBC_PORTS_PER_CLIENT = 3;

/**
Expand Down

0 comments on commit 048d344

Please sign in to comment.