Skip to content

Commit

Permalink
SVM: Clarify usage of lamports_per_signature (anza-xyz#3216)
Browse files Browse the repository at this point in the history
* SVM: use lps from `FeeStructure` directly

* SVM: rename `lamports_per_signature` to `blockhash_lamports_per_signature`

* program-runtime: rename `lamports_per_signature` to `blockhash_lamports_per_signature`
  • Loading branch information
buffalojoec authored Nov 8, 2024
1 parent 2025d85 commit 9624969
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 67 deletions.
8 changes: 4 additions & 4 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,27 @@ impl BpfAllocator {

pub struct EnvironmentConfig<'a> {
pub blockhash: Hash,
pub blockhash_lamports_per_signature: u64,
epoch_total_stake: Option<u64>,
epoch_vote_accounts: Option<&'a VoteAccountsHashMap>,
pub feature_set: Arc<FeatureSet>,
pub lamports_per_signature: u64,
sysvar_cache: &'a SysvarCache,
}
impl<'a> EnvironmentConfig<'a> {
pub fn new(
blockhash: Hash,
blockhash_lamports_per_signature: u64,
epoch_total_stake: Option<u64>,
epoch_vote_accounts: Option<&'a VoteAccountsHashMap>,
feature_set: Arc<FeatureSet>,
lamports_per_signature: u64,
sysvar_cache: &'a SysvarCache,
) -> Self {
Self {
blockhash,
blockhash_lamports_per_signature,
epoch_total_stake,
epoch_vote_accounts,
feature_set,
lamports_per_signature,
sysvar_cache,
}
}
Expand Down Expand Up @@ -764,10 +764,10 @@ macro_rules! with_mock_invoke_context {
});
let environment_config = EnvironmentConfig::new(
Hash::default(),
0,
None,
None,
Arc::new(FeatureSet::all_enabled()),
0,
&sysvar_cache,
);
let mut program_cache_for_tx_batch = ProgramCacheForTxBatch::default();
Expand Down
4 changes: 2 additions & 2 deletions programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4800,10 +4800,10 @@ mod tests {
with_mock_invoke_context!(invoke_context, transaction_context, vec![]);
invoke_context.environment_config = EnvironmentConfig::new(
Hash::default(),
0,
Some(expected_total_stake),
None, // Vote accounts are not needed for this test.
Arc::<FeatureSet>::default(),
0,
&sysvar_cache,
);

Expand Down Expand Up @@ -4854,10 +4854,10 @@ mod tests {
with_mock_invoke_context!(invoke_context, transaction_context, vec![]);
invoke_context.environment_config = EnvironmentConfig::new(
Hash::default(),
0,
None, // Total stake is not needed for this test.
Some(&vote_accounts_map),
Arc::<FeatureSet>::default(),
0,
&sysvar_cache,
);

Expand Down
45 changes: 33 additions & 12 deletions programs/system/src/system_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ pub fn advance_nonce_account(
let new_data = nonce::state::Data::new(
data.authority,
next_durable_nonce,
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
account.set_state(&Versions::new(State::Initialized(new_data)))
}
Expand Down Expand Up @@ -185,7 +187,9 @@ pub fn initialize_nonce_account(
let data = nonce::state::Data::new(
*nonce_authority,
durable_nonce,
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
let state = State::Initialized(data);
account.set_state(&Versions::new(state))
Expand Down Expand Up @@ -312,8 +316,9 @@ mod test {
($invoke_context:expr, $seed:expr) => {
$invoke_context.environment_config.blockhash =
hash(&bincode::serialize(&$seed).unwrap());
$invoke_context.environment_config.lamports_per_signature =
($seed as u64).saturating_mul(100);
$invoke_context
.environment_config
.blockhash_lamports_per_signature = ($seed as u64).saturating_mul(100);
};
}

Expand Down Expand Up @@ -350,7 +355,9 @@ mod test {
let data = nonce::state::Data::new(
data.authority,
DurableNonce::from_blockhash(&invoke_context.environment_config.blockhash),
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
// First nonce instruction drives state from Uninitialized to Initialized
assert_eq!(versions.state(), &State::Initialized(data.clone()));
Expand All @@ -360,7 +367,9 @@ mod test {
let data = nonce::state::Data::new(
data.authority,
DurableNonce::from_blockhash(&invoke_context.environment_config.blockhash),
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
// Second nonce instruction consumes and replaces stored nonce
assert_eq!(versions.state(), &State::Initialized(data.clone()));
Expand All @@ -370,7 +379,9 @@ mod test {
let data = nonce::state::Data::new(
data.authority,
DurableNonce::from_blockhash(&invoke_context.environment_config.blockhash),
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
// Third nonce instruction for fun and profit
assert_eq!(versions.state(), &State::Initialized(data));
Expand Down Expand Up @@ -429,7 +440,9 @@ mod test {
let data = nonce::state::Data::new(
authority,
DurableNonce::from_blockhash(&invoke_context.environment_config.blockhash),
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
assert_eq!(versions.state(), &State::Initialized(data));
// Nonce account did not sign
Expand Down Expand Up @@ -741,7 +754,9 @@ mod test {
let data = nonce::state::Data::new(
authority,
DurableNonce::from_blockhash(&invoke_context.environment_config.blockhash),
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
assert_eq!(versions.state(), &State::Initialized(data.clone()));
let withdraw_lamports = 42;
Expand Down Expand Up @@ -770,7 +785,9 @@ mod test {
let data = nonce::state::Data::new(
data.authority,
DurableNonce::from_blockhash(&invoke_context.environment_config.blockhash),
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
assert_eq!(versions.state(), &State::Initialized(data));
assert_eq!(nonce_account.get_lamports(), from_expect_lamports);
Expand Down Expand Up @@ -962,7 +979,9 @@ mod test {
let data = nonce::state::Data::new(
authorized,
DurableNonce::from_blockhash(&invoke_context.environment_config.blockhash),
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
assert_eq!(result, Ok(()));
let versions = nonce_account.get_state::<Versions>().unwrap();
Expand Down Expand Up @@ -1031,7 +1050,9 @@ mod test {
let data = nonce::state::Data::new(
authority,
DurableNonce::from_blockhash(&invoke_context.environment_config.blockhash),
invoke_context.environment_config.lamports_per_signature,
invoke_context
.environment_config
.blockhash_lamports_per_signature,
);
authorize_nonce_account(&mut nonce_account, &authority, &signers, &invoke_context).unwrap();
let versions = nonce_account.get_state::<Versions>().unwrap();
Expand Down
7 changes: 4 additions & 3 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3709,16 +3709,17 @@ impl Bank {
));
timings.saturating_add_in_place(ExecuteTimingType::CheckUs, check_us);

let (blockhash, lamports_per_signature) = self.last_blockhash_and_lamports_per_signature();
let (blockhash, blockhash_lamports_per_signature) =
self.last_blockhash_and_lamports_per_signature();
let rent_collector_with_metrics =
RentCollectorWithMetrics::new(self.rent_collector.clone());
let processing_environment = TransactionProcessingEnvironment {
blockhash,
blockhash_lamports_per_signature,
epoch_total_stake: Some(self.get_current_epoch_total_stake()),
epoch_vote_accounts: Some(self.get_current_epoch_vote_accounts()),
feature_set: Arc::clone(&self.feature_set),
fee_structure: Some(&self.fee_structure),
lamports_per_signature,
fee_lamports_per_signature: self.fee_structure.lamports_per_signature,
rent_collector: Some(&rent_collector_with_metrics),
};

Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bank/builtins/core_bpf_migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ impl Bank {
&mut program_cache_for_tx_batch,
EnvironmentConfig::new(
Hash::default(),
0,
None,
None,
self.feature_set.clone(),
0,
&sysvar_cache,
),
None,
Expand Down
4 changes: 2 additions & 2 deletions svm/examples/json-rpc/server/src/rpc_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,11 @@ impl JsonRpcRequestProcessor {
let (blockhash, lamports_per_signature) = self.last_blockhash_and_lamports_per_signature();
let processing_environment = TransactionProcessingEnvironment {
blockhash,
blockhash_lamports_per_signature: lamports_per_signature,
epoch_total_stake: self.epoch_total_stake(Epoch::default()),
epoch_vote_accounts: self.epoch_vote_accounts(Epoch::default()),
feature_set: Arc::clone(&bank.feature_set),
fee_structure: None,
lamports_per_signature,
fee_lamports_per_signature: lamports_per_signature,
rent_collector: None,
};

Expand Down
10 changes: 6 additions & 4 deletions svm/examples/paytube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ impl PayTubeChannel {
let compute_budget = ComputeBudget::default();
let feature_set = FeatureSet::all_enabled();
let fee_structure = FeeStructure::default();
let lamports_per_signature = fee_structure.lamports_per_signature;
let rent_collector = RentCollector::default();

// PayTube loader/callback implementation.
Expand Down Expand Up @@ -148,11 +147,11 @@ impl PayTubeChannel {
// Again, these can be configurable or hoisted from the cluster.
let processing_environment = TransactionProcessingEnvironment {
blockhash: Hash::default(),
blockhash_lamports_per_signature: fee_structure.lamports_per_signature,
epoch_total_stake: None,
epoch_vote_accounts: None,
feature_set: Arc::new(feature_set),
fee_structure: Some(&fee_structure),
lamports_per_signature,
fee_lamports_per_signature: fee_structure.lamports_per_signature,
rent_collector: Some(&rent_collector),
};

Expand All @@ -177,7 +176,10 @@ impl PayTubeChannel {
let results = processor.load_and_execute_sanitized_transactions(
&account_loader,
&svm_transactions,
get_transaction_check_results(svm_transactions.len(), lamports_per_signature),
get_transaction_check_results(
svm_transactions.len(),
fee_structure.lamports_per_signature,
),
&processing_environment,
&processing_config,
);
Expand Down
14 changes: 7 additions & 7 deletions svm/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ mod tests {
let sysvar_cache = SysvarCache::default();
let environment_config = EnvironmentConfig::new(
Hash::default(),
0,
None,
None,
Arc::new(FeatureSet::all_enabled()),
0,
&sysvar_cache,
);
let mut invoke_context = InvokeContext::new(
Expand Down Expand Up @@ -311,10 +311,10 @@ mod tests {
));
let environment_config = EnvironmentConfig::new(
Hash::default(),
0,
None,
None,
Arc::new(FeatureSet::all_enabled()),
0,
&sysvar_cache,
);
let mut invoke_context = InvokeContext::new(
Expand Down Expand Up @@ -355,10 +355,10 @@ mod tests {
));
let environment_config = EnvironmentConfig::new(
Hash::default(),
0,
None,
None,
Arc::new(FeatureSet::all_enabled()),
0,
&sysvar_cache,
);
let mut invoke_context = InvokeContext::new(
Expand Down Expand Up @@ -490,10 +490,10 @@ mod tests {
let sysvar_cache = SysvarCache::default();
let environment_config = EnvironmentConfig::new(
Hash::default(),
0,
None,
None,
Arc::new(FeatureSet::all_enabled()),
0,
&sysvar_cache,
);
let mut invoke_context = InvokeContext::new(
Expand Down Expand Up @@ -529,10 +529,10 @@ mod tests {
));
let environment_config = EnvironmentConfig::new(
Hash::default(),
0,
None,
None,
Arc::new(FeatureSet::all_enabled()),
0,
&sysvar_cache,
);
let mut invoke_context = InvokeContext::new(
Expand Down Expand Up @@ -565,10 +565,10 @@ mod tests {
));
let environment_config = EnvironmentConfig::new(
Hash::default(),
0,
None,
None,
Arc::new(FeatureSet::all_enabled()),
0,
&sysvar_cache,
);
let mut invoke_context = InvokeContext::new(
Expand Down Expand Up @@ -662,10 +662,10 @@ mod tests {
);
let environment_config = EnvironmentConfig::new(
Hash::default(),
0,
None,
None,
Arc::new(FeatureSet::all_enabled()),
0,
&sysvar_cache,
);
let mut invoke_context = InvokeContext::new(
Expand Down
Loading

0 comments on commit 9624969

Please sign in to comment.