Skip to content

Commit

Permalink
simplify Orch types (#9448)
Browse files Browse the repository at this point in the history
no ticket, incidental

## Description

Simplify JsonSafe and ResponseTo types in orchestration package


### Security Considerations

<!-- Does this change introduce new assumptions or dependencies that, if
violated, could introduce security vulnerabilities? How does this PR
change the boundaries between mutually-suspicious components? What new
authorities are introduced by this change, perhaps by new API calls?
-->

### Scaling Considerations

<!-- Does this change require or encourage significant increase in
consumption of CPU cycles, RAM, on-chain storage, message exchanges, or
other scarce resources? If so, can that be prevented or mitigated? -->

### Documentation Considerations

<!-- Give our docs folks some hints about what needs to be described to
downstream users.

Backwards compatibility: what happens to existing data or deployments
when this code is shipped? Do we need to instruct users to do something
to upgrade their saved data? If there is no upgrade path possible, how
bad will that be for users?

-->

### Testing Considerations

<!-- Every PR should of course come with tests of its own functionality.
What additional tests are still needed beyond those unit tests? How does
this affect CI, other test automation, or the testnet?
-->

### Upgrade Considerations

<!-- What aspects of this PR are relevant to upgrading live production
systems, and how should they be addressed? -->
  • Loading branch information
mergify[bot] committed Jun 4, 2024
2 parents 35d20eb + 58b3abc commit 96dbb12
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 35 deletions.
2 changes: 1 addition & 1 deletion a3p-integration/proposals/c:stake-bld/stakeBld.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ test('basic', async t => {
t.is(postDelegation.length, 2, 'new delegation now');
t.like(postDelegation[1], {
balance: { amount: '10', denom: 'ubld' },
// omit 'delegation' because it has 'delgatorAddress' which is different every test run
// omit 'delegation' because it has 'delegatorAddress' which is different every test run
});
});
2 changes: 1 addition & 1 deletion packages/cosmic-proto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"devDependencies": {
"@agoric/cosmos": "^0.34.1",
"@ava/typescript": "^4.1.0",
"@cosmology/telescope": "^1.7.0",
"@cosmology/telescope": "^1.7.1",
"ava": "^5.3.1",
"rimraf": "^5.0.0",
"tsd": "^0.30.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-proto/src/codegen/binary.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.0
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-proto/src/codegen/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.0
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-proto/src/codegen/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.0
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down
8 changes: 3 additions & 5 deletions packages/cosmic-proto/src/codegen/json-safe.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.0
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/

export type JsonSafe<T> = {
[Prop in keyof T]: T[Prop] extends Uint8Array
[Prop in keyof T]: T[Prop] extends Uint8Array | bigint | Date
? string
: T[Prop] extends bigint
? string
: T[Prop];
: T[Prop];
};
2 changes: 1 addition & 1 deletion packages/cosmic-proto/src/codegen/utf8.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.0
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-proto/src/codegen/varint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.0
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down
31 changes: 19 additions & 12 deletions packages/cosmic-proto/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type {
Bech32PrefixRequest,
Bech32PrefixResponse,
} from './codegen/cosmos/auth/v1beta1/query.js';
import type {
QueryAllBalancesRequest,
QueryAllBalancesResponse,
Expand Down Expand Up @@ -39,15 +43,8 @@ export type Proto3Shape = {
'/cosmos.staking.v1beta1.MsgUndelegateResponse': MsgUndelegateResponse;
'/ibc.applications.transfer.v1.MsgTransfer': MsgTransfer;
'/ibc.applications.transfer.v1.MsgTransferResponse': MsgTransferResponse;
};

// Often s/Request$/Response/ but not always
type ResponseMap = {
'/cosmos.bank.v1beta1.MsgSend': '/cosmos.bank.v1beta1.MsgSendResponse';
'/cosmos.bank.v1beta1.QueryAllBalancesRequest': '/cosmos.bank.v1beta1.QueryAllBalancesResponse';
'/cosmos.staking.v1beta1.MsgDelegate': '/cosmos.staking.v1beta1.MsgDelegateResponse';
'/ibc.applications.transfer.v1.MsgTransfer': '/ibc.applications.transfer.v1.MsgTransferResponse';
'/cosmos.staking.v1beta1.MsgUndelegate': '/cosmos.staking.v1beta1.MsgUndelegateResponse';
'/cosmos.auth.v1beta1.Bech32PrefixRequest': Bech32PrefixRequest;
'/cosmos.auth.v1beta1.Bech32PrefixResponse': Bech32PrefixResponse;
};

/**
Expand All @@ -64,10 +61,20 @@ export type TypedJson<T extends unknown | keyof Proto3Shape = unknown> =
}
: { '@type': string };

/** General pattern for Request that has a corresponding Response */
type RequestTypeUrl<Base extends string> = `/${Base}Request`;
/** Pattern specific to Msg sends, in which "Msg" without "Response" implies it's a request */
type TxMessageTypeUrl<
Package extends string,
Name extends Capitalize<string>,
> = `/${Package}.Msg${Name}`;

export type ResponseTo<T extends TypedJson> =
T['@type'] extends keyof ResponseMap
? TypedJson<ResponseMap[T['@type']]>
: TypedJson;
T['@type'] extends RequestTypeUrl<infer Base>
? TypedJson<`/${Base}Response`>
: T['@type'] extends TxMessageTypeUrl<infer Package, infer Name>
? TypedJson<`/${Package}.Msg${Name}Response`>
: TypedJson;

export const typedJson = <T extends keyof Proto3Shape>(
typeStr: T,
Expand Down
42 changes: 41 additions & 1 deletion packages/cosmic-proto/test/helpers.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expectType } from 'tsd';
import { typedJson } from '../src/helpers.js';
import type { ResponseTo } from '../src/helpers.ts';
import type { ResponseTo, TypedJson } from '../src/helpers.ts';
import type { JsonSafe } from '../src/codegen/json-safe.js';

// MsgSend
{
Expand Down Expand Up @@ -30,3 +31,42 @@ import type { ResponseTo } from '../src/helpers.ts';
response['@type'] = '/cosmos.bank.v1beta1.QueryAllBalancesRequest';
response.balances = [{ denom: 'ucosm', amount: '1' }];
}

// Bech32Prefix
{
const request = typedJson('/cosmos.auth.v1beta1.Bech32PrefixRequest', {});
const response: ResponseTo<typeof request> = null as any;
expectType<'/cosmos.auth.v1beta1.Bech32PrefixResponse'>(response['@type']);
response['@type'] = '/cosmos.auth.v1beta1.Bech32PrefixResponse';
}

// Delegation
{
const request = typedJson('/cosmos.staking.v1beta1.MsgDelegate', {
amount: { denom: 'ucosm', amount: '1' },
delegatorAddress: 'agoric1from',
validatorAddress: 'agoric1to',
});
const response: ResponseTo<typeof request> = null as any;
expectType<'/cosmos.staking.v1beta1.MsgDelegateResponse'>(response['@type']);
}
{
const request = typedJson('/cosmos.staking.v1beta1.MsgUndelegate', {
amount: { denom: 'ucosm', amount: '1' },
delegatorAddress: 'agoric1from',
validatorAddress: 'agoric1to',
});
const response: ResponseTo<typeof request> = null as any;
expectType<'/cosmos.staking.v1beta1.MsgUndelegateResponse'>(
response['@type'],
);
}

// JsonSafe
{
const response: TypedJson<'/cosmos.staking.v1beta1.MsgUndelegateResponse'> =
null as any;
expectType<Date>(response.completionTime);
const responseJson: JsonSafe<typeof response> = null as any;
expectType<string>(responseJson.completionTime);
}
1 change: 0 additions & 1 deletion packages/vats/tools/fake-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ export const makeFakeLocalchainBridge = (zone, onToBridge = () => {}) => {
return /** @type {JsonSafe<MsgDelegateResponse>} */ ({});
}
case '/cosmos.staking.v1beta1.MsgUndelegate': {
// @ts-expect-error XXX JsonSafe doesn't handle Date
return /** @type {JsonSafe<MsgUndelegateResponse>} */ ({
completionTime: new Date().toJSON(),
});
Expand Down
File renamed without changes.
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1272,10 +1272,10 @@
resolved "https://registry.yarnpkg.com/@cosmjs/utils/-/utils-0.32.3.tgz#5dcaee6dd7cc846cdc073e9a7a7f63242f5f7e31"
integrity sha512-WCZK4yksj2hBDz4w7xFZQTRZQ/RJhBX26uFHmmQFIcNUUVAihrLO+RerqJgk0dZqC42wstM9pEUQGtPmLcIYvg==

"@cosmology/ast@^1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@cosmology/ast/-/ast-1.7.0.tgz#f3ae766349c469ce1171b5f8a930b59b30dcde74"
integrity sha512-/0TR0Wk+uSs5rkfGCc1IzESVuSGMndbyIsRp0Rt0nzVv7MVvGKG6Vo+VvELgqHScmOsPINilOJvAdM+k2fGU8Q==
"@cosmology/ast@^1.7.1":
version "1.7.1"
resolved "https://registry.yarnpkg.com/@cosmology/ast/-/ast-1.7.1.tgz#a93e38d08c1c35d6e209cb04e580cd2b325a1110"
integrity sha512-dLqKUpxy9G8iS4Imd9JRDnP6Ca7EkyiqWWwgZacBLLuFK0mIDed0Fb8JP85pExV26XTC/Un3Nhi8yWiNQlKakQ==
dependencies:
"@babel/types" "7.23.6"
"@cosmology/types" "^1.7.0"
Expand Down Expand Up @@ -1315,16 +1315,16 @@
"@types/node" ">=13.7.0"
long "^4.0.0"

"@cosmology/telescope@^1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@cosmology/telescope/-/telescope-1.7.0.tgz#c07722afb68b2c76eec61d1d641eede22b5b52af"
integrity sha512-5pQxnkeqFvRFMHog2yIYO6DSHExRnPEhbzPRUMFcHarbF6d00d81mAmqslD+5JOLC1Yp2FO9adKzAEhAwSeO7A==
"@cosmology/telescope@^1.7.1":
version "1.7.1"
resolved "https://registry.yarnpkg.com/@cosmology/telescope/-/telescope-1.7.1.tgz#1d4357bfc49c41a03c5fbe91fe60c028ffbdfcf6"
integrity sha512-mB/h8iGfVNi+ZV3KzEy6xFKVlAe9d9pOTz/Vu+OCMcrEFS/xHgWMs+eNDGAbWuaCp06qYyZSc0pN7rMVojnElg==
dependencies:
"@babel/generator" "^7.23.6"
"@babel/parser" "^7.23.6"
"@babel/traverse" "7.23.6"
"@babel/types" "7.23.6"
"@cosmology/ast" "^1.7.0"
"@cosmology/ast" "^1.7.1"
"@cosmology/proto-parser" "^1.7.0"
"@cosmology/types" "^1.7.0"
"@cosmology/utils" "^1.7.0"
Expand Down

0 comments on commit 96dbb12

Please sign in to comment.