Skip to content

Commit

Permalink
feat: cleanup memory on rust panic
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Jun 29, 2023
1 parent 48c0595 commit c13b9af
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 39 deletions.
3 changes: 2 additions & 1 deletion apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"test": "./scripts/build-node.sh && yarn jest",
"test:watch": "./scripts/build-node.sh && yarn jest --watchAll=true",
"test:ci": "jest",
"wasm:build": "./scripts/build.sh"
"wasm:build": "./scripts/build.sh --release",
"wasm:build:dev": "./scripts/build.sh"
},
"dependencies": {
"@anoma/chains": "0.1.0",
Expand Down
11 changes: 9 additions & 2 deletions apps/extension/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd -P)
PACKAGES_PATH="../../../packages"

cd "${SCRIPT_DIR}/${PACKAGES_PATH}/crypto" && yarn wasm:build
cd "${SCRIPT_DIR}/${PACKAGES_PATH}/shared" && yarn wasm:build
suffix=""

if [ "$1" != "--release" ]
then
suffix=":dev"
fi

cd "${SCRIPT_DIR}/${PACKAGES_PATH}/crypto" && yarn wasm:build$suffix
cd "${SCRIPT_DIR}/${PACKAGES_PATH}/shared" && yarn wasm:build$suffix
21 changes: 13 additions & 8 deletions apps/extension/src/background/keyring/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,14 +735,19 @@ export class KeyRing {
throw new Error(`Account not found.`);
}

return (await this.query.query_balance(account.owner)).map(
([token, amount]: [string, string]) => {
return {
token,
amount,
};
}
);
try {
return (await this.query.query_balance(account.owner)).map(
([token, amount]: [string, string]) => {
return {
token,
amount,
};
}
);
} catch (e) {
console.error(e);
return [];
}
}

private async addSecretKey(
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"baseUrl": "./src",
"target": "es5",
"target": "es2015",
"lib": ["dom", "dom.iterable", "esnext", "webworker"],
"allowJs": true,
"skipLibCheck": true,
Expand Down
11 changes: 6 additions & 5 deletions apps/namada-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@
"lint": "npx eslint src --ext .ts,.tsx",
"lint:fix": "yarn lint -- --fix",
"lint:ci": "yarn lint --max-warnings 0",
"test": "yarn wasm:build-node && yarn jest",
"test:watch": "yarn wasm:build-node && yarn jest --watchAll=true",
"test:coverage": "yarn wasm:build-node && yarn test --coverage",
"test": "yarn wasm:build:node && yarn jest",
"test:watch": "yarn wasm:build:node && yarn jest --watchAll=true",
"test:coverage": "yarn wasm:build:node && yarn test --coverage",
"test:ci": "jest",
"e2e-test": "PLAYWRIGHT_BASE_URL=http://localhost:3000 yarn playwright test",
"e2e-test:headed": "PLAYWRIGHT_BASE_URL=http://localhost:3000 yarn playwright test --project=chromium --headed",
"eject": "npx react-scripts eject",
"wasm:build": "./scripts/build.sh",
"wasm:build-node": "./scripts/build-node.sh"
"wasm:build": "./scripts/build.sh --release",
"wasm:build:dev": "./scripts/build.sh",
"wasm:build:node": "./scripts/build-node.sh"
},
"browserslist": {
"production": [
Expand Down
8 changes: 7 additions & 1 deletion packages/crypto/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ wasm-bindgen-test = "0.3.13"
[package.metadata.wasm-pack.profile.release]
wasm-opt = true

[package.metadata.wasm-pack.profile.dev]
wasm-opt = false

[profile.release]
opt-level = "s"
lto = true
opt-level = "s"

[profile.dev]
opt-level = "s"
1 change: 0 additions & 1 deletion packages/crypto/lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ pub fn encryption_key(salt: &kdf::Salt, password: String) -> kdf::SecretKey {
.and_then(|password| kdf::derive_key(&password, salt, 3, 1 << 16, 32))
.expect("Generation of encryption secret key shouldn't fail")
}

3 changes: 2 additions & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"license": "MIT",
"private": false,
"scripts": {
"wasm:build": "./scripts/build.sh",
"wasm:build": "./scripts/build.sh --release",
"wasm:build:dev": "./scripts/build.sh",
"wasm:build:node": "./scripts/build-test.sh",
"test": "yarn wasm:build:node && yarn jest",
"test:watch": "yarn wasm:build:node && yarn test --watch",
Expand Down
19 changes: 17 additions & 2 deletions packages/crypto/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,20 @@

SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd -P)

cd $SCRIPT_DIR/../lib; cargo build --target wasm32-unknown-unknown --release
wasm-bindgen --out-dir=$SCRIPT_DIR/../src/crypto --target=web --omit-default-module-path target/wasm32-unknown-unknown/release/crypto.wasm
path=""

if [ "$1" = "" ]
then
echo "Building \"crypto\" in dev mode."
path="debug"
elif [ "$1" = "--release" ]
then
echo "Building \"crypto\" in release mode."
path="release"
else
echo "Unsupported build mode \"$1\""
exit 1
fi

cd $SCRIPT_DIR/../lib; cargo build --target wasm32-unknown-unknown ${1} $features
wasm-bindgen --out-dir=$SCRIPT_DIR/../src/crypto --target=web --omit-default-module-path target/wasm32-unknown-unknown/$path/crypto.wasm
2 changes: 1 addition & 1 deletion packages/crypto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"baseUrl": "./src",
"target": "es5",
"target": "es2015",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down
12 changes: 11 additions & 1 deletion packages/shared/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ license = "MIT"
[lib]
crate-type = ["cdylib", "rlib"]

[features]
dev = []

[dependencies]
async-trait = {version = "0.1.51"}
tiny-bip39 = "0.8.2"
Expand Down Expand Up @@ -65,8 +68,15 @@ borsh-schema-derive-internal = {git = "https://github.com/heliaxdev/borsh-rs.git
bumpalo = {git = "https://github.com/fitzgen/bumpalo", version = "3.8.0"}

[package.metadata.wasm-pack.profile.release]
wasm-opt = true

[package.metadata.wasm-pack.profile.dev]
wasm-opt = false

[profile.release]
debug = true
lto = true
opt-level = "s"

[profile.dev]
# TODO: we can experiment with optimization levels
opt-level = "s"
4 changes: 2 additions & 2 deletions packages/shared/lib/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use wasm_bindgen::prelude::*;

use crate::rpc_client::HttpClient;
use crate::sdk::masp;
use crate::utils::to_js_result;
use crate::utils::{set_panic_hook, to_js_result};

#[wasm_bindgen]
/// Represents an API for querying the ledger
Expand All @@ -23,7 +23,7 @@ pub struct Query {
impl Query {
#[wasm_bindgen(constructor)]
pub fn new(url: String) -> Query {
console_error_panic_hook::set_once();
set_panic_hook();
let client = HttpClient::new(url);
Query { client }
}
Expand Down
8 changes: 6 additions & 2 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use namada::ledger::{
};
use wasm_bindgen::{prelude::wasm_bindgen, JsError, JsValue};

use crate::{rpc_client::HttpClient, sdk::masp::WebShieldedUtils, utils::to_bytes};
use crate::{
rpc_client::HttpClient,
sdk::masp::WebShieldedUtils,
utils::{set_panic_hook, to_bytes},
};

pub mod masp;
mod tx;
Expand All @@ -24,7 +28,7 @@ pub struct Sdk {
impl Sdk {
#[wasm_bindgen(constructor)]
pub fn new(url: String) -> Self {
console_error_panic_hook::set_once();
set_panic_hook();
Sdk {
client: HttpClient::new(url),
wallet: Wallet::new(wallet::STORAGE_PATH.to_owned(), Store::default()),
Expand Down
11 changes: 11 additions & 0 deletions packages/shared/lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,14 @@ where
Err(e) => Err(JsError::new(&e.to_string())),
}
}

#[cfg(feature = "dev")]
pub fn set_panic_hook() {
web_sys::console::log_1(&"Set panic hook".into());
console_error_panic_hook::set_once();
}

#[cfg(not(feature = "dev"))]
pub fn set_panic_hook() {
web_sys::console::log_1(&"Do not set panic hook".into());
}
3 changes: 2 additions & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"license": "MIT",
"private": false,
"scripts": {
"wasm:build": "./scripts/build.sh",
"wasm:build": "./scripts/build.sh --release",
"wasm:build:dev": "./scripts/build.sh",
"wasm:build:node": "./scripts/build-test.sh",
"test-wasm:ci": "cd ./lib && cargo test"
},
Expand Down
21 changes: 19 additions & 2 deletions packages/shared/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,22 @@

SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd -P)

cd $SCRIPT_DIR/../lib; cargo build --target wasm32-unknown-unknown --release
wasm-bindgen --out-dir=$SCRIPT_DIR/../src/shared --target=web --omit-default-module-path target/wasm32-unknown-unknown/release/shared.wasm
features=""
path=""

if [ "$1" = "" ]
then
echo "Building \"shared\" in dev mode."
features="--features dev"
path="debug"
elif [ "$1" = "--release" ]
then
echo "Building \"shared\" in release mode."
path="release"
else
echo "Unsupported build mode \"$1\""
exit 1
fi

cd $SCRIPT_DIR/../lib; cargo build --target wasm32-unknown-unknown ${1} $features
wasm-bindgen --out-dir=$SCRIPT_DIR/../src/shared --target=web --omit-default-module-path target/wasm32-unknown-unknown/$path/shared.wasm
25 changes: 25 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
import { Query as RustQuery } from "./shared/shared";
export * from "./shared/shared";

/**
* CallWithTimeout - calls a function with specified timeout
*/
const cwt =
<U extends unknown[], T>(fn: (...args: U) => Promise<T>, timeout = 5) =>
(...args: U): Promise<T> => {
return new Promise(async (resolve, reject) => {
const t = setTimeout(() => {
reject(`Balance query timed out after ${timeout}s.`);
}, timeout * 1000);

const res = await fn(...args);
clearTimeout(t);
resolve(res);
});
};

export class Query extends RustQuery {
query_balance = cwt(super.query_balance.bind(this), 10);
query_epoch = cwt(super.query_epoch.bind(this));
query_all_validators = cwt(super.query_all_validators.bind(this));
query_my_validators = cwt(super.query_my_validators.bind(this));
}
2 changes: 1 addition & 1 deletion packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"baseUrl": "./src",
"target": "es5",
"target": "es2015",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd -P)

Expand Down
6 changes: 0 additions & 6 deletions scripts/build.sh

This file was deleted.

0 comments on commit c13b9af

Please sign in to comment.