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

Make vm serializable #33

Merged
merged 4 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
name: Publish package to GitHub Packages
on:
workflow_dispatch: {}
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
permissions:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
packages: write
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/version.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Auto-increment patch version number
on:
on:
workflow_dispatch: {}
push:
branches:
- main
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@terran-one/cosmwasm-vm-js",
"version": "0.1.12",
"version": "0.1.13",
"license": "MIT",
"author": "TerranOne",
"main": "dist/index.js",
Expand Down
8 changes: 8 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { eddsa as ellipticEddsa } from "elliptic";

declare global {
var _eddsa: ellipticEddsa; // we use a global to prevent serialization issues for the calling class
function eddsa(): ellipticEddsa;
}

export {};
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { eddsa } from 'elliptic';

export * from './memory';
export * from './backend';
export * from './instance';
export * from './environment';

global.eddsa = () => global._eddsa || (global._eddsa = new eddsa('ed25519'));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We want to store this globally (and lazy-initialise it) because constructing it each time is slow - on my year-old macbook, about 500ms.

15 changes: 6 additions & 9 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { bech32, BechLib } from 'bech32';
import { Region } from './memory';
import { ecdsaRecover, ecdsaVerify } from 'secp256k1';
import { eddsa } from 'elliptic';
import { IBackend, Record } from './backend';
import { toByteArray } from './helpers/byte-array';

Expand All @@ -16,12 +15,10 @@ export class VMInstance {
public instance?: WebAssembly.Instance;
public backend: IBackend;
public bech32: BechLib;
public eddsa: eddsa;

constructor(backend: IBackend) {
this.backend = backend;
this.bech32 = bech32;
this.eddsa = new eddsa('ed25519');
Copy link
Contributor Author

@lukedawilson lukedawilson Sep 23, 2022

Choose a reason for hiding this comment

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

eddsa is not serialisable, but nor does it (seem to) hold any (mutable) state - its methods appear to be pure functions.

}

public async build(wasmByteCode: ArrayBuffer) {
Expand Down Expand Up @@ -371,10 +368,10 @@ export class VMInstance {
const sig = Buffer.from(signature.data).toString('hex');
const pub = Buffer.from(pubkey.data).toString('hex');
const msg = Buffer.from(message.data).toString('hex');
const _signature = this.eddsa.makeSignature(sig);
const _pubkey = this.eddsa.keyFromPublic(pub);
const _signature = global.eddsa().makeSignature(sig);
const _pubkey = global.eddsa().keyFromPublic(pub);

const isValidSignature = this.eddsa.verify(msg, _signature, _pubkey);
const isValidSignature = global.eddsa().verify(msg, _signature, _pubkey);

if (isValidSignature) {
return 0;
Expand Down Expand Up @@ -410,10 +407,10 @@ export class VMInstance {
const _sig = Buffer.from(sig).toString('hex');
const _pub = Buffer.from(pub).toString('hex');
const _msg = Buffer.from(msg).toString('hex');
const _signature = this.eddsa.makeSignature(_sig);
const _pubkey = this.eddsa.keyFromPublic(_pub);
const _signature = global.eddsa().makeSignature(_sig);
const _pubkey = global.eddsa().keyFromPublic(_pub);

const isValidSignature = this.eddsa.verify(_msg, _signature, _pubkey);
const isValidSignature = global.eddsa().verify(_msg, _signature, _pubkey);

if (!isValidSignature) {
return 1;
Expand Down
31 changes: 31 additions & 0 deletions test/vm.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { readFileSync } from 'fs';
import { BasicKVIterStorage, VMInstance } from '../src';
import { BasicBackendApi, BasicQuerier, IBackend } from '../src/backend';
import { writeData } from './common/test-vm';
import * as testData from './common/test-data';

const wasmByteCode = readFileSync('testdata/v1.0/cosmwasm_vm_test.wasm');
const backend: IBackend = {
Expand Down Expand Up @@ -64,4 +66,33 @@ describe('CosmWasmVM', () => {
};
expect(region.json).toEqual(actual);
});

it('serializes', async () => {
// Arrange
await vm.build(wasmByteCode);
vm.instantiate(mockEnv, mockInfo, { count: 20 });

// Act
const json = JSON.stringify(vm);

// Assert
expect(json).toBeDefined();
});

it('serializes after edda usage', async () => {
// Arrange
await vm.build(wasmByteCode);
vm.instantiate(mockEnv, mockInfo, { count: 20 });

const hashPtr = writeData(vm, testData.EDDSA_MSG_HEX);
const sigPtr = writeData(vm, testData.EDDSA_SIG_HEX);
const pubkeyPtr = writeData(vm, testData.EDDSA_PUBKEY_HEX);
vm.do_ed25519_verify(hashPtr, sigPtr, pubkeyPtr);

// Act
const json = JSON.stringify(vm);

// Assert
expect(json).toBeDefined();
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"downlevelIteration": true,
"outDir": "dist",
"rootDir": "src",
"skipLibCheck": true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See https://www.typescriptlang.org/tsconfig#skipLibCheck for details.

Rather than doing a full check of all d.ts files, TypeScript will type check the code you specifically refer to in your app’s source code.

Otherwise, we get these two errors due to webpack:

node_modules/@types/eslint/index.d.ts:451:42 - error TS2724: '"/Users/lukewilson/git/terra/cosmwasm-vm-js/node_modules/@types/estree/index"' has no exported member named 'ChainExpression'. Did you mean 'Expression'?

451         ChainExpression?: ((node: ESTree.ChainExpression & NodeParentExtension) => void) | undefined;
                                             ~~~~~~~~~~~~~~~

node_modules/@types/eslint/index.d.ts:474:43 - error TS2694: Namespace '"/Users/lukewilson/git/terra/cosmwasm-vm-js/node_modules/@types/estree/index"' has no exported member 'ImportExpression'.

474         ImportExpression?: ((node: ESTree.ImportExpression & NodeParentExtension) => void) | undefined;

"sourceMap": true,
"strict": true,
"strictFunctionTypes": true,
Expand Down
Loading