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

Implement keyholder test case #13

Merged
merged 31 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9fae34a
doc(shared): improve ZenroomError doc
denizenging Jul 16, 2023
8755e40
doc(shared): rm obsolete line
denizenging Jul 16, 2023
e9f9e46
refactor(ignored): explicitly mark the return type
denizenging Jul 16, 2023
d17f869
doc(ignored): clarify usage
denizenging Jul 16, 2023
2ceb8be
refactor(shared)!: rename ZenroomResult type
denizenging Jul 17, 2023
6c77dc3
feat(shared)!: parse zencode_exec's output result
denizenging Jul 17, 2023
75809c5
Test(shared): fix the description
denizenging Jul 17, 2023
519cdca
test(shared): improve zenroom test
denizenging Jul 17, 2023
b8bae30
refactor(fs): shut linter down
denizenging Jul 17, 2023
e592e29
style(fs): spread array into lines
denizenging Jul 17, 2023
94979a4
style(fs): merge lines of an array
denizenging Jul 17, 2023
de63c35
refactor(ignored): return zenroom output as well
denizenging Jul 17, 2023
dd23b51
test(ignored): apply getIgnoredStatements() changes
denizenging Jul 17, 2023
555813d
test(fs): apply getIgnoredStatements() changes
denizenging Jul 17, 2023
ba3b107
feat(shared): add new tokens
denizenging Jul 18, 2023
e7e30a1
refactor(ignored): explicitly mark with type
denizenging Jul 18, 2023
2c97a85
refactor(fs)!: switch to shared tokens
denizenging Jul 18, 2023
350bca1
test(fs): rm And statements
denizenging Jul 18, 2023
a70e774
refactor(ignored)!: revert back changes
denizenging Jul 18, 2023
a0cd5de
fix(ignored): apply changes to ignored api
denizenging Jul 18, 2023
1ab135a
fix(fs): apply changes to ignored api
denizenging Jul 18, 2023
3a6e145
test(fs): add keyholder test case
denizenging Jul 18, 2023
b7fd3fd
chore(git): rm unnecessary line in gitignore
denizenging Jul 24, 2023
9b0c77b
refactor(*)!: better structure pkgs
denizenging Aug 1, 2023
fc93864
feat(*): support commonjs
denizenging Aug 9, 2023
5e4a47c
feat(*): build with tslib included
denizenging Sep 1, 2023
923968d
fix(shared): rm unused export
denizenging Sep 1, 2023
6a68245
fix(*): fix cjs not working
denizenging Sep 1, 2023
d8c7c6d
feat: add core module
denizenging Sep 14, 2023
99cc59d
feat: convert fs to use core
denizenging Sep 14, 2023
142425b
style: don't specify uncessary type
denizenging Sep 14, 2023
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
11 changes: 6 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
!/package.json
!/pnpm-lock.yaml
!/pnpm-workspace.yaml
!/tsconfig.json
!/jest.config.ts
!/.prettierrc
!/.prettierignore
# shared by pkgs
!/tsconfig.json
!/.npmignore

# useful stuff
!/.editorconfig
!/.cruft.json

# packages
pkg/*/node_modules/
!/pkg/*/src/**/*.ts
!/pkg/*/test/**/*.ts
!/pkg/*/package.json
!/pkg/*/pnpm-lock.yaml
!/pkg/**/*.ts
!/pkg/**/*.test.ts
!/pkg/*/tsconfig.json
!/pkg/*/.npmignore
16 changes: 9 additions & 7 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# blacklist everything
*
*/
!*/

# useful stuff
!/package.json

# built files and types
!/.build/*/*.js
!/.build/*/*.js.map
!/.build/*/*.d.ts
!/build/esm/src/**/*.js
!/build/esm/src/**/*.js.map
!/build/esm/src/**/*.d.ts

!/build/cjs/package.json
!/build/cjs/src/**/*.js
!/build/cjs/src/**/*.js.map
!/build/cjs/src/**/*.d.ts
14 changes: 0 additions & 14 deletions jest.config.ts

This file was deleted.

25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slangroom",
"version": "0.0.1",
"version": "0.0.3",
"description": "Enhance zencode smart contracts with your slang dialect",
"repository": "https://github.com/dyne/slangroom",
"author": {
Expand All @@ -9,27 +9,28 @@
},
"license": "AGPL-3.0-only",
"scripts": {
"lint": "eslint --ext .ts pkg/ jest.config.ts",
"test": "jest",
"coverage": "jest --coverage",
"clean": "rm -rf .build .coverage",
"ts-node": "ts-node",
"build": "tsc",
"lint": "eslint --ext .ts pkg/*/src pkg/*/test",
"format": "prettier --ignore-path .gitignore --write '**/*.+(js|ts|json|html)'",
"test": "pnpm build && pnpm -F @slangroom/* exec ava build/esm/test",
"coverage": "c8 -o .coverage --exclude '**/test/' pnpm test",
"clean": "rm -rf .coverage && pnpm -F @slangroom/* exec -- rm -rf build",
"build": "pnpm build:esm",
"build:dual": "pnpm build:esm && pnpm build:cjs",
"build:cjs": "pnpm -F @slangroom/* exec tsc --outDir build/cjs --module commonjs && pnpm cjs-fixup",
"build:esm": "pnpm -F @slangroom/* exec tsc --outdir build/esm --module node16",
"build-all": "pnpm build",
"format": "prettier --ignore-path .gitignore --write '**/*.+(js|ts|json|html)'"
"cjs-fixup": "pnpm -F @slangroom/* exec sh -c \"echo '{\\\"type\\\":\\\"commonjs\\\"}' >build/cjs/package.json\""
},
"devDependencies": {
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"ava": "^5.3.1",
"c8": "^8.0.1",
"esbuild": "^0.18.4",
"eslint": "^8.43.0",
"eslint-config-prettier": "^8.8.0",
"jest": "29.5.0",
"jest-junit": "^16.0.0",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"tslib": "^2.5.3",
"typedoc": "^0.24.8",
Expand Down
1 change: 1 addition & 0 deletions pkg/core/.npmignore
35 changes: 35 additions & 0 deletions pkg/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@slangroom/core",
"version": "1.0.0",
"dependencies": {
"@slangroom/shared": "workspace:*",
"@slangroom/ignored": "workspace:*"
},
"repository": "https://github.com/dyne/slangroom",
"license": "AGPL-3.0-only",
"type": "module",
"main": "./build/cjs/src/index.js",
"types": "./build/cjs/src/index.d.ts",
"exports": {
".": {
"import": {
"types": "./build/esm/src/index.d.ts",
"default": "./build/esm/src/index.js"
},
"require": {
"types": "./build/cjs/src/index.d.ts",
"default": "./build/cjs/src/index.js"
}
},
"./*": {
"import": {
"types": "./build/esm/src/*.d.ts",
"default": "./build/esm/src/*.js"
},
"require": {
"types": "./build/cjs/src/*.d.ts",
"default": "./build/cjs/src/*.js"
}
}
}
}
2 changes: 2 additions & 0 deletions pkg/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from '@slangroom/core/plugin';
export * from '@slangroom/core/slangroom';
46 changes: 46 additions & 0 deletions pkg/core/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { ZenroomParams, JsonableObject } from '@slangroom/shared';

/**
* A plugin that must be executed **before** the actual Zenroom execution takes
* place.
*
* The plugin is defined using a single parameter which is a callback,
* named [execute], which takes in the necessary parameters from [BeforeParams].
*/
export class BeforePlugin {
constructor(readonly execute: (params: BeforeParams) => Promise<void> | void) {}
}

/**
* A plugin that must be executed **after** the actual Zenroom execution takes
* place.
*
* The plugin is defined using a single parameter which is a callback,
* named [execute], which takes in the necessary parameters from [AfterParams].
*/
export class AfterPlugin {
constructor(readonly execute: (params: AfterParams) => Promise<void> | void) {}
}

/**
* The parameters passed down to [BeforePlugin]'s callback.
*
* [statement] is the ignored statement for each iteration.
* [params] is the original parameters passed to Zenroom, if any.
*/
export type BeforeParams = {
readonly statement: string;
readonly params: ZenroomParams | undefined;
};

/**
* The parameters passed down to [BeforePlugin]'s callback.
* [statement] is the ignored statement for each iteration.
* [params] is the original parameters passed to Zenroom, if any.
* [result] is the result of the actual Zenroom execution.
*/
export type AfterParams = {
readonly statement: string;
readonly params: ZenroomParams | undefined;
readonly result: JsonableObject;
};
84 changes: 84 additions & 0 deletions pkg/core/src/slangroom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { BeforePlugin, AfterPlugin } from '@slangroom/core/plugin';
import { type ZenroomParams, type ZenroomOutput, zencodeExec } from '@slangroom/shared';
import { getIgnoredStatements } from '@slangroom/ignored';

/**
* A helper type that disallows nested arrays.
*/
type Plugins =
| BeforePlugin
| AfterPlugin
| Set<BeforePlugin | AfterPlugin>
| Array<BeforePlugin | AfterPlugin | Set<BeforePlugin | AfterPlugin>>;

/**
* A Slangroom instance.
*/
export class Slangroom {
/**
* A set of plugins that needs to be executed **before** the actual Zenroom execution.
*/
private _beforeExecution = new Set<BeforePlugin>();
get beforeExecution() {
return this._beforeExecution;
}

/**
* A set of plugins that needs to be executed **after** the actual Zenroom execution.
*/
private _afterExecution = new Set<AfterPlugin>();
get afterExecution() {
return this._afterExecution;
}

constructor(first: Plugins, ...rest: Plugins[]) {
this.addPlugins(first, ...rest);
}

/**
* Adds a single or a list of plugins to the Slangroom instance.
*/
addPlugins(first: Plugins, ...rest: Plugins[]) {
const plugins = new Set<BeforePlugin | AfterPlugin>();
[first, ...rest].forEach(function recurse(x: Plugins) {
if (Array.isArray(x) || x instanceof Set) x.forEach(recurse);
else plugins.add(x);
});

for (const p of plugins) {
if (p instanceof BeforePlugin) this._beforeExecution.add(p);
if (p instanceof AfterPlugin) this._afterExecution.add(p);
}
}

/**
* Executes a contract using optional parameters with custom statements.
*/
async execute(contract: string, params?: ZenroomParams): Promise<ZenroomOutput> {
const ignoreds = await getIgnoredStatements(contract, params);

// TODO: remove the statements when they match (decide how)
for (const b of this._beforeExecution) {
for (const ignored of ignoreds) {
await b.execute({
statement: ignored,
params: params,
});
}
}

const zout = await zencodeExec(contract, params);

for (const a of this._afterExecution) {
for (const ignored of ignoreds) {
await a.execute({
statement: ignored,
result: zout.result,
params: params,
});
}
}

return zout;
}
}
65 changes: 65 additions & 0 deletions pkg/core/test/slangroom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import test from 'ava';
import { BeforePlugin, AfterPlugin, Slangroom } from '@slangroom/core';

test('adding a plugin correctly falls into either before or after', (t) => {
const before = new BeforePlugin(() => {
return;
});
const after = new AfterPlugin(() => {
return;
});
const slang = new Slangroom(before, after);

t.is(slang.beforeExecution.size, 1);
t.true(slang.beforeExecution.has(before));

t.is(slang.afterExecution.size, 1);
t.true(slang.afterExecution.has(after));
});

test('no plugins are executed if no ignored statemnets are found', async (t) => {
let hasBeforeRan = false;
let hasAfterRan = false;
const before = new BeforePlugin(() => {
hasBeforeRan = true;
return;
});
const after = new BeforePlugin(() => {
hasAfterRan = true;
return;
});
const slang = new Slangroom([before, after]);
const contract = `Given I have nothing
Then I print the string 'I love you'
`;
await slang.execute(contract);
t.false(hasBeforeRan);
t.false(hasAfterRan);
});

test('before-plugins runs before the actual execution and after-plugins runs after', async (t) => {
let hasBeforeRan = false;
let hasAfterRan = false;
const before = new BeforePlugin(() => {
t.false(hasBeforeRan);
t.false(hasAfterRan);
hasBeforeRan = true;
return;
});
const after = new AfterPlugin(() => {
t.true(hasBeforeRan);
t.false(hasAfterRan);
hasAfterRan = true;
return;
});
const slang = new Slangroom(new Set([before, after]));
const contract = `Rule unknown ignore

Given I have nothing
Then I print the string 'I love you'
Then this statement does not exist
`;
await slang.execute(contract);
t.true(hasBeforeRan);
t.true(hasAfterRan);
});
1 change: 1 addition & 0 deletions pkg/core/tsconfig.json
1 change: 1 addition & 0 deletions pkg/deps/.npmignore
35 changes: 28 additions & 7 deletions pkg/deps/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
{
"name": "@slangroom/deps",
"exports": {
"*": "../../.build/deps/*.js"
},
"version": "1.0.0",
"type": "module",
"repository": "https://github.com/dyne/slangroom",
"license": "AGPL-3.0-only",
"version": "1.0.3",
"dependencies": {
"chevrotain": "^10.5.0",
"zenroom": "^3.10.0"
},
"repository": "https://github.com/dyne/slangroom",
"license": "AGPL-3.0-only",
"type": "module",
"main": "./build/cjs/src/index.js",
"types": "./build/cjs/src/index.d.ts",
"exports": {
".": {
"import": {
"types": "./build/esm/src/index.d.ts",
"default": "./build/esm/src/index.js"
},
"require": {
"types": "./build/cjs/src/index.d.ts",
"default": "./build/cjs/src/index.js"
}
},
"./*": {
"import": {
"types": "./build/esm/src/*.d.ts",
"default": "./build/esm/src/*.js"
},
"require": {
"types": "./build/cjs/src/*.d.ts",
"default": "./build/cjs/src/*.js"
}
}
}
}
File renamed without changes.
File renamed without changes.
Loading
Loading