Skip to content

Commit

Permalink
evm: Add net_peerCount RPC (#2569)
Browse files Browse the repository at this point in the history
* Add net_peerCount RPC

* Add new line to end of file
  • Loading branch information
shohamc1 authored Oct 11, 2023
1 parent 1815ada commit aeaa1cd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ain-cpp-imports/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ pub mod ffi {
fn getClientVersion() -> String;
fn getNumCores() -> i32;
fn getCORSAllowedOrigin() -> String;
fn getNumConnections() -> i32;
}
}
7 changes: 7 additions & 0 deletions lib/ain-cpp-imports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ mod ffi {
pub fn getCORSAllowedOrigin() -> String {
unimplemented!("{}", UNIMPL_MSG)
}
pub fn getNumConnections() -> i32 {
unimplemented!("{}", UNIMPL_MSG)
}
}

pub use ffi::Attributes;
Expand Down Expand Up @@ -204,5 +207,9 @@ pub fn get_cors_allowed_origin() -> String {
ffi::getCORSAllowedOrigin()
}

pub fn get_num_connections() -> i32 {
ffi::getNumConnections()
}

#[cfg(test)]
mod tests {}
9 changes: 9 additions & 0 deletions lib/ain-grpc/src/rpc/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub trait MetachainNetRPC {
/// Returns the current network ID as a string.
#[method(name = "version")]
fn net_version(&self) -> RpcResult<String>;

#[method(name = "peerCount")]
fn peer_count(&self) -> RpcResult<String>;
}

pub struct MetachainNetRPCModule {
Expand All @@ -31,4 +34,10 @@ impl MetachainNetRPCServer for MetachainNetRPCModule {

Ok(format!("{chain_id}"))
}

fn peer_count(&self) -> RpcResult<String> {
let peer_count = ain_cpp_imports::get_num_connections();

Ok(format!("{:#x}", peer_count))
}
}
5 changes: 5 additions & 0 deletions src/ffi/ffiexports.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ffi/ffiexports.h>
#include <util/system.h>
#include <net.h>
#include <dfi/mn_rpc.h>
#include <key_io.h>
#include <logging.h>
Expand Down Expand Up @@ -295,3 +296,7 @@ int32_t getNumCores() {
rust::string getCORSAllowedOrigin() {
return gArgs.GetArg("-rpcallowcors", "");
}

int32_t getNumConnections() {
return (int32_t)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL);
}
1 change: 1 addition & 0 deletions src/ffi/ffiexports.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ rust::vec<DST20Token> getDST20Tokens(std::size_t mnview_ptr);
rust::string getClientVersion();
int32_t getNumCores();
rust::string getCORSAllowedOrigin();
int32_t getNumConnections();

#endif // DEFI_FFI_FFIEXPORTS_H

0 comments on commit aeaa1cd

Please sign in to comment.