From 1fdf8e8c78b005a34e688a87dc43a2eb979678b7 Mon Sep 17 00:00:00 2001 From: Greg Slepak Date: Thu, 28 Mar 2024 18:09:25 +0900 Subject: [PATCH] v2.2.0 - name field to contract manifests + latest eventsAfter API support --- HISTORY.md | 3 ++- README.md | 8 ++++---- bin/chel | 1 - build/main.js | 46 ++++++++++++++++++++++++---------------------- package-lock.json | 4 ++-- package.json | 2 +- 6 files changed, 33 insertions(+), 31 deletions(-) delete mode 100644 bin/chel diff --git a/HISTORY.md b/HISTORY.md index 18ca6d0..37cb408 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,8 +1,9 @@ # HISTORY -### v2.1.2 +### v2.2.0 - Add a `name` field to the manifest with the contract's name +- Updated `eventsAfter` API to support latest changes related to use of height and streaming ### v2.1.1 diff --git a/README.md b/README.md index c768270..fe9d8b6 100644 --- a/README.md +++ b/README.md @@ -93,10 +93,10 @@ cp -r path/to/contracts/* test/assets/ && ls ./test/assets/*-slim.js | sed -En ' Current release hashes will always be listed here. ``` -7560ff9459761030fa57aab206c6730395ab0f32acffd94fc52858fb9df89d98 dist/chel-v2.1.1-aarch64-apple-darwin.tar.gz -fbd8a6d19a63e4506fa22c3c4a7eb66379a571bef06c47ed0ab99cf60b86a524 dist/chel-v2.1.1-x86_64-apple-darwin.tar.gz -0f0014508aa61b6593e1626d63adbe713e8d7a8be51bec798a7f515ed2fc2966 dist/chel-v2.1.1-x86_64-pc-windows-msvc.tar.gz -20324f5a2d3bac5680ede772a8332f1563e87cd93ba18f6cc07d773e7e51fa16 dist/chel-v2.1.1-x86_64-unknown-linux-gnu.tar.gz +fb9b81db169cbba480c8a33ce807d5fdbb077813607cc9284f98b73c2ded9d76 dist/chel-v2.2.0-aarch64-apple-darwin.tar.gz +8e6b12e4ec9df02a3e18138ac14f7228dd1481ae172c33f43805eed0f2eded20 dist/chel-v2.2.0-x86_64-apple-darwin.tar.gz +e6331a9edf20de1df7aa5a78426f8f7a7068a5fc294140dab983947f13af6b04 dist/chel-v2.2.0-x86_64-pc-windows-msvc.tar.gz +70f83f77d39bb77eb523d70bd30a447f5c5a619b2a0150f0a95de12845d6f739 dist/chel-v2.2.0-x86_64-unknown-linux-gnu.tar.gz ``` ## History diff --git a/bin/chel b/bin/chel deleted file mode 100644 index c1a26eb..0000000 --- a/bin/chel +++ /dev/null @@ -1 +0,0 @@ -placeholder, will be replaced by real binary diff --git a/build/main.js b/build/main.js index abaade7..60104dd 100755 --- a/build/main.js +++ b/build/main.js @@ -371,16 +371,17 @@ async function eventsAfter(args) { const limit = Number(parsedArgs.limit ?? defaultLimit); if (!isArrayLength(limit)) exit("argument --limit must be a valid array length"); - const [urlOrLocalPath, contractID, hash2] = parsedArgs._.map(String); + const [urlOrLocalPath, contractID] = parsedArgs._.map(String); + const height = Number(parsedArgs._[2]); const src = urlOrLocalPath; try { let messages; if (isURL(src)) { - messages = await getRemoteMessagesSince(src, contractID, hash2, limit); + messages = await getRemoteMessagesSince(src, contractID, height, limit); } else { - messages = await getMessagesSince(src, contractID, hash2, limit); + messages = await getMessagesSince(src, contractID, height, limit); } - console.log(JSON.stringify(messages.map((s) => JSON.parse(s)), null, 2)); + console.log(JSON.stringify(messages, null, 2)); } catch (error) { exit(error.message); } @@ -389,44 +390,43 @@ async function getMessage(hash2) { const value = await readString(hash2); if (!value) throw new Error(`no entry for ${hash2}!`); - return JSON.parse(value).message; + return JSON.parse(value); } -async function getMessagesSince(src, contractID, since, limit) { +async function getMessagesSince(src, contractID, sinceHeight, limit) { backend = await getBackend(src); const contractHEAD = await readString(`${headPrefix}${contractID}`); if (contractHEAD === void 0) { throw new Deno.errors.NotFound(`contract ${contractID} doesn't exist!`); } const entries = []; - let currentHEAD = contractHEAD; + let currentHEAD = JSON.parse(contractHEAD).HEAD; + let currentHeight; while (true) { const entry = await getMessage(currentHEAD); if (!entry) { throw new Deno.errors.NotFound(`entry ${currentHEAD} no longer exists.`); } + const head = JSON.parse(entry.head); + currentHeight = head.height; entries.push(entry); - if (currentHEAD === since) { + if (currentHeight === sinceHeight) { break; - } else { - currentHEAD = JSON.parse(entry).previousHEAD; - if (currentHEAD === null) { - throw new Deno.errors.NotFound(`entry ${since} was not found in contract ${contractID}.`); - } } + currentHEAD = head.previousHEAD; } return entries.reverse().slice(0, limit); } -async function getRemoteMessagesSince(src, contractID, since, limit) { - const response = await fetch(`${src}/eventsAfter/${contractID}/${since}`); +async function getRemoteMessagesSince(src, contractID, sinceHeight, limit) { + const response = await fetch(`${src}/eventsAfter/${contractID}/${sinceHeight}`); if (!response.ok) { const bodyText = await response.text().catch((_) => "") || ``; throw new Error(`failed network request to ${src}: ${response.status} - ${response.statusText} - '${bodyText}'`); } - const b64messages = (await response.json()).reverse(); + const b64messages = await response.json(); if (b64messages.length > limit) { b64messages.length = limit; } - return b64messages.map((b64str) => JSON.parse(new TextDecoder().decode(base64.decode(b64str))).message); + return b64messages.map((b64str) => JSON.parse(new TextDecoder().decode(base64.decode(b64str)))); } async function readString(key) { const rv = await backend.readData(key); @@ -480,11 +480,11 @@ function help(args) { chel version chel keygen [--out ] [--pubout ] chel verifySignature [-k ] - chel manifest [-k|--key [-k|--key ...]] [--out=] [-s|--slim ] [-v|--version ] + chel manifest [-k|--key [-k|--key ...]] [--out=] [-s|--slim ] [-v|--version ] [-n|--name ] chel deploy [ [ ...]] chel upload [ [ ...]] chel latestState - chel eventsAfter [--limit N] + chel eventsAfter [--limit N] chel eventsBefore [--limit N] chel get chel hash @@ -766,10 +766,11 @@ async function manifest(args) { const parsedArgs = flags.parse(args, { collect: ["key"], alias: { "key": "k" } }); const [keyFile, contractFile] = parsedArgs._; const parsedFilepath = path.parse(contractFile); - const { name: contractName, base: contractBasename, dir: contractDir } = parsedFilepath; + const { name: contractFileName, base: contractBasename, dir: contractDir } = parsedFilepath; + const name = parsedArgs.name || parsedArgs.n || contractFileName; const version2 = parsedArgs.version || parsedArgs.v || "x"; const slim = parsedArgs.slim || parsedArgs.s; - const outFilepath = path.join(contractDir, `${contractName}.${version2}.manifest.json`); + const outFilepath = path.join(contractDir, `${contractFileName}.${version2}.manifest.json`); if (!keyFile) exit("Missing signing key file"); const signingKeyDescriptor = await readJsonFile(keyFile); @@ -783,6 +784,7 @@ async function manifest(args) { return serializeKey(key, false); }) || [])))); const body = { + name, version: version2, contract: { hash: await hash([contractFile], true), @@ -934,7 +936,7 @@ var verifySignature2 = async (args, internal = false) => { // src/version.ts function version() { - console.log("2.1.1"); + console.log("2.2.0"); } // src/main.ts diff --git a/package-lock.json b/package-lock.json index 0963ce1..29f9dec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@chelonia/cli", - "version": "2.1.2", + "version": "2.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@chelonia/cli", - "version": "2.1.2", + "version": "2.2.0", "hasInstallScript": true, "license": "AGPL-3.0", "dependencies": { diff --git a/package.json b/package.json index 72ff615..8f6261c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@chelonia/cli", - "version": "2.1.2", + "version": "2.2.0", "description": "Chelonia Command-line Interface", "main": "src/main.ts", "scripts": {