Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet/api): marshal contexts for wallet, board #5883

Merged
merged 17 commits into from
Aug 10, 2022
Merged

Conversation

dckc
Copy link
Member

@dckc dckc commented Aug 3, 2022

refs: #4398
obsoletes #5874

Description

When serializing wallet state for chainStorage, there's a tension between

  • keeping purses etc. closely held
  • recognizing identity of brands also referenced in the state of contracts such as the AMM

makeMarshal is parameterized by the type of slots. Here we use a disjoint union of

  • board ids for widely shared objects
  • kind:seq ids for closely held objects; for example purse:123

@samsiegart note that unlike #5874 , this PR doesn't include any changes to lib-wallet.js; I hope you can layer those changes on top of this as a form of review.

This PR includes makeLoggingPresence, which is some advanced work on smart wallet middleware. While the design is still in progress, test coverage of this version is good enough that I'd like to land it.

Security Considerations

As @warner noted, there's a risk that chainStorage data from the AMM could try to refer to a purse using a slots such as purse:123; such a reference must fail.

Documentation Considerations

not much; these are internal APIs.

Testing Considerations

Test coverage is 100%.

agoric-sdk/packages/wallet/api$ yarn c8 ava test/test-wallet-marshal.js test/test-middleware.js
yarn run v1.22.19
$ .../.bin/c8 ava test/test-wallet-marshal.js test/test-middleware.js

  ✔ middleware › makeImportContext in wallet UI can unserialize messages
  ✔ middleware › makeLoggingPresence logs calls on purse/payment actions
  ✔ wallet-marshal › makeImportContext preserves identity across AMM and wallet
  ✔ wallet-marshal › ensureBoardId allows re-registration; initBoardId does not
  ✔ wallet-marshal › makeExportContext.serialize handles unregistered identites
  ─

  5 tests passed
---------------------|---------|----------|---------|---------|-------------------
File                 | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------------|---------|----------|---------|---------|-------------------
All files            |     100 |      100 |     100 |     100 |                   
 marshal-contexts.js |     100 |      100 |     100 |     100 |                   
---------------------|---------|----------|---------|---------|-------------------
Done in 0.71s.

@dckc
Copy link
Member Author

dckc commented Aug 8, 2022

good progress:

agoric-sdk/packages/wallet/api$ yarn c8 ava test/test-wallet-marshal.js test/test-middleware.js
yarn run v1.22.19
$ /home/connolly/projects/agoric-sdk/node_modules/.bin/c8 ava test/test-wallet-marshal.js test/test-middleware.js

  ✔ middleware › makeImportContext in wallet UI can unserialize messages
  ✔ middleware › makeLoggingPresence logs calls on purse/payment actions
  ✔ wallet-marshal › makeImportContext preserves identity across AMM and wallet
  ✔ wallet-marshal › ensureBoardId allows re-registration; initBoardId does not
  ✔ wallet-marshal › makeExportContext.serialize handles unregistered identites
  ─

  5 tests passed
---------------------|---------|----------|---------|---------|-------------------
File                 | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------------|---------|----------|---------|---------|-------------------
All files            |   99.61 |    97.29 |     100 |   99.61 |                   
 marshal-contexts.js |   99.61 |    97.29 |     100 |   99.61 | 206               
---------------------|---------|----------|---------|---------|-------------------
Done in 0.79s.

@dckc dckc marked this pull request as ready for review August 9, 2022 00:50
@dckc dckc changed the title WIP: smart wallet middleware marshal contexts for smart wallet with board integration Aug 9, 2022
@samsiegart
Copy link
Contributor

So you'd like me to do something like b516b66 to integrate this? Can we make a checklist of things to do, like purses, payments, issuers, brands, etc

['applyFunction', purse.actions, [1, 'thing']],
]);

const ctx = makeExportContext();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about this test case, because ctx I thought would be inside lib-wallet, while the logging presence would be used outside lib-wallet

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point; I guess I'll think this over some more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to come back to this in a later PR.

packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
packages/wallet/api/src/marshal-contexts.js Outdated Show resolved Hide resolved
@dckc dckc requested a review from turadg August 9, 2022 19:11
Copy link
Member

@turadg turadg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good.

I don't know how to assess this:

there's a risk that chainStorage data from the AMM could try to refer to a purse using a slots such as purse:123; such a reference must fail.

Can a test be added for that?

Comment on lines 27 to 28
* @template T
* @typedef {`${string & keyof T}:${Digits}`} WalletSlot<T>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bravo on the string template type. non-blocking suggestion to help document how this works,

Suggested change
* @template T
* @typedef {`${string & keyof T}:${Digits}`} WalletSlot<T>
* @template {Record<string, IdTable<*,*>} T
* @typedef {`${keyof T}:${Digits}`} WalletSlot<T>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I integrated most of that suggestion. Constraining T that way helps not only documentation but also got rid of one or two @ts-expect-errors.

But getting rid of string & doesn't seem to fly.

* @returns {WalletSlot<T>}
*/
const makeWalletSlot = (_tables, kind, id) => {
const digits = /** @type {Digits} */ (`${id}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider asserting id >= 1 for the digits

*/

/**
* @template T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for documentation purposes,

Suggested change
* @template T
* @template {Record<string, IdTable<*,*>} T

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; I'll give that a try.

Earlier I explored similar things without luck and punted.

*/
const findKey = (record, predicate) => {
const key = Object.keys(record).find(predicate);
// @ts-expect-error clearly keys(r).find() is keyof T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, keyof T | undefined. the jsdoc is right but this comment is loose

/**
* @template T
* @param {T} _tables
* @param {string & keyof T} kind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the above would also simplify this,

Suggested change
* @param {string & keyof T} kind
* @param {keyof T} kind

* @template T
* @param {T} record
* @param {(value: string, index: number, obj: string[]) => boolean} predicate
* @returns {string & keyof T | undefined}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto on above suggestions about typing the template

};

/**
* Since KindSlots always include a colon and BoardIds never do,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

Comment on lines +157 to +158
t.deepEqual(cap2, {
body: JSON.stringify({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when these don't match, it'll be a string diff. consider comparing the object with JSON.parse(cap2).

If not comparing objects then it should be t.is

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is supposed to be at the CapData level, where body is a string. Writing the expected body using JSON.stringify() makes it easier to read. I suppose I could add a comment...

Meanwhile, your comment prompts me to think about this a bit more carefully... In the API, slots are compared by identity, so t.deepEqual() is only correct because we happen to be using primitives (strings). In nearby designs, the slots are { kind: 'purse', id: 123} objects where it's really easy to have things that pass t.deepEqual() but not t.is().

});
});

// {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code

@dckc
Copy link
Member Author

dckc commented Aug 10, 2022

I don't know how to assess this:

there's a risk that chainStorage data from the AMM could try to refer to a purse using a slots such as purse:123; such a reference must fail.

Can a test be added for that?

It's in there:

test('makeImportContext preserves identity across AMM and wallet', t => {
...
  t.throws(
    () => context.fromBoard.unserialize(walletCapData),
    { message: /bad board slot/ },
    'AMM cannot refer to purses',
  );

p.s. "AMM cannot forge references to purses" would be a better phrasing.

Copy link
Member

@turadg turadg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving now. (No automerge label so you'll be able to try the TS tweak before it merges.)

 - perhaps one bug fix around `purse:1` vs `1`?
@dckc dckc added the automerge:squash Automatically squash merge label Aug 10, 2022
@turadg turadg changed the title marshal contexts for smart wallet with board integration feat(wallet/api): marshal contexts for wallet, board Aug 10, 2022
@mergify mergify bot merged commit 088e144 into master Aug 10, 2022
@mergify mergify bot deleted the dc-smart-middle branch August 10, 2022 18:37
@turadg turadg mentioned this pull request Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge:squash Automatically squash merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants