Skip to content

Commit

Permalink
Add SignerListSet txn serialize test
Browse files Browse the repository at this point in the history
  • Loading branch information
surangap committed Oct 4, 2022
1 parent f6e8bd9 commit 6765c85
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
22 changes: 11 additions & 11 deletions codec/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,48 @@ 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<TransactionTypeCode> for TransactionType {
fn from(v: TransactionTypeCode) -> Self {
TransactionType(UInt16Type(v.code()))
}
}

#[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<SignerEntry>);

impl<T: CodecField> BinarySerialize for T {
Expand Down
41 changes: 38 additions & 3 deletions codec/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl SignerListSet {
account: [u8; 20],
fee: u64,
signer_quorum: u32,
signer_entries: Vec::<SignerEntry>,
signer_entries: Vec<SignerEntry>,
signing_pub_key: Option<[u8; 33]>,
) -> Self {
Self {
Expand All @@ -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
Expand All @@ -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)]
Expand Down Expand Up @@ -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::<SignerEntry>::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::<u8>::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);
}
}
22 changes: 11 additions & 11 deletions codec/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>, _for_signing: bool) {}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct UInt16Type(pub u16);

impl BinarySerialize for UInt16Type {
Expand All @@ -23,7 +23,7 @@ impl BinarySerialize for UInt16Type {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct UInt32Type(pub u32);

impl BinarySerialize for UInt32Type {
Expand All @@ -32,7 +32,7 @@ impl BinarySerialize for UInt32Type {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct UInt64Type(pub u64);

impl BinarySerialize for UInt64Type {
Expand All @@ -41,31 +41,31 @@ 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<u8>, _for_signing: bool) {
buf.extend_from_slice(self.0.as_slice());
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Hash256Type(pub [u8; 32]);
impl BinarySerialize for Hash256Type {
fn binary_serialize_to(&self, buf: &mut Vec<u8>, _for_signing: bool) {
buf.extend_from_slice(self.0.as_slice());
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct AccountIdType(pub [u8; 20]);
impl BinarySerialize for AccountIdType {
fn binary_serialize_to(&self, buf: &mut Vec<u8>, _for_signing: bool) {
buf.extend_from_slice(self.0.as_slice());
}
}

#[derive(Default, Debug)]
#[derive(Default, Debug, Clone)]
pub struct BlobType(pub Vec<u8>);

impl BinarySerialize for BlobType {
Expand All @@ -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<u8>, _for_signing: bool) {
Expand All @@ -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<u8>, _for_signing: bool) {
Expand All @@ -112,7 +112,7 @@ impl BinarySerialize for SignerEntryType {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct STArrayType<T>(pub Vec<T>);
impl<T: BinarySerialize> BinarySerialize for STArrayType<T> {
fn binary_serialize_to(&self, buf: &mut Vec<u8>, _for_signing: bool) {
Expand Down

0 comments on commit 6765c85

Please sign in to comment.