-
Notifications
You must be signed in to change notification settings - Fork 0
feat(colors): prove second-client acceptance #575
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e101ad
refactor(colors): share the private Program ABI v2 contract
lemone112 a28c590
test(colors): add the independent worker client
lemone112 bd07367
test(colors): serve admitted worker fixtures
lemone112 ff8e2fb
test(colors): kill a vacuous second-client proof
lemone112 b1f548e
fix(release): admit the private ABI package member
lemone112 c4836ce
fix(colors): close second-client review findings
lemone112 158a908
fix(colors): bind and bound the worker acceptance path
lemone112 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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,29 @@ | ||
| export const OUTPUT_BINDING = "--lab-private-program-output"; | ||
| export const EXPECTED_OUTPUT = 17; | ||
| export const EXPECTED_SINK_OUTPUT = 501; | ||
| export const REQUEST_HEX = "4c43465102004600404040000000000000e03f00000000000050409a9999999999c93f01606060f50100001f0000000100000000000000010100000080808000000000000000"; | ||
| export const UPDATE_HEX = "4c434655020028001f00000002000000000000000101020000008080800000000000000000000000"; | ||
| export const CHANGED_UPDATE_HEX = "4c434655020028001f0000000300000000000000010103000000ffffff0000000000000000000000"; | ||
| export const EXPECTED_CONTENT_IDENTITY = "4daf4731d823ba228f4189d08c76183dd8a81e8b593ff709828b7329e075a914"; | ||
|
|
||
| export function exactWireBytes(hex) { | ||
| if (!/^(?:[0-9a-f]{2})+$/u.test(hex)) throw new TypeError("wire vector must be lowercase hex"); | ||
| const bytes = new Uint8Array(hex.length / 2); | ||
| for (let index = 0; index < bytes.length; index += 1) { | ||
| bytes[index] = Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16); | ||
| } | ||
| return bytes; | ||
| } | ||
|
|
||
| export function receiptFingerprint(receipt) { | ||
| return JSON.stringify({ | ||
| output: receipt.output, | ||
| sinkOutput: receipt.sinkOutput, | ||
| state: receipt.state, | ||
| stream: receipt.stream, | ||
| revision: receipt.revision.toString(), | ||
| paintSource: receipt.paintSource, | ||
| paintOpacityBits: receipt.paintOpacityBits.toString(16).padStart(16, "0"), | ||
| contentIdentity: receipt.contentIdentity, | ||
| }); | ||
| } |
This file contains hidden or 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,171 @@ | ||
| import { | ||
| decodeCertifiedReceipt, | ||
| DISPOSE_TOKEN_BASE_V1, | ||
| EXPORTS_V1, | ||
| HOST_CONFIRM_DISPOSED_V1, | ||
| HOST_DISPOSE_CONFIRMED_V1, | ||
| HOST_INSTALL_SUCCESS_V1, | ||
| HOST_INSTALL_V1, | ||
| HOST_MODULE_V1, | ||
| OPERATION_CONFIRM_EXACT_V1, | ||
| OPERATION_REVOKE_ALL_V1, | ||
| OPERATION_SET_ALL_V1, | ||
| REQUEST_REVISION_OFFSET, | ||
| REQUEST_SINK_OUTPUT_OFFSET, | ||
| REQUEST_STREAM_OFFSET, | ||
| REQUEST_V2_LENGTH, | ||
| RESULT_V2_LENGTH, | ||
| UPDATE_REVISION_OFFSET, | ||
| UPDATE_STREAM_OFFSET, | ||
| UPDATE_V2_LENGTH, | ||
| } from "/installed/private-program/abi-v2.js"; | ||
| import { | ||
| CHANGED_UPDATE_HEX, | ||
| exactWireBytes, | ||
| receiptFingerprint, | ||
| REQUEST_HEX, | ||
| UPDATE_HEX, | ||
| } from "./vectors.mjs"; | ||
|
|
||
| const WASM_URL = "/installed/private-program/labcolors_private_program.wasm"; | ||
|
|
||
| function u32(value) { | ||
| return value >>> 0; | ||
| } | ||
|
|
||
| function checkedView(memory, pointer, length) { | ||
| const offset = u32(pointer); | ||
| if (offset > memory.buffer.byteLength || length > memory.buffer.byteLength - offset) { | ||
| throw new RangeError("private Program worker memory range is invalid"); | ||
| } | ||
| return new Uint8Array(memory.buffer, offset, length); | ||
| } | ||
|
|
||
| async function runLifecycle() { | ||
| let exports; | ||
| let installedOutput = 0; | ||
| let installedSinkOutput = 0; | ||
| let expectedSinkOutput = 0; | ||
| let sequence = 0n; | ||
| let generation = 0; | ||
| let disposeConfirmed = false; | ||
|
|
||
| function install( | ||
| rawGeneration, | ||
| rawOperation, | ||
| _revisionLow, | ||
| _revisionHigh, | ||
| expectedLow, | ||
| expectedHigh, | ||
| desiredLow, | ||
| desiredHigh, | ||
| rawOutput, | ||
| rawSinkOutput, | ||
| ) { | ||
| generation = u32(rawGeneration); | ||
| const operation = u32(rawOperation); | ||
| const expected = BigInt(u32(expectedLow)) | (BigInt(u32(expectedHigh)) << 32n); | ||
| const desired = BigInt(u32(desiredLow)) | (BigInt(u32(desiredHigh)) << 32n); | ||
| const output = u32(rawOutput); | ||
| const sinkOutput = u32(rawSinkOutput); | ||
| if (operation === OPERATION_CONFIRM_EXACT_V1) { | ||
| return expected === sequence && desired === sequence && output === installedOutput && sinkOutput === installedSinkOutput | ||
| ? HOST_INSTALL_SUCCESS_V1 | ||
| : 0; | ||
| } | ||
| if (expected !== sequence || desired !== sequence + 1n) return 0; | ||
| if (operation === OPERATION_SET_ALL_V1) { | ||
| if (output === 0 || sinkOutput !== expectedSinkOutput) return 0; | ||
| installedOutput = output; | ||
| installedSinkOutput = sinkOutput; | ||
| } else if (operation === OPERATION_REVOKE_ALL_V1) { | ||
| if (output !== 0 || sinkOutput !== 0) return 0; | ||
| installedOutput = 0; | ||
| installedSinkOutput = 0; | ||
| } else { | ||
| return 0; | ||
| } | ||
| sequence = desired; | ||
| return HOST_INSTALL_SUCCESS_V1; | ||
| } | ||
|
|
||
| function confirmDisposed(rawGeneration, rawToken) { | ||
| disposeConfirmed = u32(rawGeneration) === generation && u32(rawToken) === generation; | ||
| return disposeConfirmed ? HOST_DISPOSE_CONFIRMED_V1 : 0; | ||
| } | ||
|
|
||
| const response = await fetch(WASM_URL); | ||
| if (response.ok !== true) throw new Error(`private Program WASM fetch failed: ${response.status}`); | ||
| const source = await response.arrayBuffer(); | ||
| const instance = await WebAssembly.instantiate(source, { | ||
| [HOST_MODULE_V1]: { | ||
| [HOST_INSTALL_V1]: install, | ||
| [HOST_CONFIRM_DISPOSED_V1]: confirmDisposed, | ||
| }, | ||
| }); | ||
| exports = instance.instance.exports; | ||
| const memory = exports.memory; | ||
|
|
||
| function invoke(bytes, pointerName, length, operationName, minimumRevision) { | ||
| checkedView(memory, exports[pointerName](), length).set(bytes); | ||
| const status = u32(exports[operationName]()); | ||
| if (status !== 0) throw new Error(`${operationName} failed with status ${status}`); | ||
| return decodeCertifiedReceipt({ | ||
| bytes: new Uint8Array(checkedView(memory, exports[EXPORTS_V1.resultPointer](), RESULT_V2_LENGTH)), | ||
| expectedSinkOutput, | ||
| installedOutput, | ||
| installedSinkOutput, | ||
| expectedStream: new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32( | ||
| operationName === EXPORTS_V1.run ? REQUEST_STREAM_OFFSET : UPDATE_STREAM_OFFSET, | ||
| true, | ||
| ), | ||
| minimumRevision, | ||
| }); | ||
| } | ||
|
|
||
| const request = exactWireBytes(REQUEST_HEX); | ||
| expectedSinkOutput = new DataView(request.buffer).getUint32(REQUEST_SINK_OUTPUT_OFFSET, true); | ||
| const initial = invoke( | ||
| request, | ||
| EXPORTS_V1.requestPointer, | ||
| REQUEST_V2_LENGTH, | ||
| EXPORTS_V1.run, | ||
| new DataView(request.buffer).getBigUint64(REQUEST_REVISION_OFFSET, true), | ||
| ); | ||
| const update = exactWireBytes(UPDATE_HEX); | ||
| const updated = invoke( | ||
| update, | ||
| EXPORTS_V1.updatePointer, | ||
| UPDATE_V2_LENGTH, | ||
| EXPORTS_V1.update, | ||
| new DataView(update.buffer).getBigUint64(UPDATE_REVISION_OFFSET, true), | ||
| ); | ||
| const changedUpdate = exactWireBytes(CHANGED_UPDATE_HEX); | ||
| const changed = invoke( | ||
| changedUpdate, | ||
| EXPORTS_V1.updatePointer, | ||
| UPDATE_V2_LENGTH, | ||
| EXPORTS_V1.update, | ||
| new DataView(changedUpdate.buffer).getBigUint64(UPDATE_REVISION_OFFSET, true), | ||
| ); | ||
|
|
||
| const token = u32(exports[EXPORTS_V1.beginDispose]()); | ||
| if (token !== generation + DISPOSE_TOKEN_BASE_V1) throw new Error("dispose token mismatch"); | ||
| if (u32(exports[EXPORTS_V1.commitDispose](token)) !== 0 || !disposeConfirmed) { | ||
| throw new Error("dispose confirmation mismatch"); | ||
| } | ||
| return Object.freeze({ | ||
| initial: receiptFingerprint(initial), | ||
| updated: receiptFingerprint(updated), | ||
| changed: receiptFingerprint(changed), | ||
| }); | ||
| } | ||
|
|
||
| self.addEventListener("message", async (event) => { | ||
| if (event.data !== "run") return; | ||
| try { | ||
| self.postMessage({ ok: true, value: await runLifecycle() }); | ||
| } catch (error) { | ||
| self.postMessage({ ok: false, error: error instanceof Error ? error.message : String(error) }); | ||
| } | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.