Skip to content

Commit

Permalink
feat(rpc): add new rpc of set ban
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Jul 31, 2019
1 parent 323b8ac commit 416e7fd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 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 22 default rpc', () => {
expect(rpc.methods.length).toBe(22)
it('has 24 default rpc', () => {
expect(rpc.methods.length).toBe(24)
})

it('set node url to http://test.localhost:8114', () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/ckb-sdk-rpc/__tests__/ckb-rpc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ describe('ckb-rpc success', () => {
it('get transactions by lock hash', async () => {})

it('index lock hash', async () => {})

it('get banned addresses', async () => {
const addresses = await rpc.getBannedAddresses()
expect(Array.isArray(addresses)).toBe(true)
})

it('set address to be banned', async () => {
expect.assertions(1)
await expect(rpc.setBan('192.168.0.2', 'insert', null)).resolves.toEqual(null)
})

it('get banned address', async () => {
const addresses = await rpc.getBannedAddresses()
expect(Array.isArray(addresses)).toBe(true)
})
})

describe('send transaction', () => {
Expand Down
26 changes: 26 additions & 0 deletions packages/ckb-sdk-rpc/src/defaultRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ const defaultRPC: CKBComponents.Method[] = [
paramsFormatters: [],
resultFormatters: resultFmts.toBannedAddresses,
},
{
name: 'setBan',
method: 'set_ban',
paramsFormatters: [],
},
]

export class DefaultRPC {
Expand Down Expand Up @@ -372,6 +377,27 @@ export class DefaultRPC {
) => Promise<CKBComponents.LockHashIndexState>

public getBannedAddresses!: () => Promise<CKBComponents.BannedAddresses>
/**
* @method setBan
* @memberof DefaultRPC
* @description insert or delete an IP/Subnet from the banned list
* @param {string} address, The IP/Subnet with an optional netmask (default is /32 = single IP)
* @param {insert|delete} command, `insert` to insert an IP/Subnet to the list, `delete` to delete an IP/Subnet
* from the list
* @param {string|null} ban_time, Time in milliseconds how long (or until when if [absolute] is set) the IP is banned,
* optional parameter, null means using the default time of 24h
* @param {[boolean]} absolute, If set, the `ban_time` must be an absolute timestamp in milliseconds since epoch,
* optional parameter
* @param {[string]} reason, Ban reason, optional parameter
*/

public setBan!: (
address: string,
command: 'insert' | 'delete',
banTime: string | null,
absolute?: boolean,
reason?: string
) => Promise<null>
}

export default DefaultRPC

0 comments on commit 416e7fd

Please sign in to comment.