Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions crates/litesvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ much easier.

#[cfg(feature = "nodejs-internal")]
use qualifier_attr::qualifiers;
use solana_clock::MAX_RECENT_BLOCKHASHES;
#[allow(deprecated)]
use solana_sysvar::recent_blockhashes::IterItem;
#[allow(deprecated)]
Expand Down Expand Up @@ -1234,11 +1235,27 @@ impl LiteSVM {
pub fn expire_blockhash(&mut self) {
self.latest_blockhash = create_blockhash(&self.latest_blockhash.to_bytes());
#[allow(deprecated)]
self.set_sysvar(&RecentBlockhashes::from_iter([IterItem(
0,
&self.latest_blockhash,
self.fee_structure.lamports_per_signature,
)]));
{
let blockhashes = self.get_sysvar::<RecentBlockhashes>();
let mut entries = vec![];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to init with capacity, something like this:

let max_entries_len = blockhashes.len().min(MAX_RECENT_BLOCKHASHES);
let mut entries = Vec::with_capacity(max_entries_len);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should initialise it in LiteSVM::new and store it as a field on the LiteSVM struct

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aursen @kevinheavey, I've implemented Aursen's suggested change and run cargo fmt. I'm not quite understanding Kevin's suggestion - is that something either of you will have time to knock out?

entries.push(IterItem(
0,
&self.latest_blockhash,
self.fee_structure.lamports_per_signature,
));
for (i, entry) in blockhashes.iter().enumerate() {
if i == MAX_RECENT_BLOCKHASHES - 1 {
break;
}
entries.push(IterItem(
i as u64 + 1,
&entry.blockhash,
entry.fee_calculator.lamports_per_signature,
));
}

self.set_sysvar(&RecentBlockhashes::from_iter(entries));
}
}

/// Warps the clock to the specified slot.
Expand Down Expand Up @@ -1275,7 +1292,7 @@ impl LiteSVM {
tx: &SanitizedTransaction,
) -> solana_transaction_error::TransactionResult<()> {
let recent_blockhash = tx.message().recent_blockhash();
if recent_blockhash == &self.latest_blockhash
if self.check_blockhash_is_recent(recent_blockhash)
|| self.check_transaction_for_nonce(
tx,
&DurableNonce::from_blockhash(&self.latest_blockhash),
Expand All @@ -1292,6 +1309,13 @@ impl LiteSVM {
}
}

fn check_blockhash_is_recent(&self, recent_blockhash: &Hash) -> bool {
#[allow(deprecated)]
self.get_sysvar::<RecentBlockhashes>()
.iter()
.any(|entry| entry.blockhash == *recent_blockhash)
}

fn check_message_for_nonce(&self, message: &SanitizedMessage) -> bool {
message
.get_durable_nonce()
Expand Down
Loading