Skip to content

Commit

Permalink
feat(rpc): enable batch request (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Jul 2, 2020
1 parent 9492f6d commit 9517cae
Show file tree
Hide file tree
Showing 12 changed files with 541 additions and 42 deletions.
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,55 @@ After that you can use the `ckb` object to generate addresses, send requests, et

# RPC

## Default RPC
## Basic RPC

Please see [Default RPC](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/packages/ckb-sdk-rpc/src/defaultRPC.ts#L188)
Please see [Basic RPC](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/packages/ckb-sdk-rpc/src/Base.ts#L161)

## Batch Request

```javascript
/**
* The following batch includes two requests
* 1. getBlock('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
* 2. getTransactionsByLockHash('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', '0x0', '0x1)
*/
const batch = rpc.createBatchRequest([
['getBlock', '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'],
['getTransactionsByLockHash', '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', '0x0', '0x1'],
])

/**
* Add a request and the batch should include three requests
* 1. getBlock('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
* 2. getTransactionsByLockHash('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', '0x0', '0x1)
* 3. getTransactionsByLockHash('0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc', '0x0', '0x1)
*/
batch.add(
'getTransactionsByLockHash',
'0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
'0x0',
'0x1',
)

/**
* Remove a request by index and the batch should include two requests
* 1. getBlock('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
* 2. getTransactionsByLockHash('0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc', '0x0', '0x1)
*/
batch.remove(1)

/**
* Send the batch request
*/
batch.exec().then(console.log)
// [
// { "header": { }, "uncles": [], "transactions": [], "proposals": [] },
// [ { "consumedBy": { }, "createdBy": { } } ]
// ]

// chaning usage
batch.add().remove().exec()
```

## Persistent Connection

Expand Down
15 changes: 7 additions & 8 deletions codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@ codecov:
coverage:
precision: 2
round: down
range: '70...100'
range: '90...100'

status:
project:
default:
target: auto
threshold: 20
threshold: 50
base: auto
patch:
default:
target: auto
threshold: 20
base: auto
patch: off
changes: yes


parsers:
gcov:
branch_detection:
Expand All @@ -32,3 +27,7 @@ comment:
layout: 'reach,diff,flags,tree'
behavior: default
require_changes: no

ignore:
# Tested but not recognized by codecov
- 'packages/ckb-sdk-rpc/src/index.ts'
45 changes: 45 additions & 0 deletions packages/ckb-sdk-core/__tests__/signWitnessGroup/fixtures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"empty witnesses": {
"privateKey": "0xdcec27d0d975b0378471183a03f7071dea8532aaf968be796719ecd20af6988f",
"transactionHash": "0x4a4bcfef1b7448e27edf533df2f1de9f56be05eba645fb83f42d55816797ad2a",
"witnesses": [],
"exception": "WitnessGroup cannot be empty"
},
"first one is witness args": {
"privateKey": "0xdcec27d0d975b0378471183a03f7071dea8532aaf968be796719ecd20af6988f",
"transactionHash": "0x4a4bcfef1b7448e27edf533df2f1de9f56be05eba645fb83f42d55816797ad2a",
"witnesses": [
"0x",
{
"lock": "",
"inputType": "",
"outputType": ""
}
],
"exception": "The first witness in the group should be type of WitnessArgs"
},
"Should pass": {
"privateKey": "0xdcec27d0d975b0378471183a03f7071dea8532aaf968be796719ecd20af6988f",
"transactionHash": "0x4a4bcfef1b7448e27edf533df2f1de9f56be05eba645fb83f42d55816797ad2a",
"witnesses": [
{
"lock": "",
"inputType": "",
"outputType": ""
},
{
"lock": "",
"inputType": "",
"outputType": ""
}
],
"expected": [
"0x55000000100000005500000055000000410000005bc073bf55db333d5680ddf36e4814b9ce2118cfe4504f95c7d3e9a7548e16886cf1a1481fd80ce70d5e19108a43fd17fa32aad0d46c30c3410001ed2934ad2f00",
{
"lock": "",
"inputType": "",
"outputType": ""
}
]
}
}
25 changes: 25 additions & 0 deletions packages/ckb-sdk-core/__tests__/signWitnessGroup/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const signWitnessGroup = require('../../lib/signWitnessGroup').default
const fixtures = require('./fixtures.json')

describe('test sign witness group', () => {
const fixtureTable = Object.entries(
fixtures,
).map(([title, { privateKey, transactionHash, witnesses, expected, exception }]) => [
title,
privateKey,
transactionHash,
witnesses,
exception,
expected,
])

test.each(fixtureTable)('%s', (_title, privateKey, transactionHash, witnesses, exception, expected) => {
expect.assertions(1)
if (exception !== undefined) {
expect(() => signWitnessGroup(privateKey, transactionHash, witnesses)).toThrowError(exception)
} else if (privateKey !== undefined) {
const signedWitnessGroup = signWitnessGroup(privateKey, transactionHash, witnesses)
expect(signedWitnessGroup).toEqual(expected)
}
})
})
4 changes: 2 additions & 2 deletions packages/ckb-sdk-rpc/__tests__/ckb-rpc-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe('ckb-rpc settings and helpers', () => {
expect(rpc.node.httpsAgent).toBeDefined()
})

it('has 31 default rpc', () => {
expect(rpc.methods.length).toBe(31)
it('has 31 basic rpc', () => {
expect(Object.values(rpc)).toHaveLength(31)
})

it('set node url to http://test.localhost:8114', () => {
Expand Down

0 comments on commit 9517cae

Please sign in to comment.