Skip to content

Commit

Permalink
Merge branch 'murisi/expand-correct-test-vectors' (#3494)
Browse files Browse the repository at this point in the history
* origin/murisi/expand-correct-test-vectors:
  Added changelog entry.
  Augment the IBC Transfer printer with MASP details.
  Integrate MASP Transactions into the generation of IBC transfers.
  Augment IBC test vectors with MASP data.
  Expanded printer to support NFT transfers.
  Updated the printing of IBC transfers.
  Added capability to generate NFT transfers.
  Enable generation of Borsh schema for TxData. Fixed transaction generated for IBC transfer.
  Now construct Transactions with both shielded and unshielded artifacts. The corresponding Transfers now match these.
  • Loading branch information
brentstone committed Jul 24, 2024
2 parents 72dc592 + 1e431ea commit cd8f6b4
Show file tree
Hide file tree
Showing 12 changed files with 686 additions and 391 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Expanded the scope of test vector generation and updated outdated components
of the test vector code. ([\#3494](https://github.com/anoma/namada/pull/3494))
9 changes: 8 additions & 1 deletion crates/governance/src/storage/proposal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Display;

use borsh::{BorshDeserialize, BorshSerialize};
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use itertools::Itertools;
use namada_core::address::Address;
use namada_core::hash::Hash;
Expand Down Expand Up @@ -33,6 +33,7 @@ pub enum ProposalError {
Debug,
Clone,
PartialEq,
BorshSchema,
BorshSerialize,
BorshDeserialize,
BorshDeserializer,
Expand Down Expand Up @@ -69,6 +70,7 @@ impl InitProposalData {
Debug,
Clone,
PartialEq,
BorshSchema,
BorshSerialize,
BorshDeserialize,
BorshDeserializer,
Expand Down Expand Up @@ -191,6 +193,7 @@ impl StoragePgfFunding {
Debug,
Clone,
PartialEq,
BorshSchema,
BorshSerialize,
BorshDeserialize,
BorshDeserializer,
Expand Down Expand Up @@ -221,6 +224,7 @@ pub enum ProposalType {
Eq,
PartialOrd,
Ord,
BorshSchema,
BorshSerialize,
BorshDeserialize,
Serialize,
Expand Down Expand Up @@ -250,6 +254,7 @@ where
Debug,
Clone,
PartialEq,
BorshSchema,
BorshSerialize,
BorshDeserialize,
BorshDeserializer,
Expand Down Expand Up @@ -303,6 +308,7 @@ impl Display for PGFTarget {
Debug,
Clone,
PartialEq,
BorshSchema,
BorshSerialize,
BorshDeserialize,
BorshDeserializer,
Expand Down Expand Up @@ -413,6 +419,7 @@ impl borsh::BorshSchema for PGFIbcTarget {
Debug,
Clone,
PartialEq,
BorshSchema,
BorshSerialize,
BorshDeserialize,
BorshDeserializer,
Expand Down
3 changes: 2 additions & 1 deletion crates/governance/src/storage/vote.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Display;

use borsh::{BorshDeserialize, BorshSerialize};
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use namada_macros::BorshDeserializer;
#[cfg(feature = "migrations")]
use namada_migrations::*;
Expand All @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
Debug,
Clone,
PartialEq,
BorshSchema,
BorshSerialize,
BorshDeserialize,
BorshDeserializer,
Expand Down
125 changes: 122 additions & 3 deletions crates/ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ pub fn received_ibc_token(
pub mod testing {
use std::str::FromStr;

use ibc::apps::nft_transfer::types::msgs::transfer::MsgTransfer as MsgNftTransfer;
use ibc::apps::nft_transfer::types::packet::PacketData as NftPacketData;
use ibc::apps::nft_transfer::types::{
ClassData, ClassId, ClassUri, Memo as NftMemo, PrefixedClassId,
TokenData, TokenId, TokenIds, TokenUri,
};
use ibc::apps::transfer::types::msgs::transfer::MsgTransfer;
use ibc::apps::transfer::types::packet::PacketData;
use ibc::apps::transfer::types::{
Expand All @@ -456,7 +462,7 @@ pub mod testing {
use ibc::primitives::proto::Any;
use ibc::primitives::{Timestamp, ToProto};
use proptest::prelude::{Just, Strategy};
use proptest::{collection, prop_compose, prop_oneof};
use proptest::{collection, option, prop_compose, prop_oneof};

prop_compose! {
/// Generate an arbitrary port ID
Expand Down Expand Up @@ -505,6 +511,13 @@ pub mod testing {
}
}

prop_compose! {
/// Generate an arbitrary IBC NFT memo
pub fn arb_ibc_nft_memo()(memo in "[a-zA-Z0-9_]*") -> NftMemo {
memo.into()
}
}

prop_compose! {
/// Generate an arbitrary IBC memo
pub fn arb_ibc_signer()(signer in "[a-zA-Z0-9_]*") -> Signer {
Expand Down Expand Up @@ -605,10 +618,116 @@ pub mod testing {
}
}

prop_compose! {
/// Generate an arbitrary IBC token ID
pub fn arb_ibc_token_id()(token_id in "[a-zA-Z0-9_]+") -> TokenId {
TokenId::from_str(&token_id).expect("generated invalid IBC token ID")
}
}

prop_compose! {
/// Generate an arbitrary IBC token ID vector
pub fn arb_ibc_token_ids()(token_ids in collection::vec(arb_ibc_token_id(), 1..10)) -> TokenIds {
TokenIds(token_ids)
}
}

prop_compose! {
/// Generate arbitrary IBC class data
pub fn arb_ibc_class_data()(class_data in "[a-zA-Z0-9_]*") -> ClassData {
ClassData::from_str(&class_data).expect("generated invalid IBC class data")
}
}

prop_compose! {
/// Generate an arbitrary IBC class ID
pub fn arb_ibc_class_id()(token_id in "[a-zA-Z0-9_]+") -> ClassId {
ClassId::from_str(&token_id).expect("generated invalid IBC class ID")
}
}

prop_compose! {
/// Generate an arbitrary IBC prefixed class ID
pub fn arb_ibc_prefixed_class_id()(
trace_path in arb_ibc_trace_path(),
base_class_id in arb_ibc_class_id(),
) -> PrefixedClassId {
PrefixedClassId {
trace_path,
base_class_id,
}
}
}

prop_compose! {
/// Generate arbitrary IBC token data
pub fn arb_ibc_token_data()(
token_data in "[a-zA-Z0-9_]*",
) -> TokenData {
TokenData::from_str(&token_data).expect("generated invalid IBC token data")
}
}

// An arbitrary URI for the tests. Generating random URIs would not increase
// test coverage since they are encoded as length-prefixed strings.
const ARBITRARY_URI: &str = "https://namada.net/#ibc-interoperability";

prop_compose! {
/// Generate arbitrary NFT packet data
pub fn arb_ibc_nft_packet_data()(
token_ids in arb_ibc_token_ids(),
token_uri in Just(TokenUri::from_str(ARBITRARY_URI).unwrap()),
)(
sender in arb_ibc_signer(),
receiver in arb_ibc_signer(),
memo in option::of(arb_ibc_nft_memo()),
class_data in option::of(arb_ibc_class_data()),
class_id in arb_ibc_prefixed_class_id(),
class_uri in option::of(Just(ClassUri::from_str(ARBITRARY_URI).unwrap())),
token_uris in option::of(collection::vec(Just(token_uri), token_ids.0.len())),
token_data in option::of(collection::vec(arb_ibc_token_data(), token_ids.0.len())),
token_ids in Just(token_ids),
) -> NftPacketData {
NftPacketData {
token_ids,
sender,
receiver,
memo,
class_data,
class_id,
class_uri,
token_uris,
token_data,
}
}
}

prop_compose! {
/// Generate an arbitrary IBC NFT transfer message
pub fn arb_ibc_msg_nft_transfer()(
port_id_on_a in arb_ibc_port_id(),
chan_id_on_a in arb_ibc_channel_id(),
packet_data in arb_ibc_nft_packet_data(),
timeout_height_on_b in arb_ibc_timeout_data(),
timeout_timestamp_on_b in arb_ibc_timestamp(),
) -> MsgNftTransfer {
MsgNftTransfer {
port_id_on_a,
chan_id_on_a,
packet_data,
timeout_height_on_b,
timeout_timestamp_on_b,
}
}
}

prop_compose! {
/// Generate an arbitrary IBC any object
pub fn arb_ibc_any()(msg_transfer in arb_ibc_msg_transfer()) -> Any {
msg_transfer.to_any()
pub fn arb_ibc_any()(any in prop_oneof![
arb_ibc_msg_transfer().prop_map(|x| x.to_any()),
arb_ibc_msg_nft_transfer().prop_map(|x| x.to_any()),
]) -> Any {
any
}
}
}
37 changes: 36 additions & 1 deletion crates/ibc/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use borsh::{BorshDeserialize, BorshSerialize};
use std::collections::BTreeMap;

use borsh::schema::{Declaration, Definition, Fields};
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use data_encoding::HEXUPPER;
use ibc::apps::nft_transfer::types::msgs::transfer::MsgTransfer as IbcMsgNftTransfer;
use ibc::apps::nft_transfer::types::packet::PacketData as NftPacketData;
Expand Down Expand Up @@ -59,6 +62,22 @@ impl BorshDeserialize for MsgTransfer {
}
}

impl BorshSchema for MsgTransfer {
fn add_definitions_recursively(
definitions: &mut BTreeMap<Declaration, Definition>,
) {
<(Vec<u8>, Option<Transfer>)>::add_definitions_recursively(definitions);
let fields = Fields::UnnamedFields(vec![
<(Vec<u8>, Option<Transfer>)>::declaration(),
]);
definitions.insert(Self::declaration(), Definition::Struct { fields });
}

fn declaration() -> Declaration {
"MsgTransfer".into()
}
}

/// IBC NFT transfer message with `Transfer`
#[derive(Debug, Clone)]
pub struct MsgNftTransfer {
Expand Down Expand Up @@ -92,6 +111,22 @@ impl BorshDeserialize for MsgNftTransfer {
}
}

impl BorshSchema for MsgNftTransfer {
fn add_definitions_recursively(
definitions: &mut BTreeMap<Declaration, Definition>,
) {
<(Vec<u8>, Option<Transfer>)>::add_definitions_recursively(definitions);
let fields = Fields::UnnamedFields(vec![
<(Vec<u8>, Option<Transfer>)>::declaration(),
]);
definitions.insert(Self::declaration(), Definition::Struct { fields });
}

fn declaration() -> Declaration {
"MsgNftTransfer".into()
}
}

/// Shielding data in IBC packet memo
#[derive(Debug, Clone, BorshDeserialize, BorshSerialize)]
pub struct IbcShieldingData {
Expand Down
Loading

0 comments on commit cd8f6b4

Please sign in to comment.