Skip to content
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

Various fixes and improvements #768

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ commands:
pkg-manager: yarn


setup-sdk:
description: "Sets up job with SDK"
steps:
- setup
- attach_workspace:
at: .
iamalwaysuncomfortable marked this conversation as resolved.
Show resolved Hide resolved


jobs:
sdk:
executor: rust-node
Expand All @@ -60,24 +68,38 @@ jobs:
sdk-test:
executor: rust-node
steps:
- setup
- attach_workspace:
at: .
- setup-sdk
- run: |
yarn test


template-node:
executor: rust-node
steps:
- setup
- attach_workspace:
at: .
- setup-sdk
- run:
working_directory: create-aleo-app/template-node
command: |
yarn start

template-extension:
executor: rust-node
steps:
- setup-sdk
- run:
working_directory: create-aleo-app/template-extension
command: |
yarn build

template-react-leo:
executor: rust-node
steps:
- setup-sdk
- run:
working_directory: create-aleo-app/template-react-leo
command: |
yarn build


check-clippy:
executor: rust-node
Expand Down Expand Up @@ -116,6 +138,12 @@ workflows:
- template-node:
requires:
- sdk
- template-extension:
requires:
- sdk
- template-react-leo:
requires:
- sdk
#- test-website:
# requires:
# - sdk
9 changes: 2 additions & 7 deletions .github/workflows/staging-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ jobs:
override: true
components: rustfmt, rust-src

- uses: jetli/[email protected]
with:
# Optional version of wasm-pack to install(eg. 'v0.9.1', 'latest')
version: 'latest'

- uses: actions/cache@v2
with:
path: |
Expand All @@ -37,9 +32,9 @@ jobs:

- name: Install and Build
run: |
rustup component add rust-src
cd website
yarn
yarn build:all
cd website
yarn build
env:
CI: ""
Expand Down
9 changes: 2 additions & 7 deletions .github/workflows/test-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ jobs:
override: true
components: rustfmt, rust-src

- uses: jetli/[email protected]
with:
# Optional version of wasm-pack to install(eg. 'v0.9.1', 'latest')
version: 'latest'

- uses: actions/cache@v2
with:
path: |
Expand All @@ -34,9 +29,9 @@ jobs:

- name: Install and Build
run: |
rustup component add rust-src
cd website
yarn
yarn build:all
cd website
yarn build
env:
CI: ""
Expand Down
9 changes: 2 additions & 7 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ jobs:
override: true
components: rustfmt, rust-src

- uses: jetli/[email protected]
with:
# Optional version of wasm-pack to install(eg. 'v0.9.1', 'latest')
version: 'latest'

- uses: actions/cache@v2
with:
path: |
Expand All @@ -37,9 +32,9 @@ jobs:

- name: Install and Build
run: |
rustup component add rust-src
cd website
yarn
yarn build:all
cd website
yarn build
env:
CI: ""
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ rust/src/program/.DS_Store

# Local Netlify folder
.netlify

package-lock.json
2 changes: 2 additions & 0 deletions create-aleo-app/template-extension/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
static/js
15 changes: 15 additions & 0 deletions create-aleo-app/template-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "extension-starter",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"build": "rimraf static/js && rollup --config"
},
"devDependencies": {
"@aleohq/sdk": "^0.6.0",
"@web/rollup-plugin-import-meta-assets": "^2.1.0",
"rimraf": "^5.0.1",
"rollup": "^4.0.0"
}
}
20 changes: 20 additions & 0 deletions create-aleo-app/template-extension/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { importMetaAssets } from "@web/rollup-plugin-import-meta-assets";

export default {
input: {
background: "./src/background.js",
worker: "./src/worker.js",
Comment on lines +6 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we document what these are/what their function is?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could, but it's super standard Chrome extension stuff, which is common knowledge for anybody creating a Chrome extension.

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/background

The sample is really simple, anybody who is familiar with Chrome extensions should understand it right away.

},
output: {
dir: "static/js",
format: "es",
sourcemap: true,
},
plugins: [
nodeResolve(),
importMetaAssets({
exclude: "./src/background.js",
}),
],
};
1 change: 1 addition & 0 deletions create-aleo-app/template-extension/src/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new Worker(new URL("worker.js", import.meta.url), { type: "module" });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the main worker that does all of the program execution? If an extension builder wanted to spin up another - how would that be done?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the SDK must be run in a Worker, so this runs the SDK code in a Worker. The SDK code is in the worker.js file.

I don't know why an extension would want to have multiple workers running SDK code, but if they wanted to they would just create a new worker2.js file, add it into the Rollup config:

    input: {
        background: "./src/background.js",
        worker: "./src/worker.js",
        worker2: "./src/worker2.js",
    },

And then spawn it in the background.js file:

new Worker(new URL("worker.js", import.meta.url), { type: "module" });

new Worker(new URL("worker2.js", import.meta.url), { type: "module" });

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aleo functions are compute heavy so they might need to run multiple workers to execute multiple functions concurrently (say multiple transfers at once). Some wallets need to open these workers in popup windows.

Also there is the opposite case where Aleo wasm can run in the main thread because no executions or compute heavy tasks are being done.

34 changes: 34 additions & 0 deletions create-aleo-app/template-extension/src/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {Account, initThreadPool, PrivateKey, ProgramManager,} from "@aleohq/sdk";

await initThreadPool();

const hello_hello_program =
"program hello_hello.aleo;\n" +
"\n" +
"function hello:\n" +
" input r0 as u32.public;\n" +
" input r1 as u32.private;\n" +
" add r0 r1 into r2;\n" +
" output r2 as u32.private;\n";

async function localProgramExecution(program, aleoFunction, inputs) {
const programManager = new ProgramManager();

// Create a temporary account for the execution of the program
const account = new Account();
programManager.setAccount(account);

const executionResponse = await programManager.executeOffline(
hello_hello_program,
"hello",
["5u32", "5u32"],
false,
);
return executionResponse.getOutputs();
}

const start = Date.now();
console.log("Starting execute!");
const result = await localProgramExecution();
console.log(result);
console.log("Execute finished!", Date.now() - start);
9 changes: 9 additions & 0 deletions create-aleo-app/template-extension/static/background.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script type="module" src="js/background.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions create-aleo-app/template-extension/static/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"manifest_version": 2,
onetrickwolf marked this conversation as resolved.
Show resolved Hide resolved
"name": "extension-starter",
"version": "0.1.0",
"description": "Example for using Aleo in a Chrome / Firefox extension",
"background": {
"page": "background.html"
},
"content_security_policy": "script-src 'self' 'wasm-eval'; object-src 'self'"
}
2 changes: 1 addition & 1 deletion create-aleo-app/template-node/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Account, initThreadPool, PrivateKey, ProgramManager,} from "@aleohq/sdk";

await initThreadPool(10);
await initThreadPool();

const hello_hello_program =
"program hello_hello.aleo;\n" +
Expand Down
2 changes: 1 addition & 1 deletion create-aleo-app/template-react-leo/src/workers/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@aleohq/sdk";
import { expose, proxy } from "comlink";

await initThreadPool(10);
await initThreadPool();

async function localProgramExecution(program, aleoFunction, inputs) {
const programManager = new ProgramManager();
Expand Down
10 changes: 9 additions & 1 deletion create-aleo-app/template-react-leo/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CopyPlugin from "copy-webpack-plugin";

import TerserPlugin from "terser-webpack-plugin";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this currently required or just an optimization?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is currently required if you want to use minification. If you don't want minification then it's not required.

Note that terser is used by default in Webpack, this is just changing some settings for terser.

import HtmlWebpackPlugin from "html-webpack-plugin";

import path from "path";
Expand Down Expand Up @@ -81,6 +81,14 @@ const appConfig = {
maxAssetSize: 13 * 1024 * 1024, // 12 MiB
maxEntrypointSize: 13 * 1024 * 1024, // 12 MiB
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin({
terserOptions: {
module: true,
}
})],
},
stats: {
warnings: false,
},
Expand Down
2 changes: 1 addition & 1 deletion create-aleo-app/template-vanilla/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ProgramManager,
} from "@aleohq/sdk";

await initThreadPool(10);
await initThreadPool();

const hello_hello_program =
"program hello_hello.aleo;\n" +
Expand Down
Loading