Skip to content

Commit

Permalink
Merge pull request ProvableHQ#461 from AleoHQ/feature/getnodeinfo-syn…
Browse files Browse the repository at this point in the history
…cing

Add `is_syncing` flag to `getnodeinfo`
  • Loading branch information
howardwu authored Sep 19, 2020
2 parents 183f4bf + 1c94cc0 commit 3bfa2ac
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
7 changes: 4 additions & 3 deletions rpc/documentation/public_endpoints/getnodeinfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ None

### Response

| Parameter | Type | Description |
|:----------:|:----:|:--------------------------------------:|
| `is_miner` | bool | Flag indicating if the node is a miner |
| Parameter | Type | Description |
|:------------:|:----:|:---------------------------------------------:|
| `is_miner` | bool | Flag indicating if the node is a miner |
| `is_snycing` | bool | Flag indicating if the node currently syncing |

### Example
```ignore
Expand Down
17 changes: 16 additions & 1 deletion rpc/src/rpc_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use snarkos_dpc::base_dpc::{
};
use snarkos_errors::rpc::RpcError;
use snarkos_models::objects::Transaction;
use snarkos_network::internal::{context::Context, process_transaction_internal};
use snarkos_network::{
external::SyncHandler,
internal::{context::Context, process_transaction_internal},
};
use snarkos_objects::BlockHeaderHash;
use snarkos_utilities::{
bytes::{FromBytes, ToBytes},
Expand Down Expand Up @@ -60,6 +63,9 @@ pub struct RpcImpl {
/// Handle to access the memory pool of transactions.
pub(crate) memory_pool_lock: Arc<Mutex<MemoryPool<Tx>>>,

/// Handle to access the sync state of the node
pub(crate) sync_handler_lock: Arc<Mutex<SyncHandler>>,

/// RPC credentials for accessing guarded endpoints
pub(crate) credentials: Option<RpcCredentials>,
}
Expand All @@ -73,6 +79,7 @@ impl RpcImpl {
server_context: Arc<Context>,
consensus: ConsensusParameters,
memory_pool_lock: Arc<Mutex<MemoryPool<Tx>>>,
sync_handler_lock: Arc<Mutex<SyncHandler>>,
credentials: Option<RpcCredentials>,
) -> Self {
Self {
Expand All @@ -82,6 +89,7 @@ impl RpcImpl {
server_context,
consensus,
memory_pool_lock,
sync_handler_lock,
credentials,
}
}
Expand Down Expand Up @@ -307,8 +315,15 @@ impl RpcFunctions for RpcImpl {

/// Returns data about the node.
fn get_node_info(&self) -> Result<NodeInfo, RpcError> {
let mut is_syncing = false;

if let Ok(sync_handler) = self.sync_handler_lock.try_lock() {
is_syncing = sync_handler.is_syncing();
}

Ok(NodeInfo {
is_miner: self.server_context.is_miner,
is_syncing,
})
}

Expand Down
4 changes: 3 additions & 1 deletion rpc/src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use snarkos_dpc::base_dpc::{
instantiated::{Components, Tx},
parameters::PublicParameters,
};
use snarkos_network::internal::context::Context;
use snarkos_network::{external::SyncHandler, internal::context::Context};

use jsonrpc_http_server::{cors::AccessControlAllowHeaders, hyper, ServerBuilder};
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
Expand All @@ -43,6 +43,7 @@ pub async fn start_rpc_server(
server_context: Arc<Context>,
consensus: ConsensusParameters,
memory_pool_lock: Arc<Mutex<MemoryPool<Tx>>>,
sync_handler_lock: Arc<Mutex<SyncHandler>>,
username: Option<String>,
password: Option<String>,
) -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -60,6 +61,7 @@ pub async fn start_rpc_server(
server_context,
consensus,
memory_pool_lock,
sync_handler_lock,
credentials,
);
let mut io = jsonrpc_core::MetaIoHandler::default();
Expand Down
3 changes: 3 additions & 0 deletions rpc/src/rpc_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ pub struct DecryptRecordInput {
pub struct NodeInfo {
/// Flag indicating if the node is operating as a miner
pub is_miner: bool,

/// Flag indicating if the node is currently syncing
pub is_syncing: bool,
}

/// Returned value for the `getpeerinfo` rpc call
Expand Down
6 changes: 5 additions & 1 deletion rpc/tests/protected_rpc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod protected_rpc_tests {
record::DPCRecord,
};
use snarkos_models::dpc::Record;
use snarkos_network::internal::context::Context;
use snarkos_network::{external::SyncHandler, internal::context::Context};
use snarkos_objects::{AccountAddress, AccountPrivateKey, AccountViewKey};
use snarkos_rpc::*;
use snarkos_testing::{consensus::*, dpc::load_verifying_parameters, network::*, storage::*};
Expand Down Expand Up @@ -77,6 +77,9 @@ mod protected_rpc_tests {
let memory_pool = MemoryPool::new();
let memory_pool_lock = Arc::new(Mutex::new(memory_pool));

let sync_handler = SyncHandler::new(server_address.clone());
let sync_handler_lock = Arc::new(Mutex::new(sync_handler));

let context = Context::new(server_address, 5, 1, 10, true, vec![], false);

let storage = storage.clone();
Expand All @@ -89,6 +92,7 @@ mod protected_rpc_tests {
Arc::new(context),
consensus,
memory_pool_lock,
sync_handler_lock,
Some(credentials),
);
let mut io = jsonrpc_core::MetaIoHandler::default();
Expand Down
2 changes: 2 additions & 0 deletions rpc/tests/rpc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mod rpc_tests {
server.context.clone(),
consensus,
server.memory_pool_lock,
server.sync_handler_lock,
None,
)
.to_delegate(),
Expand Down Expand Up @@ -342,6 +343,7 @@ mod rpc_tests {
let peer_info: NodeInfo = serde_json::from_value(result).unwrap();

assert_eq!(peer_info.is_miner, false);
assert_eq!(peer_info.is_syncing, false);

drop(rpc);
kill_storage_sync(storage);
Expand Down
1 change: 1 addition & 0 deletions snarkos/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ async fn start_server(config: Config) -> Result<(), NodeError> {
server.context.clone(),
consensus.clone(),
memory_pool_lock.clone(),
sync_handler_lock.clone(),
config.rpc.username,
config.rpc.password,
)
Expand Down

0 comments on commit 3bfa2ac

Please sign in to comment.