2021-08-17 15:17:56 -07:00
|
|
|
#![cfg(feature = "full")]
|
|
|
|
|
|
|
|
use {
|
|
|
|
crate::{
|
|
|
|
hash::Hash,
|
2021-12-10 08:04:04 -08:00
|
|
|
message::{
|
|
|
|
v0::{self, LoadedAddresses},
|
|
|
|
SanitizedMessage, VersionedMessage,
|
|
|
|
},
|
2021-08-17 15:17:56 -07:00
|
|
|
nonce::NONCED_TX_MARKER_IX_INDEX,
|
2021-09-17 11:36:57 -07:00
|
|
|
precompiles::verify_if_precompile,
|
2021-08-17 15:17:56 -07:00
|
|
|
program_utils::limited_deserialize,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
sanitize::Sanitize,
|
|
|
|
signature::Signature,
|
2021-08-30 00:58:45 -07:00
|
|
|
solana_sdk::feature_set,
|
2021-08-17 15:17:56 -07:00
|
|
|
transaction::{Result, Transaction, TransactionError, VersionedTransaction},
|
|
|
|
},
|
|
|
|
solana_program::{system_instruction::SystemInstruction, system_program},
|
2021-08-30 00:58:45 -07:00
|
|
|
std::sync::Arc,
|
2021-08-17 15:17:56 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Sanitized transaction and the hash of its message
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct SanitizedTransaction {
|
|
|
|
message: SanitizedMessage,
|
|
|
|
message_hash: Hash,
|
2021-11-03 02:02:48 -07:00
|
|
|
is_simple_vote_tx: bool,
|
2021-08-17 15:17:56 -07:00
|
|
|
signatures: Vec<Signature>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set of accounts that must be locked for safe transaction processing
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct TransactionAccountLocks<'a> {
|
|
|
|
/// List of readonly account key locks
|
|
|
|
pub readonly: Vec<&'a Pubkey>,
|
|
|
|
/// List of writable account key locks
|
|
|
|
pub writable: Vec<&'a Pubkey>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SanitizedTransaction {
|
|
|
|
/// Create a sanitized transaction from an unsanitized transaction.
|
2021-12-10 08:04:04 -08:00
|
|
|
/// If the input transaction uses address tables, attempt to lookup
|
|
|
|
/// the address for each table index.
|
2021-08-17 15:17:56 -07:00
|
|
|
pub fn try_create(
|
|
|
|
tx: VersionedTransaction,
|
|
|
|
message_hash: Hash,
|
2021-11-03 02:02:48 -07:00
|
|
|
is_simple_vote_tx: Option<bool>,
|
2021-12-10 08:04:04 -08:00
|
|
|
address_loader: impl Fn(&v0::Message) -> Result<LoadedAddresses>,
|
2021-08-17 15:17:56 -07:00
|
|
|
) -> Result<Self> {
|
|
|
|
tx.sanitize()?;
|
|
|
|
|
|
|
|
let signatures = tx.signatures;
|
|
|
|
let message = match tx.message {
|
|
|
|
VersionedMessage::Legacy(message) => SanitizedMessage::Legacy(message),
|
2021-12-10 08:04:04 -08:00
|
|
|
VersionedMessage::V0(message) => SanitizedMessage::V0(v0::LoadedMessage {
|
|
|
|
loaded_addresses: address_loader(&message)?,
|
2021-08-17 15:17:56 -07:00
|
|
|
message,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
if message.has_duplicates() {
|
|
|
|
return Err(TransactionError::AccountLoadedTwice);
|
|
|
|
}
|
|
|
|
|
2021-11-03 02:02:48 -07:00
|
|
|
let is_simple_vote_tx = is_simple_vote_tx.unwrap_or_else(|| {
|
|
|
|
let mut ix_iter = message.program_instructions_iter();
|
|
|
|
ix_iter.next().map(|(program_id, _ix)| program_id) == Some(&crate::vote::program::id())
|
|
|
|
});
|
|
|
|
|
2021-08-17 15:17:56 -07:00
|
|
|
Ok(Self {
|
|
|
|
message,
|
|
|
|
message_hash,
|
2021-11-03 02:02:48 -07:00
|
|
|
is_simple_vote_tx,
|
2021-08-17 15:17:56 -07:00
|
|
|
signatures,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-28 11:25:46 -08:00
|
|
|
pub fn try_from_legacy_transaction(tx: Transaction) -> Result<Self> {
|
|
|
|
tx.sanitize()?;
|
2021-10-27 10:09:16 -07:00
|
|
|
|
|
|
|
if tx.message.has_duplicates() {
|
2021-12-28 11:25:46 -08:00
|
|
|
return Err(TransactionError::AccountLoadedTwice);
|
2021-10-27 10:09:16 -07:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:25:46 -08:00
|
|
|
Ok(Self {
|
2021-10-27 10:09:16 -07:00
|
|
|
message_hash: tx.message.hash(),
|
|
|
|
message: SanitizedMessage::Legacy(tx.message),
|
2021-11-03 02:02:48 -07:00
|
|
|
is_simple_vote_tx: false,
|
2021-10-27 10:09:16 -07:00
|
|
|
signatures: tx.signatures,
|
2021-12-28 11:25:46 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a sanitized transaction from a legacy transaction. Used for tests only.
|
|
|
|
pub fn from_transaction_for_tests(tx: Transaction) -> Self {
|
|
|
|
Self::try_from_legacy_transaction(tx).unwrap()
|
2021-10-27 10:09:16 -07:00
|
|
|
}
|
|
|
|
|
2021-08-17 15:17:56 -07:00
|
|
|
/// Return the first signature for this transaction.
|
|
|
|
///
|
|
|
|
/// Notes:
|
|
|
|
///
|
|
|
|
/// Sanitized transactions must have at least one signature because the
|
|
|
|
/// number of signatures must be greater than or equal to the message header
|
|
|
|
/// value `num_required_signatures` which must be greater than 0 itself.
|
|
|
|
pub fn signature(&self) -> &Signature {
|
|
|
|
&self.signatures[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the list of signatures for this transaction
|
|
|
|
pub fn signatures(&self) -> &[Signature] {
|
|
|
|
&self.signatures
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the signed message
|
|
|
|
pub fn message(&self) -> &SanitizedMessage {
|
|
|
|
&self.message
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the hash of the signed message
|
|
|
|
pub fn message_hash(&self) -> &Hash {
|
|
|
|
&self.message_hash
|
|
|
|
}
|
|
|
|
|
2021-11-03 02:02:48 -07:00
|
|
|
/// Returns true if this transaction is a simple vote
|
|
|
|
pub fn is_simple_vote_transaction(&self) -> bool {
|
|
|
|
self.is_simple_vote_tx
|
|
|
|
}
|
|
|
|
|
2021-08-17 15:17:56 -07:00
|
|
|
/// Convert this sanitized transaction into a versioned transaction for
|
|
|
|
/// recording in the ledger.
|
|
|
|
pub fn to_versioned_transaction(&self) -> VersionedTransaction {
|
|
|
|
let signatures = self.signatures.clone();
|
|
|
|
match &self.message {
|
2021-12-10 08:04:04 -08:00
|
|
|
SanitizedMessage::V0(sanitized_msg) => VersionedTransaction {
|
2021-08-17 15:17:56 -07:00
|
|
|
signatures,
|
2021-12-10 08:04:04 -08:00
|
|
|
message: VersionedMessage::V0(sanitized_msg.message.clone()),
|
2021-08-17 15:17:56 -07:00
|
|
|
},
|
|
|
|
SanitizedMessage::Legacy(message) => VersionedTransaction {
|
|
|
|
signatures,
|
|
|
|
message: VersionedMessage::Legacy(message.clone()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the list of accounts that must be locked during processing this transaction.
|
2021-12-16 14:27:22 -08:00
|
|
|
pub fn get_account_locks(&self) -> TransactionAccountLocks {
|
2021-08-17 15:17:56 -07:00
|
|
|
let message = &self.message;
|
|
|
|
let num_readonly_accounts = message.num_readonly_accounts();
|
|
|
|
let num_writable_accounts = message
|
|
|
|
.account_keys_len()
|
|
|
|
.saturating_sub(num_readonly_accounts);
|
|
|
|
|
|
|
|
let mut account_locks = TransactionAccountLocks {
|
|
|
|
writable: Vec::with_capacity(num_writable_accounts),
|
|
|
|
readonly: Vec::with_capacity(num_readonly_accounts),
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i, key) in message.account_keys_iter().enumerate() {
|
2021-12-16 14:27:22 -08:00
|
|
|
if message.is_writable(i) {
|
2021-08-17 15:17:56 -07:00
|
|
|
account_locks.writable.push(key);
|
|
|
|
} else {
|
|
|
|
account_locks.readonly.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
account_locks
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the transaction uses a durable nonce, return the pubkey of the nonce account
|
2021-11-16 15:01:00 -08:00
|
|
|
pub fn get_durable_nonce(&self, nonce_must_be_writable: bool) -> Option<&Pubkey> {
|
2021-08-17 15:17:56 -07:00
|
|
|
self.message
|
|
|
|
.instructions()
|
|
|
|
.get(NONCED_TX_MARKER_IX_INDEX as usize)
|
|
|
|
.filter(
|
|
|
|
|ix| match self.message.get_account_key(ix.program_id_index as usize) {
|
|
|
|
Some(program_id) => system_program::check_id(program_id),
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.filter(|ix| {
|
|
|
|
matches!(
|
|
|
|
limited_deserialize(&ix.data),
|
|
|
|
Ok(SystemInstruction::AdvanceNonceAccount)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.and_then(|ix| {
|
|
|
|
ix.accounts.get(0).and_then(|idx| {
|
|
|
|
let idx = *idx as usize;
|
2021-12-16 14:27:22 -08:00
|
|
|
if nonce_must_be_writable && !self.message.is_writable(idx) {
|
2021-11-16 15:01:00 -08:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.message.get_account_key(idx)
|
|
|
|
}
|
2021-08-17 15:17:56 -07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the serialized message data to sign.
|
|
|
|
fn message_data(&self) -> Vec<u8> {
|
|
|
|
match &self.message {
|
|
|
|
SanitizedMessage::Legacy(message) => message.serialize(),
|
2021-12-10 08:04:04 -08:00
|
|
|
SanitizedMessage::V0(message) => message.serialize(),
|
2021-08-17 15:17:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify the transaction signatures
|
|
|
|
pub fn verify(&self) -> Result<()> {
|
|
|
|
let message_bytes = self.message_data();
|
|
|
|
if self
|
|
|
|
.signatures
|
|
|
|
.iter()
|
|
|
|
.zip(self.message.account_keys_iter())
|
|
|
|
.map(|(signature, pubkey)| signature.verify(pubkey.as_ref(), &message_bytes))
|
|
|
|
.any(|verified| !verified)
|
|
|
|
{
|
|
|
|
Err(TransactionError::SignatureFailure)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:36:57 -07:00
|
|
|
/// Verify the precompiled programs in this transaction
|
2021-08-30 00:58:45 -07:00
|
|
|
pub fn verify_precompiles(&self, feature_set: &Arc<feature_set::FeatureSet>) -> Result<()> {
|
2021-08-17 15:17:56 -07:00
|
|
|
for (program_id, instruction) in self.message.program_instructions_iter() {
|
2021-09-17 11:36:57 -07:00
|
|
|
verify_if_precompile(
|
|
|
|
program_id,
|
|
|
|
instruction,
|
|
|
|
self.message().instructions(),
|
|
|
|
feature_set,
|
|
|
|
)
|
|
|
|
.map_err(|_| TransactionError::InvalidAccountIndex)?;
|
2021-08-17 15:17:56 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|