From d914007fd14456217d4f4430ad4e868ca998f65d Mon Sep 17 00:00:00 2001 From: Keith Date: Tue, 11 Feb 2020 18:21:28 +0800 Subject: [PATCH 1/3] fix(core): fix the calculation of epoch locks in nervos dao operation --- .../__tests__/utils/fixtures.json | 121 ++++++++++++++++++ .../__tests__/utils/index.test.js | 21 +++ packages/ckb-sdk-core/src/index.ts | 16 +-- packages/ckb-sdk-core/src/utils.ts | 37 ++++++ 4 files changed, 181 insertions(+), 14 deletions(-) create mode 100644 packages/ckb-sdk-core/__tests__/utils/fixtures.json create mode 100644 packages/ckb-sdk-core/__tests__/utils/index.test.js create mode 100644 packages/ckb-sdk-core/src/utils.ts diff --git a/packages/ckb-sdk-core/__tests__/utils/fixtures.json b/packages/ckb-sdk-core/__tests__/utils/fixtures.json new file mode 100644 index 00000000..e0de3cfd --- /dev/null +++ b/packages/ckb-sdk-core/__tests__/utils/fixtures.json @@ -0,0 +1,121 @@ +{ + "calculateLockEpochs": [ + { + "depositEpoch": { + "number": "0", + "index": "0", + "length": "100" + }, + "withdrawEpoch": { + "number": "0", + "index": "0", + "length": "100" + }, + "expected": "180" + }, + { + "depositEpoch": { + "number": "1", + "index": "0", + "length": "100" + }, + "withdrawEpoch": { + "number": "1", + "index": "0", + "length": "100" + }, + "expected": "180" + }, + { + "depositEpoch": { + "number": "0", + "index": "0", + "length": "100" + }, + "withdrawEpoch": { + "number": "179", + "index": "0", + "length": "100" + }, + "expected": "180" + }, + { + "depositEpoch": { + "number": "0", + "index": "0", + "length": "100" + }, + "withdrawEpoch": { + "number": "179", + "index": "0", + "length": "100" + }, + "expected": "180" + }, + { + "depositEpoch": { + "number": "0", + "index": "1", + "length": "100" + }, + "withdrawEpoch": { + "number": "179", + "index": "0", + "length": "100" + }, + "expected": "180" + }, + { + "depositEpoch": { + "number": "0", + "index": "0", + "length": "100" + }, + "withdrawEpoch": { + "number": "179", + "index": "1", + "length": "100" + }, + "expected": "180" + }, + { + "depositEpoch": { + "number": "0", + "index": "0", + "length": "100" + }, + "withdrawEpoch": { + "number": "180", + "index": "0", + "length": "100" + }, + "expected": "180" + }, + { + "depositEpoch": { + "number": "0", + "index": "0", + "length": "100" + }, + "withdrawEpoch": { + "number": "180", + "index": "1", + "length": "100" + }, + "expected": "360" + }, + { + "depositEpoch": { + "number": "1", + "index": "634", + "length": "1800" + }, + "withdrawEpoch": { + "number": "4", + "index": "820", + "length": "1800" + }, + "expected": "180" + } + ] +} diff --git a/packages/ckb-sdk-core/__tests__/utils/index.test.js b/packages/ckb-sdk-core/__tests__/utils/index.test.js new file mode 100644 index 00000000..5a857799 --- /dev/null +++ b/packages/ckb-sdk-core/__tests__/utils/index.test.js @@ -0,0 +1,21 @@ +const { calculateLockEpochs } = require('../../lib/utils') +const fixtures = require('./fixtures.json') + +describe('Test utils', () => { + describe('Test calculate lock epochs', () => { + const fixtureTable = fixtures.calculateLockEpochs.map(({ depositEpoch, withdrawEpoch, expected }) => [ + depositEpoch, + withdrawEpoch, + expected, + ]) + + test.each(fixtureTable)(`(%s, %s) => %s`, (depositEpoch, withdrawEpoch, expected) => { + const actual = calculateLockEpochs({ + depositEpoch, + withdrawEpoch, + DAO_LOCK_PERIOD_EPOCHS: 180, + }) + expect(actual.toString()).toBe(expected) + }) + }) +}) diff --git a/packages/ckb-sdk-core/src/index.ts b/packages/ckb-sdk-core/src/index.ts index 1618968d..a9e6209b 100644 --- a/packages/ckb-sdk-core/src/index.ts +++ b/packages/ckb-sdk-core/src/index.ts @@ -9,6 +9,7 @@ import generateRawTransaction, { Cell, RawTransactionParamsBase } from './genera import loadCells from './loadCells' import signWitnesses, { isMap } from './signWitnesses' +import {calculateLockEpochs} from './utils' type Key = string @@ -420,20 +421,7 @@ class CKB { const withdrawBlockHeader = await this.rpc.getBlock(tx.txStatus.blockHash).then(block => block.header) const withdrawEpoch = this.utils.parseEpoch(withdrawBlockHeader.epoch) - const withdrawFraction = JSBI.multiply(JSBI.BigInt(withdrawEpoch.index), JSBI.BigInt(depositEpoch.length)) - const depositFraction = JSBI.multiply(JSBI.BigInt(depositEpoch.index) , JSBI.BigInt(withdrawEpoch.length)) - let depositedEpochs = JSBI.subtract(JSBI.BigInt(withdrawEpoch.number) , JSBI.BigInt(depositEpoch.number)) - if (JSBI.greaterThan(withdrawFraction, depositFraction)) { - depositedEpochs = JSBI.add(depositedEpochs, JSBI.BigInt(1)) - } - const lockEpochs = JSBI.multiply( - JSBI.divide( - JSBI.add( - depositedEpochs, JSBI.BigInt(DAO_LOCK_PERIOD_EPOCHS - 1) - ), - JSBI.BigInt(DAO_LOCK_PERIOD_EPOCHS) - ), JSBI.BigInt(DAO_LOCK_PERIOD_EPOCHS) - ) + const lockEpochs = calculateLockEpochs({withdrawEpoch,depositEpoch,DAO_LOCK_PERIOD_EPOCHS}) const minimalSince = this.absoluteEpochSince({ length: `0x${JSBI.BigInt(depositEpoch.length).toString(16)}`, index: `0x${JSBI.BigInt(depositEpoch.index).toString(16)}`, diff --git a/packages/ckb-sdk-core/src/utils.ts b/packages/ckb-sdk-core/src/utils.ts new file mode 100644 index 00000000..a3ca468d --- /dev/null +++ b/packages/ckb-sdk-core/src/utils.ts @@ -0,0 +1,37 @@ +import { JSBI } from '@nervosnetwork/ckb-sdk-utils' + +interface EpochInfo { + index: string + length: string + number: string +} + +export const calculateLockEpochs = ({ + withdrawEpoch, + depositEpoch, + DAO_LOCK_PERIOD_EPOCHS, +}: { + withdrawEpoch: EpochInfo + depositEpoch: EpochInfo + DAO_LOCK_PERIOD_EPOCHS: number +}) => { + const withdrawFraction = JSBI.multiply(JSBI.BigInt(withdrawEpoch.index), JSBI.BigInt(depositEpoch.length)) + const depositFraction = JSBI.multiply(JSBI.BigInt(depositEpoch.index), JSBI.BigInt(withdrawEpoch.length)) + let depositedEpochs = JSBI.subtract(JSBI.BigInt(withdrawEpoch.number), JSBI.BigInt(depositEpoch.number)) + if (JSBI.greaterThan(withdrawFraction, depositFraction)) { + depositedEpochs = JSBI.add(depositedEpochs, JSBI.BigInt(1)) + } + /* eslint-disable indent */ + const lockEpochs = JSBI.lessThan(depositedEpochs, JSBI.BigInt(DAO_LOCK_PERIOD_EPOCHS)) + ? JSBI.BigInt(DAO_LOCK_PERIOD_EPOCHS) + : JSBI.multiply( + JSBI.divide( + JSBI.add(depositedEpochs, JSBI.BigInt(DAO_LOCK_PERIOD_EPOCHS - 1)), + JSBI.BigInt(DAO_LOCK_PERIOD_EPOCHS), + ), + JSBI.BigInt(DAO_LOCK_PERIOD_EPOCHS), + ) + /* eslint-enable indent */ + return lockEpochs +} +export default { calculateLockEpochs } From dacec65239c3683f4ea0e5b1946f9ec74d275500 Mon Sep 17 00:00:00 2001 From: Keith Date: Fri, 28 Feb 2020 14:00:35 +0800 Subject: [PATCH 2/3] chore: update package versions and changelogs --- CHANGELOG.md | 11 +++++++++++ lerna.json | 2 +- packages/ckb-sdk-core/CHANGELOG.md | 11 +++++++++++ packages/ckb-sdk-core/package.json | 8 ++++---- packages/ckb-sdk-rpc/CHANGELOG.md | 16 ++++++++++++++++ packages/ckb-sdk-rpc/package.json | 6 +++--- packages/ckb-sdk-utils/CHANGELOG.md | 8 ++++++++ packages/ckb-sdk-utils/package.json | 4 ++-- packages/ckb-types/CHANGELOG.md | 8 ++++++++ packages/ckb-types/package.json | 2 +- 10 files changed, 65 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8925363..7d6f6382 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.29.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.1) (2020-02-28) + + +### Bug Fixes + +* **core:** fix the calculation of epoch locks in nervos dao operation ([d914007](https://github.com/nervosnetwork/ckb-sdk-js/commit/d914007fd14456217d4f4430ad4e868ca998f65d)) + + + + + # [0.29.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.0) (2020-02-28) diff --git a/lerna.json b/lerna.json index b0e7e5eb..a1995a7f 100644 --- a/lerna.json +++ b/lerna.json @@ -4,5 +4,5 @@ ], "npmClient": "yarn", "useWorkspaces": true, - "version": "0.29.0" + "version": "0.29.1" } diff --git a/packages/ckb-sdk-core/CHANGELOG.md b/packages/ckb-sdk-core/CHANGELOG.md index 7dc0165d..c9e16e92 100644 --- a/packages/ckb-sdk-core/CHANGELOG.md +++ b/packages/ckb-sdk-core/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.29.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.1) (2020-02-28) + + +### Bug Fixes + +* **core:** fix the calculation of epoch locks in nervos dao operation ([d914007](https://github.com/nervosnetwork/ckb-sdk-js/commit/d914007fd14456217d4f4430ad4e868ca998f65d)) + + + + + # [0.29.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.0) (2020-02-28) **Note:** Version bump only for package @nervosnetwork/ckb-sdk-core diff --git a/packages/ckb-sdk-core/package.json b/packages/ckb-sdk-core/package.json index 14fa671a..c37ab337 100644 --- a/packages/ckb-sdk-core/package.json +++ b/packages/ckb-sdk-core/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-sdk-core", - "version": "0.29.0", + "version": "0.29.1", "description": "JavaScript SDK for Nervos Network CKB Project", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", @@ -31,9 +31,9 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-sdk-rpc": "0.29.0", - "@nervosnetwork/ckb-sdk-utils": "0.29.0", - "@nervosnetwork/ckb-types": "0.29.0" + "@nervosnetwork/ckb-sdk-rpc": "0.29.1", + "@nervosnetwork/ckb-sdk-utils": "0.29.1", + "@nervosnetwork/ckb-types": "0.29.1" }, "gitHead": "0907e4ead0d2ff70dab46fd9ba637d9ee9f02a15" } diff --git a/packages/ckb-sdk-rpc/CHANGELOG.md b/packages/ckb-sdk-rpc/CHANGELOG.md index 542accd9..84bacad7 100644 --- a/packages/ckb-sdk-rpc/CHANGELOG.md +++ b/packages/ckb-sdk-rpc/CHANGELOG.md @@ -3,6 +3,22 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.29.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.1) (2020-02-28) + + +### Features + +* **rpc:** update the action of outputs validator when it is null ([4932c47](https://github.com/nervosnetwork/ckb-sdk-js/commit/4932c479141b6d7a109705c389290b66d67c83a2)) + + +### BREAKING CHANGES + +* **rpc:** null outputs validator is equivalent to the passthrough one + + + + + # [0.29.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.0) (2020-02-28) diff --git a/packages/ckb-sdk-rpc/package.json b/packages/ckb-sdk-rpc/package.json index 9cdb36a6..1cea5387 100644 --- a/packages/ckb-sdk-rpc/package.json +++ b/packages/ckb-sdk-rpc/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-sdk-rpc", - "version": "0.29.0", + "version": "0.29.1", "description": "RPC module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js/packages/ckb-rpc#readme", @@ -33,11 +33,11 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-sdk-utils": "0.29.0", + "@nervosnetwork/ckb-sdk-utils": "0.29.1", "axios": "0.19.0" }, "devDependencies": { - "@nervosnetwork/ckb-types": "0.29.0", + "@nervosnetwork/ckb-types": "0.29.1", "dotenv": "8.1.0" }, "gitHead": "0907e4ead0d2ff70dab46fd9ba637d9ee9f02a15" diff --git a/packages/ckb-sdk-utils/CHANGELOG.md b/packages/ckb-sdk-utils/CHANGELOG.md index 877c0219..ee5cc24b 100644 --- a/packages/ckb-sdk-utils/CHANGELOG.md +++ b/packages/ckb-sdk-utils/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.29.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.1) (2020-02-28) + +**Note:** Version bump only for package @nervosnetwork/ckb-sdk-utils + + + + + # [0.29.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.0) (2020-02-28) **Note:** Version bump only for package @nervosnetwork/ckb-sdk-utils diff --git a/packages/ckb-sdk-utils/package.json b/packages/ckb-sdk-utils/package.json index c19821ee..41c78838 100644 --- a/packages/ckb-sdk-utils/package.json +++ b/packages/ckb-sdk-utils/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-sdk-utils", - "version": "0.29.0", + "version": "0.29.1", "description": "Utils module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", @@ -31,7 +31,7 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-types": "0.29.0", + "@nervosnetwork/ckb-types": "0.29.1", "blake2b-wasm": "2.1.0", "elliptic": "6.5.2", "jsbi": "3.1.1" diff --git a/packages/ckb-types/CHANGELOG.md b/packages/ckb-types/CHANGELOG.md index db2dd9c8..14d2288e 100644 --- a/packages/ckb-types/CHANGELOG.md +++ b/packages/ckb-types/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.29.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.1) (2020-02-28) + +**Note:** Version bump only for package @nervosnetwork/ckb-types + + + + + # [0.29.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.28.0...v0.29.0) (2020-02-28) **Note:** Version bump only for package @nervosnetwork/ckb-types diff --git a/packages/ckb-types/package.json b/packages/ckb-types/package.json index c35ea55a..4636cd90 100644 --- a/packages/ckb-types/package.json +++ b/packages/ckb-types/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-types", - "version": "0.29.0", + "version": "0.29.1", "description": "Type module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", From be1036d1f788e2382f7e985e0d9c5254bb75fd1d Mon Sep 17 00:00:00 2001 From: Keith Date: Fri, 28 Feb 2020 17:53:20 +0800 Subject: [PATCH 3/3] chore: update lerna hashes --- packages/ckb-sdk-core/package.json | 2 +- packages/ckb-sdk-rpc/package.json | 2 +- packages/ckb-sdk-utils/package.json | 2 +- packages/ckb-types/package.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/ckb-sdk-core/package.json b/packages/ckb-sdk-core/package.json index c37ab337..91d63948 100644 --- a/packages/ckb-sdk-core/package.json +++ b/packages/ckb-sdk-core/package.json @@ -35,5 +35,5 @@ "@nervosnetwork/ckb-sdk-utils": "0.29.1", "@nervosnetwork/ckb-types": "0.29.1" }, - "gitHead": "0907e4ead0d2ff70dab46fd9ba637d9ee9f02a15" + "gitHead": "dacec65239c3683f4ea0e5b1946f9ec74d275500" } diff --git a/packages/ckb-sdk-rpc/package.json b/packages/ckb-sdk-rpc/package.json index 1cea5387..c3307655 100644 --- a/packages/ckb-sdk-rpc/package.json +++ b/packages/ckb-sdk-rpc/package.json @@ -40,5 +40,5 @@ "@nervosnetwork/ckb-types": "0.29.1", "dotenv": "8.1.0" }, - "gitHead": "0907e4ead0d2ff70dab46fd9ba637d9ee9f02a15" + "gitHead": "dacec65239c3683f4ea0e5b1946f9ec74d275500" } diff --git a/packages/ckb-sdk-utils/package.json b/packages/ckb-sdk-utils/package.json index 41c78838..8d1e5310 100644 --- a/packages/ckb-sdk-utils/package.json +++ b/packages/ckb-sdk-utils/package.json @@ -41,5 +41,5 @@ "@types/elliptic": "6.4.8", "@types/utf8": "2.1.6" }, - "gitHead": "0907e4ead0d2ff70dab46fd9ba637d9ee9f02a15" + "gitHead": "dacec65239c3683f4ea0e5b1946f9ec74d275500" } diff --git a/packages/ckb-types/package.json b/packages/ckb-types/package.json index 4636cd90..d16a32ce 100644 --- a/packages/ckb-types/package.json +++ b/packages/ckb-types/package.json @@ -23,5 +23,5 @@ "scripts": { "doc": "../../node_modules/.bin/typedoc --out docs ./index.d.ts --mode modules --includeDeclarations --excludeExternals --ignoreCompilerErrors --theme default --readme README.md" }, - "gitHead": "0907e4ead0d2ff70dab46fd9ba637d9ee9f02a15" + "gitHead": "dacec65239c3683f4ea0e5b1946f9ec74d275500" }