From 003ce8a9718669b61ff10bfab7384886956c9dc4 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Fri, 9 Aug 2024 14:23:49 +0200 Subject: [PATCH] Implement Deserialize for transaction --- src/transactions.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/transactions.rs b/src/transactions.rs index 98d0d54..d3a61cc 100644 --- a/src/transactions.rs +++ b/src/transactions.rs @@ -125,7 +125,7 @@ pub struct TransactionSignature { ImplHashID!(TransactionID, "txn"); -#[derive(Default, Debug, Clone, Serialize)] +#[derive(Default, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Transaction { pub siacoin_inputs: Vec, @@ -1026,4 +1026,41 @@ mod tests { assert_eq!(sig.signature.to_string(), expected) } } + + #[test] + fn test_serialize_transaction() { + let transaction = Transaction { + siacoin_inputs: Vec::new(), + siacoin_outputs: Vec::new(), + file_contracts: Vec::new(), + file_contract_revisions: Vec::new(), + storage_proofs: Vec::new(), + siafund_inputs: Vec::new(), + siafund_outputs: Vec::new(), + miner_fees: Vec::new(), + arbitrary_data: Vec::new(), + signatures: Vec::new(), + }; + + // binary + let transaction_serialized = to_bytes(&transaction).unwrap(); + let transaction_deserialized: Transaction = + from_reader(&mut &transaction_serialized[..]).unwrap(); + assert_eq!( + transaction_serialized, + [ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + ] + ); + assert_eq!(transaction_deserialized, transaction); + + // json + let transaction_serialized = serde_json::to_string(&transaction).unwrap(); + let transaction_deserialized: Transaction = + serde_json::from_str(&transaction_serialized).unwrap(); + assert_eq!(transaction_serialized, "{\"siacoinInputs\":[],\"siacoinOutputs\":[],\"fileContracts\":[],\"fileContractRevisions\":[],\"storageProofs\":[],\"siafundInputs\":[],\"siafundOutputs\":[],\"minerFees\":[],\"arbitraryData\":[],\"signatures\":[]}"); + assert_eq!(transaction_deserialized, transaction); + } }