Skip to content

Commit

Permalink
Merge pull request #29 from Chia-Mine/v1.0.7
Browse files Browse the repository at this point in the history
V1.0.7
  • Loading branch information
ChiaMineJP authored Sep 3, 2021
2 parents c15fdd4 + a93c18e commit a9fdc12
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 77 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ webpack.*.js
tests/
bin/
pre_build.js
jest.config.js
jest.config.js
src/__bls_signatures__/loader.js
29 changes: 22 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# Changelog

## [1.0.7]
This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm)

### Added
- Added type declaration file `browser/index.d.ts` on build.
### Changed
- Changed SExp/CLVMObject properties `atom` and `pair` to readonly to prevent potential bug.
- Changed `src/__bls_signatures__.ts` location to `src/__bls_signatures__/index.ts` to prepare for future wasm-loading tweaks.
- Upgraded `@chiamine/bls-signatures` to 0.2.1-beta.2.
- Use `CLVMType` instead of `CLVMObject` as a valid type representation of `CLVMObject`.
(CLVMObject should not be used as a type because there might be number of type incompatibility due to new private field)
- <strike>Replace `**` operator on `BigInt` by user-defined function because babel or some transpiler always converts `**` to `Math.pow` which cannot be used with `BigInt`</strike>
- Although I tried this, it turns out to be 100x slower than `**` operator, so I gave up. It is required to disable such a transform by end-developer. See detail [here](./README.md#browser-compatibility)

## [1.0.6]
This version is compatible with [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [clvm@0.9.7](https://github.com/Chia-Network/clvm)
This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm)

### Changed
- Improved `bigint_from_bytes` around 3-10 times faster.
Expand All @@ -14,27 +28,27 @@ This version is compatible with [`ab4560900cf475ff515054bec0ca9a4491aca366`](htt
- Added `divmod` utility function.

## [1.0.5]
This version is compatible with [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [clvm@0.9.7](https://github.com/Chia-Network/clvm)
This version is compatible with [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [clvm](https://github.com/Chia-Network/clvm)

### Fixed
- Fixed an issue where `op_lsh` did not work as expected.

## [1.0.4]
This version is compatible with [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [clvm@0.9.7](https://github.com/Chia-Network/clvm)
This version is compatible with [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [clvm](https://github.com/Chia-Network/clvm)

### Fixed
- Fixed an issue where `int_from_bytes` and `bigint_from_bytes` returned always signed int/bigint.
- Fixed an issue where `int_to_bytes` and `bigint_to_bytes` blindly recognized the argument as a signed int/bigint.

## [1.0.3]
This version is compatible with [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [clvm@0.9.7](https://github.com/Chia-Network/clvm)
This version is compatible with [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [clvm](https://github.com/Chia-Network/clvm)

### Fixed
- Fixed an issue where `op_logand`, `op_logior`, `op_logxor` did not work
- Fixed an issue where result of div/mod against negative `bigint` was not compatible with original `clvm`

## [1.0.2]
This version is compatible with [`1a5cb17895d8707f784a85180bc97d3c6ebe71a0`](https://github.com/Chia-Network/clvm/tree/1a5cb17895d8707f784a85180bc97d3c6ebe71a0) of [clvm@0.9.7](https://github.com/Chia-Network/clvm)
This version is compatible with [`1a5cb17895d8707f784a85180bc97d3c6ebe71a0`](https://github.com/Chia-Network/clvm/tree/1a5cb17895d8707f784a85180bc97d3c6ebe71a0) of [clvm](https://github.com/Chia-Network/clvm)

### Fixed
- Fixed an issue where `op_pubkey_for_exp` was missing.
Expand All @@ -44,13 +58,13 @@ This version is compatible with [`1a5cb17895d8707f784a85180bc97d3c6ebe71a0`](htt
- Added tests for Bytes-BigInt conversion.

## [1.0.1]
This version is compatible with [`1a5cb17895d8707f784a85180bc97d3c6ebe71a0`](https://github.com/Chia-Network/clvm/tree/1a5cb17895d8707f784a85180bc97d3c6ebe71a0) of [clvm@0.9.7](https://github.com/Chia-Network/clvm)
This version is compatible with [`1a5cb17895d8707f784a85180bc97d3c6ebe71a0`](https://github.com/Chia-Network/clvm/tree/1a5cb17895d8707f784a85180bc97d3c6ebe71a0) of [clvm](https://github.com/Chia-Network/clvm)

### Changed
- Improved `Stream::read` performance by removing extra memory copy.

## [1.0.0]
This version is compatible with [`1a5cb17895d8707f784a85180bc97d3c6ebe71a0`](https://github.com/Chia-Network/clvm/tree/1a5cb17895d8707f784a85180bc97d3c6ebe71a0) of [clvm@0.9.7](https://github.com/Chia-Network/clvm)
This version is compatible with [`1a5cb17895d8707f784a85180bc97d3c6ebe71a0`](https://github.com/Chia-Network/clvm/tree/1a5cb17895d8707f784a85180bc97d3c6ebe71a0) of [clvm](https://github.com/Chia-Network/clvm)

### Removed
- Removed `.dist` folder from git.
Expand Down Expand Up @@ -213,6 +227,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-->
[1.0.7]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.6...v1.0.7
[1.0.6]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.5...v1.0.6
[1.0.5]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.4...v1.0.5
[1.0.4]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.3...v1.0.4
Expand Down
80 changes: 65 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# clvm

Javascript implementation of CLVM (Chia Lisp VM)

## Install
Expand All @@ -9,18 +8,36 @@ npm install clvm
yarn add clvm
```

## Test
`clvm-js` passes all the test equivalent to Python's original `clvm`.
You can compare test files for [Javascript](./tests) and [Python](https://github.com/Chia-Network/clvm/tree/main/tests)
To run the test, execute the following command.
```shell
git clone https://github.com/Chia-Mine/clvm-js
cd clvm-js

npm install
npm run test
# or
yarn
yarn test
```
If you find something not compatible with Python's clvm, please report it to GitHub issues.

## Compatibility
This code is compatible with:
- [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [[email protected]](https://github.com/Chia-Network/clvm)
- [Diff to the latest clvm](https://github.com/Chia-Network/clvm/compare/ab4560900cf475ff515054bec0ca9a4491aca366...main)
- [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm)
- [Diff to the latest clvm](https://github.com/Chia-Network/clvm/compare/2722c78ddb92f067c5025196f397e4d2955f9053...main)
- [`f9db7faa370c4743e48be8a9823232e4d906c4d0`](https://github.com/Chia-Network/bls-signatures/tree/f9db7faa370c4743e48be8a9823232e4d906c4d0) of [bls-signatures](https://github.com/Chia-Network/bls-signatures)
- [Diff to the latest bls-signatures](https://github.com/Chia-Network/bls-signatures/compare/f9db7faa370c4743e48be8a9823232e4d906c4d0...main)

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

// 'clvm.initialization()' here is not required
// '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.
Expand All @@ -32,11 +49,16 @@ async function main(){
// which requires asynchronous loading.
await clvm.initialize();

const {SExp, OPERATOR_LOOKUP, KEYWORD_TO_ATOM} = clvm;
const v1 = OPERATOR_LOOKUP(KEYWORD_TO_ATOM["+"], SExp.to([3,4,5]))[1];
const v2 = SExp.to(12);
const ok = v1.equal_to(v2);
console.log(`ok: ${ok}`); // 'ok: true'
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);
let isEqual = result.equal_to(SExp.to(25 + 175));
console.log(`isEqual: ${isEqual}`); // 'isEqual: true'
isEqual = result.as_int() === (25 + 175);
console.log(`isEqual: ${isEqual}`); // 'isEqual: true'
}

main().catch(e => console.error(e));
Expand All @@ -49,13 +71,41 @@ you need to put `blsjs.wasm` to the same directory as the code who loads `clvm`.
<pre>
├── ...
├── main.js # js file which clvm is compiled into
└── blsjs.wasm # copy it from npm_modules/clvm/dist/browser/blsjs.wasm
└── blsjs.wasm # copy it from npm_modules/clvm/browser/blsjs.wasm
</pre>

**Note1**: Don't forget to wait `clvm.initialize()` if you are not sure whether `pubkey_for_exp`/`point_add` will be called.
**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.

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.

**Note1**
Don't forget to wait `clvm.initialize()` if you are not sure whether `pubkey_for_exp`/`point_add` will be called.
**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)

### Browser compatibility
`clvm-js` uses `BigInt`. So if runtime environment does not support `BigInt`, `clvm-js` doesn't work as well.
If you transpile code using babel or something which uses babel (like create-react-app),
you need to tell the transpiler to optimize code only for the target browsers.
Just copy and paste below to your `package.json` and you can avoid a lot of runtime incompatibility issues.
```
"browserslist": [
"edge >= 79",
"firefox >= 68",
"chrome >= 67",
"safari > 14",
"opera >= 54",
"ios_saf >= 14.4",
"android >= 67",
"op_mob >= 48",
"and_chr >= 67",
"and_ff >= 68",
"samsung >= 9.2",
"node >= 10.4.0",
"electron >= 4.0.0"
]
```

## Differences with Python's `clvm`
Although I try hard to make it look like Python's `clvm`, there are things users should be aware of.
Expand Down Expand Up @@ -163,7 +213,7 @@ str(SExp.to([1, [2, 3]])); // You can use str() function as well as Python by th
[Apache license 2.0](https://github.com/Chia-Network/clvm/blob/main/LICENSE)

## bls-signatures license
[bls-signatures](https://github.com/Chia-Network/bls-signatures) is used under the
[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)

## jscrypto license
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
tsConfig: {
sourceMap: true,
inlineSourceMap: true,
allowJs: true,
}
},
},
Expand Down
23 changes: 19 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "clvm",
"version": "1.0.6",
"author": "Admin ChiaMineJP <[email protected]>",
"version": "1.0.7",
"author": "ChiaMineJP <[email protected]>",
"description": "Javascript implementation of chia lisp",
"license": "MIT",
"repository": {
Expand All @@ -25,7 +25,7 @@
"node": ">=12.13.0"
},
"dependencies": {
"@chiamine/bls-signatures": "^0.2.1-beta.1",
"@chiamine/bls-signatures": "^0.2.1-beta.2",
"jscrypto": "^1.0.2"
},
"devDependencies": {
Expand All @@ -44,5 +44,20 @@
"typescript": "^4.2.4",
"webpack": "^5.39.1",
"webpack-cli": "^4.7.2"
}
},
"browserslist": [
"edge >= 79",
"firefox >= 68",
"chrome >= 67",
"safari > 14",
"opera >= 54",
"ios_saf >= 14.4",
"android >= 67",
"op_mob >= 48",
"and_chr >= 67",
"and_ff >= 68",
"samsung >= 9.2",
"node >= 10.4.0",
"electron >= 4.0.0"
]
}
2 changes: 2 additions & 0 deletions pre_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ if(!fs.existsSync(blsWasmSrcPath)){
return;
}
fs.copyFileSync(blsWasmSrcPath, blsWasmDestPath);
const browserDtsPath = path.join(browserDir, "index.d.ts");
fs.writeFileSync(browserDtsPath, 'export * from "..";\n');


const packageJson = require("./package.json");
Expand Down
26 changes: 16 additions & 10 deletions src/CLVMObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,28 @@ export type Cons = {
by just having an "atom" and a "pair" field
*/
export class CLVMObject implements CLVMType {
atom: Optional<Bytes> = None;
// this is always a 2-tuple of an object implementing the CLVM object protocol.
pair: Optional<Tuple<any, any>> = None;
private readonly _atom: Optional<Bytes> = None;
private readonly _pair: Optional<Tuple<any, any>> = None;

get atom(){
return this._atom;
}
get pair(){
return this._pair;
}

public constructor(v: any) {
if(isCLVMObject(v)){
this.atom = v.atom;
this.pair = v.pair;
this._atom = v.atom;
this._pair = v.pair;
}
else if(isTuple(v)){
this.pair = v;
this.atom = None;
this._pair = v;
this._atom = None;
}
else{
this.atom = v;
this.pair = None;
this._atom = v;
this._pair = None;
}
}
}
Expand All @@ -56,6 +62,6 @@ export function isCons(obj: CLVMType): obj is Cons {
return Boolean((!obj.atom && obj.pair));
}

export function isCLVMObject(v: any): v is CLVMObject {
export function isCLVMObject(v: any): v is CLVMType {
return v && typeof v.atom !== "undefined" && typeof v.pair !== "undefined";
}
6 changes: 3 additions & 3 deletions src/EvalError.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {CLVMObject} from "./CLVMObject";
import type {CLVMType} from "./CLVMObject";

export class EvalError extends Error {
public _sexp?: CLVMObject;
public _sexp?: CLVMType;
name = "EvalError";

public constructor(message: string, sexp: CLVMObject) {
public constructor(message: string, sexp: CLVMType) {
super(message);
this._sexp = sexp;
}
Expand Down
Loading

0 comments on commit a9fdc12

Please sign in to comment.