From b4e1634355fcb3eb5c95d1e3b9f22b316b739b41 Mon Sep 17 00:00:00 2001 From: amalcaraz Date: Sat, 1 Jul 2023 12:53:27 +0200 Subject: [PATCH 1/7] bump dep versions --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4e0423f8..bc651b84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,7 +40,7 @@ "@types/bs58": "^4.0.1", "@types/elliptic": "^6.4.13", "@types/jest": "^27.4.1", - "@types/node": "^17.0.25", + "@types/node": "^17.0.45", "@types/node-fetch": "^2.6.2", "@types/node-hid": "^1.3.1", "@types/ripemd160": "^2.0.0", diff --git a/package.json b/package.json index c16a6787..4a8028c8 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "@types/bs58": "^4.0.1", "@types/elliptic": "^6.4.13", "@types/jest": "^27.4.1", - "@types/node": "^17.0.25", + "@types/node": "^17.0.45", "@types/node-fetch": "^2.6.2", "@types/node-hid": "^1.3.1", "@types/ripemd160": "^2.0.0", From dc412fa9288a200f40cc738969524836e0f29c57 Mon Sep 17 00:00:00 2001 From: amalcaraz Date: Sat, 1 Jul 2023 12:53:48 +0200 Subject: [PATCH 2/7] improvements on tsconfig --- tsconfig.json | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index e5aa54ae..1f101c92 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,28 +1,44 @@ { "compilerOptions": { - "module": "CommonJS", + "target": "esnext", + "lib": [ + "esnext", + ], + "module": "esnext", + "moduleResolution": "node", + "strict": true, + "outDir": "dist", + "esModuleInterop": true, + "declaration": true, + "declarationMap": true, + "allowJs": false, + "resolveJsonModule": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, "noFallthroughCasesInSwitch": true, - "noImplicitAny": true, "noImplicitOverride": true, "noImplicitReturns": true, "noImplicitThis": true, "noUnusedLocals": true, "noUnusedParameters": true, - "strict": true, - "outDir": "dist", - "declaration": true, - "target": "ES6", - "esModuleInterop": true, - "resolveJsonModule": true, - "lib": [ - "dom", - "dom.iterable" - ] }, - "include": ["src"], - "exclude": ["node_modules", "tests"], + "include": [ + "src" + ], + "exclude": [ + "node_modules", + "tests", + "dist", + "scripts", + "**/*.spec.ts", + "**/*.test.ts", + "**/__tests__", + "**/__mocks__" + ], "typedocOptions": { - "entryPoints": ["src/index.ts"], + "entryPoints": [ + "src/index.ts" + ], "out": "docs" } -} +} \ No newline at end of file From 2370bcd5b2f16cfb87cfb8ba40fbbebb6ceeba5d Mon Sep 17 00:00:00 2001 From: amalcaraz Date: Sat, 1 Jul 2023 15:11:52 +0200 Subject: [PATCH 3/7] feat: Added support for instance messages --- src/accounts/account.ts | 2 +- src/accounts/avalanche.ts | 2 +- src/accounts/cosmos.ts | 2 +- src/accounts/ethereum.ts | 2 +- src/accounts/nuls2.ts | 2 +- src/accounts/providers/Ledger/ethereum.ts | 2 +- src/accounts/solana.ts | 2 +- src/accounts/substrate.ts | 2 +- src/accounts/tezos.ts | 2 +- src/messages/aggregate/publish.ts | 2 +- src/messages/any/getMessage.ts | 2 +- src/messages/any/getMessages.ts | 2 +- src/messages/any/getType.ts | 2 +- src/messages/constants.ts | 14 +++ src/messages/create/publish.ts | 2 +- src/messages/create/signature.ts | 2 +- src/messages/forget/publish.ts | 2 +- src/messages/index.ts | 5 +- src/messages/instance/index.ts | 3 + src/messages/instance/publish.ts | 105 +++++++++++++++++++++ src/messages/instance/types.ts | 20 ++++ src/messages/post/publish.ts | 2 +- src/messages/program/programModel.ts | 71 +------------- src/messages/program/publish.ts | 4 +- src/messages/program/spawn.ts | 4 +- src/messages/store/pin.ts | 2 +- src/messages/store/publish.ts | 2 +- src/messages/{message.ts => types/base.ts} | 4 +- src/messages/types/execution.ts | 73 ++++++++++++++ src/messages/types/index.ts | 3 + src/messages/types/volumes.ts | 59 ++++++++++++ src/utils/messageBuilder.ts | 21 ++++- tsconfig.json | 1 + 33 files changed, 327 insertions(+), 98 deletions(-) create mode 100644 src/messages/constants.ts create mode 100644 src/messages/instance/index.ts create mode 100644 src/messages/instance/publish.ts create mode 100644 src/messages/instance/types.ts rename src/messages/{message.ts => types/base.ts} (97%) create mode 100644 src/messages/types/execution.ts create mode 100644 src/messages/types/index.ts create mode 100644 src/messages/types/volumes.ts diff --git a/src/accounts/account.ts b/src/accounts/account.ts index ed663f2e..fe87637e 100644 --- a/src/accounts/account.ts +++ b/src/accounts/account.ts @@ -1,4 +1,4 @@ -import { BaseMessage, Chain } from "../messages/message"; +import { BaseMessage, Chain } from "../messages/types"; import { ProviderEncryptionLabel } from "./providers/ProviderEncryptionLib"; /** diff --git a/src/accounts/avalanche.ts b/src/accounts/avalanche.ts index 8d05a02e..a66d2315 100644 --- a/src/accounts/avalanche.ts +++ b/src/accounts/avalanche.ts @@ -1,7 +1,7 @@ import shajs from "sha.js"; import { ECIESAccount } from "./account"; import { GetVerificationBuffer } from "../messages"; -import { BaseMessage, Chain } from "../messages/message"; +import { BaseMessage, Chain } from "../messages/types"; import { decrypt as secp256k1_decrypt, encrypt as secp256k1_encrypt } from "eciesjs"; import { KeyPair } from "avalanche/dist/apis/avm"; import { Avalanche, BinTools, Buffer as AvaBuff } from "avalanche"; diff --git a/src/accounts/cosmos.ts b/src/accounts/cosmos.ts index aa27804c..e0051fdf 100644 --- a/src/accounts/cosmos.ts +++ b/src/accounts/cosmos.ts @@ -7,7 +7,7 @@ import { } from "@cosmjs/amino"; import { Account } from "./account"; import { GetVerificationBuffer } from "../messages"; -import { BaseMessage, Chain } from "../messages/message"; +import { BaseMessage, Chain } from "../messages/types"; /** * CosmosAccount implements the Account class for the Cosmos protocol. diff --git a/src/accounts/ethereum.ts b/src/accounts/ethereum.ts index 6cd32cd6..642d27d8 100644 --- a/src/accounts/ethereum.ts +++ b/src/accounts/ethereum.ts @@ -2,7 +2,7 @@ import * as bip39 from "bip39"; import { ethers } from "ethers"; import { ECIESAccount } from "./account"; import { GetVerificationBuffer } from "../messages"; -import { BaseMessage, Chain } from "../messages/message"; +import { BaseMessage, Chain } from "../messages/types"; import { decrypt as secp256k1_decrypt, encrypt as secp256k1_encrypt } from "eciesjs"; import { ChangeRpcParam, JsonRPCWallet, RpcChainType } from "./providers/JsonRPCWallet"; import { BaseProviderWallet } from "./providers/BaseProviderWallet"; diff --git a/src/accounts/nuls2.ts b/src/accounts/nuls2.ts index fc9b6c98..8900e6fb 100644 --- a/src/accounts/nuls2.ts +++ b/src/accounts/nuls2.ts @@ -3,7 +3,7 @@ import * as bip32 from "bip32"; import secp256k1 from "secp256k1"; import { generateMnemonic } from "bip39"; import { ECIESAccount } from "./account"; -import { BaseMessage, Chain } from "../messages/message"; +import { BaseMessage, Chain } from "../messages/types"; import { GetVerificationBuffer } from "../messages"; import { decrypt as secp256k1_decrypt, encrypt as secp256k1_encrypt } from "eciesjs"; import bs58 from "bs58"; diff --git a/src/accounts/providers/Ledger/ethereum.ts b/src/accounts/providers/Ledger/ethereum.ts index 034e0811..85de5d05 100644 --- a/src/accounts/providers/Ledger/ethereum.ts +++ b/src/accounts/providers/Ledger/ethereum.ts @@ -2,7 +2,7 @@ import EthApp from "@ledgerhq/hw-app-eth"; import semver from "semver"; import { Account } from "../../account"; -import { Chain, BaseMessage } from "../../../messages/message"; +import { Chain, BaseMessage } from "../../../messages/types"; import { GetVerificationBuffer } from "../../../messages"; import { getTransport } from "./transport"; import { JSExecutionEnvironment } from "../../../utils/env"; diff --git a/src/accounts/solana.ts b/src/accounts/solana.ts index 866667ed..6e3d8a51 100644 --- a/src/accounts/solana.ts +++ b/src/accounts/solana.ts @@ -1,5 +1,5 @@ import { Account } from "./account"; -import { BaseMessage, Chain } from "../messages/message"; +import { BaseMessage, Chain } from "../messages/types"; import { GetVerificationBuffer } from "../messages"; import { Keypair, PublicKey } from "@solana/web3.js"; import nacl from "tweetnacl"; diff --git a/src/accounts/substrate.ts b/src/accounts/substrate.ts index 84080a4f..96d53233 100644 --- a/src/accounts/substrate.ts +++ b/src/accounts/substrate.ts @@ -1,5 +1,5 @@ import { Account } from "./account"; -import { BaseMessage, Chain } from "../messages/message"; +import { BaseMessage, Chain } from "../messages/types"; import { GetVerificationBuffer } from "../messages"; import { Keyring } from "@polkadot/keyring"; diff --git a/src/accounts/tezos.ts b/src/accounts/tezos.ts index b94965b0..3ade1e8b 100644 --- a/src/accounts/tezos.ts +++ b/src/accounts/tezos.ts @@ -1,5 +1,5 @@ import { Account } from "./account"; -import { BaseMessage, Chain } from "../messages/message"; +import { BaseMessage, Chain } from "../messages/types"; import { GetVerificationBuffer } from "../messages"; import { InMemorySigner } from "@taquito/signer"; import { b58cdecode, b58cencode, prefix } from "@taquito/utils"; diff --git a/src/messages/aggregate/publish.ts b/src/messages/aggregate/publish.ts index 46a93c70..40ca0767 100644 --- a/src/messages/aggregate/publish.ts +++ b/src/messages/aggregate/publish.ts @@ -1,6 +1,6 @@ import { Account } from "../../accounts/account"; import { DEFAULT_API_V2 } from "../../global"; -import { AggregateContent, AggregateContentKey, AggregateMessage, ItemType, MessageType } from "../message"; +import { AggregateContent, AggregateContentKey, AggregateMessage, ItemType, MessageType } from "../types"; import { PutContentToStorageEngine } from "../create/publish"; import { SignAndBroadcast } from "../create/signature"; import { MessageBuilder } from "../../utils/messageBuilder"; diff --git a/src/messages/any/getMessage.ts b/src/messages/any/getMessage.ts index 4d106384..f52fef5b 100644 --- a/src/messages/any/getMessage.ts +++ b/src/messages/any/getMessage.ts @@ -1,5 +1,5 @@ import { DEFAULT_API_V2 } from "../../global"; -import { BaseMessage, MessageType } from "../message"; +import { BaseMessage, MessageType } from "../types"; import { GetMessages } from "./getMessages"; type GetMessageParams = { diff --git a/src/messages/any/getMessages.ts b/src/messages/any/getMessages.ts index ba780d8b..f7c55c2b 100644 --- a/src/messages/any/getMessages.ts +++ b/src/messages/any/getMessages.ts @@ -1,7 +1,7 @@ import axios from "axios"; import { DEFAULT_API_V2 } from "../../global"; import { getSocketPath, stripTrailingSlash } from "../../utils/url"; -import { BaseMessage, Chain, MessageType } from "../message"; +import { BaseMessage, Chain, MessageType } from "../types"; type MessageQueryResponse = { messages: BaseMessage[]; diff --git a/src/messages/any/getType.ts b/src/messages/any/getType.ts index 9271f54a..64bfee6f 100644 --- a/src/messages/any/getType.ts +++ b/src/messages/any/getType.ts @@ -1,4 +1,4 @@ -import { AggregateMessage, BaseMessage, ForgetMessage, PostMessage, ProgramMessage, StoreMessage } from "../message"; +import { AggregateMessage, BaseMessage, ForgetMessage, PostMessage, ProgramMessage, StoreMessage } from "../types"; export function Program(message: BaseMessage): message is ProgramMessage { if (message !== null && typeof message === "object") { diff --git a/src/messages/constants.ts b/src/messages/constants.ts new file mode 100644 index 00000000..a1768e00 --- /dev/null +++ b/src/messages/constants.ts @@ -0,0 +1,14 @@ +import { FunctionEnvironment, MachineResources } from "./types"; + +export const defaultExecutionEnvironment: FunctionEnvironment = { + reproducible: false, + internet: true, + aleph_api: true, + shared_cache: false, +}; + +export const defaultResources: MachineResources = { + memory: 128, + vcpus: 1, + seconds: 30, +}; diff --git a/src/messages/create/publish.ts b/src/messages/create/publish.ts index 106d2dac..b37249ce 100644 --- a/src/messages/create/publish.ts +++ b/src/messages/create/publish.ts @@ -1,6 +1,6 @@ import shajs from "sha.js"; -import { BaseMessage, ItemType } from "../message"; +import { BaseMessage, ItemType } from "../types"; import axios from "axios"; import FormDataNode from "form-data"; import { getSocketPath, stripTrailingSlash } from "../../utils/url"; diff --git a/src/messages/create/signature.ts b/src/messages/create/signature.ts index 163f1561..330f2fea 100644 --- a/src/messages/create/signature.ts +++ b/src/messages/create/signature.ts @@ -1,4 +1,4 @@ -import { BaseMessage } from "../message"; +import { BaseMessage } from "../types"; import { Account } from "../../accounts/account"; import axios from "axios"; import { getSocketPath, stripTrailingSlash } from "../../utils/url"; diff --git a/src/messages/forget/publish.ts b/src/messages/forget/publish.ts index 7f7e743d..5aa62972 100644 --- a/src/messages/forget/publish.ts +++ b/src/messages/forget/publish.ts @@ -1,5 +1,5 @@ import { PutContentToStorageEngine } from "../create/publish"; -import { ForgetContent, ForgetMessage, ItemType, MessageType } from "../message"; +import { ForgetContent, ForgetMessage, ItemType, MessageType } from "../types"; import { Account } from "../../accounts/account"; import { DEFAULT_API_V2 } from "../../global"; import { SignAndBroadcast } from "../create/signature"; diff --git a/src/messages/index.ts b/src/messages/index.ts index 1f1f1fa1..a4376d17 100644 --- a/src/messages/index.ts +++ b/src/messages/index.ts @@ -1,12 +1,13 @@ -import { BaseMessage } from "./message"; +import { BaseMessage } from "./types"; import * as aggregate from "./aggregate"; import * as forget from "./forget"; import * as post from "./post"; import * as program from "./program"; +import * as instance from "./instance"; import * as store from "./store"; import * as any from "./any"; -export { aggregate, forget, post, program, store, any }; +export { aggregate, forget, post, program, instance, store, any }; /** * Extracts some fields from an Aleph message to sign it using an account. diff --git a/src/messages/instance/index.ts b/src/messages/instance/index.ts new file mode 100644 index 00000000..5067b8bb --- /dev/null +++ b/src/messages/instance/index.ts @@ -0,0 +1,3 @@ +import { publish } from "./publish"; + +export { publish }; diff --git a/src/messages/instance/publish.ts b/src/messages/instance/publish.ts new file mode 100644 index 00000000..1f482b25 --- /dev/null +++ b/src/messages/instance/publish.ts @@ -0,0 +1,105 @@ +import { Account } from "../../accounts/account"; +import { + ItemType, + MessageType, + ProgramMessage, + MachineVolume, + HostRequirements, + FunctionEnvironment, + MachineResources, + VolumePersistence, +} from "../types"; +import { InstanceContent } from "./types"; +import { PutContentToStorageEngine } from "../create/publish"; +import { SignAndBroadcast } from "../create/signature"; +import { DEFAULT_API_V2 } from "../../global"; +import { MessageBuilder } from "../../utils/messageBuilder"; +import { defaultExecutionEnvironment, defaultResources } from "../constants"; + +type InstancePublishConfiguration = { + account: Account; + channel: string; + metadata?: Record; + variables?: Record; + authorized_keys?: string[]; + resources?: Partial; + requirements?: HostRequirements; + environment?: FunctionEnvironment; + image?: string; + volumes?: MachineVolume[]; + inlineRequested?: boolean; + storageEngine?: ItemType.ipfs | ItemType.storage; + APIServer?: string; +}; + +// TODO: Check that program_ref, runtime and data_ref exist +// Guard some numbers values +export async function publish({ + account, + channel, + metadata, + variables, + authorized_keys, + resources, + requirements, + environment, + image = "549ec451d9b099cad112d4aaa2c00ac40fb6729a92ff252ff22eef0b5c3cb613", + volumes = [], + inlineRequested = true, + storageEngine = ItemType.ipfs, + APIServer = DEFAULT_API_V2, +}: InstancePublishConfiguration): Promise { + const timestamp = Date.now() / 1000; + const { address } = account; + + const programContent: InstanceContent = { + address, + time: timestamp, + metadata, + authorized_keys, + volumes, + variables, + requirements, + allow_amend: false, + resources: { + ...defaultResources, + ...resources, + }, + environment: { + ...defaultExecutionEnvironment, + ...environment, + }, + rootfs: { + parent: { + ref: image, + use_latest: true, + }, + persistence: VolumePersistence.host, + size_mib: 5000, + }, + }; + + const message = MessageBuilder({ + account, + channel, + timestamp, + storageEngine, + content: programContent, + type: MessageType.program, + }); + + await PutContentToStorageEngine({ + message: message, + content: programContent, + inline: inlineRequested, + APIServer, + }); + + await SignAndBroadcast({ + message: message, + account, + APIServer, + }); + + return message as unknown as ProgramMessage; +} diff --git a/src/messages/instance/types.ts b/src/messages/instance/types.ts new file mode 100644 index 00000000..517ad2a7 --- /dev/null +++ b/src/messages/instance/types.ts @@ -0,0 +1,20 @@ +import { BaseExecutableContent, ParentVolume, VolumePersistence } from "../types"; + +/** + * Root file system of a VM instance. + * The root file system of an instance is built as a copy of a reference image, named parent image. The user determines a custom size and persistence model. + */ +export type RootfsVolume = { + parent: ParentVolume; + persistence: VolumePersistence; + size_mib: number; //Limit to 1 GiB +}; + +/** + * Message content for scheduling a VM instance on the network. + * + * rootfs: Root filesystem of the system, will be booted by the kernel" + */ +export type InstanceContent = BaseExecutableContent & { + rootfs: RootfsVolume; +}; diff --git a/src/messages/post/publish.ts b/src/messages/post/publish.ts index f83536f9..f4dc7348 100644 --- a/src/messages/post/publish.ts +++ b/src/messages/post/publish.ts @@ -1,5 +1,5 @@ import { Account } from "../../accounts/account"; -import { ChainRef, ItemType, MessageType, PostContent, PostMessage } from "../message"; +import { ChainRef, ItemType, MessageType, PostContent, PostMessage } from "../types"; import { PutContentToStorageEngine } from "../create/publish"; import { SignAndBroadcast } from "../create/signature"; import { DEFAULT_API_V2 } from "../../global"; diff --git a/src/messages/program/programModel.ts b/src/messages/program/programModel.ts index 7fb35ab7..0f262dcd 100644 --- a/src/messages/program/programModel.ts +++ b/src/messages/program/programModel.ts @@ -1,4 +1,5 @@ -import { BaseContent } from "../message"; +import { BaseContent, MachineVolume } from "../types"; +import { FunctionEnvironment, MachineResources } from "../types"; /** * Type of Encoding @@ -53,25 +54,6 @@ export type FunctionTriggers = { persistent?: boolean; }; -/** - * Properties of the execution environment - */ -export type FunctionEnvironment = { - reproducible: boolean; - internet: boolean; - aleph_api: boolean; - shared_cache: boolean; -}; - -/** - * System resources required - */ -export type MachineResources = { - vcpus: number; - memory: number; - seconds: number; -}; - /** * Execution runtime (rootfs with Python interpreter) */ @@ -81,55 +63,6 @@ export type FunctionRuntime = { comment: string; }; -export type AbstractVolume = { - comment?: string[]; - mount?: string[]; - is_read_only: () => boolean; -}; - -/** - * Immutable volumes contain extra files that can be used by a program and are stored on the Aleph network. - * They can be shared by multiple programs and updated independently of the code of the program. - * - * You can use them to store Python libraries that your program depends on, - * use them in multiple programs and update them independently of other programs. - */ -export type ImmutableVolume = AbstractVolume & { - ref: string; - use_latest: boolean; - is_read_only: () => true; -}; - -/** - * Ephemeral volumes provide temporary disk storage to a VM during its execution without requiring more memory. - */ -export type EphemeralVolume = AbstractVolume & { - ephemeral: true; - size_mib: number; //Limit to 1 GiB - is_read_only: () => false; -}; - -export enum VolumePersistence { - host = "host", - store = "store", -} - -/** - * Host persistent volumes are empty volumes that your program can use to store information that, - * would be useful to persist between executions but can be recovered from other sources. - * - * There is no guarantee that these volumes will not be deleted anytime, - * when the program is not running and important data must be persisted on the Aleph network. - */ -export type PersistentVolume = AbstractVolume & { - persistence: VolumePersistence; - name: string; - size_mib: number; //Limit to 1 GiB - is_read_only: () => false; -}; - -export type MachineVolume = ImmutableVolume | EphemeralVolume | PersistentVolume; - export type ProgramContent = BaseContent & { type: MachineType; allow_amend: boolean; diff --git a/src/messages/program/publish.ts b/src/messages/program/publish.ts index cad9b032..5877dd02 100644 --- a/src/messages/program/publish.ts +++ b/src/messages/program/publish.ts @@ -1,7 +1,7 @@ import { Account } from "../../accounts/account"; -import { ItemType, MessageType, ProgramMessage, StoreMessage } from "../message"; +import { ItemType, MessageType, ProgramMessage, StoreMessage, MachineVolume } from "../types"; import { Publish as storePublish } from "../../messages/store/index"; -import { Encoding, FunctionTriggers, MachineType, MachineVolume, ProgramContent } from "./programModel"; +import { Encoding, FunctionTriggers, MachineType, ProgramContent } from "./programModel"; import { PutContentToStorageEngine } from "../create/publish"; import { SignAndBroadcast } from "../create/signature"; import { DEFAULT_API_V2 } from "../../global"; diff --git a/src/messages/program/spawn.ts b/src/messages/program/spawn.ts index c3214a63..2aa3c32b 100644 --- a/src/messages/program/spawn.ts +++ b/src/messages/program/spawn.ts @@ -1,6 +1,6 @@ import { Account } from "../../accounts/account"; -import { ItemType, ProgramMessage } from "../message"; -import { Encoding, MachineVolume } from "./programModel"; +import { ItemType, ProgramMessage, MachineVolume } from "../types"; +import { Encoding } from "./programModel"; import { DEFAULT_API_V2 } from "../../global"; import { publish } from "./publish"; diff --git a/src/messages/store/pin.ts b/src/messages/store/pin.ts index 761d72e8..03375f54 100644 --- a/src/messages/store/pin.ts +++ b/src/messages/store/pin.ts @@ -1,5 +1,5 @@ import * as base from "../../accounts/account"; -import { ItemType, StoreMessage } from "../message"; +import { ItemType, StoreMessage } from "../types"; import { Publish } from "./publish"; import { DEFAULT_API_V2 } from "../../global"; diff --git a/src/messages/store/publish.ts b/src/messages/store/publish.ts index 690b69c3..bcf151db 100644 --- a/src/messages/store/publish.ts +++ b/src/messages/store/publish.ts @@ -1,5 +1,5 @@ import * as base from "../../accounts/account"; -import { ItemType, MessageType, StoreContent, StoreMessage } from "../message"; +import { ItemType, MessageType, StoreContent, StoreMessage } from "../types"; import { PushFileToStorageEngine, PutContentToStorageEngine } from "../create/publish"; import { SignAndBroadcast } from "../create/signature"; import { RequireOnlyOne } from "../../utils/requiredOnlyOne"; diff --git a/src/messages/message.ts b/src/messages/types/base.ts similarity index 97% rename from src/messages/message.ts rename to src/messages/types/base.ts index 5f51643b..218c34ee 100644 --- a/src/messages/message.ts +++ b/src/messages/types/base.ts @@ -4,7 +4,7 @@ * * Warning: Avax, CSDK, NEO are currently not supported by the TS sdk. */ -import { ProgramContent } from "./program/programModel"; +import { ProgramContent } from "../program/programModel"; export enum Chain { DOT = "DOT", @@ -151,3 +151,5 @@ export type ProgramMessage = BaseMessage & { content: ProgramContent; type: MessageType.program; }; + +export type ItemHash = string; diff --git a/src/messages/types/execution.ts b/src/messages/types/execution.ts new file mode 100644 index 00000000..179edfa3 --- /dev/null +++ b/src/messages/types/execution.ts @@ -0,0 +1,73 @@ +import { BaseContent } from "./base"; +import { MachineVolume } from "./volumes"; + +/** + * Properties of the execution environment + */ +export type FunctionEnvironment = { + reproducible: boolean; + internet: boolean; + aleph_api: boolean; + shared_cache: boolean; +}; + +/** + * System resources required + */ +export type MachineResources = { + vcpus: number; + memory: number; + seconds: number; +}; + +/** + * architecture: CPU architecture + * vendor: CPU vendor. Allows other vendors. + */ +export type CpuProperties = { + architecture?: "x86_64" | "arm64"; + vendor?: "AuthenticAMD" | "GenuineIntel" | string; +}; + +/** + * Address of the node owner + * Node address must match this regular expression + */ +export type NodeRequirements = { + owner?: string; + address_regex?: string; +}; + +/** + * cpu: Required CPU properties + * node: Required Compute Resource Node properties + */ +export type HostRequirements = { + cpu?: CpuProperties; + node?: NodeRequirements; +}; + +/** + * Abstract content for execution messages (Instances, Programs). + * + * allow_amend: Allow amends to update this function + * metadata: Metadata of the VM + * authorized_keys: SSH public keys authorized to connect to the VM + * variables: Environment variables available in the VM + * environment: Properties of the execution environment + * resources: System resources required + * requirements: System properties required + * volumes: Volumes to mount on the filesystem + * replaces: Previous version to replace. Must be signed by the same address" + */ +export type BaseExecutableContent = BaseContent & { + allow_amend: boolean; + metadata?: Record; + authorized_keys?: string[]; + variables?: Record; + environment: FunctionEnvironment; + resources: MachineResources; + requirements?: HostRequirements; + volumes: MachineVolume[]; + replaces?: string; +}; diff --git a/src/messages/types/index.ts b/src/messages/types/index.ts new file mode 100644 index 00000000..2fe8f9b2 --- /dev/null +++ b/src/messages/types/index.ts @@ -0,0 +1,3 @@ +export * from "./base"; +export * from "./volumes"; +export * from "./execution"; diff --git a/src/messages/types/volumes.ts b/src/messages/types/volumes.ts new file mode 100644 index 00000000..a04b5b6a --- /dev/null +++ b/src/messages/types/volumes.ts @@ -0,0 +1,59 @@ +import { ItemHash } from "./base"; + +export type AbstractVolume = { + comment?: string[]; + mount?: string[]; + is_read_only: () => boolean; +}; + +/** + * Immutable volumes contain extra files that can be used by a program and are stored on the Aleph network. + * They can be shared by multiple programs and updated independently of the code of the program. + * + * You can use them to store Python libraries that your program depends on, + * use them in multiple programs and update them independently of other programs. + */ +export type ImmutableVolume = AbstractVolume & { + ref: string; + use_latest: boolean; + is_read_only: () => true; +}; + +/** + * Ephemeral volumes provide temporary disk storage to a VM during its execution without requiring more memory. + */ +export type EphemeralVolume = AbstractVolume & { + ephemeral: true; + size_mib: number; //Limit to 1 GiB + is_read_only: () => false; +}; + +export enum VolumePersistence { + host = "host", + store = "store", +} + +/** + * A reference volume to copy as a persistent volume. + */ +export type ParentVolume = { + ref: ItemHash; + use_latest: boolean; +}; + +/** + * Host persistent volumes are empty volumes that your program can use to store information that, + * would be useful to persist between executions but can be recovered from other sources. + * + * There is no guarantee that these volumes will not be deleted anytime, + * when the program is not running and important data must be persisted on the Aleph network. + */ +export type PersistentVolume = AbstractVolume & { + parent?: ParentVolume; + persistence: VolumePersistence; + name: string; + size_mib: number; //Limit to 1 GiB + is_read_only: () => false; +}; + +export type MachineVolume = ImmutableVolume | EphemeralVolume | PersistentVolume; diff --git a/src/utils/messageBuilder.ts b/src/utils/messageBuilder.ts index d8fd67c4..f5e1a2a3 100644 --- a/src/utils/messageBuilder.ts +++ b/src/utils/messageBuilder.ts @@ -1,7 +1,7 @@ -import { ItemType } from "../messages/message"; +import { Chain, ItemType } from "../messages/types"; import { Account } from "../accounts/account"; -type MessageBuilderConfig = { +export type MessageBuilderConfig = { storageEngine: ItemType; account: Account; channel: string; @@ -10,7 +10,22 @@ type MessageBuilderConfig = { type: T; }; -export function MessageBuilder(config: MessageBuilderConfig) { +export type BuiltMessage = { + type: T; + time: number; + channel: string; + content: C; + item_type: ItemType; + sender: string; + chain: Chain; + size: 0; + item_hash: ""; + signature: ""; + item_content: ""; + confirmed: false; +}; + +export function MessageBuilder(config: MessageBuilderConfig): BuiltMessage { return { type: config.type, time: config.timestamp, diff --git a/tsconfig.json b/tsconfig.json index 1f101c92..7de12b3f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "target": "esnext", "lib": [ "esnext", + "DOM" ], "module": "esnext", "moduleResolution": "node", From 60edec75f5e503b1b3dc367647d2b5fab12aa7cf Mon Sep 17 00:00:00 2001 From: amalcaraz Date: Sat, 1 Jul 2023 15:12:23 +0200 Subject: [PATCH 4/7] 3.6.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index bc651b84..320e095d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "aleph-sdk-ts", - "version": "3.5.0", + "version": "3.6.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "aleph-sdk-ts", - "version": "3.5.0", + "version": "3.6.0", "license": "MIT", "dependencies": { "@cosmjs/amino": "^0.29.0", diff --git a/package.json b/package.json index 4a8028c8..6f0ad8cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aleph-sdk-ts", - "version": "3.5.0", + "version": "3.6.0", "description": "Aleph.im Typescript SDK", "keywords": [ "web3", From 7cdddf1071bf91fa04d1587b6c61b6c641ee7098 Mon Sep 17 00:00:00 2001 From: amalcaraz Date: Tue, 4 Jul 2023 17:57:47 +0200 Subject: [PATCH 5/7] fix: wrong message type on instance push method --- src/messages/instance/publish.ts | 20 ++++++++++---------- src/messages/types/base.ts | 7 +++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/messages/instance/publish.ts b/src/messages/instance/publish.ts index 1f482b25..1c03b9f0 100644 --- a/src/messages/instance/publish.ts +++ b/src/messages/instance/publish.ts @@ -2,7 +2,7 @@ import { Account } from "../../accounts/account"; import { ItemType, MessageType, - ProgramMessage, + InstanceMessage, MachineVolume, HostRequirements, FunctionEnvironment, @@ -16,7 +16,7 @@ import { DEFAULT_API_V2 } from "../../global"; import { MessageBuilder } from "../../utils/messageBuilder"; import { defaultExecutionEnvironment, defaultResources } from "../constants"; -type InstancePublishConfiguration = { +export type InstancePublishConfiguration = { account: Account; channel: string; metadata?: Record; @@ -24,7 +24,7 @@ type InstancePublishConfiguration = { authorized_keys?: string[]; resources?: Partial; requirements?: HostRequirements; - environment?: FunctionEnvironment; + environment?: Partial; image?: string; volumes?: MachineVolume[]; inlineRequested?: boolean; @@ -48,11 +48,11 @@ export async function publish({ inlineRequested = true, storageEngine = ItemType.ipfs, APIServer = DEFAULT_API_V2, -}: InstancePublishConfiguration): Promise { +}: InstancePublishConfiguration): Promise { const timestamp = Date.now() / 1000; const { address } = account; - const programContent: InstanceContent = { + const instanceContent: InstanceContent = { address, time: timestamp, metadata, @@ -79,18 +79,18 @@ export async function publish({ }, }; - const message = MessageBuilder({ + const message = MessageBuilder({ account, channel, timestamp, storageEngine, - content: programContent, - type: MessageType.program, + content: instanceContent, + type: MessageType.instance, }); await PutContentToStorageEngine({ message: message, - content: programContent, + content: instanceContent, inline: inlineRequested, APIServer, }); @@ -101,5 +101,5 @@ export async function publish({ APIServer, }); - return message as unknown as ProgramMessage; + return message as unknown as InstanceMessage; } diff --git a/src/messages/types/base.ts b/src/messages/types/base.ts index 218c34ee..1d48c7d1 100644 --- a/src/messages/types/base.ts +++ b/src/messages/types/base.ts @@ -4,6 +4,7 @@ * * Warning: Avax, CSDK, NEO are currently not supported by the TS sdk. */ +import { InstanceContent } from "../instance/types"; import { ProgramContent } from "../program/programModel"; export enum Chain { @@ -36,6 +37,7 @@ export enum MessageType { store = "STORE", program = "PROGRAM", forget = "FORGET", + instance = "INSTANCE", } export enum ItemType { @@ -152,4 +154,9 @@ export type ProgramMessage = BaseMessage & { type: MessageType.program; }; +export type InstanceMessage = BaseMessage & { + content: InstanceContent; + type: MessageType.instance; +}; + export type ItemHash = string; From 1e56a8dfbdf577402588158e633b60bc8a5c4faa Mon Sep 17 00:00:00 2001 From: amalcaraz Date: Tue, 4 Jul 2023 17:57:52 +0200 Subject: [PATCH 6/7] 3.6.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 320e095d..c02a32e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "aleph-sdk-ts", - "version": "3.6.0", + "version": "3.6.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "aleph-sdk-ts", - "version": "3.6.0", + "version": "3.6.1", "license": "MIT", "dependencies": { "@cosmjs/amino": "^0.29.0", diff --git a/package.json b/package.json index 6f0ad8cb..41c4a8d6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aleph-sdk-ts", - "version": "3.6.0", + "version": "3.6.1", "description": "Aleph.im Typescript SDK", "keywords": [ "web3", From bd74fc83f9e5055accaf370d5dd22971fe36fad5 Mon Sep 17 00:00:00 2001 From: amalcaraz Date: Tue, 4 Jul 2023 18:17:19 +0200 Subject: [PATCH 7/7] temporally use target ES6 and module cjs for a better support until we migrate to rollup --- tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 7de12b3f..4037fef0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,11 @@ { "compilerOptions": { - "target": "esnext", + "target": "ES6", "lib": [ "esnext", "DOM" ], - "module": "esnext", + "module": "CommonJS", "moduleResolution": "node", "strict": true, "outDir": "dist",