Skip to content

Commit 7195745

Browse files
dev-jodeepkxro
andauthored
temp: Graceful error for versioned txns, until we have it implemented (#98)
* Graceful error for versioned txns, until we have it implemented * Fixed error message --------- Co-authored-by: pkxro <[email protected]>
1 parent 2b90a3e commit 7195745

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

crates/lib/src/transaction/transaction.rs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use solana_client::nonblocking::rpc_client::RpcClient;
22
use solana_sdk::{
33
commitment_config::CommitmentConfig,
44
instruction::{AccountMeta, CompiledInstruction, Instruction},
5-
message::Message,
5+
message::{Message, VersionedMessage},
66
pubkey::Pubkey,
7-
transaction::Transaction,
7+
transaction::{Transaction, VersionedTransaction},
88
};
99

1010
use crate::{
@@ -89,33 +89,46 @@ pub async fn sign_and_send_transaction(
8989
}
9090

9191
pub fn encode_b64_transaction(transaction: &Transaction) -> Result<String, KoraError> {
92-
let serialized = bincode::serialize(transaction).map_err(|e| {
93-
KoraError::SerializationError(format!("Base64 serialization failed: {}", e))
94-
})?;
92+
let serialized = bincode::serialize(transaction)
93+
.map_err(|e| KoraError::SerializationError(format!("Base64 serialization failed: {e}")))?;
9594
Ok(STANDARD.encode(serialized))
9695
}
9796

9897
pub fn encode_b64_message(message: &Message) -> Result<String, KoraError> {
99-
let serialized = bincode::serialize(message).map_err(|e| {
100-
KoraError::SerializationError(format!("Base64 serialization failed: {}", e))
101-
})?;
98+
let serialized = bincode::serialize(message)
99+
.map_err(|e| KoraError::SerializationError(format!("Base64 serialization failed: {e}")))?;
102100
Ok(STANDARD.encode(serialized))
103101
}
104102

105103
pub fn decode_b64_transaction(encoded: &str) -> Result<Transaction, KoraError> {
106104
let decoded = STANDARD.decode(encoded).map_err(|e| {
107-
KoraError::InvalidTransaction(format!("Failed to decode base64 transaction: {}", e))
105+
KoraError::InvalidTransaction(format!("Failed to decode base64 transaction: {e}"))
108106
})?;
109107

108+
// For now we don't support versioned transactions, will be added in the future (where it checks the addresses in the lookup tables)
109+
if let Ok(versioned_tx) = bincode::deserialize::<VersionedTransaction>(&decoded) {
110+
if let VersionedMessage::V0(_) = versioned_tx.message {
111+
return Err(KoraError::InvalidTransaction(
112+
"Versioned transaction not supported".to_string(),
113+
));
114+
}
115+
}
116+
110117
bincode::deserialize(&decoded).map_err(|e| {
111-
KoraError::InvalidTransaction(format!("Failed to deserialize transaction: {}", e))
118+
KoraError::InvalidTransaction(format!("Failed to deserialize transaction: {e}"))
112119
})
113120
}
114121

115122
#[cfg(test)]
116123
mod tests {
117124
use super::*;
118-
use solana_sdk::{hash::Hash, message::Message, signature::Keypair, signer::Signer as _};
125+
use solana_sdk::{
126+
hash::Hash,
127+
message::{v0::Message as V0Message, Message, VersionedMessage},
128+
signature::Keypair,
129+
signer::Signer as _,
130+
transaction::VersionedTransaction,
131+
};
119132

120133
#[test]
121134
fn test_encode_decode_b64_transaction() {
@@ -184,4 +197,30 @@ mod tests {
184197
assert_eq!(uncompiled.accounts[1].pubkey, account2);
185198
assert_eq!(uncompiled.data, vec![1, 2, 3]);
186199
}
200+
201+
#[test]
202+
fn test_encode_decode_b64_versioned_transaction_unsupported() {
203+
let keypair = Keypair::new();
204+
let instruction = Instruction::new_with_bytes(
205+
Pubkey::new_unique(),
206+
&[1, 2, 3],
207+
vec![AccountMeta::new(keypair.pubkey(), true)],
208+
);
209+
let tx = VersionedTransaction::try_new(
210+
VersionedMessage::V0(
211+
V0Message::try_compile(&keypair.pubkey(), &[instruction], &[], Hash::default())
212+
.unwrap(),
213+
),
214+
&[&keypair],
215+
)
216+
.unwrap();
217+
218+
let serialized = bincode::serialize(&tx).unwrap();
219+
let encoded = STANDARD.encode(serialized);
220+
221+
assert!(matches!(
222+
decode_b64_transaction(&encoded),
223+
Err(KoraError::InvalidTransaction(e)) if e == "Versioned transaction not supported"
224+
));
225+
}
187226
}

0 commit comments

Comments
 (0)