Move BankCommitmentCache to solana_runtime (#10816)
* Remove Blockstore member variable from BlockCommitmentCache * Hoist is_confirmed_rooted() to its only caller BlockCommitmentCache no longer depends on Blockstore * Move BlockCommitmentCache to solana_runtime
This commit is contained in:
@@ -1,14 +1,9 @@
|
||||
//! The `rpc` module implements the Solana RPC interface.
|
||||
|
||||
use crate::{
|
||||
cluster_info::ClusterInfo,
|
||||
commitment::{BlockCommitmentArray, BlockCommitmentCache},
|
||||
contact_info::ContactInfo,
|
||||
non_circulating_supply::calculate_non_circulating_supply,
|
||||
rpc_error::RpcCustomError,
|
||||
rpc_health::*,
|
||||
send_transaction_service::SendTransactionService,
|
||||
validator::ValidatorExit,
|
||||
cluster_info::ClusterInfo, contact_info::ContactInfo,
|
||||
non_circulating_supply::calculate_non_circulating_supply, rpc_error::RpcCustomError,
|
||||
rpc_health::*, send_transaction_service::SendTransactionService, validator::ValidatorExit,
|
||||
};
|
||||
use bincode::serialize;
|
||||
use jsonrpc_core::{Error, Metadata, Result};
|
||||
@@ -26,7 +21,11 @@ use solana_faucet::faucet::request_airdrop_transaction;
|
||||
use solana_ledger::{blockstore::Blockstore, blockstore_db::BlockstoreError, get_tmp_ledger_path};
|
||||
use solana_perf::packet::PACKET_DATA_SIZE;
|
||||
use solana_runtime::{
|
||||
accounts::AccountAddressFilter, bank::Bank, bank_forks::BankForks, log_collector::LogCollector,
|
||||
accounts::AccountAddressFilter,
|
||||
bank::Bank,
|
||||
bank_forks::BankForks,
|
||||
commitment::{BlockCommitmentArray, BlockCommitmentCache},
|
||||
log_collector::LogCollector,
|
||||
};
|
||||
use solana_sdk::{
|
||||
clock::{Epoch, Slot, UnixTimestamp},
|
||||
@@ -60,6 +59,19 @@ fn new_response<T>(bank: &Bank, value: T) -> Result<RpcResponse<T>> {
|
||||
Ok(Response { context, value })
|
||||
}
|
||||
|
||||
pub fn is_confirmed_rooted(
|
||||
block_commitment_cache: &BlockCommitmentCache,
|
||||
blockstore: &Blockstore,
|
||||
slot: Slot,
|
||||
) -> bool {
|
||||
slot <= block_commitment_cache.largest_confirmed_root()
|
||||
&& (blockstore.is_root(slot)
|
||||
|| block_commitment_cache
|
||||
.bank()
|
||||
.status_cache_ancestors()
|
||||
.contains(&slot))
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct JsonRpcConfig {
|
||||
pub enable_validator_exit: bool,
|
||||
@@ -174,7 +186,6 @@ impl JsonRpcRequestProcessor {
|
||||
0,
|
||||
0,
|
||||
bank.clone(),
|
||||
blockstore.clone(),
|
||||
0,
|
||||
0,
|
||||
))),
|
||||
@@ -670,7 +681,7 @@ impl JsonRpcRequestProcessor {
|
||||
let r_block_commitment_cache = self.block_commitment_cache.read().unwrap();
|
||||
|
||||
let confirmations = if r_block_commitment_cache.root() >= slot
|
||||
&& r_block_commitment_cache.is_confirmed_rooted(slot)
|
||||
&& is_confirmed_rooted(&r_block_commitment_cache, &self.blockstore, slot)
|
||||
{
|
||||
None
|
||||
} else {
|
||||
@@ -1601,8 +1612,7 @@ pub(crate) fn create_validator_exit(exit: &Arc<AtomicBool>) -> Arc<RwLock<Option
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
commitment::BlockCommitment, contact_info::ContactInfo,
|
||||
non_circulating_supply::non_circulating_accounts,
|
||||
contact_info::ContactInfo, non_circulating_supply::non_circulating_accounts,
|
||||
replay_stage::tests::create_test_transactions_and_populate_blockstore,
|
||||
};
|
||||
use bincode::deserialize;
|
||||
@@ -1616,6 +1626,7 @@ pub mod tests {
|
||||
entry::next_entry_mut,
|
||||
genesis_utils::{create_genesis_config, GenesisConfigInfo},
|
||||
};
|
||||
use solana_runtime::commitment::BlockCommitment;
|
||||
use solana_sdk::{
|
||||
clock::MAX_RECENT_BLOCKHASHES,
|
||||
fee_calculator::DEFAULT_BURN_PERCENT,
|
||||
@@ -1688,7 +1699,6 @@ pub mod tests {
|
||||
0,
|
||||
10,
|
||||
bank.clone(),
|
||||
blockstore.clone(),
|
||||
0,
|
||||
0,
|
||||
)));
|
||||
@@ -2775,9 +2785,7 @@ pub mod tests {
|
||||
let validator_exit = create_validator_exit(&exit);
|
||||
let ledger_path = get_tmp_ledger_path!();
|
||||
let blockstore = Arc::new(Blockstore::open(&ledger_path).unwrap());
|
||||
let block_commitment_cache = Arc::new(RwLock::new(
|
||||
BlockCommitmentCache::default_with_blockstore(blockstore.clone()),
|
||||
));
|
||||
let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::default()));
|
||||
|
||||
let mut io = MetaIoHandler::default();
|
||||
let rpc = RpcSolImpl;
|
||||
@@ -2813,9 +2821,7 @@ pub mod tests {
|
||||
let validator_exit = create_validator_exit(&exit);
|
||||
let ledger_path = get_tmp_ledger_path!();
|
||||
let blockstore = Arc::new(Blockstore::open(&ledger_path).unwrap());
|
||||
let block_commitment_cache = Arc::new(RwLock::new(
|
||||
BlockCommitmentCache::default_with_blockstore(blockstore.clone()),
|
||||
));
|
||||
let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::default()));
|
||||
let bank_forks = new_bank_forks().0;
|
||||
let health = RpcHealth::stub();
|
||||
|
||||
@@ -2956,9 +2962,7 @@ pub mod tests {
|
||||
let validator_exit = create_validator_exit(&exit);
|
||||
let ledger_path = get_tmp_ledger_path!();
|
||||
let blockstore = Arc::new(Blockstore::open(&ledger_path).unwrap());
|
||||
let block_commitment_cache = Arc::new(RwLock::new(
|
||||
BlockCommitmentCache::default_with_blockstore(blockstore.clone()),
|
||||
));
|
||||
let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::default()));
|
||||
let cluster_info = Arc::new(ClusterInfo::default());
|
||||
let bank_forks = new_bank_forks().0;
|
||||
let request_processor = JsonRpcRequestProcessor::new(
|
||||
@@ -2986,9 +2990,7 @@ pub mod tests {
|
||||
let validator_exit = create_validator_exit(&exit);
|
||||
let ledger_path = get_tmp_ledger_path!();
|
||||
let blockstore = Arc::new(Blockstore::open(&ledger_path).unwrap());
|
||||
let block_commitment_cache = Arc::new(RwLock::new(
|
||||
BlockCommitmentCache::default_with_blockstore(blockstore.clone()),
|
||||
));
|
||||
let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::default()));
|
||||
let mut config = JsonRpcConfig::default();
|
||||
config.enable_validator_exit = true;
|
||||
let bank_forks = new_bank_forks().0;
|
||||
@@ -3076,7 +3078,6 @@ pub mod tests {
|
||||
0,
|
||||
42,
|
||||
bank_forks.read().unwrap().working_bank(),
|
||||
blockstore.clone(),
|
||||
0,
|
||||
0,
|
||||
)));
|
||||
@@ -3556,4 +3557,40 @@ pub mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_confirmed_rooted() {
|
||||
let bank = Arc::new(Bank::default());
|
||||
let ledger_path = get_tmp_ledger_path!();
|
||||
let blockstore = Arc::new(Blockstore::open(&ledger_path).unwrap());
|
||||
blockstore.set_roots(&[0, 1]).unwrap();
|
||||
// Build BlockCommitmentCache with rooted slots
|
||||
let mut cache0 = BlockCommitment::default();
|
||||
cache0.increase_rooted_stake(50);
|
||||
let mut cache1 = BlockCommitment::default();
|
||||
cache1.increase_rooted_stake(40);
|
||||
let mut cache2 = BlockCommitment::default();
|
||||
cache2.increase_rooted_stake(20);
|
||||
|
||||
let mut block_commitment = HashMap::new();
|
||||
block_commitment.entry(1).or_insert(cache0);
|
||||
block_commitment.entry(2).or_insert(cache1);
|
||||
block_commitment.entry(3).or_insert(cache2);
|
||||
let largest_confirmed_root = 1;
|
||||
let block_commitment_cache =
|
||||
BlockCommitmentCache::new(block_commitment, largest_confirmed_root, 50, bank, 0, 0);
|
||||
|
||||
assert!(is_confirmed_rooted(&block_commitment_cache, &blockstore, 0));
|
||||
assert!(is_confirmed_rooted(&block_commitment_cache, &blockstore, 1));
|
||||
assert!(!is_confirmed_rooted(
|
||||
&block_commitment_cache,
|
||||
&blockstore,
|
||||
2
|
||||
));
|
||||
assert!(!is_confirmed_rooted(
|
||||
&block_commitment_cache,
|
||||
&blockstore,
|
||||
3
|
||||
));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user