Skip to content

Commit

Permalink
fix(rpc): update the signatures of rpc methods
Browse files Browse the repository at this point in the history
use bigint instead of number in signatures of rpc methods

BREAKING CHANGE: use bigint instead of number in signatures of rpc methods

re #363, fix #365
  • Loading branch information
Keith-CY committed Oct 12, 2019
1 parent 9aeec49 commit 7eb6726
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 51 deletions.
18 changes: 0 additions & 18 deletions packages/ckb-sdk-core/__tests__/loadCells/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,6 @@
},
"exception": "Hex string 12c should start with 0x"
},
"parameters including invalid integer of start should throw an error": {
"params": {
"lockHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6",
"start": 1.1,
"end": "0x12c",
"STEP": 100
},
"exception": "1.1 cannot be converted into an integer"
},
"parameters including invalid integer of end should throw an error": {
"params": {
"lockHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6",
"start": "0x1",
"end": 1.1,
"STEP": 100
},
"exception": "1.1 cannot be converted into an integer"
},
"end less than start should throw an error": {
"params": {
"lockHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6",
Expand Down
15 changes: 15 additions & 0 deletions packages/ckb-sdk-core/__tests__/loadCells/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-param-reassign */
const { default: loadCells } = require('../../lib/loadCells')
const rpc = require('../../__mocks__/rpc')
const fixtures = require('./fixtures.json')
Expand All @@ -11,6 +12,20 @@ describe('load cells', () => {
exception,
])
test.each(fixtureTable)('%s case: %j', async (_title, params, expectedCells, expectedCalls, exception) => {
Object.keys(params).forEach(key => {
if (typeof params[key] === 'number') {
params[key] = BigInt(params[key])
}
})
if (expectedCalls) {
expectedCalls.forEach(call => {
call.forEach((v, i) => {
if (typeof v === 'number') {
call[i] = BigInt(v)
}
})
})
}
rpc.getCellsByLockHash.mockClear()
if (undefined === exception) {
const cells = await loadCells({
Expand Down
10 changes: 5 additions & 5 deletions packages/ckb-sdk-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ class Core {

public loadCells = async ({
lockHash,
start = 0,
start = BigInt(0),
end,
STEP = 100,
STEP = BigInt(100),
save = false,
}: {
lockHash: string
start?: string | number
end?: string | number
STEP?: number
start?: string | bigint
end?: string | bigint
STEP?: bigint
save?: boolean
}) => {
const cells = await loadCells({ lockHash, start, end, STEP, rpc: this.rpc })
Expand Down
31 changes: 13 additions & 18 deletions packages/ckb-sdk-core/src/loadCells.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import RPC from '@nervosnetwork/ckb-sdk-rpc'
import { HexStringShouldStartWith0x, ArgumentRequired } from '@nervosnetwork/ckb-sdk-utils/lib/exceptions'

const getMinBigInt = (x: bigint, y: bigint) => (x > y ? y : x)

const loadCells = async ({
lockHash,
start = 0,
start = BigInt(0),
end,
STEP = 100,
STEP = BigInt(100),
rpc,
}: {
lockHash: string
start?: string | number
end?: string | number
STEP?: number
start?: string | bigint
end?: string | bigint
STEP?: bigint
rpc: RPC
}) => {
if (!lockHash) {
Expand All @@ -28,19 +30,12 @@ const loadCells = async ({
throw new HexStringShouldStartWith0x(end)
}

const from = +start
if (!Number.isInteger(from)) {
throw new Error(`${start} cannot be converted into an integer`)
}
const from = BigInt(start)
const tipBlockNumber = await rpc.getTipBlockNumber()

let to = end === undefined ? +tipBlockNumber : Math.min(+end, +tipBlockNumber)

if (!Number.isInteger(to)) {
throw new Error(`${end} cannot be converted into an integer`)
}
let to = end === undefined ? BigInt(tipBlockNumber) : getMinBigInt(BigInt(end), BigInt(tipBlockNumber))

to = Math.min(to, +tipBlockNumber)
to = getMinBigInt(to, BigInt(tipBlockNumber))

if (to < from) {
throw new Error(`start(${start}) should not be less than end(${end})`)
Expand All @@ -49,9 +44,9 @@ const loadCells = async ({
const range = to - from

const groups = range
? Array.from({ length: Math.ceil(range / STEP) }, (_, idx) => [
from + idx * STEP + (idx ? 1 : 0),
Math.min(from + (idx + 1) * STEP, to),
? Array.from({ length: Math.ceil(Number(range) / Number(STEP)) }, (_, idx) => [
from + BigInt(idx) * STEP + (idx ? BigInt(1) : BigInt(0)),
getMinBigInt(from + BigInt(idx + 1) * STEP, to),
])
: [[from, to]]

Expand Down
20 changes: 10 additions & 10 deletions packages/ckb-sdk-rpc/src/defaultRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class DefaultRPC {
* @param {string} number - the block number of the target block
* @returns {Promise<object>} block object
*/
public getBlockByNumber!: (number: CKBComponents.BlockNumber | number) => Promise<CKBComponents.Block>
public getBlockByNumber!: (number: CKBComponents.BlockNumber | bigint) => Promise<CKBComponents.Block>

/**
* @method getBlockByNumber
Expand All @@ -199,7 +199,7 @@ export class DefaultRPC {
* @param {string} hash - block hash
* @return {Promise<string>} block hash
*/
public getBlockHash!: (number: CKBComponents.BlockNumber | number) => Promise<CKBComponents.Hash>
public getBlockHash!: (number: CKBComponents.BlockNumber | bigint) => Promise<CKBComponents.Hash>

/**
* @method getTipHeader
Expand All @@ -221,8 +221,8 @@ export class DefaultRPC {
*/
public getCellsByLockHash!: (
hash: CKBComponents.Hash256,
from: CKBComponents.BlockNumber | number,
to: CKBComponents.BlockNumber | number
from: CKBComponents.BlockNumber | bigint,
to: CKBComponents.BlockNumber | bigint
) => Promise<CKBComponents.CellIncludingOutPoint[]>

/**
Expand Down Expand Up @@ -317,7 +317,7 @@ export class DefaultRPC {
* @description rpc to get the epoch info by its number
* @return {Promise<object>} epoch info
*/
public getEpochByNumber!: (epoch: string | number) => Promise<CKBComponents.Epoch>
public getEpochByNumber!: (epoch: string | bigint) => Promise<CKBComponents.Epoch>

/**
* @method dryRunTransaction
Expand Down Expand Up @@ -352,8 +352,8 @@ export class DefaultRPC {
*/
public getLiveCellsByLockHash!: (
lockHash: CKBComponents.Hash256,
pageNumber: string | number,
pageSize: string | number,
pageNumber: string | bigint,
pageSize: string | bigint,
reverseOrder?: boolean
) => Promise<CKBComponents.LiveCellsByLockHash>

Expand All @@ -378,8 +378,8 @@ export class DefaultRPC {
*/
public getTransactionsByLockHash!: (
lockHash: CKBComponents.Hash256,
pageNumber: string | number,
pageSize: string | number,
pageNumber: string | bigint,
pageSize: string | bigint,
reverseOrder?: boolean
) => Promise<CKBComponents.TransactionsByLockHash>

Expand Down Expand Up @@ -438,7 +438,7 @@ export class DefaultRPC {
* @description Returns the information about a block header by block number
* @params {string} block number
*/
public getHeaderByNumber!: (blockNumber: CKBComponents.BlockNumber | number) => Promise<CKBComponents.BlockHeader>
public getHeaderByNumber!: (blockNumber: CKBComponents.BlockNumber | bigint) => Promise<CKBComponents.BlockHeader>

/**
* @method getCellbaseOutputCapacityDetails
Expand Down

0 comments on commit 7eb6726

Please sign in to comment.