harmonize percentage members (#5459)

* harmonize percentage members

* update tests

* update capitalization when burning fees

* verify capitalization in fee burn

* fixup
This commit is contained in:
Rob Walker 2019-08-09 13:58:46 -07:00 committed by GitHub
parent 07a049aa59
commit ed093f86f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 12 deletions

View File

@ -627,6 +627,7 @@ mod tests {
use crate::contact_info::ContactInfo;
use crate::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};
@ -958,7 +959,7 @@ mod tests {
let expected = json!({
"jsonrpc": "2.0",
"result": [ blockhash.to_string(), {
"burnPercent": 50,
"burnPercent": DEFAULT_BURN_PERCENT,
"lamportsPerSignature": 0,
"maxLamportsPerSignature": 0,
"minLamportsPerSignature": 0,

View File

@ -467,8 +467,11 @@ impl Bank {
let collector_fees = self.collector_fees.load(Ordering::Relaxed) as u64;
if collector_fees != 0 {
let (unburned, burned) = self.fee_calculator.burn(collector_fees);
// burn a portion of fees
self.deposit(&self.collector_id, self.fee_calculator.burn(collector_fees));
self.deposit(&self.collector_id, unburned);
self.capitalization
.fetch_sub(burned as usize, Ordering::Relaxed);
}
}
@ -1791,10 +1794,13 @@ mod tests {
genesis_block.fee_calculator.lamports_per_signature = 4; // something divisible by 2
let expected_fee_paid = genesis_block.fee_calculator.lamports_per_signature;
let expected_fee_collected = genesis_block.fee_calculator.burn(expected_fee_paid);
let (expected_fee_collected, expected_fee_burned) =
genesis_block.fee_calculator.burn(expected_fee_paid);
let mut bank = Bank::new(&genesis_block);
let capitalization = bank.capitalization();
let key = Keypair::new();
let tx = system_transaction::transfer(
&mint_keypair,
@ -1819,6 +1825,9 @@ mod tests {
initial_balance + expected_fee_collected
); // Leader collects fee after the bank is frozen
// verify capitalization
assert_eq!(capitalization - expected_fee_burned, bank.capitalization());
// Verify that an InstructionError collects fees, too
let mut bank = Bank::new_from_parent(&Arc::new(bank), &leader, 1);
let mut tx =
@ -1929,6 +1938,7 @@ mod tests {
+ bank
.fee_calculator
.burn(bank.fee_calculator.lamports_per_signature * 2)
.0
);
assert_eq!(results[0], Ok(()));
assert_eq!(results[1], Ok(()));

View File

@ -21,7 +21,7 @@ pub struct FeeCalculator {
pub min_lamports_per_signature: u64,
pub max_lamports_per_signature: u64,
// What portion of collected fees are to be destroyed, percentage-wise
// What portion of collected fees are to be destroyed, as a fraction of std::u8::MAX
pub burn_percent: u8,
}
@ -29,7 +29,7 @@ pub struct FeeCalculator {
pub const DEFAULT_TARGET_LAMPORTS_PER_SIGNATURE: u64 = 42;
pub const DEFAULT_TARGET_SIGNATURES_PER_SLOT: usize =
710_000 * DEFAULT_TICKS_PER_SLOT as usize / DEFAULT_NUM_TICKS_PER_SECOND as usize;
pub const DEFAULT_BURN_PERCENT: u8 = 50;
pub const DEFAULT_BURN_PERCENT: u8 = 127;
impl Default for FeeCalculator {
fn default() -> Self {
@ -127,9 +127,10 @@ impl FeeCalculator {
self.lamports_per_signature * u64::from(message.header.num_required_signatures)
}
/// calculate unburned fee from a fee total
pub fn burn(&self, fees: u64) -> u64 {
fees * u64::from(100 - self.burn_percent) / 100
/// calculate unburned fee from a fee total, returns (unburned, burned)
pub fn burn(&self, fees: u64) -> (u64, u64) {
let unburned = fees * u64::from(std::u8::MAX - self.burn_percent) / u64::from(std::u8::MAX);
(unburned, fees - unburned)
}
}
@ -143,13 +144,13 @@ mod tests {
fn test_fee_calculator_burn() {
let mut fee_calculator = FeeCalculator::default();
assert_eq!(fee_calculator.burn(2), 1);
assert_eq!(fee_calculator.burn(2), (1, 1));
fee_calculator.burn_percent = 0;
assert_eq!(fee_calculator.burn(2), 2);
fee_calculator.burn_percent = 100;
assert_eq!(fee_calculator.burn(2), 0);
assert_eq!(fee_calculator.burn(2), (2, 0));
fee_calculator.burn_percent = std::u8::MAX;
assert_eq!(fee_calculator.burn(2), (0, 2));
}
#[test]