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

enha: add endpoint to check node mode #1637

Merged
merged 11 commits into from
Aug 13, 2024
14 changes: 14 additions & 0 deletions e2e/cloudwalk-contracts/integration/test/leader-follower.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ import {
} from "./helpers/rpc";

describe("Leader & Follower integration test", function () {
it("Validate node modes for leader and follower", async function () {
// Check Stratus Leader node mode
updateProviderUrl("stratus");
const leaderMode = await sendWithRetry("stratus_mode", []);
expect(leaderMode.mode).to.equal("leader");

// Check Stratus Follower node mode
updateProviderUrl("stratus-follower");
const followerMode = await sendWithRetry("stratus_mode", []);
expect(followerMode.mode).to.equal("follower");

updateProviderUrl("stratus");
});

before(async function () {
await setDeployer();
});
Expand Down
10 changes: 10 additions & 0 deletions src/eth/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use crate::eth::storage::StratusStorage;
use crate::ext::not;
use crate::ext::to_json_string;
use crate::ext::to_json_value;
use crate::globals::ModeString;
use crate::infra::build_info;
use crate::infra::metrics;
use crate::infra::tracing::SpanExt;
Expand Down Expand Up @@ -168,6 +169,7 @@ fn register_methods(mut module: RpcModule<RpcContext>) -> anyhow::Result<RpcModu
// stratus state
module.register_method("stratus_version", stratus_version)?;
module.register_method("stratus_config", stratus_config)?;
module.register_method("stratus_mode", stratus_mode)?;

module.register_async_method("stratus_getSubscriptions", stratus_get_subscriptions)?;

Expand Down Expand Up @@ -313,6 +315,14 @@ fn stratus_config(_: Params<'_>, ctx: &RpcContext, _: &Extensions) -> Result<Jso
Ok(ctx.app_config.clone())
}

fn stratus_mode(_: Params<'_>, _: &RpcContext, _: &Extensions) -> Result<JsonValue, StratusError> {
let mode = GlobalState::get_node_mode().as_str();

Ok(json!({
"mode": mode
}))
}

async fn stratus_get_subscriptions(_: Params<'_>, ctx: Arc<RpcContext>, ext: Extensions) -> Result<JsonValue, StratusError> {
reject_unknown_client(ext.rpc_client())?;

Expand Down
13 changes: 13 additions & 0 deletions src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ pub enum NodeMode {
Follower,
}

pub trait ModeString {
gabriel-aranha-cw marked this conversation as resolved.
Show resolved Hide resolved
fn as_str(&self) -> &'static str;
}

impl ModeString for NodeMode {
fn as_str(&self) -> &'static str {
match self {
NodeMode::Leader => "leader",
NodeMode::Follower => "follower",
}
}
}

// -----------------------------------------------------------------------------
// Global state
// -----------------------------------------------------------------------------
Expand Down