Skip to content

Commit

Permalink
chore(vats): fix the lint
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Aug 5, 2023
1 parent 9c081da commit 9dae20d
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 116 deletions.
2 changes: 1 addition & 1 deletion packages/vats/src/network/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function bytesToString(bytes) {
* @returns {string} base64 encoding
*/
export function dataToBase64(data) {
/** @type {Uint8Array?} */
/** @type {Uint8Array} */
let bytes;
if (typeof data === 'string') {
bytes = new Uint8Array(data.length);
Expand Down
41 changes: 24 additions & 17 deletions packages/vats/src/network/multiaddr.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
/**
* @typedef {[string, string][]} Multiaddr
* @typedef {string} Textaddr An address string formatted as in https://github.com/multiformats/multiaddr
*
* Here is the difference between Textaddr and Multiaddr:
*
* unspecified port on local ibc interface: /if/ibc0 [['if', 'ibc0']]
* specific local port: /if/ibc0/ordered/transfer [['if', 'ibc0'], ['ordered', 'transfer']]
* - unspecified port on local ibc interface:
* - /if/ibc0
* - [['if', 'ibc0']]
* - specific local port:
* - /if/ibc0/ordered/transfer
* - [['if', 'ibc0'], ['ordered', 'transfer']]
* - remote pointer to chain:
* - /dnsaddr/ibc.testnet.agoric.com/ordered/transfer
* - [['dnsaddr', 'ibc.testnet.agoric.com'], ['ordered', 'transfer']]
* - resolve step to another pointer:
* - /dnsaddr/rpc.testnet.agoric.com/ibc/testnet-1.19.0/gci/4bc8d.../ordered/transfer
* - [['dnsaddr', 'rpc.testnet.agoric.com'], ['ibc', 'testnet-1.19.0'], ['gci',
* '4bc8d...'], ['ordered', 'transfer']]
* - resolve to the individual interfaces:
* - /ip4/172.17.0.4/tcp/26657/tendermint/0.33/ibc/testnet-1.19.0/gci/4bc8d.../ordered/transfer
* - [['ip4', '172.17.0.4'], ['tcp', '26657'], ['tendermint', '0.33'], ['ibc',
* 'testnet-1.19.0'], ['gci', '4bc8d...'], ['ordered', 'transfer']]
*
* @typedef {[string, string][]} Multiaddr
*
* remote pointer to chain: /dnsaddr/ibc.testnet.agoric.com/ordered/transfer
* [['dnsaddr', 'ibc.testnet.agoric.com'], ['ordered', 'transfer']]
* resolve step to another pointer: /dnsaddr/rpc.testnet.agoric.com/ibc/testnet-1.19.0/gci/4bc8d.../ordered/transfer
* [['dnsaddr', 'rpc.testnet.agoric.com'], ['ibc', 'testnet-1.19.0'], ['gci', '4bc8d...'], ['ordered', 'transfer']]
* resolve to the individual interfaces: /ip4/172.17.0.4/tcp/26657/tendermint/0.33/ibc/testnet-1.19.0/gci/4bc8d.../ordered/transfer
* [['ip4', '172.17.0.4'], ['tcp', '26657'], ['tendermint', '0.33'],
* ['ibc', 'testnet-1.19.0'], ['gci', '4bc8d...'], ['ordered', 'transfer']]
* @typedef {string} Textaddr An address string formatted as in
* https://github.com/multiformats/multiaddr
*/

/**
Expand All @@ -28,13 +37,11 @@ export function parse(ma) {
}
let s = ma;
let m;
/**
* @type {[string, string][]}
*/
/** @type {[string, string][]} */
const acc = [];
// eslint-disable-next-line no-cond-assign
while ((m = s.match(/^\/([^/]*)(\/([^/]*))?/))) {
s = s.substr(m[0].length);
s = s.slice(m[0].length);
if (m[2]) {
acc.push([m[1], m[3]]);
} else {
Expand All @@ -53,7 +60,7 @@ export function parse(ma) {
/**
* Transform a parsed multiaddr to a string.
*
* @param {Multiaddr|Textaddr} ma
* @param {Multiaddr | Textaddr} ma
* @returns {Textaddr}
*/
export function unparse(ma) {
Expand Down
68 changes: 19 additions & 49 deletions packages/vats/src/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import '@agoric/store/exported.js';
import './types.js';

/**
* Compatibility note: this must match what our peers use,
* so don't change it casually.
* Compatibility note: this must match what our peers use, so don't change it
* casually.
*/
export const ENDPOINT_SEPARATOR = '/';

Expand Down Expand Up @@ -42,13 +42,9 @@ export const makeConnection = (
current = new Set(),
) => {
let closed;
/**
* @type {Set<import('@endo/promise-kit').PromiseKit<Bytes>>}
*/
/** @type {Set<import('@endo/promise-kit').PromiseKit<Bytes>>} */
const pendingAcks = new Set();
/**
* @type {Connection}
*/
/** @type {Connection} */
const connection = Far('Connection', {
getLocalAddress() {
return localAddr;
Expand Down Expand Up @@ -120,17 +116,11 @@ export function crossoverConnection(
addr1,
current = new WeakSet(),
) {
/**
* @type {Connection[]}
*/
/** @type {Connection[]} */
const conns = [];
/**
* @type {ConnectionHandler[]}
*/
/** @type {ConnectionHandler[]} */
const handlers = [handler0, handler1];
/**
* @type {Endpoint[]}
*/
/** @type {Endpoint[]} */
const addrs = [addr0, addr1];

function makeHalfConnection(l, r) {
Expand Down Expand Up @@ -193,9 +183,7 @@ export function crossoverConnection(
export function getPrefixes(addr) {
const parts = addr.split(ENDPOINT_SEPARATOR);

/**
* @type {string[]}
*/
/** @type {string[]} */
const ret = [];
for (let i = parts.length; i > 0; i -= 1) {
// Try most specific match.
Expand All @@ -217,21 +205,17 @@ export function makeNetworkProtocol(protocolHandler) {
const currentConnections = makeLegacyMap('port');

/**
* Currently must be a single listenHandler.
* TODO: Do something sensible with multiple handlers?
* Currently must be a single listenHandler. TODO: Do something sensible with
* multiple handlers?
*
* @type {MapStore<Endpoint, [Port, ListenHandler]>}
*/
const listening = makeScalarMapStore('localAddr');

/**
* @type {MapStore<string, Port>}
*/
/** @type {MapStore<string, Port>} */
const boundPorts = makeScalarMapStore('localAddr');

/**
* @param {Endpoint} localAddr
*/
/** @param {Endpoint} localAddr */
const bind = async localAddr => {
// Check if we are underspecified (ends in slash)
const underspecified = localAddr.endsWith(ENDPOINT_SEPARATOR);
Expand All @@ -251,24 +235,18 @@ export function makeNetworkProtocol(protocolHandler) {
return boundPorts.get(localAddr);
}

/**
* @enum {number}
*/
/** @enum {number} */
const RevokeState = {
NOT_REVOKED: 0,
REVOKING: 1,
REVOKED: 2,
};

/**
* @type {RevokeState}
*/
/** @type {RevokeState} */
let revoked = RevokeState.NOT_REVOKED;
const openConnections = new Set();

/**
* @type {Port}
*/
/** @type {Port} */
const port = Far('Port', {
getLocalAddress() {
// Works even after revoke().
Expand Down Expand Up @@ -318,9 +296,7 @@ export function makeNetworkProtocol(protocolHandler) {
},
async connect(remotePort, connectionHandler = {}) {
!revoked || Fail`Port ${localAddr} is revoked`;
/**
* @type {Endpoint}
*/
/** @type {Endpoint} */
const dst = harden(remotePort);
// eslint-disable-next-line no-use-before-define
const conn = await protocolImpl.outbound(port, dst, connectionHandler);
Expand Down Expand Up @@ -361,9 +337,7 @@ export function makeNetworkProtocol(protocolHandler) {
return port;
};

/**
* @type {ProtocolImpl}
*/
/** @type {ProtocolImpl} */
const protocolImpl = Far('ProtocolImpl', {
bind,
async inbound(listenAddr, remoteAddr) {
Expand Down Expand Up @@ -515,9 +489,7 @@ export function makeNetworkProtocol(protocolHandler) {
*/
export function makeEchoConnectionHandler() {
let closed;
/**
* @type {Connection}
*/
/** @type {Connection} */
return Far('ConnectionHandler', {
async onReceive(_connection, bytes, _connectionHandler) {
if (closed) {
Expand Down Expand Up @@ -551,9 +523,7 @@ export function makeNonceMaker(prefix = '', suffix = '') {
export function makeLoopbackProtocolHandler(
onInstantiate = makeNonceMaker('nonce/'),
) {
/**
* @type {MapStore<string, [Port, ListenHandler]>}
*/
/** @type {MapStore<string, [Port, ListenHandler]>} */
const listeners = makeScalarMapStore('localAddr');

const makePortID = makeNonceMaker('port');
Expand Down
17 changes: 8 additions & 9 deletions packages/vats/src/network/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import './types.js';
/**
* @template T
* @typedef {object} Router A delimited string router implementation
* @property {(addr: string) => [string, T][]} getRoutes Return the match and route in order of preference
* @property {(prefix: string, route: T) => void} register Add a prefix->route to the database
* @property {(prefix: string, route: T) => void} unregister Remove a prefix->route from the database
* @property {(addr: string) => [string, T][]} getRoutes Return the match and
* route in order of preference
* @property {(prefix: string, route: T) => void} register Add a prefix->route
* to the database
* @property {(prefix: string, route: T) => void} unregister Remove a
* prefix->route from the database
*/

/**
Expand All @@ -21,16 +24,12 @@ import './types.js';
* @returns {Router<T>} a new Router
*/
export default function makeRouter() {
/**
* @type {MapStore<string, T>}
*/
/** @type {MapStore<string, T>} */
const prefixToRoute = makeScalarMapStore('prefix');
return Far('Router', {
getRoutes(addr) {
const parts = addr.split(ENDPOINT_SEPARATOR);
/**
* @type {[string, T][]}
*/
/** @type {[string, T][]} */
const ret = [];
for (let i = parts.length; i > 0; i -= 1) {
// Try most specific match.
Expand Down
Loading

0 comments on commit 9dae20d

Please sign in to comment.