Skip to content

Commit

Permalink
feat(rpc): add get_header rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Jul 31, 2019
1 parent 416e7fd commit d2a6bbb
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 82 deletions.
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 @@ -40,8 +40,8 @@ describe('ckb-rpc settings and helpers', () => {
expect(rpc.node).toEqual(node)
})

it('has 24 default rpc', () => {
expect(rpc.methods.length).toBe(24)
it('has 25 default rpc', () => {
expect(rpc.methods.length).toBe(25)
})

it('set node url to http://test.localhost:8114', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/ckb-sdk-rpc/__tests__/ckb-rpc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ describe('ckb-rpc success', () => {
const addresses = await rpc.getBannedAddresses()
expect(Array.isArray(addresses)).toBe(true)
})

it('get header', async () => {
const zeroBlock = await rpc.getBlockByNumber('0')
const zeroBlockHeader = zeroBlock.header
const zeroBlockHash = zeroBlockHeader.hash
const header = await rpc.getHeader(zeroBlockHash)
expect(header).toEqual(zeroBlockHeader)
})
})

describe('send transaction', () => {
Expand Down
114 changes: 60 additions & 54 deletions packages/ckb-sdk-rpc/__tests__/formatters/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,43 +248,45 @@
{
"source": {
"header": {
"difficulty": "0x100",
"hash": "0xe5f2d07b747f38718293987a9175764d4c03229a8a69d0bd48f7bea6c18930b1",
"number": "2",
"dao": "0x010000000000000063ae2493990ab00000f0c890408900000061eb7ada030000",
"difficulty": "0x3e8",
"epoch": "0",
"parent_hash": "0xa1dba96cdebaa25f2be69316c943269e23c23fcc6e90eedd94ecd88b43f77170",
"hash": "0x0a663a245ae0b3545a24e4ec06066b9e9f0bd32e5fdbb9038ec15af59ef2593d",
"number": "1024",
"parent_hash": "0x5eae6dfdac4b3099619a131975fd0be6da3c099eeaae8261182627a53548fc87",
"proposals_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"seal": {
"nonce": "9828930232171500901",
"proof": "0x7408000066230000942c00004735000028420000f046000059550000d56200005f640000f96a000043730000d3750000"
"nonce": "0",
"proof": "0x"
},
"timestamp": "1556199760706",
"transactions_root": "0x8027376875e45318ed469bed6314408e8f1986de246cbd03a786b69073c948b8",
"timestamp": "1557311767",
"transactions_root": "0x64a872caca3c7b671b64897230e88dec8800b594347d192661908a87f7fbcb9b",
"uncles_count": "0",
"uncles_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"version": "0",
"witnesses_root": "0x0000000000000000000000000000000000000000000000000000000000000000"
"witnesses_root": "0x688c1ff7df5b9b454bd76a031d40e13b4e462aeee5f8497d0f9f6f1dbd5d8111"
},
"proposals": []
},
"target": {
"header": {
"difficulty": "0x100",
"hash": "0xe5f2d07b747f38718293987a9175764d4c03229a8a69d0bd48f7bea6c18930b1",
"number": "2",
"dao": "0x010000000000000063ae2493990ab00000f0c890408900000061eb7ada030000",
"difficulty": "0x3e8",
"epoch": "0",
"parentHash": "0xa1dba96cdebaa25f2be69316c943269e23c23fcc6e90eedd94ecd88b43f77170",
"hash": "0x0a663a245ae0b3545a24e4ec06066b9e9f0bd32e5fdbb9038ec15af59ef2593d",
"number": "1024",
"parentHash": "0x5eae6dfdac4b3099619a131975fd0be6da3c099eeaae8261182627a53548fc87",
"proposalsHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"seal": {
"nonce": "9828930232171500901",
"proof": "0x7408000066230000942c00004735000028420000f046000059550000d56200005f640000f96a000043730000d3750000"
"nonce": "0",
"proof": "0x"
},
"timestamp": "1556199760706",
"transactionsRoot": "0x8027376875e45318ed469bed6314408e8f1986de246cbd03a786b69073c948b8",
"timestamp": "1557311767",
"transactionsRoot": "0x64a872caca3c7b671b64897230e88dec8800b594347d192661908a87f7fbcb9b",
"unclesCount": "0",
"unclesHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"version": "0",
"witnessesRoot": "0x0000000000000000000000000000000000000000000000000000000000000000"
"witnessesRoot": "0x688c1ff7df5b9b454bd76a031d40e13b4e462aeee5f8497d0f9f6f1dbd5d8111"
},
"proposals": []
}
Expand All @@ -294,22 +296,23 @@
{
"source": {
"header": {
"difficulty": "0x100",
"hash": "0xe5f2d07b747f38718293987a9175764d4c03229a8a69d0bd48f7bea6c18930b1",
"number": "2",
"dao": "0x010000000000000063ae2493990ab00000f0c890408900000061eb7ada030000",
"difficulty": "0x3e8",
"epoch": "0",
"parent_hash": "0xa1dba96cdebaa25f2be69316c943269e23c23fcc6e90eedd94ecd88b43f77170",
"hash": "0x0a663a245ae0b3545a24e4ec06066b9e9f0bd32e5fdbb9038ec15af59ef2593d",
"number": "1024",
"parent_hash": "0x5eae6dfdac4b3099619a131975fd0be6da3c099eeaae8261182627a53548fc87",
"proposals_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"seal": {
"nonce": "9828930232171500901",
"proof": "0x7408000066230000942c00004735000028420000f046000059550000d56200005f640000f96a000043730000d3750000"
"nonce": "0",
"proof": "0x"
},
"timestamp": "1556199760706",
"transactions_root": "0x8027376875e45318ed469bed6314408e8f1986de246cbd03a786b69073c948b8",
"timestamp": "1557311767",
"transactions_root": "0x64a872caca3c7b671b64897230e88dec8800b594347d192661908a87f7fbcb9b",
"uncles_count": "0",
"uncles_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"version": "0",
"witnesses_root": "0x0000000000000000000000000000000000000000000000000000000000000000"
"witnesses_root": "0x688c1ff7df5b9b454bd76a031d40e13b4e462aeee5f8497d0f9f6f1dbd5d8111"
},
"proposals": [],
"transactions": [
Expand Down Expand Up @@ -348,22 +351,23 @@
},
"target": {
"header": {
"difficulty": "0x100",
"hash": "0xe5f2d07b747f38718293987a9175764d4c03229a8a69d0bd48f7bea6c18930b1",
"number": "2",
"dao": "0x010000000000000063ae2493990ab00000f0c890408900000061eb7ada030000",
"difficulty": "0x3e8",
"epoch": "0",
"parentHash": "0xa1dba96cdebaa25f2be69316c943269e23c23fcc6e90eedd94ecd88b43f77170",
"hash": "0x0a663a245ae0b3545a24e4ec06066b9e9f0bd32e5fdbb9038ec15af59ef2593d",
"number": "1024",
"parentHash": "0x5eae6dfdac4b3099619a131975fd0be6da3c099eeaae8261182627a53548fc87",
"proposalsHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"seal": {
"nonce": "9828930232171500901",
"proof": "0x7408000066230000942c00004735000028420000f046000059550000d56200005f640000f96a000043730000d3750000"
"nonce": "0",
"proof": "0x"
},
"timestamp": "1556199760706",
"transactionsRoot": "0x8027376875e45318ed469bed6314408e8f1986de246cbd03a786b69073c948b8",
"timestamp": "1557311767",
"transactionsRoot": "0x64a872caca3c7b671b64897230e88dec8800b594347d192661908a87f7fbcb9b",
"unclesCount": "0",
"unclesHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"version": "0",
"witnessesRoot": "0x0000000000000000000000000000000000000000000000000000000000000000"
"witnessesRoot": "0x688c1ff7df5b9b454bd76a031d40e13b4e462aeee5f8497d0f9f6f1dbd5d8111"
},
"proposals": [],
"transactions": [
Expand Down Expand Up @@ -404,43 +408,45 @@
{
"source": {
"header": {
"difficulty": "0x100",
"hash": "0xe5f2d07b747f38718293987a9175764d4c03229a8a69d0bd48f7bea6c18930b1",
"number": "2",
"dao": "0x010000000000000063ae2493990ab00000f0c890408900000061eb7ada030000",
"difficulty": "0x3e8",
"epoch": "0",
"parent_hash": "0xa1dba96cdebaa25f2be69316c943269e23c23fcc6e90eedd94ecd88b43f77170",
"hash": "0x0a663a245ae0b3545a24e4ec06066b9e9f0bd32e5fdbb9038ec15af59ef2593d",
"number": "1024",
"parent_hash": "0x5eae6dfdac4b3099619a131975fd0be6da3c099eeaae8261182627a53548fc87",
"proposals_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"seal": {
"nonce": "9828930232171500901",
"proof": "0x7408000066230000942c00004735000028420000f046000059550000d56200005f640000f96a000043730000d3750000"
"nonce": "0",
"proof": "0x"
},
"timestamp": "1556199760706",
"transactions_root": "0x8027376875e45318ed469bed6314408e8f1986de246cbd03a786b69073c948b8",
"timestamp": "1557311767",
"transactions_root": "0x64a872caca3c7b671b64897230e88dec8800b594347d192661908a87f7fbcb9b",
"uncles_count": "0",
"uncles_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"version": "0",
"witnesses_root": "0x0000000000000000000000000000000000000000000000000000000000000000"
"witnesses_root": "0x688c1ff7df5b9b454bd76a031d40e13b4e462aeee5f8497d0f9f6f1dbd5d8111"
},
"proposals": []
},
"target": {
"header": {
"difficulty": "0x100",
"hash": "0xe5f2d07b747f38718293987a9175764d4c03229a8a69d0bd48f7bea6c18930b1",
"number": "2",
"dao": "0x010000000000000063ae2493990ab00000f0c890408900000061eb7ada030000",
"difficulty": "0x3e8",
"epoch": "0",
"parentHash": "0xa1dba96cdebaa25f2be69316c943269e23c23fcc6e90eedd94ecd88b43f77170",
"hash": "0x0a663a245ae0b3545a24e4ec06066b9e9f0bd32e5fdbb9038ec15af59ef2593d",
"number": "1024",
"parentHash": "0x5eae6dfdac4b3099619a131975fd0be6da3c099eeaae8261182627a53548fc87",
"proposalsHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"seal": {
"nonce": "9828930232171500901",
"proof": "0x7408000066230000942c00004735000028420000f046000059550000d56200005f640000f96a000043730000d3750000"
"nonce": "0",
"proof": "0x"
},
"timestamp": "1556199760706",
"transactionsRoot": "0x8027376875e45318ed469bed6314408e8f1986de246cbd03a786b69073c948b8",
"timestamp": "1557311767",
"transactionsRoot": "0x64a872caca3c7b671b64897230e88dec8800b594347d192661908a87f7fbcb9b",
"unclesCount": "0",
"unclesHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"version": "0",
"witnessesRoot": "0x0000000000000000000000000000000000000000000000000000000000000000"
"witnessesRoot": "0x688c1ff7df5b9b454bd76a031d40e13b4e462aeee5f8497d0f9f6f1dbd5d8111"
},
"proposals": [],
"transactions": [],
Expand Down
8 changes: 8 additions & 0 deletions packages/ckb-sdk-rpc/src/defaultRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ const defaultRPC: CKBComponents.Method[] = [
method: 'set_ban',
paramsFormatters: [],
},
{
name: 'getHeader',
method: 'get_header',
paramsFormatters: [paramsFmts.toHash],
resultFormatters: resultFmts.toHeader,
},
]

export class DefaultRPC {
Expand Down Expand Up @@ -398,6 +404,8 @@ export class DefaultRPC {
absolute?: boolean,
reason?: string
) => Promise<null>

public getHeader!: (hash: CKBComponents.Hash) => Promise<CKBComponents.BlockHeader>
}

export default DefaultRPC
19 changes: 10 additions & 9 deletions packages/ckb-sdk-rpc/types/CKB_RPC/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,20 @@ declare module CKB_RPC {
export type Seal = CKBComponents.Seal

export interface Header {
version: Version
dao: DAO
difficulty: Difficulty
epoch: EpochInHeader
hash: Hash256
number: BlockNumber
parent_hash: Hash256
proposals_hash: Hash256
seal: Seal
timestamp: Timestamp
number: BlockNumber
epoch: EpochInHeader
transactions_root: Hash256
proposals_hash: Hash256
witnesses_root: Hash256
difficulty: Difficulty
uncles_hash: Hash256
uncles_count: Count
seal: Seal
hash: Hash256
uncles_hash: Hash256
witnesses_root: Hash256
version: Version
}

export interface UncleBlock {
Expand Down
38 changes: 21 additions & 17 deletions packages/ckb-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

declare namespace CKBComponents {
export type DAO = string
export type Hash = string
export type Hash256 = string
export type UInt32 = number
Expand Down Expand Up @@ -178,33 +179,36 @@ declare namespace CKBComponents {

/**
* @typedef BlockHeader, header of a block
* @property version
* @property dao
* @property difficulty
* @property epoch
* @property hash
* @property number
* @property parentHash
* @property proposalsHash
* @property seal
* @property timestamp
* @property number
* @property epoch
* @property transactionsRoot
* @property proposalsHash
* @property difficulty
* @property unclesHash
* @property unclesCount
* @property seal
* @property hash
* @property unclesHash
* @property witnessesRoot
* @property version
*/
export interface BlockHeader {
version: Version
dao: DAO
difficulty: Difficulty
epoch: EpochInHeader
hash: Hash256
number: BlockNumber
parentHash: Hash256
proposalsHash: Hash256
seal: Seal
timestamp: Timestamp
number: BlockNumber
epoch: EpochInHeader
transactionsRoot: Hash256
proposalsHash: Hash256
witnessesRoot: Hash256
difficulty: Difficulty
unclesHash: Hash256
unclesCount: Count
seal: Seal
hash: Hash256
unclesHash: Hash256
witnessesRoot: Hash256
version: Version
}

/**
Expand Down

0 comments on commit d2a6bbb

Please sign in to comment.