Skip to content

Commit

Permalink
[CI Skip] wasmer -> wasmi
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyTimes committed Jan 13, 2022
1 parent 5aaa90e commit e86de5f
Show file tree
Hide file tree
Showing 23 changed files with 2,316 additions and 1,296 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
workflow_dispatch:
jobs:
check:
if: "! startsWith(github.event.head_commit.message, '[CI Skip]')"
runs-on: ubuntu-20.04

steps:
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ exclude = [
'enclave/crates/skw-types',
'enclave/crates/skw-unit-test',
'enclave/crates/skw-utils',
'mock-enclave/src/near-vm-errors',
'mock-enclave/src/near-vm-logic',
'mock-enclave/src/near-vm-runner',
'mock-enclave/src/near-vm-runner-standalone',
]
[profile.release]
panic = 'unwind'
29 changes: 29 additions & 0 deletions mock-enclave/host/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,32 @@ export class MockBlockchainEnv {
}, 6000);
}
}


export class BlockchainSubscriber {

// Map contractId -> nextCallIndex(as number)
#nextCallIndex: { [key: string]: number }
#callHistory: {[key: string]: any}
#keyring: KeyringPair
#api: ApiPromise
#provider: WsProvider

public async init() {
await waitReady();
this.#keyring = new Keyring({ type: 'sr25519' }).addFromUri(`//Alice`)
this.#provider = new WsProvider('wss://sgdev.skye.kiwi');
this.#api = await ApiPromise.create({ provider: this.#provider });
}

public async registerSecretKeeper() {
const key = randomBytes(32);
const sealer = new DefaultSealer();
sealer.unlock(key);

console.log(`keypair - private ${u8aToHex(key)}; public ${u8aToHex(sealer.getAuthorKey())}`);


}

}
5 changes: 3 additions & 2 deletions mock-enclave/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"license": "GPL-3.0-or-later",
"scripts": {
"vm": "npx ts-node ./scripts/vm.ts",
"build": "npx ts-node ./scripts/build.ts",
"start": "yarn build && yarn vm",
"test": "npx ts-node ./scripts/test.ts",
"contract:build": "npx ts-node ./scripts/contract-build.ts",
"start": "yarn contract:build && yarn vm",
"serve": "npx ts-node ./host/index.ts"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions mock-enclave/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"outcome": "None",
"state": "{\"YgkAAABzeXN0ZW0uc2s=\":\"DAAAAHN5c3RlbV9oZWxsbw==\",\"U1RBVEU=\":\"AQAAAGI=\"}",
"error": "None"
}
18 changes: 18 additions & 0 deletions mock-enclave/scripts/contract-build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2021 @skyekiwi authors & contributors
// SPDX-License-Identifier: GPL-3.0-or-later

import fs from 'fs';
import { fromByteArray, toByteArray } from 'base64-js';
import { u8aToString } from '@skyekiwi/util';

const { execute } = require('./execSync');

console.log('$ yarn build', process.argv.slice(2).join(' '));

function build() {
// compile the runner
execute('cd contract && cargo build --target wasm32-unknown-unknown --release');
execute('cp contract/target/wasm32-unknown-unknown/release/greeting.wasm wasm/');
}

build();
16 changes: 16 additions & 0 deletions mock-enclave/scripts/execSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2021 @skyekiwi authors & contributors
// SPDX-License-Identifier: GPL-3.0-or-later

const { execSync } = require('child_process');

const execute = (cmd: string, noLog: boolean) => {
!noLog && console.log(`$ ${cmd}`);

try {
execSync(cmd, { stdio: 'inherit' });
} catch (error) {
process.exit(-1);
}
};

export {execute};
20 changes: 20 additions & 0 deletions mock-enclave/scripts/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2021 @skyekiwi authors & contributors
// SPDX-License-Identifier: GPL-3.0-or-later

import fs from 'fs';
import path from 'path';

const { execute } = require('./execSync');

console.log('$ yarn test', process.argv.slice(2).join(' '));

function build() {
const srcPath = path.join(__dirname, '../src');

// compile the runner
execute(`cd ${srcPath}/near-vm-logic && cargo test --release`);
execute(`cd ${srcPath}/near-vm-runner && cargo test --release`);
execute(`cd ${srcPath}/near-vm-errors && cargo test --release`);
}

build();
110 changes: 110 additions & 0 deletions mock-enclave/scripts/vm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2021 @skyekiwi authors & contributors
// SPDX-License-Identifier: GPL-3.0-or-later

import fs from 'fs';
import { fromByteArray, toByteArray } from 'base64-js';
import { u8aToString } from '@skyekiwi/util';

const { execute } = require('./execSync');

console.log('$ yarn vm', process.argv.slice(2).join(' '));

function compile() {
// compile the runner
execute('cd src/near-vm-runner-standalone && cargo build --release')
}


const defaultContext = {
current_account_id: 'contract.sk',
signer_account_id: 'system.sk',
signer_account_pk: '15T',
predecessor_account_id: 'system.sk',
input: '',
block_index: 1,
block_timestamp: '1586796191203000000',
epoch_height: 1,
account_balance: '10000000000000000000000000',
account_locked_balance: '0',
storage_usage: 100,
attached_deposit: '0',
prepaid_gas: 1000000000000000000,
random_seed: '15T',
view_config: null,
output_data_receivers: []
}

const injectOrigin = (origin: string) => {
let thisContext = defaultContext;
thisContext['signer_account_id'] = origin;
return JSON.stringify(thisContext);
}

function runVM({
methodName = "",
stateInput = "{}",
input = "",
origin = "system.sk",
wasmFile = "./wasm/greeting.wasm",
profiling = false
}) {
const runnerPath = "./src/near-vm-runner-standalone/target/release/near-vm-runner-standalone";
execute(`${runnerPath} --context '${injectOrigin(origin)}' --wasm-file ${wasmFile} --method-name ${methodName} --input \'${input}\' --state \'${stateInput}\' ${profiling ? "--timings" : ""} > result.json`)

// parse the output
const contentRaw = fs.readFileSync('result.json');
const content = JSON.parse(contentRaw.toString());
const stateB64 = JSON.parse(content.state);
let state: {[key: string]: string} = {}

for (const key in stateB64) {
const k = u8aToString(toByteArray(key))
const v = u8aToString(toByteArray(stateB64[key]))
state[k] = v;
}

console.log()
console.log("-------EXEC RESULT BEGINS-------");
try {
console.log("Return Value", u8aToString(Uint8Array.from(JSON.parse(content.outcome))));
} catch(err) {
// pass - in case of the outcome is 'None'
// console.error(err)
}

console.log(state);
console.log("------- EXEC RESULT ENDS -------");
console.log()

return stateB64;
}

compile()

let state = {}

state = runVM({
methodName: 'set_greeting',
input: '{"message": "system_hello"}',
stateInput: '{}',
})

// state = runVM({
// contextFile: './context/bob.json',
// methodName: 'set_greeting',
// input: '{"message": "bob_hello"}',
// stateInput: JSON.stringify(state),
// })

// state = runVM({
// contextFile: './context/zs.json',
// methodName: 'set_greeting',
// input: '{"message": "zs_hello"}',
// stateInput: JSON.stringify(state),
// })

// state = runVM({
// methodName: 'get_greeting',
// input: '{"account_id": "bob.sk"}',
// stateInput: JSON.stringify(state),
// })
Loading

0 comments on commit e86de5f

Please sign in to comment.