Deprecate FeeCalculator returning APIs (#19120)

This commit is contained in:
Jack May
2021-08-13 09:08:20 -07:00
committed by GitHub
parent 26e963f436
commit 0b50bb2b20
47 changed files with 1119 additions and 463 deletions

View File

@ -54,7 +54,7 @@ pub fn create_builtin_transactions(
.unwrap_or_else(|_| panic!("{}:{}", line!(), file!()));
let instruction = create_invoke_instruction(rando0.pubkey(), program_id, &1u8);
let (blockhash, _fee_calculator) = bank_client.get_recent_blockhash().unwrap();
let blockhash = bank_client.get_latest_blockhash().unwrap();
let message = Message::new(&[instruction], Some(&mint_keypair.pubkey()));
Transaction::new(&[&rando0], message, blockhash)
})
@ -76,7 +76,7 @@ pub fn create_native_loader_transactions(
.unwrap_or_else(|_| panic!("{}:{}", line!(), file!()));
let instruction = create_invoke_instruction(rando0.pubkey(), program_id, &1u8);
let (blockhash, _fee_calculator) = bank_client.get_recent_blockhash().unwrap();
let blockhash = bank_client.get_latest_blockhash().unwrap();
let message = Message::new(&[instruction], Some(&mint_keypair.pubkey()));
Transaction::new(&[&rando0], message, blockhash)
})

View File

@ -450,11 +450,13 @@ impl Accounts {
.as_ref()
.map(|nonce_rollback| nonce_rollback.fee_calculator())
.unwrap_or_else(|| {
#[allow(deprecated)]
hash_queue
.get_fee_calculator(&tx.message().recent_blockhash)
.cloned()
});
let fee = if let Some(fee_calculator) = fee_calculator {
#[allow(deprecated)]
fee_calculator.calculate_fee(tx.message())
} else {
return (Err(TransactionError::BlockhashNotFound), None);
@ -1292,7 +1294,9 @@ mod tests {
);
let fee_calculator = FeeCalculator::new(10);
assert_eq!(fee_calculator.calculate_fee(tx.message()), 10);
#[allow(deprecated)]
let fee = fee_calculator.calculate_fee(tx.message());
assert_eq!(fee, 10);
let loaded_accounts =
load_accounts_with_fee(tx, &accounts, &fee_calculator, &mut error_counters);

View File

@ -2667,15 +2667,25 @@ impl Bank {
self.blockhash_queue.read().unwrap().last_hash()
}
pub fn is_blockhash_valid(&self, hash: &Hash) -> bool {
let blockhash_queue = self.blockhash_queue.read().unwrap();
blockhash_queue.check_hash(hash)
}
pub fn get_minimum_balance_for_rent_exemption(&self, data_len: usize) -> u64 {
self.rent_collector.rent.minimum_balance(data_len)
}
#[deprecated(
since = "1.8.0",
note = "Please use `last_blockhash` and `get_fee_for_message` instead"
)]
pub fn last_blockhash_with_fee_calculator(&self) -> (Hash, FeeCalculator) {
let blockhash_queue = self.blockhash_queue.read().unwrap();
let last_hash = blockhash_queue.last_hash();
(
last_hash,
#[allow(deprecated)]
blockhash_queue
.get_fee_calculator(&last_hash)
.unwrap()
@ -2683,15 +2693,26 @@ impl Bank {
)
}
#[deprecated(since = "1.8.0", note = "Please use `get_fee_for_message` instead")]
pub fn get_fee_calculator(&self, hash: &Hash) -> Option<FeeCalculator> {
let blockhash_queue = self.blockhash_queue.read().unwrap();
#[allow(deprecated)]
blockhash_queue.get_fee_calculator(hash).cloned()
}
#[deprecated(since = "1.8.0", note = "Please use `get_fee_for_message` instead")]
pub fn get_fee_rate_governor(&self) -> &FeeRateGovernor {
&self.fee_rate_governor
}
pub fn get_fee_for_message(&self, hash: &Hash, message: &Message) -> Option<u64> {
let blockhash_queue = self.blockhash_queue.read().unwrap();
#[allow(deprecated)]
let fee_calculator = blockhash_queue.get_fee_calculator(hash)?;
#[allow(deprecated)]
Some(fee_calculator.calculate_fee(message))
}
#[deprecated(
since = "1.6.11",
note = "Please use `get_blockhash_last_valid_block_height`"
@ -2714,18 +2735,36 @@ impl Bank {
.map(|age| self.block_height + blockhash_queue.len() as u64 - age)
}
pub fn confirmed_last_blockhash(&self) -> (Hash, FeeCalculator) {
#[deprecated(
since = "1.8.0",
note = "Please use `confirmed_last_blockhash` and `get_fee_for_message` instead"
)]
pub fn confirmed_last_blockhash_with_fee_calculator(&self) -> (Hash, FeeCalculator) {
const NUM_BLOCKHASH_CONFIRMATIONS: usize = 3;
let parents = self.parents();
if parents.is_empty() {
#[allow(deprecated)]
self.last_blockhash_with_fee_calculator()
} else {
let index = NUM_BLOCKHASH_CONFIRMATIONS.min(parents.len() - 1);
#[allow(deprecated)]
parents[index].last_blockhash_with_fee_calculator()
}
}
pub fn confirmed_last_blockhash(&self) -> Hash {
const NUM_BLOCKHASH_CONFIRMATIONS: usize = 3;
let parents = self.parents();
if parents.is_empty() {
self.last_blockhash()
} else {
let index = NUM_BLOCKHASH_CONFIRMATIONS.min(parents.len() - 1);
parents[index].last_blockhash()
}
}
/// Forget all signatures. Useful for benchmarking.
pub fn clear_signatures(&self) {
self.src.status_cache.write().unwrap().clear();
@ -3400,6 +3439,7 @@ impl Bank {
let blockhash = blockhash_queue.last_hash();
(
blockhash,
#[allow(deprecated)]
blockhash_queue
.get_fee_calculator(&blockhash)
.cloned()
@ -3582,6 +3622,7 @@ impl Bank {
.map(|maybe_fee_calculator| (maybe_fee_calculator, true))
.unwrap_or_else(|| {
(
#[allow(deprecated)]
hash_queue
.get_fee_calculator(&tx.message().recent_blockhash)
.cloned(),
@ -3590,6 +3631,7 @@ impl Bank {
});
let fee_calculator = fee_calculator.ok_or(TransactionError::BlockhashNotFound)?;
#[allow(deprecated)]
let fee = fee_calculator.calculate_fee(tx.message());
let message = tx.message();
@ -3658,6 +3700,7 @@ impl Bank {
}
let mut write_time = Measure::start("write_time");
#[allow(deprecated)]
self.rc.accounts.store_cached(
self.slot(),
sanitized_txs.as_transactions_iter(),
@ -8304,11 +8347,13 @@ pub(crate) mod tests {
let mut bank = Bank::new_for_tests(&genesis_config);
goto_end_of_slot(&mut bank);
#[allow(deprecated)]
let (cheap_blockhash, cheap_fee_calculator) = bank.last_blockhash_with_fee_calculator();
assert_eq!(cheap_fee_calculator.lamports_per_signature, 0);
let mut bank = Bank::new_from_parent(&Arc::new(bank), &leader, 1);
goto_end_of_slot(&mut bank);
#[allow(deprecated)]
let (expensive_blockhash, expensive_fee_calculator) =
bank.last_blockhash_with_fee_calculator();
assert!(

View File

@ -148,6 +148,7 @@ impl SyncClient for BankClient {
}
fn get_recent_blockhash(&self) -> Result<(Hash, FeeCalculator)> {
#[allow(deprecated)]
Ok(self.bank.last_blockhash_with_fee_calculator())
}
@ -155,6 +156,7 @@ impl SyncClient for BankClient {
&self,
_commitment_config: CommitmentConfig,
) -> Result<(Hash, FeeCalculator, u64)> {
#[allow(deprecated)]
let (blockhash, fee_calculator) = self.bank.last_blockhash_with_fee_calculator();
#[allow(deprecated)]
let last_valid_slot = self
@ -165,10 +167,12 @@ impl SyncClient for BankClient {
}
fn get_fee_calculator_for_blockhash(&self, blockhash: &Hash) -> Result<Option<FeeCalculator>> {
#[allow(deprecated)]
Ok(self.bank.get_fee_calculator(blockhash))
}
fn get_fee_rate_governor(&self) -> Result<FeeRateGovernor> {
#[allow(deprecated)]
Ok(self.bank.get_fee_rate_governor().clone())
}
@ -258,9 +262,10 @@ impl SyncClient for BankClient {
}
fn get_new_blockhash(&self, blockhash: &Hash) -> Result<(Hash, FeeCalculator)> {
let (last_blockhash, fee_calculator) = self.get_recent_blockhash()?;
if last_blockhash != *blockhash {
Ok((last_blockhash, fee_calculator))
#[allow(deprecated)]
let (recent_blockhash, fee_calculator) = self.get_recent_blockhash()?;
if recent_blockhash != *blockhash {
Ok((recent_blockhash, fee_calculator))
} else {
Err(TransportError::IoError(io::Error::new(
io::ErrorKind::Other,
@ -272,6 +277,53 @@ impl SyncClient for BankClient {
fn get_epoch_info(&self) -> Result<EpochInfo> {
Ok(self.bank.get_epoch_info())
}
fn get_latest_blockhash(&self) -> Result<Hash> {
Ok(self.bank.last_blockhash())
}
fn get_latest_blockhash_with_commitment(
&self,
_commitment_config: CommitmentConfig,
) -> Result<(Hash, u64)> {
let blockhash = self.bank.last_blockhash();
let last_valid_block_height = self
.bank
.get_blockhash_last_valid_block_height(&blockhash)
.expect("bank blockhash queue should contain blockhash");
Ok((blockhash, last_valid_block_height))
}
fn is_blockhash_valid(
&self,
blockhash: &Hash,
_commitment_config: CommitmentConfig,
) -> Result<bool> {
Ok(self.bank.is_blockhash_valid(blockhash))
}
fn get_fee_for_message(&self, blockhash: &Hash, message: &Message) -> Result<u64> {
self.bank
.get_fee_for_message(blockhash, message)
.ok_or_else(|| {
TransportError::IoError(io::Error::new(
io::ErrorKind::Other,
"Unable calculate fee",
))
})
}
fn get_new_latest_blockhash(&self, blockhash: &Hash) -> Result<Hash> {
let latest_blockhash = self.get_latest_blockhash()?;
if latest_blockhash != *blockhash {
Ok(latest_blockhash)
} else {
Err(TransportError::IoError(io::Error::new(
io::ErrorKind::Other,
"Unable to get new blockhash",
)))
}
}
}
impl BankClient {

View File

@ -46,6 +46,10 @@ impl BlockhashQueue {
self.last_hash.expect("no hash has been set")
}
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
pub fn get_fee_calculator(&self, hash: &Hash) -> Option<&FeeCalculator> {
self.ages.get(hash).map(|hash_age| &hash_age.fee_calculator)
}
@ -66,9 +70,8 @@ impl BlockhashQueue {
}
/// check if hash is valid
#[cfg(test)]
pub fn check_hash(&self, hash: Hash) -> bool {
self.ages.get(&hash).is_some()
pub fn check_hash(&self, hash: &Hash) -> bool {
self.ages.get(hash).is_some()
}
pub fn genesis_hash(&mut self, hash: &Hash, fee_calculator: &FeeCalculator) {
@ -148,9 +151,9 @@ mod tests {
fn test_register_hash() {
let last_hash = Hash::default();
let mut hash_queue = BlockhashQueue::new(100);
assert!(!hash_queue.check_hash(last_hash));
assert!(!hash_queue.check_hash(&last_hash));
hash_queue.register_hash(&last_hash, &FeeCalculator::default());
assert!(hash_queue.check_hash(last_hash));
assert!(hash_queue.check_hash(&last_hash));
assert_eq!(hash_queue.hash_height(), 1);
}
@ -163,12 +166,12 @@ mod tests {
hash_queue.register_hash(&last_hash, &FeeCalculator::default());
}
// Assert we're no longer able to use the oldest hash.
assert!(!hash_queue.check_hash(last_hash));
assert!(!hash_queue.check_hash(&last_hash));
assert_eq!(None, hash_queue.check_hash_age(&last_hash, 0));
// Assert we are not able to use the oldest remaining hash.
let last_valid_hash = hash(&serialize(&1).unwrap());
assert!(hash_queue.check_hash(last_valid_hash));
assert!(hash_queue.check_hash(&last_valid_hash));
assert_eq!(Some(false), hash_queue.check_hash_age(&last_valid_hash, 0));
}