Skip to content

Commit

Permalink
Add node health check to transaction preflight
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Jun 2, 2020
1 parent 9dbf3d5 commit 9158479
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
21 changes: 20 additions & 1 deletion core/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
contact_info::ContactInfo,
non_circulating_supply::calculate_non_circulating_supply,
rpc_error::RpcCustomError,
rpc_health::*,
validator::ValidatorExit,
};
use bincode::serialize;
Expand Down Expand Up @@ -74,6 +75,7 @@ pub struct JsonRpcRequestProcessor {
blockstore: Arc<Blockstore>,
config: JsonRpcConfig,
validator_exit: Arc<RwLock<Option<ValidatorExit>>>,
health: Arc<RpcHealth>,
}

impl JsonRpcRequestProcessor {
Expand Down Expand Up @@ -128,13 +130,15 @@ impl JsonRpcRequestProcessor {
block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>,
blockstore: Arc<Blockstore>,
validator_exit: Arc<RwLock<Option<ValidatorExit>>>,
health: Arc<RpcHealth>,
) -> Self {
JsonRpcRequestProcessor {
config,
bank_forks,
block_commitment_cache,
blockstore,
validator_exit,
health,
}
}

Expand Down Expand Up @@ -1442,6 +1446,13 @@ impl RpcSol for RpcSolImpl {
.into());
}

if meta.request_processor.read().unwrap().health.check() != RpcHealthStatus::Ok {
return Err(RpcCustomError::SendTransactionPreflightFailure {
message: "RPC node is unhealthy, unable to simulate transaction".into(),
}
.into());
}

let bank = &*meta.request_processor.read().unwrap().bank(None)?;
if let Err(err) = run_transaction_simulation(&bank, &[transaction]) {
// Note: it's possible that the transaction simulation failed but the actual
Expand Down Expand Up @@ -1832,6 +1843,7 @@ pub mod tests {
block_commitment_cache.clone(),
blockstore,
validator_exit,
RpcHealth::stub(),
)));
let cluster_info = Arc::new(ClusterInfo::new_with_invalid_keypair(ContactInfo::default()));

Expand Down Expand Up @@ -1880,6 +1892,7 @@ pub mod tests {
block_commitment_cache,
blockstore,
validator_exit,
RpcHealth::stub(),
);
thread::spawn(move || {
let blockhash = bank.confirmed_last_blockhash().0;
Expand Down Expand Up @@ -2824,6 +2837,7 @@ pub mod tests {
block_commitment_cache,
blockstore,
validator_exit,
RpcHealth::stub(),
);
Arc::new(RwLock::new(request_processor))
},
Expand Down Expand Up @@ -2894,7 +2908,9 @@ pub mod tests {
)
}

pub fn create_validator_exit(exit: &Arc<AtomicBool>) -> Arc<RwLock<Option<ValidatorExit>>> {
pub(crate) fn create_validator_exit(
exit: &Arc<AtomicBool>,
) -> Arc<RwLock<Option<ValidatorExit>>> {
let mut validator_exit = ValidatorExit::default();
let exit_ = exit.clone();
validator_exit.register_exit(Box::new(move || exit_.store(true, Ordering::Relaxed)));
Expand All @@ -2916,6 +2932,7 @@ pub mod tests {
block_commitment_cache,
blockstore,
validator_exit,
RpcHealth::stub(),
);
assert_eq!(request_processor.validator_exit(), Ok(false));
assert_eq!(exit.load(Ordering::Relaxed), false);
Expand All @@ -2938,6 +2955,7 @@ pub mod tests {
block_commitment_cache,
blockstore,
validator_exit,
RpcHealth::stub(),
);
assert_eq!(request_processor.validator_exit(), Ok(true));
assert_eq!(exit.load(Ordering::Relaxed), true);
Expand Down Expand Up @@ -3020,6 +3038,7 @@ pub mod tests {
block_commitment_cache,
blockstore,
validator_exit,
RpcHealth::stub(),
);
assert_eq!(
request_processor.get_block_commitment(0),
Expand Down
13 changes: 13 additions & 0 deletions core/src/rpc_health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
sync::Arc,
};

#[derive(PartialEq)]
pub enum RpcHealthStatus {
Ok,
Behind, // Validator is behind its trusted validators
Expand Down Expand Up @@ -88,4 +89,16 @@ impl RpcHealth {
RpcHealthStatus::Ok
}
}

#[cfg(test)]
pub(crate) fn stub() -> Arc<Self> {
Arc::new(Self::new(
Arc::new(ClusterInfo::new_with_invalid_keypair(
crate::contact_info::ContactInfo::default(),
)),
None,
42,
Arc::new(AtomicBool::new(false)),
))
}
}
28 changes: 12 additions & 16 deletions core/src/rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ impl JsonRpcService {
block_commitment_cache,
blockstore,
validator_exit.clone(),
health.clone(),
)));

#[cfg(test)]
Expand Down Expand Up @@ -431,15 +432,12 @@ mod tests {
#[test]
fn test_is_file_get_path() {
let bank_forks = create_bank_forks();
let health = Arc::new(RpcHealth::new(
Arc::new(ClusterInfo::new_with_invalid_keypair(ContactInfo::default())),
let rrm = RpcRequestMiddleware::new(
PathBuf::from("/"),
None,
42,
Arc::new(AtomicBool::new(false)),
));

let rrm =
RpcRequestMiddleware::new(PathBuf::from("/"), None, bank_forks.clone(), health.clone());
bank_forks.clone(),
RpcHealth::stub(),
);
let rrm_with_snapshot_config = RpcRequestMiddleware::new(
PathBuf::from("/"),
Some(SnapshotConfig {
Expand All @@ -449,7 +447,7 @@ mod tests {
compression: CompressionType::Bzip2,
}),
bank_forks,
health,
RpcHealth::stub(),
);

assert!(rrm.is_file_get_path("/genesis.tar.bz2"));
Expand All @@ -475,14 +473,12 @@ mod tests {

#[test]
fn test_health_check_with_no_trusted_validators() {
let health = Arc::new(RpcHealth::new(
Arc::new(ClusterInfo::new_with_invalid_keypair(ContactInfo::default())),
let rm = RpcRequestMiddleware::new(
PathBuf::from("/"),
None,
42,
Arc::new(AtomicBool::new(false)),
));

let rm = RpcRequestMiddleware::new(PathBuf::from("/"), None, create_bank_forks(), health);
create_bank_forks(),
RpcHealth::stub(),
);
assert_eq!(rm.health_check(), "ok");
}

Expand Down

0 comments on commit 9158479

Please sign in to comment.