Skip to content

Commit

Permalink
feat: add contract bundle test (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
eng-cc authored Aug 21, 2023
1 parent 7fdb188 commit 9fbaffd
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Install Playwright Browsers
run: cd packages/webui && pnpm exec playwright install --with-deps
- name: Build fe
run: cd packages/webui && npm run build
run: cd packages/webui && npm run build && npm run build-contract
- name: Run Playwright tests
run: cd packages/webui && pnpm exec playwright test
- uses: actions/upload-artifact@v3
Expand Down
8 changes: 4 additions & 4 deletions packages/contract_builder/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ const builder = async (input: string, opts: BuildOption) => {
writeFileSync(tsFile, tsCode.code, {
flag: 'w+',
});
const boundleCode = await boundleContract(tsFile);
// const boundleCodeFile = resolve(input, '../index.contract.boundle.js');
// writeFileSync(boundleCodeFile, boundleCode, {
const bundleCode = await boundleContract(tsFile);
// const bundleCodeFile = resolve(input, '../index.contract.bundle.js');
// writeFileSync(bundleCodeFile, bundleCode, {
// flag: 'w+',
// });

const resultUint8 = bytes.fromString(boundleCode);
const resultUint8 = bytes.fromString(bundleCode);
const resultU8String = `export default new Uint8Array([${resultUint8.toString()}]);`;
// console.log(resultUint8);
writeFileSync(resolve(input, `../${fileName}.js`), resultU8String, {
Expand Down
11 changes: 11 additions & 0 deletions packages/docs/src/content/docs/zh-cn/dev_log/202308.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: 'Dev Log 2023-08'
description: ''
---

### 8-01~8-21

- [x] 因为 swcpack 无法在浏览器运行,所以选择先不做浏览器内打包合约的功能,仅在 NodeJs 环境下做合约编译打包
- [x] swcpack 在 NodeJs 环境下打包仍然有问题,从 swc 主仓库看起来是相关功能还没做完,所以选择 rollup 来做合约打包
- [x] rollup 打包合约成功,但是打包出来的合约文件中的代码在 vm 中运行报错(Date.now() 未定义),排查了好久复现是 vm 入口文件未引入 chrono, 并修复了这个问题
- [x] 添加了合约打包的测试用例,e2e 测试用例中包含了合约打包后的代码在 vm 中运行的测试
2 changes: 1 addition & 1 deletion packages/skvm/pkg/node/skvm_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ export function __wbindgen_memory() {
return addHeapObject(ret);
};

export function __wbindgen_closure_wrapper5109(arg0, arg1, arg2) {
export function __wbindgen_closure_wrapper5105(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1333, __wbg_adapter_28);
return addHeapObject(ret);
};
Expand Down
Binary file modified packages/skvm/pkg/node/skvm_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/skvm/pkg/web/skvm.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ function __wbg_get_imports() {
const ret = wasm.memory;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5109 = function(arg0, arg1, arg2) {
imports.wbg.__wbindgen_closure_wrapper5105 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1333, __wbg_adapter_28);
return addHeapObject(ret);
};
Expand Down
Binary file modified packages/skvm/pkg/web/skvm_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/skvm/skboa
2 changes: 2 additions & 0 deletions packages/webui/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ dist-ssr
*.njsproj
*.sln
*.sw?

*.contract.js
11 changes: 11 additions & 0 deletions packages/webui/e2e/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ test('contract', async ({ page }) => {
await page.waitForTimeout(6000);
await expect(passed).toHaveText('true');
});

test('contract bundle', async ({ page }) => {
await page.goto('/ci');
await page.waitForTimeout(1000);
const startBtn = page.getByTestId('test_contract_bundle');
await startBtn.click();
await page.screenshot({ path: './playwright-report/contract.png' });
const passed = page.getByTestId('contract_bundle_passed');
await page.waitForTimeout(6000);
await expect(passed).toHaveText('true');
});
1 change: 1 addition & 0 deletions packages/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "vite",
"e2e": "playwright test",
"build": "tsc && vite build",
"build-contract": "npx @trustack/contract_builder build ./src/ci/contract/test.contract.ts",
"server": "node ./e2e/server.mjs",
"preview": "vite preview"
},
Expand Down
8 changes: 5 additions & 3 deletions packages/webui/src/ci/contract/test.contract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { BaseContract } from 'skchain';
import { random } from 'lodash';
import { add } from './test_func';

export class TestContract extends BaseContract {
// TODO add extends BaseContract
export class TestContract {
constructor() {
super();
this.data = {
value: 10000000000000n,
random: BigInt(add(1, random(1, 100))),
};
}

Expand Down
10 changes: 6 additions & 4 deletions packages/webui/src/ci/contract/testRunner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { evalCode } from '@trustack/contract';
import { bytes } from 'multiformats';

export const runBuildContractTest = async (): Promise<boolean> => {
export const runContractBundleTest = async (): Promise<boolean> => {
const codeByte = (await import('./test.contract.js')) as unknown as {
default: Uint8Array;
};
const codeString = bytes.toString(codeByte.default);

console.log(codeString);
// TODO: eval code to test
return true;
const res = await evalCode(
`${codeString};const func = new __contract_class_name__(); func.getValue();`,
);
return res.funcResult === '10000000000000n';
};
1 change: 1 addition & 0 deletions packages/webui/src/ci/contract/test_func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const add = (a: number, b: number): number => a + b;
16 changes: 8 additions & 8 deletions packages/webui/src/ci/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Button } from 'antd';
import { useState } from 'react';
import { runBuildContractTest } from './contract/testRunner';
import { runContractBundleTest } from './contract/testRunner';
import { runContractTest, runSkvmTest, runTest } from './run';

export default function CiPage() {
const [passed, setPass] = useState(false);
const [skvmPassed, setSkvmPass] = useState(false);
const [contractPassed, setContractPass] = useState(false);
const [contractBuildPassed, setContractBuildPass] = useState(false);
const [contractBundlePassed, setContractBundlePass] = useState(false);
return (
<div className="ci-box">
<div>
Expand Down Expand Up @@ -54,17 +54,17 @@ export default function CiPage() {

<div style={{ padding: 50 }}>
<Button
data-testid="test_contract_build"
data-testid="test_contract_bundle"
type="default"
onClick={async () => {
const res = await runBuildContractTest();
setContractBuildPass(res);
const res = await runContractBundleTest();
setContractBundlePass(res);
}}
>
test_build_contract
test_bundle_contract
</Button>
<span data-testid="contract_build_passed">
{contractBuildPassed.toString()}
<span data-testid="contract_bundle_passed">
{contractBundlePassed.toString()}
</span>
</div>
</div>
Expand Down

0 comments on commit 9fbaffd

Please sign in to comment.