Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evm: Add net_peerCount RPC #2569

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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