Skip to content

Commit

Permalink
Merge pull request #39 from Chia-Mine/v3.0.0
Browse files Browse the repository at this point in the history
V3.0.0
  • Loading branch information
ChiaMineJP authored May 15, 2024
2 parents a5b8a17 + 6391563 commit 18cfeec
Show file tree
Hide file tree
Showing 55 changed files with 12,143 additions and 4,459 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ bin/
pre_build.js
jest.config.js
src/__bls_signatures__/loader.js
src/__clvm_wasm__/loader.js
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup Node 16
uses: actions/setup-node@v3
- name: Setup Node 18
uses: actions/setup-node@v4
with:
node-version: '16.x'
node-version: '18.x'

- name: Get yarn version
run: echo "yarn_version=$(yarn -v)" >> $GITHUB_ENV
Expand All @@ -34,7 +34,7 @@ jobs:

- name: Cache Node.js modules
id: yarn-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ env.cache_dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
Expand Down
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
# Changelog

## [3.0.0]
This version is compatible with [`960f8d139940fa0814d3fac44da9a2975642f5d3`](https://github.com/Chia-Network/clvm/tree/960f8d139940fa0814d3fac44da9a2975642f5d3) of [clvm](https://github.com/Chia-Network/clvm)
### Breaking Change
- The type of `None` is now `undefined` (Previously it was `null`)
- Removed `isSExp()` and `isCLVMObject()` since these don't exist in the Python's `clvm` and they brought slightly different behaviour.
- `run_program()` has been deprecated. Use `run_chia_program` or `run_clvm` instead.
Note: `run_program()` is still available, but it cannot handle new CHIP-0011 operators like `secp256k1_verify`.
### Changed
- Now `op_div` does not accept negative operands.
- `convert_atom_to_bytes()` now is able to convert an object which has `toBytes()` method.
- `SExp` can be initialized from `LazyNode` of `clvm_wasm` (Rust version of `clvm`)
- Upgraded `bls-signatures` to `2.0.3`
- Upgraded npm dev dependencies
- Replaced `yarn.lock` with `pnpm-lock.yaml`
### Added
- Added `clvm_rs`(`clvm_wasm`) support
- Exposed `run_chia_program`
- Exposed `run_clvm`
- Exposed `LazyNode`
- Added `toJSON()` method to `LazyNode` (This is not implemented in `clvm_wasm`)
- Exposed `Flag`
- Exposed `serialized_length`
- Exposed `node_from_bytes`
- Added `asUint8Array()` method to `Stream`
- Added `toJSON()` method to `Bytes`
- Added `toJSON()` method to `SExp`
- Added tests
- Added example code
### Known Issues
- Loading `blsjs.wasm` into a web browser from ESModule code requires to put the wasm file into url's root path.
For example, when the main (compiled) js file is loaded by
`<script type='module' src="https://xxx.yyy.zzz/aaa/bbb/ccc/main.js"></script>`
`blsjs.wasm` will be fetched from `https://xxx.yyy.zzz/blsjs.wasm`.
This is module context only. if the main js file is not on module context like
`<script defer src="https://xxx.yyy.zzz/aaa/bbb/ccc/main.js"></script>`,
`blsjs.wasm` will be fetched from `https://xxx.yyy.zzz/aaa/bbb/ccc/blsjs.wasm`.
However, with clvm >= 3.0.0, most of bls operations run inside clvm_wasm_bg.wasm.
So I believe you don't need to load `blsjs.wasm` in most cases.

## [2.0.1]
This version is compatible with [`480b32840c525e17b5ab2f29036c033febaae71e`](https://github.com/Chia-Network/clvm/tree/480b32840c525e17b5ab2f29036c033febaae71e) of [clvm](https://github.com/Chia-Network/clvm)

Expand Down Expand Up @@ -263,6 +302,7 @@ At this version, I've managed to improve test complete time to `79s` -> `2s` by
Initial (beta) release.

<!--[Unreleased]: https://github.com/Chia-Mine/clvm-js/compare/v0.0.1...v0.0.2-->
[3.0.0]: https://github.com/Chia-Mine/clvm-js/compare/v2.0.1...v3.0.0
[2.0.1]: https://github.com/Chia-Mine/clvm-js/compare/v2.0.0...v2.0.1
[2.0.0]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.9...v2.0.0
[1.0.9]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.8...v1.0.9
Expand Down
77 changes: 48 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,32 @@ If you find something not compatible with Python's clvm, please report it to Git

## Compatibility
This code is compatible with:
- [`480b32840c525e17b5ab2f29036c033febaae71e`](https://github.com/Chia-Network/clvm/tree/480b32840c525e17b5ab2f29036c033febaae71e) of [clvm](https://github.com/Chia-Network/clvm)
- [Diff to the latest clvm](https://github.com/Chia-Network/clvm/compare/480b32840c525e17b5ab2f29036c033febaae71e...main)
- [`960f8d139940fa0814d3fac44da9a2975642f5d3`](https://github.com/Chia-Network/clvm/tree/960f8d139940fa0814d3fac44da9a2975642f5d3) of [clvm](https://github.com/Chia-Network/clvm)
- [Diff to the latest clvm](https://github.com/Chia-Network/clvm/compare/960f8d139940fa0814d3fac44da9a2975642f5d3...main)
- [`34f504bd0ef2cd3a219fea8ce6b15ff7684687fd`](https://github.com/Chia-Network/bls-signatures/tree/34f504bd0ef2cd3a219fea8ce6b15ff7684687fd) of [bls-signatures](https://github.com/Chia-Network/bls-signatures)
- [Diff to the latest bls-signatures](https://github.com/Chia-Network/bls-signatures/compare/34f504bd0ef2cd3a219fea8ce6b15ff7684687fd...main)

## Example
```javascript
// in nodejs context
async function main(){
var clvm = require("clvm");

// 'clvm.initialize()' here is not required
// if you're so sure it never calls 'pubkey_for_exp' or 'point_add' operation.
// When one of those operations is called without prior 'clvm.initialize()'
// it will raise an Error.
// If it is unknown whether 'pubkey_for_exp' or 'point_add' will be called,
// then put 'await clvm.initialize()' for safety.
// I know this 'await clvm.initialize()' makes code asynchronous
// and really impacts on code architecture.
// This is because 'clvm' relys on a wasm of 'bls-signatures',
// which requires asynchronous loading.
await clvm.initialize();
const clvm = require("clvm");

// `await clvm.initializeClvmWasm()` is now always necessary.
// This loads clvm_wasm_bg.wasm.
// If you have a strong reason to use features of bls-signatures,
// you need to do `await clvm.initialize()` instead.
// `clvm.initialize()` loads both blsjs.wasm and clvm_wasm_bg.wasm.
await clvm.initializeClvmWasm();

const {SExp, OPERATOR_LOOKUP, KEYWORD_TO_ATOM, h, t, run_program} = clvm;
const plus = h(KEYWORD_TO_ATOM["+"]);
const q = h(KEYWORD_TO_ATOM["q"]);
const program = SExp.to([plus, 1, t(q, 175)]);
const env = SExp.to(25);
const [cost, result] = run_program(program, env, OPERATOR_LOOKUP);
const {SExp, KEYWORD_TO_ATOM, h, t, run_chia_program, Flag} = clvm;
const plus = h(KEYWORD_TO_ATOM["+"]); // byte representation of '+' operator
const q = h(KEYWORD_TO_ATOM["q"]); // byte representation of 'q' operator
const program = SExp.to([plus, 1, t(q, 175)]).as_bin().raw(); // (+ . (1 . (q . 175)))
const env = SExp.to(25).as_bin().raw();
const max_cost = BigInt(10000000);
const [cost, lazyNode] = run_chia_program(program, env, max_cost, Flag.allow_backrefs());
const result = new SExp(lazyNode);
let isEqual = result.equal_to(SExp.to(25 + 175));
console.log(`isEqual: ${isEqual}`); // 'isEqual: true'
isEqual = result.as_int() === (25 + 175);
Expand All @@ -64,25 +61,43 @@ async function main(){
main().catch(e => console.error(e));
```

## More example with clvm_wasm
See this [test case for clvm_wasm](https://github.com/Chia-Mine/clvm-js/blob/v3.0.0/tests/original/clvm_wasm_test.ts)

## Use in browser
- [Sample code - Use clvm-js with TypeScript, Webpack](https://github.com/Chia-Mine/clvm-js/blob/v3.0.0/example/typescript_webpack)
- [Sample code - Use clvm-js with TypeScript, React, Vite](https://github.com/Chia-Mine/clvm-js/blob/v3.0.0/example/typescript_react)

If you'd like to run some javascript code which depends on `clvm` on browser,
you need to put `blsjs.wasm` to the same directory as the code who loads `clvm`.
you need to put `clvm_wasm_bg.wasm` and optionally `blsjs.wasm` to the same directory as the code who loads `clvm`.
Because most of BLS operations are now performed inside `clvm_wasm_bg.wasm`, in most cases you don't need `blsjs.wasm`.

<pre>
├── ...
├── main.js # js file which clvm is compiled into
└── blsjs.wasm # copy it from npm_modules/clvm/browser/blsjs.wasm
├── clvm_wasm_bg.wasm # copy it from npm_modules/clvm/browser/clvm_wasm_bg.wasm
└── (Optional) blsjs.wasm # copy it from npm_modules/clvm/browser/blsjs.wasm
</pre>

If you use [React](https://reactjs.org/), copy `blsjs.wasm` into `<react-project-root>/public/static/js/` folder. It automatically copies wasm file next to main js file.
If you use [React](https://reactjs.org/) with [CRA(create-react-app)](https://github.com/facebook/create-react-app), copy `blsjs.wasm` and `clvm_wasm_bg.wasm` into `<react-project-root>/public/static/js/` folder. It automatically copies wasm file next to main js file.

If you use [React](https://reactjs.org/) with [vite](https://vitejs.dev/),
copy `blsjs.wasm` and `clvm_wasm_bg.wasm` into `<react-project-root>/public/assets/` folder.

**IMPORTANT NOTE**
When your code is loaded as a module, such as with `<script type='module'/>` (common in React with Vite),
there is a path restriction for loading the WebAssembly (WASM) module.
See [Known Issues](https://github.com/Chia-Mine/clvm-js/blob/v3.0.0/CHANGELOG.md#known-issues). Also see [code comment here](https://github.com/Chia-Mine/clvm-js/blob/v3.0.0/example/typescript_react/src/index.tsx)

**Note1**
Don't forget to wait `clvm.initialize()` if you are not sure whether `pubkey_for_exp`/`point_add` will be called.
Don't forget to wait `clvm.initializeClvmWasm()`.
`clvm.initializeClvmWasm()` only loads `clvm_wasm_bg.wasm`.
If you have a strong reason to use features of `bls-signatures` inside `clvm-js`, you need to wait
`clvm.initialize()` instead, since `clvm.initialize()` loads both `blsjs.wasm` and `clvm_wasm_bg.wasm`.
**Note2**
If you're really sure that `pubkey_for_exp`/`point_add` will never be called, then you can opt out `blsjs.wasm` and `await clvm.initialize()`.
If so, you can make your code fully synchronous.
**Note3**
Redistributing your project with bundled `blsjs.wasm` must be compliant with Apache2.0 License provided by [Chia-Network](https://github.com/Chia-Network/bls-signatures/blob/main/LICENSE)
Redistributing your project with bundled `blsjs.wasm` and/or `clvm_wasm_bg.wasm` must be compliant with Apache2.0 License provided by [Chia-Network](https://github.com/Chia-Network/bls-signatures/blob/main/LICENSE)
**Note3**
You may need `blsjs.wasm` if you want to run `run_program`, which has been deprecated as of [email protected].

### Browser compatibility
`clvm-js` uses `BigInt`. So if runtime environment does not support `BigInt`, `clvm-js` doesn't work as well.
Expand Down Expand Up @@ -212,6 +227,10 @@ str(SExp.to([1, [2, 3]])); // You can use str() function as well as Python by th
`clvm-js` is based on [clvm](https://github.com/Chia-Network/clvm) with the
[Apache license 2.0](https://github.com/Chia-Network/clvm/blob/main/LICENSE)

## clvm_wasm license
[clvm_wasm](https://github.com/Chia-Network/clvm_rs/tree/main/wasm) is used and redistributed under the
[Apache license 2.0](https://github.com/Chia-Network/clvm_rs/blob/main/wasm/LICENSE)

## bls-signatures license
[bls-signatures](https://github.com/Chia-Network/bls-signatures) is used and redistributed under the
[Apache license 2.0](https://github.com/Chia-Network/bls-signatures/blob/main/LICENSE)
Expand Down
74 changes: 74 additions & 0 deletions example/typescript_react/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[*]
charset=utf-8
end_of_line=lf
trim_trailing_whitespace=false
insert_final_newline=false
indent_style=space
indent_size=2

[{*.ng,*.sht,*.html,*.shtm,*.shtml,*.htm}]
indent_style=space
indent_size=2

[{.eslintrc,.prettierrc,.babelrc,jest.config,.stylelintrc,*.json,*.jsb3,*.jsb2,*.bowerrc}]
indent_style=space
indent_size=2

[*.svg]
indent_style=space
indent_size=2

[*.css]
indent_style=space
indent_size=2

[*.scss]
indent_style=space
indent_size=2

[*.styl]
indent_style=space
indent_size=2

[*.js]
indent_style=space
indent_size=2

[{*.ats,*.ts}]
indent_style=space
indent_size=2
ij_typescript_import_use_node_resolution = false

[*.tsx]
indent_style=space
indent_size=2
ij_typescript_import_use_node_resolution = false

[{*.mjs,*.es6}]
indent_style=space
indent_size=2

[*.js.flow]
indent_style=space
indent_size=2

[*.jsx]
indent_style=space
indent_size=2

[{jshint.json,*.jshintrc}]
indent_style=space
indent_size=2

[{tsconfig.e2e.json,tsconfig.spec.json,tsconfig.app.json,tsconfig.json}]
indent_style=space
indent_size=2

[*.js.map]
indent_style=space
indent_size=2

[{.analysis_options,*.yml,*.yaml}]
indent_style=space
indent_size=2

Loading

0 comments on commit 18cfeec

Please sign in to comment.