-
Notifications
You must be signed in to change notification settings - Fork 74
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(marshal): encode capData in 1 level of JSON #1804
Draft
dckc
wants to merge
2
commits into
master
Choose a base branch
from
dc-1-layer-json
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// @ts-check | ||
|
||
const { Fail } = assert; | ||
|
||
/** @param {unknown} x */ | ||
const assertJSON = x => { | ||
assert.typeof(x, 'string'); | ||
void JSON.parse(x); | ||
}; | ||
|
||
/** | ||
* @param {import('./types').CapData<unknown>} capData - with "simple" slots; | ||
* that is: slots whose JSON form has no occurrence of `:[`. | ||
* @returns {string} | ||
*/ | ||
export const capDataToJSON = ({ body, slots }) => { | ||
assert(Array.isArray(slots)); | ||
const slotj = JSON.stringify(slots); | ||
slotj.indexOf(':[') < 0 || Fail`expected simple slots`; | ||
const body1 = body.replace(/^#/, ''); | ||
assertJSON(body1); | ||
const json = `{"$body":${body1},"slots":${slotj}}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In #1478 (comment) I suggest |
||
assertJSON(json); | ||
return json; | ||
}; | ||
|
||
export const JSONToCapData = json => { | ||
assert.typeof(json, 'string'); | ||
json.startsWith('{"$body":') || Fail`expected $body`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this only works when this body is first in the serialized JSON, not second? |
||
json.endsWith('}') || Fail`expected }`; | ||
const pos = json.lastIndexOf(':['); | ||
pos > 0 || Fail`expected slots`; | ||
const body = `#${json.slice('{"$body":'.length, pos - ',"slots"'.length)}`; | ||
const slotj = json.slice(pos + 1, -1); | ||
const slots = JSON.parse(slotj); | ||
Array.isArray(slots) || Fail`expected slots to be Array`; | ||
return { body, slots }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/** | ||
* @file avoid double-JSON encoding capData | ||
*/ | ||
|
||
// @ts-check | ||
|
||
// eslint-disable-next-line import/order | ||
import { test } from './prepare-test-env-ava.js'; | ||
|
||
import { arbPassable } from '@endo/pass-style/tools.js'; | ||
import { fc } from '@fast-check/ava'; | ||
import { isKey, keyEQ } from '@endo/patterns'; | ||
|
||
import { Far, passStyleOf } from '@endo/pass-style'; | ||
import { makeTranslationTable } from './translationTable.js'; | ||
import { makeMarshal } from '../src/marshal.js'; | ||
import { JSONToCapData, capDataToJSON } from '../src/capDataJSON.js'; | ||
|
||
const smallCaps = /** @type {const} */ ({ | ||
serializeBodyFormat: 'smallcaps', | ||
marshalSaveError: err => err, | ||
}); | ||
|
||
const makeTestMarshal = () => { | ||
const synthesizeRemotable = (_slot, iface) => | ||
Far(iface.replace(/^Alleged: /, ''), {}); | ||
const makeSlot = (v, serial) => { | ||
const sty = passStyleOf(v); | ||
if (sty === 'remotable') return `r${serial}`; | ||
return `a(n) ${sty}`; | ||
}; | ||
const tt = makeTranslationTable(makeSlot, synthesizeRemotable); | ||
|
||
const m = makeMarshal(tt.convertValToSlot, tt.convertSlotToVal, smallCaps); | ||
return m; | ||
}; | ||
|
||
const brands = { | ||
IST: Far('IST Brand', {}), | ||
ATOM: Far('ATOM Brand', {}), | ||
}; | ||
|
||
const suite = [ | ||
{ obj: null, json: '{"$body":null,"slots":[]}' }, | ||
{ obj: [1, 2, undefined], json: '{"$body":[1,2,"#undefined"],"slots":[]}' }, | ||
{ obj: { slots: [] }, json: '{"$body":{"slots":[]},"slots":[]}' }, | ||
// example from https://github.com/Agoric/agoric-sdk/issues/7999 | ||
{ | ||
obj: { | ||
method: 'executeOffer', | ||
offer: { | ||
id: 'bid-1688229012779', | ||
invitationSpec: { | ||
callPipe: [['makeBidInvitation', [brands.ATOM]]], | ||
instancePath: ['auctioneer'], | ||
source: 'agoricContract', | ||
}, | ||
offerArgs: { | ||
maxBuy: { | ||
brand: brands.ATOM, | ||
value: 1_000_000_000_000n, | ||
}, | ||
offerPrice: { | ||
denominator: { | ||
brand: brands.ATOM, | ||
value: 1n, | ||
}, | ||
numerator: { | ||
brand: brands.IST, | ||
value: 7n, | ||
}, | ||
}, | ||
}, | ||
proposal: { | ||
give: { | ||
Bid: { | ||
brand: brands.IST, | ||
value: 3000n, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
json: '{"$body":{"method":"executeOffer","offer":{"id":"bid-1688229012779","invitationSpec":{"callPipe":[["makeBidInvitation",["$0.Alleged: ATOM Brand"]]],"instancePath":["auctioneer"],"source":"agoricContract"},"offerArgs":{"maxBuy":{"brand":"$0","value":"+1000000000000"},"offerPrice":{"denominator":{"brand":"$0","value":"+1"},"numerator":{"brand":"$1.Alleged: IST Brand","value":"+7"}}},"proposal":{"give":{"Bid":{"brand":"$1","value":"+3000"}}}}},"slots":["r0","r1"]}', | ||
}, | ||
]; | ||
harden(suite); | ||
|
||
test('encode example passables in 1 level of JSON', t => { | ||
const m = makeTestMarshal(); | ||
|
||
for (const { obj, json } of suite) { | ||
// t.log(obj); | ||
const cd = m.toCapData(obj); | ||
const j = capDataToJSON(cd); | ||
t.is(j, json); | ||
|
||
const cd2 = JSONToCapData(j); | ||
t.deepEqual(cd, cd2); | ||
const v = m.fromCapData(cd); | ||
keyEQ(obj, v) ? t.pass() : t.deepEqual(obj, v); | ||
} | ||
}); | ||
|
||
test('encode arbitrary passable in 1 level of JSON', t => { | ||
const m = makeTestMarshal(); | ||
fc.assert( | ||
fc.property(fc.record({ x: arbPassable }), ({ x }) => { | ||
const { body, slots } = m.toCapData(x); | ||
// t.log({ body, slots }); | ||
const j = capDataToJSON({ body, slots }); | ||
const cd = JSONToCapData(j); | ||
// t.log({ cd }); | ||
|
||
const j2 = capDataToJSON(cd); | ||
t.is(j, j2); | ||
|
||
const cd2 = JSONToCapData(j2); | ||
t.deepEqual(cd, cd2); | ||
|
||
if (isKey(x)) { | ||
const v = m.fromCapData(cd); | ||
try { | ||
if (keyEQ(x, v)) { | ||
t.pass(); | ||
} else { | ||
// explain what's different | ||
t.deepEqual(x, v); | ||
} | ||
} catch (err) { | ||
if (err.message.startsWith('Map comparison not yet implemented:')) { | ||
t.pass(); | ||
} else { | ||
t.fail(err); | ||
} | ||
} | ||
} | ||
}), | ||
{ numRuns: 5_000 }, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// #region marshal-table | ||
const makeSlot1 = (val, serial) => { | ||
const prefix = Promise.resolve(val) === val ? 'promise' : 'object'; | ||
return `${prefix}${serial}`; | ||
}; | ||
|
||
export const makeTranslationTable = ( | ||
makeSlot = makeSlot1, | ||
makeVal = x => x, | ||
) => { | ||
const valToSlot = new Map(); | ||
const slotToVal = new Map(); | ||
|
||
const convertValToSlot = val => { | ||
if (valToSlot.has(val)) return valToSlot.get(val); | ||
const slot = makeSlot(val, valToSlot.size); | ||
valToSlot.set(val, slot); | ||
slotToVal.set(slot, val); | ||
return slot; | ||
}; | ||
|
||
const convertSlotToVal = (slot, iface) => { | ||
if (slotToVal.has(slot)) return slotToVal.get(slot); | ||
if (makeVal) { | ||
const val = makeVal(slot, iface); | ||
valToSlot.set(val, slot); | ||
slotToVal.set(slot, val); | ||
return val; | ||
} | ||
throw Error(`no such ${iface}: ${slot}`); | ||
}; | ||
|
||
return harden({ convertValToSlot, convertSlotToVal }); | ||
}; | ||
// #endregion marshal-table |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not check body[0] === '#' and do
body.slice(1)
, I think that's a lot more efficient.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to assume that the argument is a CapData record whose
body
is a "#"-prefixed JSON serialization of SmallCaps-encoded data, which would need a lot more explanation than appears here (and a better name).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
body.replace(/^#/, '')
handles both smallCaps and qclass, no? (I haven't tested it, though).Why is
.slice(1)
significantly more efficient?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that depends upon what this function is expected to return. Regardless of the answer to that, though, CapData like
{ body: `{"@qclass":"bigint","digits":"0"}`, slots: [] }
and{ body: `#"+0"`, slots: [] }
represent exactly the same data (0n
) but would have distinctString('{"$body":{"@qclass":"bigint","digits":"0"},"slots":[]}')
andString('{"$body":"+0","slots":[]}')
return values (respectively) from the current implementation—which seems like a problem because there's no remaining signal differentiating smallcaps from the legacy encoding.The answer is implementation-specific, but basically comes down to being zero-copy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...
I guess I trained my regex intuitions in perl where such things are optimized out the wazoo.
Thanks for the
esbench
details.