From 5d998535026bae70f26a80ec56a2af52a40f3f53 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 14 Oct 2019 16:24:10 -0600 Subject: [PATCH] Add getBlockConfidence rpc endpoint (#6350) automerge --- book/src/api-reference/jsonrpc-api.md | 30 ++- core/src/confidence.rs | 16 +- core/src/rpc.rs | 302 +++++++++++++++++++++----- core/src/rpc_service.rs | 31 +-- core/src/validator.rs | 3 +- 5 files changed, 303 insertions(+), 79 deletions(-) diff --git a/book/src/api-reference/jsonrpc-api.md b/book/src/api-reference/jsonrpc-api.md index 932c6c6d4c..447ac76bcf 100644 --- a/book/src/api-reference/jsonrpc-api.md +++ b/book/src/api-reference/jsonrpc-api.md @@ -17,6 +17,7 @@ To interact with a Solana node inside a JavaScript application, use the [solana- * [confirmTransaction](jsonrpc-api.md#confirmtransaction) * [getAccountInfo](jsonrpc-api.md#getaccountinfo) * [getBalance](jsonrpc-api.md#getbalance) +* [getBlockConfidence](jsonrpc-api.md#getblockconfidence) * [getClusterNodes](jsonrpc-api.md#getclusternodes) * [getEpochInfo](jsonrpc-api.md#getepochinfo) * [getGenesisBlockhash](jsonrpc-api.md#getgenesisblockhash) @@ -149,6 +150,34 @@ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0", "id":1, " {"jsonrpc":"2.0","result":0,"id":1} ``` +### getBlockConfidence + +Returns confidence for particular block + +#### Parameters: + +* `u64` - block, identified by Slot + +#### Results: + +The result field will be an array with two fields: + +* Confidence + * `null` - Unknown block + * `object` - BankConfidence + * `array` - confidence, array of u64 integers logging the amount of cluster stake in lamports that has voted on the block at each depth from 0 to `MAX_LOCKOUT_HISTORY` +* 'integer' - total active stake, in lamports, of the current epoch + +#### Example: + +```bash +// Request +curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "method":"getBlockConfidence","params":[5]}' http://localhost:8899 + +// Result +{"jsonrpc":"2.0","result":[{"confidence":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,32]},42],"id":1} +``` + ### getClusterNodes Returns information about all the nodes participating in the cluster @@ -808,4 +837,3 @@ Unsubscribe from signature confirmation notification // Result {"jsonrpc": "2.0","result": true,"id": 1} ``` - diff --git a/core/src/confidence.rs b/core/src/confidence.rs index 50c7b6c702..7dcb70db8c 100644 --- a/core/src/confidence.rs +++ b/core/src/confidence.rs @@ -10,7 +10,7 @@ use std::sync::{Arc, RwLock}; use std::thread::{self, Builder, JoinHandle}; use std::time::Duration; -#[derive(Debug, Default, Eq, PartialEq)] +#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] pub struct BankConfidence { confidence: [u64; MAX_LOCKOUT_HISTORY], } @@ -25,25 +25,33 @@ impl BankConfidence { assert!(confirmation_count > 0 && confirmation_count <= MAX_LOCKOUT_HISTORY); self.confidence[confirmation_count - 1] } + #[cfg(test)] + pub(crate) fn new(confidence: [u64; MAX_LOCKOUT_HISTORY]) -> Self { + Self { confidence } + } } -#[derive(Default)] +#[derive(Debug, Default)] pub struct ForkConfidenceCache { bank_confidence: HashMap, - _total_stake: u64, + total_stake: u64, } impl ForkConfidenceCache { pub fn new(bank_confidence: HashMap, total_stake: u64) -> Self { Self { bank_confidence, - _total_stake: total_stake, + total_stake, } } pub fn get_fork_confidence(&self, fork: u64) -> Option<&BankConfidence> { self.bank_confidence.get(&fork) } + + pub fn total_stake(&self) -> u64 { + self.total_stake + } } pub struct ConfidenceAggregationData { diff --git a/core/src/rpc.rs b/core/src/rpc.rs index d2a9d08f16..b552845683 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -1,30 +1,37 @@ //! The `rpc` module implements the Solana RPC interface. -use crate::bank_forks::BankForks; -use crate::cluster_info::ClusterInfo; -use crate::contact_info::ContactInfo; -use crate::packet::PACKET_DATA_SIZE; -use crate::storage_stage::StorageState; -use crate::validator::ValidatorExit; -use crate::version::VERSION; +use crate::{ + bank_forks::BankForks, + cluster_info::ClusterInfo, + confidence::{BankConfidence, ForkConfidenceCache}, + contact_info::ContactInfo, + packet::PACKET_DATA_SIZE, + storage_stage::StorageState, + validator::ValidatorExit, + version::VERSION, +}; use bincode::{deserialize, serialize}; use jsonrpc_core::{Error, Metadata, Result}; use jsonrpc_derive::rpc; use solana_client::rpc_request::RpcEpochInfo; use solana_drone::drone::request_airdrop_transaction; use solana_runtime::bank::Bank; -use solana_sdk::account::Account; -use solana_sdk::fee_calculator::FeeCalculator; -use solana_sdk::hash::Hash; -use solana_sdk::inflation::Inflation; -use solana_sdk::pubkey::Pubkey; -use solana_sdk::signature::Signature; -use solana_sdk::transaction::{self, Transaction}; +use solana_sdk::{ + account::Account, + fee_calculator::FeeCalculator, + hash::Hash, + inflation::Inflation, + pubkey::Pubkey, + signature::Signature, + transaction::{self, Transaction}, +}; use solana_vote_api::vote_state::{VoteState, MAX_LOCKOUT_HISTORY}; -use std::net::{SocketAddr, UdpSocket}; -use std::sync::{Arc, RwLock}; -use std::thread::sleep; -use std::time::{Duration, Instant}; +use std::{ + net::{SocketAddr, UdpSocket}, + sync::{Arc, RwLock}, + thread::sleep, + time::{Duration, Instant}, +}; #[derive(Debug, Clone)] pub struct JsonRpcConfig { @@ -44,6 +51,7 @@ impl Default for JsonRpcConfig { #[derive(Clone)] pub struct JsonRpcRequestProcessor { bank_forks: Arc>, + fork_confidence_cache: Arc>, storage_state: StorageState, config: JsonRpcConfig, validator_exit: Arc>>, @@ -58,10 +66,12 @@ impl JsonRpcRequestProcessor { storage_state: StorageState, config: JsonRpcConfig, bank_forks: Arc>, + fork_confidence_cache: Arc>, validator_exit: &Arc>>, ) -> Self { JsonRpcRequestProcessor { bank_forks, + fork_confidence_cache, storage_state, config, validator_exit: validator_exit.clone(), @@ -100,6 +110,14 @@ impl JsonRpcRequestProcessor { (blockhash.to_string(), fee_calculator) } + fn get_block_confidence(&self, block: u64) -> (Option, u64) { + let r_fork_confidence = self.fork_confidence_cache.read().unwrap(); + ( + r_fork_confidence.get_fork_confidence(block).cloned(), + r_fork_confidence.total_stake(), + ) + } + pub fn get_signature_status(&self, signature: Signature) -> Option> { self.get_signature_confirmation_status(signature) .map(|x| x.1) @@ -307,6 +325,13 @@ pub trait RpcSol { #[rpc(meta, name = "getEpochInfo")] fn get_epoch_info(&self, _: Self::Metadata) -> Result; + #[rpc(meta, name = "getBlockConfidence")] + fn get_block_confidence( + &self, + _: Self::Metadata, + _: u64, + ) -> Result<(Option, u64)>; + #[rpc(meta, name = "getGenesisBlockhash")] fn get_genesis_blockhash(&self, _: Self::Metadata) -> Result; @@ -487,6 +512,18 @@ impl RpcSol for RpcSolImpl { }) } + fn get_block_confidence( + &self, + meta: Self::Metadata, + block: u64, + ) -> Result<(Option, u64)> { + Ok(meta + .request_processor + .read() + .unwrap() + .get_block_confidence(block)) + } + fn get_genesis_blockhash(&self, meta: Self::Metadata) -> Result { debug!("get_genesis_blockhash rpc request received"); Ok(meta.genesis_blockhash.to_string()) @@ -708,25 +745,49 @@ impl RpcSol for RpcSolImpl { #[cfg(test)] pub mod tests { use super::*; - use crate::contact_info::ContactInfo; - use crate::genesis_utils::{create_genesis_block, GenesisBlockInfo}; + use crate::{ + contact_info::ContactInfo, + genesis_utils::{create_genesis_block, GenesisBlockInfo}, + }; use jsonrpc_core::{MetaIoHandler, Output, Response, Value}; - use solana_sdk::fee_calculator::DEFAULT_BURN_PERCENT; - use solana_sdk::hash::{hash, Hash}; - use solana_sdk::instruction::InstructionError; - use solana_sdk::signature::{Keypair, KeypairUtil}; - use solana_sdk::system_transaction; - use solana_sdk::transaction::TransactionError; - use std::sync::atomic::{AtomicBool, Ordering}; - use std::thread; + use solana_sdk::{ + fee_calculator::DEFAULT_BURN_PERCENT, + hash::{hash, Hash}, + instruction::InstructionError, + signature::{Keypair, KeypairUtil}, + system_transaction, + transaction::TransactionError, + }; + use std::{ + collections::HashMap, + sync::atomic::{AtomicBool, Ordering}, + thread, + }; const TEST_MINT_LAMPORTS: u64 = 10_000; - fn start_rpc_handler_with_tx( - pubkey: &Pubkey, - ) -> (MetaIoHandler, Meta, Arc, Hash, Keypair, Pubkey) { + struct RpcHandler { + io: MetaIoHandler, + meta: Meta, + bank: Arc, + blockhash: Hash, + alice: Keypair, + leader_pubkey: Pubkey, + fork_confidence_cache: Arc>, + } + + fn start_rpc_handler_with_tx(pubkey: &Pubkey) -> RpcHandler { let (bank_forks, alice) = new_bank_forks(); let bank = bank_forks.read().unwrap().working_bank(); + + let confidence_slot0 = BankConfidence::new([8; MAX_LOCKOUT_HISTORY]); + let confidence_slot1 = BankConfidence::new([9; MAX_LOCKOUT_HISTORY]); + let mut bank_confidence: HashMap = HashMap::new(); + bank_confidence.entry(0).or_insert(confidence_slot0.clone()); + bank_confidence.entry(1).or_insert(confidence_slot1.clone()); + let fork_confidence_cache = + Arc::new(RwLock::new(ForkConfidenceCache::new(bank_confidence, 42))); + let leader_pubkey = *bank.collector_id(); let exit = Arc::new(AtomicBool::new(false)); let validator_exit = create_validator_exit(&exit); @@ -742,6 +803,7 @@ pub mod tests { StorageState::default(), JsonRpcConfig::default(), bank_forks, + fork_confidence_cache.clone(), &validator_exit, ))); let cluster_info = Arc::new(RwLock::new(ClusterInfo::new_with_invalid_keypair( @@ -764,7 +826,15 @@ pub mod tests { cluster_info, genesis_blockhash: Hash::default(), }; - (io, meta, bank, blockhash, alice, leader_pubkey) + RpcHandler { + io, + meta, + bank, + blockhash, + alice, + leader_pubkey, + fork_confidence_cache, + } } #[test] @@ -774,10 +844,12 @@ pub mod tests { let validator_exit = create_validator_exit(&exit); let (bank_forks, alice) = new_bank_forks(); let bank = bank_forks.read().unwrap().working_bank(); + let fork_confidence_cache = Arc::new(RwLock::new(ForkConfidenceCache::default())); let request_processor = JsonRpcRequestProcessor::new( StorageState::default(), JsonRpcConfig::default(), bank_forks, + fork_confidence_cache, &validator_exit, ); thread::spawn(move || { @@ -793,8 +865,7 @@ pub mod tests { #[test] fn test_rpc_get_balance() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, _blockhash, _alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { io, meta, .. } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!( r#"{{"jsonrpc":"2.0","id":1,"method":"getBalance","params":["{}"]}}"#, @@ -812,8 +883,12 @@ pub mod tests { #[test] fn test_rpc_get_cluster_nodes() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, _blockhash, _alice, leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { + io, + meta, + leader_pubkey, + .. + } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getClusterNodes"}}"#); let res = io.handle_request_sync(&req, meta); @@ -833,8 +908,12 @@ pub mod tests { #[test] fn test_rpc_get_slot_leader() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, _blockhash, _alice, leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { + io, + meta, + leader_pubkey, + .. + } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getSlotLeader"}}"#); let res = io.handle_request_sync(&req, meta); @@ -849,8 +928,7 @@ pub mod tests { #[test] fn test_rpc_get_tx_count() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, _blockhash, _alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { io, meta, .. } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getTransactionCount"}}"#); let res = io.handle_request_sync(&req, meta); @@ -865,8 +943,7 @@ pub mod tests { #[test] fn test_rpc_get_total_supply() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, _blockhash, _alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { io, meta, .. } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getTotalSupply"}}"#); let rep = io.handle_request_sync(&req, meta); @@ -892,8 +969,7 @@ pub mod tests { fn test_rpc_get_minimum_balance_for_rent_exemption() { let bob_pubkey = Pubkey::new_rand(); let data_len = 50; - let (io, meta, bank, _blockhash, _alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { io, meta, bank, .. } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!( r#"{{"jsonrpc":"2.0","id":1,"method":"getMinimumBalanceForRentExemption","params":[{}]}}"#, @@ -924,8 +1000,7 @@ pub mod tests { #[test] fn test_rpc_get_inflation() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, bank, _blockhash, _alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { io, meta, bank, .. } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getInflation"}}"#); let rep = io.handle_request_sync(&req, meta); @@ -946,8 +1021,7 @@ pub mod tests { #[test] fn test_rpc_get_account_info() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, _blockhash, _alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { io, meta, .. } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!( r#"{{"jsonrpc":"2.0","id":1,"method":"getAccountInfo","params":["{}"]}}"#, @@ -975,8 +1049,13 @@ pub mod tests { #[test] fn test_rpc_get_program_accounts() { let bob = Keypair::new(); - let (io, meta, bank, blockhash, _alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob.pubkey()); + let RpcHandler { + io, + meta, + bank, + blockhash, + .. + } = start_rpc_handler_with_tx(&bob.pubkey()); let new_program_id = Pubkey::new_rand(); let tx = system_transaction::assign(&bob, blockhash, &new_program_id); @@ -1011,8 +1090,13 @@ pub mod tests { #[test] fn test_rpc_confirm_tx() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, blockhash, alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { + io, + meta, + blockhash, + alice, + .. + } = start_rpc_handler_with_tx(&bob_pubkey); let tx = system_transaction::transfer(&alice, &bob_pubkey, 20, blockhash); let req = format!( @@ -1031,8 +1115,13 @@ pub mod tests { #[test] fn test_rpc_get_signature_status() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, blockhash, alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { + io, + meta, + blockhash, + alice, + .. + } = start_rpc_handler_with_tx(&bob_pubkey); let tx = system_transaction::transfer(&alice, &bob_pubkey, 20, blockhash); let req = format!( @@ -1096,8 +1185,12 @@ pub mod tests { #[test] fn test_rpc_get_recent_blockhash() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, blockhash, _alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { + io, + meta, + blockhash, + .. + } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getRecentBlockhash"}}"#); let res = io.handle_request_sync(&req, meta); @@ -1123,8 +1216,7 @@ pub mod tests { #[test] fn test_rpc_fail_request_airdrop() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, _bank, _blockhash, _alice, _leader_pubkey) = - start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { io, meta, .. } = start_rpc_handler_with_tx(&bob_pubkey); // Expect internal error because no drone is available let req = format!( @@ -1145,6 +1237,7 @@ pub mod tests { fn test_rpc_send_bad_tx() { let exit = Arc::new(AtomicBool::new(false)); let validator_exit = create_validator_exit(&exit); + let fork_confidence_cache = Arc::new(RwLock::new(ForkConfidenceCache::default())); let mut io = MetaIoHandler::default(); let rpc = RpcSolImpl; @@ -1155,6 +1248,7 @@ pub mod tests { StorageState::default(), JsonRpcConfig::default(), new_bank_forks().0, + fork_confidence_cache, &validator_exit, ); Arc::new(RwLock::new(request_processor)) @@ -1241,10 +1335,12 @@ pub mod tests { fn test_rpc_request_processor_config_default_trait_validator_exit_fails() { let exit = Arc::new(AtomicBool::new(false)); let validator_exit = create_validator_exit(&exit); + let fork_confidence_cache = Arc::new(RwLock::new(ForkConfidenceCache::default())); let request_processor = JsonRpcRequestProcessor::new( StorageState::default(), JsonRpcConfig::default(), new_bank_forks().0, + fork_confidence_cache, &validator_exit, ); assert_eq!(request_processor.validator_exit(), Ok(false)); @@ -1255,12 +1351,14 @@ pub mod tests { fn test_rpc_request_processor_allow_validator_exit_config() { let exit = Arc::new(AtomicBool::new(false)); let validator_exit = create_validator_exit(&exit); + let fork_confidence_cache = Arc::new(RwLock::new(ForkConfidenceCache::default())); let mut config = JsonRpcConfig::default(); config.enable_validator_exit = true; let request_processor = JsonRpcRequestProcessor::new( StorageState::default(), config, new_bank_forks().0, + fork_confidence_cache, &validator_exit, ); assert_eq!(request_processor.validator_exit(), Ok(true)); @@ -1270,7 +1368,7 @@ pub mod tests { #[test] fn test_rpc_get_version() { let bob_pubkey = Pubkey::new_rand(); - let (io, meta, ..) = start_rpc_handler_with_tx(&bob_pubkey); + let RpcHandler { io, meta, .. } = start_rpc_handler_with_tx(&bob_pubkey); let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getVersion"}}"#); let res = io.handle_request_sync(&req, meta); @@ -1287,4 +1385,90 @@ pub mod tests { .expect("actual response deserialization"); assert_eq!(expected, result); } + + #[test] + fn test_rpc_processor_get_block_confidence() { + let exit = Arc::new(AtomicBool::new(false)); + let validator_exit = create_validator_exit(&exit); + let confidence_slot0 = BankConfidence::new([8; MAX_LOCKOUT_HISTORY]); + let confidence_slot1 = BankConfidence::new([9; MAX_LOCKOUT_HISTORY]); + let mut bank_confidence: HashMap = HashMap::new(); + bank_confidence.entry(0).or_insert(confidence_slot0.clone()); + bank_confidence.entry(1).or_insert(confidence_slot1.clone()); + let fork_confidence_cache = + Arc::new(RwLock::new(ForkConfidenceCache::new(bank_confidence, 42))); + + let mut config = JsonRpcConfig::default(); + config.enable_validator_exit = true; + let request_processor = JsonRpcRequestProcessor::new( + StorageState::default(), + config, + new_bank_forks().0, + fork_confidence_cache, + &validator_exit, + ); + assert_eq!( + request_processor.get_block_confidence(0), + (Some(confidence_slot0), 42) + ); + assert_eq!( + request_processor.get_block_confidence(1), + (Some(confidence_slot1), 42) + ); + assert_eq!(request_processor.get_block_confidence(2), (None, 42)); + } + + #[test] + fn test_rpc_get_block_confidence() { + let bob_pubkey = Pubkey::new_rand(); + let RpcHandler { + io, + meta, + fork_confidence_cache, + .. + } = start_rpc_handler_with_tx(&bob_pubkey); + + let req = + format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getBlockConfidence","params":[0]}}"#); + let res = io.handle_request_sync(&req, meta.clone()); + let result: Response = serde_json::from_str(&res.expect("actual response")) + .expect("actual response deserialization"); + let (confidence, total_staked): (Option, u64) = + if let Response::Single(res) = result { + if let Output::Success(res) = res { + serde_json::from_value(res.result).unwrap() + } else { + panic!("Expected success"); + } + } else { + panic!("Expected single response"); + }; + assert_eq!( + confidence, + fork_confidence_cache + .read() + .unwrap() + .get_fork_confidence(0) + .cloned() + ); + assert_eq!(total_staked, 42); + + let req = + format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getBlockConfidence","params":[2]}}"#); + let res = io.handle_request_sync(&req, meta); + let result: Response = serde_json::from_str(&res.expect("actual response")) + .expect("actual response deserialization"); + let (confidence, total_staked): (Option, u64) = + if let Response::Single(res) = result { + if let Output::Success(res) = res { + serde_json::from_value(res.result).unwrap() + } else { + panic!("Expected success"); + } + } else { + panic!("Expected single response"); + }; + assert_eq!(confidence, None); + assert_eq!(total_staked, 42); + } } diff --git a/core/src/rpc_service.rs b/core/src/rpc_service.rs index d2f40b7263..69bc6e4ab9 100644 --- a/core/src/rpc_service.rs +++ b/core/src/rpc_service.rs @@ -1,23 +1,22 @@ //! The `rpc_service` module implements the Solana JSON RPC service. -use crate::bank_forks::BankForks; -use crate::cluster_info::ClusterInfo; -use crate::rpc::*; -use crate::service::Service; -use crate::storage_stage::StorageState; -use crate::validator::ValidatorExit; +use crate::{ + bank_forks::BankForks, cluster_info::ClusterInfo, confidence::ForkConfidenceCache, rpc::*, + service::Service, storage_stage::StorageState, validator::ValidatorExit, +}; use jsonrpc_core::MetaIoHandler; -use jsonrpc_http_server::CloseHandle; use jsonrpc_http_server::{ - hyper, AccessControlAllowOrigin, DomainsValidation, RequestMiddleware, RequestMiddlewareAction, - ServerBuilder, + hyper, AccessControlAllowOrigin, CloseHandle, DomainsValidation, RequestMiddleware, + RequestMiddlewareAction, ServerBuilder, }; use solana_sdk::hash::Hash; -use std::net::SocketAddr; -use std::path::{Path, PathBuf}; -use std::sync::mpsc::channel; -use std::sync::{Arc, RwLock}; -use std::thread::{self, Builder, JoinHandle}; +use std::{ + net::SocketAddr, + path::{Path, PathBuf}, + sync::mpsc::channel, + sync::{Arc, RwLock}, + thread::{self, Builder, JoinHandle}, +}; use tokio::prelude::Future; pub struct JsonRpcService { @@ -91,6 +90,7 @@ impl JsonRpcService { storage_state: StorageState, config: JsonRpcConfig, bank_forks: Arc>, + fork_confidence_cache: Arc>, ledger_path: &Path, genesis_blockhash: Hash, validator_exit: &Arc>>, @@ -101,6 +101,7 @@ impl JsonRpcService { storage_state, config, bank_forks, + fork_confidence_cache, validator_exit, ))); let request_processor_ = request_processor.clone(); @@ -197,12 +198,14 @@ mod tests { solana_netutil::find_available_port_in_range((10000, 65535)).unwrap(), ); let bank_forks = Arc::new(RwLock::new(BankForks::new(bank.slot(), bank))); + let fork_confidence_cache = Arc::new(RwLock::new(ForkConfidenceCache::default())); let mut rpc_service = JsonRpcService::new( &cluster_info, rpc_addr, StorageState::default(), JsonRpcConfig::default(), bank_forks, + fork_confidence_cache, &PathBuf::from("farf"), Hash::default(), &validator_exit, diff --git a/core/src/validator.rs b/core/src/validator.rs index a791560915..e2ac6d9084 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -158,6 +158,7 @@ impl Validator { let bank_info = &bank_forks_info[0]; let bank = bank_forks[bank_info.bank_slot].clone(); let bank_forks = Arc::new(RwLock::new(bank_forks)); + let fork_confidence_cache = Arc::new(RwLock::new(ForkConfidenceCache::default())); let mut validator_exit = ValidatorExit::default(); let exit_ = exit.clone(); @@ -185,6 +186,7 @@ impl Validator { storage_state.clone(), config.rpc_config.clone(), bank_forks.clone(), + fork_confidence_cache.clone(), ledger_path, genesis_blockhash, &validator_exit, @@ -298,7 +300,6 @@ impl Validator { Some(voting_keypair) }; - let fork_confidence_cache = Arc::new(RwLock::new(ForkConfidenceCache::default())); let tvu = Tvu::new( vote_account, voting_keypair,