Update fee api to use blockhash (#21054)

This commit is contained in:
Jack May
2021-10-29 13:52:59 -07:00
committed by GitHub
parent aea3c66fa8
commit bced07a099
16 changed files with 142 additions and 93 deletions

View File

@ -3118,8 +3118,11 @@ impl Bank {
&self.fee_rate_governor
}
pub fn get_fee_for_message(&self, message: &SanitizedMessage) -> u64 {
Self::calculate_fee(message, self.fee_rate_governor.lamports_per_signature)
pub fn get_fee_for_message(&self, message: &SanitizedMessage) -> Option<u64> {
let blockhash_queue = self.blockhash_queue.read().unwrap();
let lamports_per_signature =
blockhash_queue.get_lamports_per_signature(message.recent_blockhash())?;
Some(Self::calculate_fee(message, lamports_per_signature))
}
pub fn get_fee_for_message_with_lamports_per_signature(
@ -11022,8 +11025,12 @@ pub(crate) mod tests {
assert_eq!(bank.process_transaction(&durable_tx), Ok(()));
/* Check balances */
let mut expected_balance =
4_650_000 - bank.get_fee_for_message(&durable_tx.message.try_into().unwrap());
let mut recent_message = durable_tx.message;
recent_message.recent_blockhash = bank.last_blockhash();
let mut expected_balance = 4_650_000
- bank
.get_fee_for_message(&recent_message.try_into().unwrap())
.unwrap();
assert_eq!(bank.get_balance(&custodian_pubkey), expected_balance);
assert_eq!(bank.get_balance(&nonce_pubkey), 250_000);
assert_eq!(bank.get_balance(&alice_pubkey), 100_000);
@ -11075,8 +11082,11 @@ pub(crate) mod tests {
))
);
/* Check fee charged and nonce has advanced */
let mut recent_message = durable_tx.message.clone();
recent_message.recent_blockhash = bank.last_blockhash();
expected_balance -= bank
.get_fee_for_message(&SanitizedMessage::try_from(durable_tx.message.clone()).unwrap());
.get_fee_for_message(&SanitizedMessage::try_from(recent_message).unwrap())
.unwrap();
assert_eq!(bank.get_balance(&custodian_pubkey), expected_balance);
assert_ne!(nonce_hash, get_nonce_account(&bank, &nonce_pubkey).unwrap());
/* Confirm replaying a TX that failed with InstructionError::* now
@ -11135,10 +11145,14 @@ pub(crate) mod tests {
))
);
/* Check fee charged and nonce has *not* advanced */
let mut recent_message = durable_tx.message;
recent_message.recent_blockhash = bank.last_blockhash();
assert_eq!(
bank.get_balance(&custodian_pubkey),
initial_custodian_balance
- bank.get_fee_for_message(&durable_tx.message.try_into().unwrap())
- bank
.get_fee_for_message(&recent_message.try_into().unwrap())
.unwrap()
);
assert_eq!(nonce_hash, get_nonce_account(&bank, &nonce_pubkey).unwrap());
}
@ -11185,10 +11199,14 @@ pub(crate) mod tests {
))
);
/* Check fee charged and nonce has advanced */
let mut recent_message = durable_tx.message;
recent_message.recent_blockhash = bank.last_blockhash();
assert_eq!(
bank.get_balance(&nonce_pubkey),
nonce_starting_balance
- bank.get_fee_for_message(&durable_tx.message.try_into().unwrap())
- bank
.get_fee_for_message(&recent_message.try_into().unwrap())
.unwrap()
);
assert_ne!(nonce_hash, get_nonce_account(&bank, &nonce_pubkey).unwrap());
}

View File

@ -314,26 +314,16 @@ impl SyncClient for BankClient {
fn get_fee_for_message(&self, message: &Message) -> Result<u64> {
SanitizedMessage::try_from(message.clone())
.map(|message| self.bank.get_fee_for_message(&message))
.map_err(|_| {
.ok()
.map(|sanitized_message| self.bank.get_fee_for_message(&sanitized_message))
.flatten()
.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

@ -3042,18 +3042,19 @@ mod tests {
let slot = slot + 1;
let bank2 = Arc::new(Bank::new_from_parent(&bank1, &collector, slot));
let blockhash = bank2.last_blockhash();
let tx = SanitizedTransaction::from_transaction_for_tests(system_transaction::transfer(
&key1,
&key2.pubkey(),
lamports_to_transfer,
bank2.last_blockhash(),
blockhash,
));
let fee = bank2.get_fee_for_message(tx.message());
let fee = bank2.get_fee_for_message(tx.message()).unwrap();
let tx = system_transaction::transfer(
&key1,
&key2.pubkey(),
lamports_to_transfer - fee,
bank2.last_blockhash(),
blockhash,
);
bank2.process_transaction(&tx).unwrap();
assert_eq!(