Add fees to tx-wide caps (#22081)

This commit is contained in:
Jack May
2022-02-11 16:23:16 -08:00
committed by GitHub
parent 12dffc105a
commit 3d9874b95a
15 changed files with 1149 additions and 186 deletions

View File

@ -2,38 +2,49 @@
use {
crate::instruction::Instruction,
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
borsh::{BorshDeserialize, BorshSerialize},
};
crate::declare_id!("ComputeBudget111111111111111111111111111111");
/// Compute Budget Instructions
#[derive(
Serialize,
Deserialize,
BorshSerialize,
BorshDeserialize,
BorshSchema,
Debug,
Clone,
PartialEq,
AbiExample,
AbiEnumVisitor,
BorshDeserialize,
BorshSerialize,
Clone,
Debug,
Deserialize,
PartialEq,
Serialize,
)]
pub enum ComputeBudgetInstruction {
/// Request a specific maximum number of compute units the transaction is
/// allowed to consume.
RequestUnits(u32),
/// Request a specific transaction-wide program heap frame size in bytes.
/// The value requested must be a multiple of 1024. This new heap frame size
/// applies to each program executed, including all calls to CPIs.
/// allowed to consume and an additional fee to pay.
RequestUnits {
/// Units to request
units: u32,
/// Additional fee to add
additional_fee: u32,
},
/// Request a specific transaction-wide program heap region size in bytes.
/// The value requested must be a multiple of 1024. This new heap region
/// size applies to each program executed, including all calls to CPIs.
RequestHeapFrame(u32),
}
impl ComputeBudgetInstruction {
/// Create a `ComputeBudgetInstruction::RequestUnits` `Instruction`
pub fn request_units(units: u32) -> Instruction {
Instruction::new_with_borsh(id(), &ComputeBudgetInstruction::RequestUnits(units), vec![])
pub fn request_units(units: u32, additional_fee: u32) -> Instruction {
Instruction::new_with_borsh(
id(),
&ComputeBudgetInstruction::RequestUnits {
units,
additional_fee,
},
vec![],
)
}
/// Create a `ComputeBudgetInstruction::RequestHeapFrame` `Instruction`

67
sdk/src/fee.rs Normal file
View File

@ -0,0 +1,67 @@
use crate::native_token::sol_to_lamports;
/// A fee and its associated compute unit limit
#[derive(Debug, Default, Clone)]
pub struct FeeBin {
/// maximum compute units for which this fee will be charged
pub limit: u64,
/// fee in lamports
pub fee: u64,
}
/// Information used to calculate fees
#[derive(Debug, Clone)]
pub struct FeeStructure {
/// lamports per signature
pub lamports_per_signature: u64,
/// lamports_per_write_lock
pub lamports_per_write_lock: u64,
/// Compute unit fee bins
pub compute_fee_bins: Vec<FeeBin>,
}
impl FeeStructure {
pub fn new(
sol_per_signature: f64,
sol_per_write_lock: f64,
compute_fee_bins: Vec<(u64, f64)>,
) -> Self {
let compute_fee_bins = compute_fee_bins
.iter()
.map(|(limit, sol)| FeeBin {
limit: *limit,
fee: sol_to_lamports(*sol),
})
.collect::<Vec<_>>();
FeeStructure {
lamports_per_signature: sol_to_lamports(sol_per_signature),
lamports_per_write_lock: sol_to_lamports(sol_per_write_lock),
compute_fee_bins,
}
}
pub fn get_max_fee(&self, num_signatures: u64, num_write_locks: u64) -> u64 {
num_signatures
.saturating_mul(self.lamports_per_signature)
.saturating_add(num_write_locks.saturating_mul(self.lamports_per_write_lock))
.saturating_add(
self.compute_fee_bins
.last()
.map(|bin| bin.fee)
.unwrap_or_default(),
)
}
}
impl Default for FeeStructure {
fn default() -> Self {
Self::new(0.000005, 0.0, vec![(1_400_000, 0.0)])
}
}
#[cfg(RUSTC_WITH_SPECIALIZATION)]
impl ::solana_frozen_abi::abi_example::AbiExample for FeeStructure {
fn example() -> Self {
FeeStructure::default()
}
}

View File

@ -25,6 +25,7 @@ pub mod example_mocks;
pub mod exit;
pub mod feature;
pub mod feature_set;
pub mod fee;
pub mod genesis_config;
pub mod hard_forks;
pub mod hash;