Add system_peers RPC endpoint (#577)
* Add system_peers RPC endpoint * Don't double-up on error logs
This commit is contained in:
@@ -209,8 +209,6 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
|
||||
.pipe(
|
||||
// if we find an error (invalid params, etc), reject the promise
|
||||
catchError((error) => {
|
||||
console.error(error);
|
||||
|
||||
if (!isCompleted) {
|
||||
isCompleted = true;
|
||||
|
||||
|
||||
@@ -38,6 +38,13 @@ const health: RpcMethodOpt = {
|
||||
type: 'Health'
|
||||
};
|
||||
|
||||
const peers: RpcMethodOpt = {
|
||||
description: 'Returns the currently connected peers',
|
||||
params: [],
|
||||
// @ts-ignore the Vec<> wrap is fine
|
||||
type: 'Vec<PeerInfo>'
|
||||
};
|
||||
|
||||
const section = 'system';
|
||||
|
||||
/**
|
||||
@@ -52,6 +59,7 @@ export default {
|
||||
chain: createMethod(section, 'chain', chain),
|
||||
health: createMethod(section, 'health', health),
|
||||
name: createMethod(section, 'name', name),
|
||||
peers: createMethod(section, 'peers', peers),
|
||||
properties: createMethod(section, 'properties', properties),
|
||||
version: createMethod(section, 'version', version)
|
||||
}
|
||||
|
||||
@@ -4,16 +4,21 @@
|
||||
|
||||
import Struct from './codec/Struct';
|
||||
import Bool from './Bool';
|
||||
import U32 from './U32';
|
||||
import USize from './USize';
|
||||
|
||||
const JSON_MAP = new Map([
|
||||
['isSyncing', 'is_syncing']
|
||||
]);
|
||||
|
||||
/**
|
||||
* @name Health
|
||||
* @description
|
||||
* A system health indicator, reported back over RPC
|
||||
*/
|
||||
export default class Health extends Struct {
|
||||
constructor (value?: any) {
|
||||
super({
|
||||
peers: U32,
|
||||
peers: USize,
|
||||
isSyncing: Bool
|
||||
}, value, JSON_MAP);
|
||||
}
|
||||
@@ -28,7 +33,7 @@ export default class Health extends Struct {
|
||||
/**
|
||||
* @description The numbers of peers as [[U32]]
|
||||
*/
|
||||
get peers (): U32 {
|
||||
return this.get('peers') as U32;
|
||||
get peers (): USize {
|
||||
return this.get('peers') as USize;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
// Copyright 2017-2019 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Struct from './codec/Struct';
|
||||
import BlockNumber from './BlockNumber';
|
||||
import Hash from './Hash';
|
||||
import Text from './Text';
|
||||
import U32 from './U32';
|
||||
import USize from './USize';
|
||||
|
||||
const JSON_MAP = new Map([
|
||||
['bestHash', 'best_hash'],
|
||||
['bestNumber', 'best_number'],
|
||||
['protocolVersion', 'protocol_version']
|
||||
]);
|
||||
|
||||
/**
|
||||
* @name PeerInfo
|
||||
* @description
|
||||
* A system peer info indicator, reported back over RPC
|
||||
*/
|
||||
export default class PeerInfo extends Struct {
|
||||
constructor (value?: any) {
|
||||
super({
|
||||
index: USize,
|
||||
peerId: Text,
|
||||
roles: Text,
|
||||
protocolVersion: U32,
|
||||
bestHash: Hash,
|
||||
bestNumber: BlockNumber
|
||||
}, value, JSON_MAP);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The best block hash for the peer
|
||||
*/
|
||||
get bestHash (): Hash {
|
||||
return this.get('bestHash') as Hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The best block hash for the peer
|
||||
*/
|
||||
get bestNumber (): BlockNumber {
|
||||
return this.get('bestNumber') as BlockNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The index of the peer in our list
|
||||
*/
|
||||
get index (): USize {
|
||||
return this.get('index') as USize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The p2p network id for the peer
|
||||
*/
|
||||
get peerId (): Text {
|
||||
return this.get('peerId') as Text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The index of the peer in our list
|
||||
*/
|
||||
get protocolVersion (): U32 {
|
||||
return this.get('protocolVersion') as U32;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The roles of the peer on the network
|
||||
*/
|
||||
get roles (): Text {
|
||||
return this.get('roles') as Text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2019 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import U64 from './U64';
|
||||
|
||||
/**
|
||||
* @name USize
|
||||
* @description
|
||||
* A System default unsigned number, typically used in RPC to report non-consensus
|
||||
* data. It is a wrapper for [[U64]] as a 64-binary default
|
||||
*/
|
||||
export default class USize extends U64 {
|
||||
}
|
||||
@@ -64,6 +64,7 @@ export { default as IndexCompact } from './NonceCompact';
|
||||
export { default as Justification } from './Justification';
|
||||
export { default as Origin } from './Origin';
|
||||
export { default as ParaId } from './ParaId';
|
||||
export { default as PeerInfo } from './PeerInfo';
|
||||
export { default as PendingExtrinsics } from './PendingExtrinsics';
|
||||
export { default as Permill } from './Permill';
|
||||
export { default as Perbill } from './Perbill';
|
||||
@@ -89,6 +90,7 @@ export { default as u32 } from './U32';
|
||||
export { default as u64 } from './U64';
|
||||
export { default as u128 } from './U128';
|
||||
export { default as u256 } from './U256';
|
||||
export { default as usize } from './USize';
|
||||
export { default as ValidatorPrefs } from './ValidatorPrefs';
|
||||
export { default as Vote } from './Vote';
|
||||
export { default as VoteThreshold } from './VoteThreshold';
|
||||
|
||||
Reference in New Issue
Block a user