Skip to content

Commit

Permalink
feat(dev-cli): auto-set memory-limit flag based on module requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterFarber committed Aug 28, 2024
1 parent 17f5917 commit c345b2c
Showing 1 changed file with 84 additions and 41 deletions.
125 changes: 84 additions & 41 deletions dev-cli/src/commands/publish.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,86 @@
/* global Deno */

import { Command, basename, resolve } from '../deps.js'
import { hostArgs, tagsArg } from '../utils.js'
import { VERSION } from '../versions.js'
import {Command, basename, resolve} from "../deps.js";
import {hostArgs, tagsArg} from "../utils.js";
import {VERSION} from "../versions.js";
import {parse, stringify} from "jsr:@std/yaml";

function walletArgs (wallet) {
function walletArgs(wallet) {
/**
* Use wallet in pwd by default
*/
wallet = wallet || 'wallet.json'
const walletName = basename(wallet)
const walletDest = `/src/${walletName}`
wallet = wallet || "wallet.json";
const walletName = basename(wallet);
const walletDest = `/src/${walletName}`;

const walletSrc = resolve(wallet)
const walletSrc = resolve(wallet);

return [
// mount the wallet to file in /src
'-v',
"-v",
`${walletSrc}:${walletDest}`,
'-e',
`WALLET_PATH=${walletDest}`
]
"-e",
`WALLET_PATH=${walletDest}`,
];
}

function contractSourceArgs (contractWasmPath) {
function contractSourceArgs(contractWasmPath) {
/**
* Use contract.wasm in pwd by default
*/
contractWasmPath = contractWasmPath || 'process.wasm'
const contractName = basename(contractWasmPath)
const contractWasmDest = `/src/${contractName}`
contractWasmPath = contractWasmPath || "process.wasm";
const contractName = basename(contractWasmPath);
const contractWasmDest = `/src/${contractName}`;

const contractWasmSrc = resolve(contractWasmPath)
const contractWasmSrc = resolve(contractWasmPath);

return [
// mount the wasm contract in pwd to /src
'-v',
"-v",
`${contractWasmSrc}:${contractWasmDest}`,
'-e',
`MODULE_WASM_PATH=${contractWasmDest}`
]
"-e",
`MODULE_WASM_PATH=${contractWasmDest}`,
];
}

/**
* Retrieves the memory limit based on the configuration preset or custom memory limit.
* @returns {string} The memory limit
*/
async function GetMemoryLimit() {
const configPath = `${Deno.cwd()}/config.yml`;
const config = parse(await Deno.readTextFile(configPath));

let memory_limit = "256-mb";
if (config) {
switch (config.preset) {
case "xs":
memory_limit = "64-mb";
break;
case "sm":
memory_limit = "128-mb";
break;
case "md":
memory_limit = "256-mb";
break;
case "lg":
memory_limit = "256-mb";
break;
case "xl":
memory_limit = "512-mb";
break;
case "xxl":
memory_limit = "4096-mb";
break;
default:
memory_limit = "256-mb";
break;
}
}
if (config?.memory_limit) {
memory_limit = `${config.memory_limit}-b`;
}
return memory_limit;
}

/**
Expand All @@ -49,13 +90,15 @@ function contractSourceArgs (contractWasmPath) {
* - allow using environment variables to set things like path to wallet
* - require confirmation and bypass with --yes
*/
export async function publish ({ wallet, host, tag, value }, contractWasmPath) {
export async function publish({wallet, host, tag, value}, contractWasmPath) {
tag.push("Memory-Limit");
value.push(await GetMemoryLimit());
const cmdArgs = [
...walletArgs(wallet),
...hostArgs(host),
...contractSourceArgs(contractWasmPath),
...tagsArg({ tags: tag, values: value })
]
...tagsArg({tags: tag, values: value}),
];

const p = Deno.run({
cmd: [
Expand All @@ -69,30 +112,30 @@ export async function publish ({ wallet, host, tag, value }, contractWasmPath) {
'ao-module'
]
})
await p.status()
await p.status();
}

export const command = new Command()
.description('Publish the file to Arweave')
.usage('-w ./wallet.json -b "https://up.arweave.net" --tag="Foo" --value="Bar" contract.wasm')
.option(
'-w, --wallet <path:string>',
'the path to the wallet that should be used to sign the transaction',
{ required: true }
.description("Publish the file to Arweave")
.usage(
'-w ./wallet.json -b "https://up.arweave.net" --tag="Foo" --value="Bar" contract.wasm'
)
.option(
'-b, --bundler <bundler:string>',
'the url of the bundler you would like to fund.'
"-w, --wallet <path:string>",
"the path to the wallet that should be used to sign the transaction",
{required: true}
)
.option(
'-t, --tag <tag:string>',
'additional tag to add to the transaction. MUST have a corresponding --value',
{ collect: true }
"-b, --bundler <bundler:string>",
"the url of the bundler you would like to fund."
)
.option(
'-v, --value <value:string>',
'value of the preceding tag name',
{ collect: true }
"-t, --tag <tag:string>",
"additional tag to add to the transaction. MUST have a corresponding --value",
{collect: true}
)
.arguments('<wasmfile:string>')
.action(publish)
.option("-v, --value <value:string>", "value of the preceding tag name", {
collect: true,
})
.arguments("<wasmfile:string>")
.action(publish);

0 comments on commit c345b2c

Please sign in to comment.