Skip to content

Commit

Permalink
vat-safe cosmic-proto top level (#9743)
Browse files Browse the repository at this point in the history
closes: #9578

## Description

The reason for #9578 was an import from `@agoric/cosmic-proto` in a vat instead of `@agoric/cosmic-proto/vatsafe`. That's quite an easy error to make (a footgun) so this makes the top level module export be the "vat safe" one.

When consumers want the convenience of a barrel export or rpc clients that can resolve anything (#9200), they can use a different subpath.

### Security Considerations
none

### Scaling Considerations
can reduce bundle size

### Documentation Considerations
reduces need to document

### Testing Considerations
existing coverage

### Upgrade Considerations
affects only new bundles
  • Loading branch information
mergify[bot] authored Jul 22, 2024
2 parents e6a0e81 + 7dd34e3 commit ee2126b
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 102 deletions.
24 changes: 3 additions & 21 deletions packages/cosmic-proto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,14 @@ npm install @agoric/cosmic-proto
## Usage
### RPC Clients

```js
import { agoric } from '@agoric/cosmic-proto';

const { createRPCQueryClient } = agoric.ClientFactory;
const client = await createRPCQueryClient({ rpcEndpoint: RPC_ENDPOINT });

const swingsetParams = await client.agoric.swingset.params()
```
TODO #9200

### Composing Messages

Import the `agoric` object from `@agoric/cosmic-proto`.
Import the `agoric` object from the Agoric bundle.

```js
import { agoric } from '@agoric/cosmic-proto';
import { agoric } from '@agoric/cosmic-proto/agoric/bundle.js';

const {
installBundle,
Expand All @@ -54,18 +47,7 @@ const {

Here are the docs on [creating signers](https://github.com/cosmology-tech/cosmos-kit/tree/main/packages/react#signing-clients) in cosmos-kit that can be used with Keplr and other wallets.

### Initializing the Stargate Client

Use `getSigningAgoricClient` to get your `SigningStargateClient`, with the proto/amino messages full-loaded. No need to manually add amino types, just require and initialize the client:

```js
import { getSigningAgoricClient } from '@agoric/cosmic-proto';

const stargateClient = await getSigningAgoricClient({
rpcEndpoint,
signer // OfflineSigner
});
```
### Creating Signers

To broadcast messages, you can create signers with a variety of options:
Expand Down
4 changes: 0 additions & 4 deletions packages/cosmic-proto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./vatsafe": {
"types": "./dist/vatsafe.d.ts",
"default": "./dist/vatsafe.js"
},
"./package.json": "./package.json",
"./agoric/*.js": {
"types": "./dist/codegen/agoric/*.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion packages/cosmic-proto/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from './codegen/index.js';
// Do not export codegen/index.js because it entrains multiple megabytes of module imports.
// Instead we have the top level be relatively conservative, just things that are safe to import into a vat.
// This can include all the types because those are free (never included in a JS bundle).

export * from './codegen/json-safe.js';
export * from './helpers.js';
2 changes: 0 additions & 2 deletions packages/cosmic-proto/src/vatsafe.ts

This file was deleted.

12 changes: 8 additions & 4 deletions packages/cosmic-proto/test/exports.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// @ts-check
/* eslint-disable import/no-extraneous-dependencies -- requiring the package itself to check exports map */
// @ts-check

import test from 'ava';

import '@endo/init';

import * as index from '@agoric/cosmic-proto';
import { agoric } from '@agoric/cosmic-proto/agoric/bundle.js';
import { cosmos } from '@agoric/cosmic-proto/cosmos/bundle.js';
import { ibc } from '@agoric/cosmic-proto/ibc/bundle.js';
import * as swingsetMsgs from '@agoric/cosmic-proto/swingset/msgs.js';
import * as swingsetQuery from '@agoric/cosmic-proto/swingset/query.js';
import * as vstorageQuery from '@agoric/cosmic-proto/vstorage/query.js';
Expand All @@ -26,13 +30,13 @@ test('vstorage/query', t => {
});

test('agoric', t => {
t.snapshot(Object.keys(index.agoric).sort());
t.snapshot(Object.keys(agoric).sort());
});

test('cosmos', t => {
t.snapshot(Object.keys(index.cosmos).sort());
t.snapshot(Object.keys(cosmos).sort());
});

test('ibc', t => {
t.snapshot(Object.keys(index.ibc).sort());
t.snapshot(Object.keys(ibc).sort());
});
16 changes: 2 additions & 14 deletions packages/cosmic-proto/test/readme.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
// @ts-check
/** @file snippets from the README */
/* eslint-disable import/no-extraneous-dependencies -- requiring the package itself to check exports map */
import test from 'ava';

import { agoric } from '../dist/index.js';

const RPC_ENDPOINT = '';

// Skip because we don't have a real endpoint, still tests the types
test.skip('RPC Clients', async t => {
// @ts-expect-error
const { createRPCQueryClient } = agoric.ClientFactory;
const client = await createRPCQueryClient({ rpcEndpoint: RPC_ENDPOINT });
import test from 'ava';

const swingsetParams = await client.agoric.swingset.params();
t.truthy(swingsetParams);
});
import { agoric } from '../dist/codegen/agoric/bundle.js';

test.failing('Composing Messages', t => {
// @ts-expect-error
Expand Down
32 changes: 0 additions & 32 deletions packages/cosmic-proto/test/snapshots/exports.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,9 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
[
'BinaryReader',
'BinaryWriter',
'WireType',
'agoric',
'amino',
'cosmos',
'cosmos_proto',
'gogoproto',
'google',
'ibc',
'icq',
'ics23',
'int64FromString',
'int64Length',
'int64ToString',
'readInt32',
'readUInt32',
'tendermint',
'toRequestQueryJson',
'typeUrlToGrpcPath',
'typedJson',
'uInt64ToString',
'utf8Length',
'utf8Read',
'utf8Write',
'varint32read',
'varint32write',
'varint64read',
'varint64write',
'writeByte',
'writeFixed32',
'writeVarint32',
'writeVarint64',
'zzDecode',
'zzEncode',
]

## swingset/msgs
Expand Down
Binary file modified packages/cosmic-proto/test/snapshots/exports.test.js.snap
Binary file not shown.
22 changes: 0 additions & 22 deletions packages/cosmic-proto/test/vatsafe.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
// @ts-check
/* eslint-disable import/no-extraneous-dependencies -- requiring the package itself to check exports map */
import test from '@endo/ses-ava/prepare-endo.js';
import { cosmos } from '@agoric/cosmic-proto';

import { MsgDelegate } from '@agoric/cosmic-proto/cosmos/staking/v1beta1/tx.js';

// @ts-expect-error
cosmos.staking.v1beta1.MsgDelegate.missing;
// @ts-expect-error
MsgDelegate.missing;

test('imports', t => {
t.is(MsgDelegate, cosmos.staking.v1beta1.MsgDelegate);

t.deepEqual(cosmos.staking.v1beta1.MsgDelegate.fromPartial({}), {
amount: undefined,
delegatorAddress: '',
validatorAddress: '',
});

t.deepEqual(MsgDelegate.fromPartial({}), {
amount: undefined,
delegatorAddress: '',
validatorAddress: '',
});
});

test('proto encoding', t => {
const contents = {
delegatorAddress:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @file Use-object for the owner of a localchain account */
import { typedJson } from '@agoric/cosmic-proto/vatsafe';
import { typedJson } from '@agoric/cosmic-proto';
import { AmountShape, PaymentShape } from '@agoric/ertp';
import { makeTracer } from '@agoric/internal';
import { Shape as NetworkShape } from '@agoric/network';
Expand Down
2 changes: 1 addition & 1 deletion packages/vats/src/proposals/localchain-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
import { heapVowE as E } from '@agoric/vow/vat.js';
import { typedJson } from '@agoric/cosmic-proto/vatsafe';
import { typedJson } from '@agoric/cosmic-proto';

/**
* @param {BootstrapPowers & {
Expand Down

0 comments on commit ee2126b

Please sign in to comment.