(cherry picked from commit e7fd7d46cf
)
Co-authored-by: Justin Starry <justin@solana.com>
This commit is contained in:
132
core/src/rpc.rs
132
core/src/rpc.rs
@@ -30,13 +30,17 @@ use solana_client::{
|
||||
TokenAccountsFilter, DELINQUENT_VALIDATOR_SLOT_DISTANCE, MAX_GET_CONFIRMED_BLOCKS_RANGE,
|
||||
MAX_GET_CONFIRMED_SIGNATURES_FOR_ADDRESS2_LIMIT,
|
||||
MAX_GET_CONFIRMED_SIGNATURES_FOR_ADDRESS_SLOT_RANGE, MAX_GET_PROGRAM_ACCOUNT_FILTERS,
|
||||
MAX_GET_SIGNATURE_STATUSES_QUERY_ITEMS, MAX_MULTIPLE_ACCOUNTS, NUM_LARGEST_ACCOUNTS,
|
||||
MAX_GET_SIGNATURE_STATUSES_QUERY_ITEMS, MAX_GET_SLOT_LEADERS, MAX_MULTIPLE_ACCOUNTS,
|
||||
NUM_LARGEST_ACCOUNTS,
|
||||
},
|
||||
rpc_response::Response as RpcResponse,
|
||||
rpc_response::*,
|
||||
};
|
||||
use solana_faucet::faucet::request_airdrop_transaction;
|
||||
use solana_ledger::{blockstore::Blockstore, blockstore_db::BlockstoreError, get_tmp_ledger_path};
|
||||
use solana_ledger::{
|
||||
blockstore::Blockstore, blockstore_db::BlockstoreError, get_tmp_ledger_path,
|
||||
leader_schedule_cache::LeaderScheduleCache,
|
||||
};
|
||||
use solana_metrics::inc_new_counter_info;
|
||||
use solana_perf::packet::PACKET_DATA_SIZE;
|
||||
use solana_runtime::{
|
||||
@@ -140,6 +144,7 @@ pub struct JsonRpcRequestProcessor {
|
||||
optimistically_confirmed_bank: Arc<RwLock<OptimisticallyConfirmedBank>>,
|
||||
largest_accounts_cache: Arc<RwLock<LargestAccountsCache>>,
|
||||
max_slots: Arc<MaxSlots>,
|
||||
leader_schedule_cache: Arc<LeaderScheduleCache>,
|
||||
}
|
||||
impl Metadata for JsonRpcRequestProcessor {}
|
||||
|
||||
@@ -224,6 +229,7 @@ impl JsonRpcRequestProcessor {
|
||||
optimistically_confirmed_bank: Arc<RwLock<OptimisticallyConfirmedBank>>,
|
||||
largest_accounts_cache: Arc<RwLock<LargestAccountsCache>>,
|
||||
max_slots: Arc<MaxSlots>,
|
||||
leader_schedule_cache: Arc<LeaderScheduleCache>,
|
||||
) -> (Self, Receiver<TransactionInfo>) {
|
||||
let (sender, receiver) = channel();
|
||||
(
|
||||
@@ -243,6 +249,7 @@ impl JsonRpcRequestProcessor {
|
||||
optimistically_confirmed_bank,
|
||||
largest_accounts_cache,
|
||||
max_slots,
|
||||
leader_schedule_cache,
|
||||
},
|
||||
receiver,
|
||||
)
|
||||
@@ -284,6 +291,7 @@ impl JsonRpcRequestProcessor {
|
||||
})),
|
||||
largest_accounts_cache: Arc::new(RwLock::new(LargestAccountsCache::new(30))),
|
||||
max_slots: Arc::new(MaxSlots::default()),
|
||||
leader_schedule_cache: Arc::new(LeaderScheduleCache::new_from_bank(bank)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2019,6 +2027,14 @@ pub trait RpcSol {
|
||||
commitment: Option<CommitmentConfig>,
|
||||
) -> Result<String>;
|
||||
|
||||
#[rpc(meta, name = "getSlotLeaders")]
|
||||
fn get_slot_leaders(
|
||||
&self,
|
||||
meta: Self::Metadata,
|
||||
start_slot: Slot,
|
||||
end_slot: Slot,
|
||||
) -> Result<Vec<String>>;
|
||||
|
||||
#[rpc(meta, name = "minimumLedgerSlot")]
|
||||
fn minimum_ledger_slot(&self, meta: Self::Metadata) -> Result<Slot>;
|
||||
|
||||
@@ -2751,6 +2767,56 @@ impl RpcSol for RpcSolImpl {
|
||||
Ok(meta.get_slot_leader(commitment))
|
||||
}
|
||||
|
||||
fn get_slot_leaders(
|
||||
&self,
|
||||
meta: Self::Metadata,
|
||||
start_slot: Slot,
|
||||
limit: u64,
|
||||
) -> Result<Vec<String>> {
|
||||
debug!(
|
||||
"get_slot_leaders rpc request received (start: {} limit: {})",
|
||||
start_slot, limit
|
||||
);
|
||||
|
||||
let limit = limit as usize;
|
||||
if limit > MAX_GET_SLOT_LEADERS {
|
||||
return Err(Error::invalid_params(format!(
|
||||
"Invalid limit; max {}",
|
||||
MAX_GET_SLOT_LEADERS
|
||||
)));
|
||||
}
|
||||
|
||||
let bank = meta.bank(None);
|
||||
let (mut epoch, mut slot_index) =
|
||||
bank.epoch_schedule().get_epoch_and_slot_index(start_slot);
|
||||
|
||||
let mut slot_leaders = Vec::with_capacity(limit);
|
||||
while slot_leaders.len() < limit {
|
||||
if let Some(leader_schedule) =
|
||||
meta.leader_schedule_cache.get_epoch_leader_schedule(epoch)
|
||||
{
|
||||
slot_leaders.extend(
|
||||
leader_schedule
|
||||
.get_slot_leaders()
|
||||
.iter()
|
||||
.skip(slot_index as usize)
|
||||
.take(limit.saturating_sub(slot_leaders.len()))
|
||||
.map(|pubkey| pubkey.to_string()),
|
||||
);
|
||||
} else {
|
||||
return Err(Error::invalid_params(format!(
|
||||
"Invalid slot range: leader schedule for epoch {} is unavailable",
|
||||
epoch
|
||||
)));
|
||||
}
|
||||
|
||||
epoch += 1;
|
||||
slot_index = 0;
|
||||
}
|
||||
|
||||
Ok(slot_leaders)
|
||||
}
|
||||
|
||||
fn minimum_ledger_slot(&self, meta: Self::Metadata) -> Result<Slot> {
|
||||
debug!("minimum_ledger_slot rpc request received");
|
||||
meta.minimum_ledger_slot()
|
||||
@@ -3293,6 +3359,7 @@ pub mod tests {
|
||||
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks),
|
||||
Arc::new(RwLock::new(LargestAccountsCache::new(30))),
|
||||
max_slots,
|
||||
Arc::new(LeaderScheduleCache::new_from_bank(&bank)),
|
||||
);
|
||||
SendTransactionService::new(tpu_address, &bank_forks, None, receiver, 1000, 1);
|
||||
|
||||
@@ -3790,6 +3857,62 @@ pub mod tests {
|
||||
assert_eq!(schedule, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rpc_get_slot_leaders() {
|
||||
let bob_pubkey = solana_sdk::pubkey::new_rand();
|
||||
let RpcHandler { io, meta, bank, .. } = start_rpc_handler_with_tx(&bob_pubkey);
|
||||
|
||||
// Test that slot leaders will be returned across epochs
|
||||
let query_start = 0;
|
||||
let query_limit = 2 * bank.epoch_schedule().slots_per_epoch;
|
||||
|
||||
let req = format!(
|
||||
r#"{{"jsonrpc":"2.0","id":1,"method":"getSlotLeaders", "params": [{}, {}]}}"#,
|
||||
query_start, query_limit
|
||||
);
|
||||
let rep = io.handle_request_sync(&req, meta.clone());
|
||||
let res: Response = serde_json::from_str(&rep.expect("actual response"))
|
||||
.expect("actual response deserialization");
|
||||
|
||||
let slot_leaders: Vec<String> = if let Response::Single(res) = res {
|
||||
if let Output::Success(res) = res {
|
||||
serde_json::from_value(res.result).unwrap()
|
||||
} else {
|
||||
panic!("Expected success for {} but received: {:?}", req, res);
|
||||
}
|
||||
} else {
|
||||
panic!("Expected single response");
|
||||
};
|
||||
|
||||
assert_eq!(slot_leaders.len(), query_limit as usize);
|
||||
|
||||
// Test that invalid limit returns an error
|
||||
let query_start = 0;
|
||||
let query_limit = 5001;
|
||||
|
||||
let req = format!(
|
||||
r#"{{"jsonrpc":"2.0","id":1,"method":"getSlotLeaders", "params": [{}, {}]}}"#,
|
||||
query_start, query_limit
|
||||
);
|
||||
let rep = io.handle_request_sync(&req, meta.clone());
|
||||
let res: Value = serde_json::from_str(&rep.expect("actual response"))
|
||||
.expect("actual response deserialization");
|
||||
assert!(res.get("error").is_some());
|
||||
|
||||
// Test that invalid epoch returns an error
|
||||
let query_start = 2 * bank.epoch_schedule().slots_per_epoch;
|
||||
let query_limit = 10;
|
||||
|
||||
let req = format!(
|
||||
r#"{{"jsonrpc":"2.0","id":1,"method":"getSlotLeaders", "params": [{}, {}]}}"#,
|
||||
query_start, query_limit
|
||||
);
|
||||
let rep = io.handle_request_sync(&req, meta);
|
||||
let res: Value = serde_json::from_str(&rep.expect("actual response"))
|
||||
.expect("actual response deserialization");
|
||||
assert!(res.get("error").is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rpc_get_account_info() {
|
||||
let bob_pubkey = solana_sdk::pubkey::new_rand();
|
||||
@@ -4696,6 +4819,7 @@ pub mod tests {
|
||||
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks),
|
||||
Arc::new(RwLock::new(LargestAccountsCache::new(30))),
|
||||
Arc::new(MaxSlots::default()),
|
||||
Arc::new(LeaderScheduleCache::default()),
|
||||
);
|
||||
SendTransactionService::new(tpu_address, &bank_forks, None, receiver, 1000, 1);
|
||||
|
||||
@@ -4894,6 +5018,7 @@ pub mod tests {
|
||||
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks),
|
||||
Arc::new(RwLock::new(LargestAccountsCache::new(30))),
|
||||
Arc::new(MaxSlots::default()),
|
||||
Arc::new(LeaderScheduleCache::default()),
|
||||
);
|
||||
SendTransactionService::new(tpu_address, &bank_forks, None, receiver, 1000, 1);
|
||||
assert_eq!(request_processor.validator_exit(), false);
|
||||
@@ -4929,6 +5054,7 @@ pub mod tests {
|
||||
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks),
|
||||
Arc::new(RwLock::new(LargestAccountsCache::new(30))),
|
||||
Arc::new(MaxSlots::default()),
|
||||
Arc::new(LeaderScheduleCache::default()),
|
||||
);
|
||||
SendTransactionService::new(tpu_address, &bank_forks, None, receiver, 1000, 1);
|
||||
assert_eq!(request_processor.validator_exit(), true);
|
||||
@@ -5041,6 +5167,7 @@ pub mod tests {
|
||||
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks),
|
||||
Arc::new(RwLock::new(LargestAccountsCache::new(30))),
|
||||
Arc::new(MaxSlots::default()),
|
||||
Arc::new(LeaderScheduleCache::default()),
|
||||
);
|
||||
SendTransactionService::new(tpu_address, &bank_forks, None, receiver, 1000, 1);
|
||||
assert_eq!(
|
||||
@@ -6330,6 +6457,7 @@ pub mod tests {
|
||||
optimistically_confirmed_bank.clone(),
|
||||
Arc::new(RwLock::new(LargestAccountsCache::new(30))),
|
||||
Arc::new(MaxSlots::default()),
|
||||
Arc::new(LeaderScheduleCache::default()),
|
||||
);
|
||||
|
||||
let mut io = MetaIoHandler::default();
|
||||
|
@@ -18,7 +18,7 @@ use jsonrpc_http_server::{
|
||||
};
|
||||
use regex::Regex;
|
||||
use solana_client::rpc_cache::LargestAccountsCache;
|
||||
use solana_ledger::blockstore::Blockstore;
|
||||
use solana_ledger::{blockstore::Blockstore, leader_schedule_cache::LeaderScheduleCache};
|
||||
use solana_metrics::inc_new_counter_info;
|
||||
use solana_runtime::{
|
||||
bank_forks::{BankForks, SnapshotConfig},
|
||||
@@ -275,6 +275,7 @@ impl JsonRpcService {
|
||||
send_transaction_retry_ms: u64,
|
||||
send_transaction_leader_forward_count: u64,
|
||||
max_slots: Arc<MaxSlots>,
|
||||
leader_schedule_cache: Arc<LeaderScheduleCache>,
|
||||
) -> Self {
|
||||
info!("rpc bound to {:?}", rpc_addr);
|
||||
info!("rpc configuration: {:?}", config);
|
||||
@@ -352,6 +353,7 @@ impl JsonRpcService {
|
||||
optimistically_confirmed_bank,
|
||||
largest_accounts_cache,
|
||||
max_slots,
|
||||
leader_schedule_cache,
|
||||
);
|
||||
|
||||
let leader_info =
|
||||
@@ -512,6 +514,7 @@ mod tests {
|
||||
1000,
|
||||
1,
|
||||
Arc::new(MaxSlots::default()),
|
||||
Arc::new(LeaderScheduleCache::default()),
|
||||
);
|
||||
let thread = rpc_service.thread_hdl.thread();
|
||||
assert_eq!(thread.name().unwrap(), "solana-jsonrpc");
|
||||
|
@@ -493,6 +493,7 @@ impl Validator {
|
||||
config.send_transaction_retry_ms,
|
||||
config.send_transaction_leader_forward_count,
|
||||
max_slots.clone(),
|
||||
leader_schedule_cache.clone(),
|
||||
),
|
||||
pubsub_service: PubSubService::new(
|
||||
config.pubsub_config.clone(),
|
||||
|
Reference in New Issue
Block a user