From 6765c854c1b180e1778e6e9eeabfa663cb18966c Mon Sep 17 00:00:00 2001 From: surangap Date: Tue, 4 Oct 2022 18:05:40 +0530 Subject: [PATCH] Add SignerListSet txn serialize test --- codec/src/field.rs | 22 ++++++++++----------- codec/src/transaction.rs | 41 +++++++++++++++++++++++++++++++++++++--- codec/src/types.rs | 22 ++++++++++----------- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/codec/src/field.rs b/codec/src/field.rs index 04d6ed7..7aadff0 100644 --- a/codec/src/field.rs +++ b/codec/src/field.rs @@ -12,13 +12,13 @@ use crate::{ // TODO: auto-generate the structs from definitions.json -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct Account(pub AccountIdType); -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct Destination(pub AccountIdType); -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct TransactionType(pub UInt16Type); impl From for TransactionType { fn from(v: TransactionTypeCode) -> Self { @@ -26,34 +26,34 @@ impl From for TransactionType { } } -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct Fee(pub AmountType); -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct Flags(pub UInt32Type); -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct Sequence(pub UInt32Type); #[derive(Field, Debug, Default)] pub struct SigningPubKey(pub BlobType); -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct Amount(pub AmountType); #[derive(Field, Debug, Default)] pub struct TxnSignature(pub BlobType); -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct SignerQuorum(pub UInt32Type); -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct SignerWeight(pub UInt16Type); -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct SignerEntry(pub SignerEntryType); -#[derive(Field, Debug)] +#[derive(Field, Debug, Clone)] pub struct SignerEntries(pub STArrayType); impl BinarySerialize for T { diff --git a/codec/src/transaction.rs b/codec/src/transaction.rs index 1d75aa6..1ab8e21 100644 --- a/codec/src/transaction.rs +++ b/codec/src/transaction.rs @@ -151,7 +151,7 @@ impl SignerListSet { account: [u8; 20], fee: u64, signer_quorum: u32, - signer_entries: Vec::, + signer_entries: Vec, signing_pub_key: Option<[u8; 33]>, ) -> Self { Self { @@ -160,7 +160,6 @@ impl SignerListSet { fee: Fee(AmountType(fee)), // https://xrpl.org/transaction-common-fields.html#global-flags flags: Flags(UInt32Type(0x8000_0000_u32)), - /// payment only signer_quorum: SignerQuorum(UInt32Type(signer_quorum)), signer_entries: SignerEntries(STArrayType(signer_entries)), signing_pub_key: signing_pub_key @@ -177,9 +176,10 @@ impl SignerListSet { #[cfg(test)] mod tests { + use alloc::vec::Vec; use crate::field::{Account, SignerEntry, SignerWeight}; use crate::types::{AccountIdType, SignerEntryType, UInt16Type}; - use super::{CodecToFields, Payment, SignerListSet}; + use super::*; #[test] #[allow(non_snake_case)] @@ -246,4 +246,39 @@ mod tests { } } } + #[test] + #[allow(non_snake_case)] + fn test_SignerListSet_serialize() { + let account = [1_u8; 20]; + let fee = 1_000; // 1000 drops + let signing_pub_key = [1_u8; 33]; + let signer_quorum = 3_u32; + let mut signer_entries = Vec::::default(); + signer_entries.push(SignerEntry(SignerEntryType(Account(AccountIdType([1_u8; 20])), + SignerWeight(UInt16Type(1_u16))))); + signer_entries.push(SignerEntry(SignerEntryType(Account(AccountIdType([2_u8; 20])), + SignerWeight(UInt16Type(2_u16))))); + + let signer_list_set = SignerListSet::new( + account, + fee, + signer_quorum, + signer_entries.clone(), + Some(signing_pub_key), + ); + + let buf = signer_list_set.binary_serialize(true); + // Construct the expected buf manually + let mut expected_buf = Vec::::default(); + expected_buf.extend_from_slice(&TransactionType(UInt16Type(TransactionTypeCode::SignerListSet.code())).binary_serialize(true)); // TransactionType + expected_buf.extend_from_slice(&Flags(UInt32Type(0x8000_0000_u32)).binary_serialize(true)); // Flags + expected_buf.extend_from_slice(&SignerQuorum(UInt32Type(signer_quorum)).binary_serialize(true)); // SignerQuorum + expected_buf.extend_from_slice(&Fee(AmountType(fee)).binary_serialize(true)); // Fee + expected_buf.extend_from_slice(&SigningPubKey(BlobType(signing_pub_key.to_vec())).binary_serialize(true)); // SigningPubKey + expected_buf.extend_from_slice(&TxnSignature::default().binary_serialize(true)); // TxnSignature + expected_buf.extend_from_slice(&Account(AccountIdType(account)).binary_serialize(true)); // Account + expected_buf.extend_from_slice(&SignerEntries(STArrayType(signer_entries)).binary_serialize(true)); // SignerEntries + + assert_eq!(buf, expected_buf); + } } diff --git a/codec/src/types.rs b/codec/src/types.rs index 70b1b65..0449a5f 100644 --- a/codec/src/types.rs +++ b/codec/src/types.rs @@ -8,13 +8,13 @@ use crate::field::{Account, SignerWeight}; pub const ACCOUNT_ID_TYPE_CODE: u16 = 8; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct NotPresentType; impl BinarySerialize for NotPresentType { fn binary_serialize_to(&self, _buf: &mut Vec, _for_signing: bool) {} } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct UInt16Type(pub u16); impl BinarySerialize for UInt16Type { @@ -23,7 +23,7 @@ impl BinarySerialize for UInt16Type { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct UInt32Type(pub u32); impl BinarySerialize for UInt32Type { @@ -32,7 +32,7 @@ impl BinarySerialize for UInt32Type { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct UInt64Type(pub u64); impl BinarySerialize for UInt64Type { @@ -41,7 +41,7 @@ impl BinarySerialize for UInt64Type { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Hash160Type(pub [u8; 20]); impl BinarySerialize for Hash160Type { fn binary_serialize_to(&self, buf: &mut Vec, _for_signing: bool) { @@ -49,7 +49,7 @@ impl BinarySerialize for Hash160Type { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Hash256Type(pub [u8; 32]); impl BinarySerialize for Hash256Type { fn binary_serialize_to(&self, buf: &mut Vec, _for_signing: bool) { @@ -57,7 +57,7 @@ impl BinarySerialize for Hash256Type { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct AccountIdType(pub [u8; 20]); impl BinarySerialize for AccountIdType { fn binary_serialize_to(&self, buf: &mut Vec, _for_signing: bool) { @@ -65,7 +65,7 @@ impl BinarySerialize for AccountIdType { } } -#[derive(Default, Debug)] +#[derive(Default, Debug, Clone)] pub struct BlobType(pub Vec); impl BinarySerialize for BlobType { @@ -76,7 +76,7 @@ impl BinarySerialize for BlobType { /// Current ///ly supporting native XRP amounts only -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct AmountType(pub u64); impl BinarySerialize for AmountType { fn binary_serialize_to(&self, buf: &mut Vec, _for_signing: bool) { @@ -99,7 +99,7 @@ impl BinarySerialize for AmountType { // } // } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SignerEntryType(pub Account, pub SignerWeight); impl BinarySerialize for SignerEntryType { fn binary_serialize_to(&self, buf: &mut Vec, _for_signing: bool) { @@ -112,7 +112,7 @@ impl BinarySerialize for SignerEntryType { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct STArrayType(pub Vec); impl BinarySerialize for STArrayType { fn binary_serialize_to(&self, buf: &mut Vec, _for_signing: bool) {