Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Test cross-chain withdrawals with xanchor + anchor #161

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions pallets/anchor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ pub mod pallet {
pub enum Error<T, I = ()> {
/// Invalid Merkle Roots
InvalidMerkleRoots,
/// Unknown root
UnknownRoot,
/// Invalid withdraw proof
InvalidWithdrawProof,
/// Mixer not found.
Expand Down Expand Up @@ -375,6 +373,11 @@ impl<T: Config<I>, I: 'static> AnchorInterface<AnchorConfigration<T, I>> for Pal
let refund_bytes = refund.using_encoded(element_encoder);
let chain_id_type_bytes =
T::LinkableTree::get_chain_id_type().using_encoded(element_encoder);
println!("on chain id: {:?}", T::LinkableTree::get_chain_id_type());
// println!("on chain nullifier_hash: {:?}", nullifier_hash.encode());
// println!("on chain roots: {:?}", roots.clone());
// println!("on chain recipient: {:?}", recipient);
// println!("on chain relayer: {:?}", relayer);
bytes.extend_from_slice(&chain_id_type_bytes);
bytes.extend_from_slice(&nullifier_hash.encode());
for root in &roots {
Expand Down
2 changes: 2 additions & 0 deletions pallets/anchor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ fn anchor_works() {

// inputs
let tree_id = create_anchor(0);
dbg!(tree_id);
let src_chain_id = compute_chain_id_type(1u32, SUBSTRATE_CHAIN_TYPE);
dbg!(src_chain_id);
let sender_account_id = account::<AccountId>("", 1, SEED);
let recipient_account_id = account::<AccountId>("", 2, SEED);
let relayer_account_id = account::<AccountId>("", 0, SEED);
Expand Down
1 change: 0 additions & 1 deletion pallets/linkable-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ impl<T: Config<I>, I: 'static> LinkableTreeInspector<LinkableTreeConfigration<T,
if roots.len() > 1 {
// Get edges and corresponding chain IDs for the anchor
let edges = EdgeList::<T, I>::iter_prefix(id).into_iter().collect::<Vec<_>>();

// Check membership of provided historical neighbor roots
for (i, (chain_id, _)) in edges.iter().enumerate() {
Self::ensure_known_neighbor_root(id, *chain_id, roots[i + 1])?;
Expand Down
3 changes: 3 additions & 0 deletions pallets/xanchor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ polkadot-core-primitives = { git = "https://github.com/paritytech/polkadot", bra
polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false }
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false }

wasm-utils = { version = "0.1.1" }
hex = "0.4"

[features]
default = ["std"]
std = [
Expand Down
11 changes: 6 additions & 5 deletions pallets/xanchor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,9 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
) -> DispatchResultWithPostInfo {
// extract the resource id information
let (tree_id, chain_id) = utils::parse_resource_id::<T::TreeId, T::ChainId>(r_id);
println!("register_new_resource_id");
println!("tree_id: {:?}", tree_id);
println!("chain_id: {:?}", chain_id);
// println!("register_new_resource_id");
// println!("tree_id: {:?}", tree_id);
// println!("chain_id: {:?}", chain_id);
// and we need to also ensure that the anchor exists
ensure!(Self::anchor_exists(tree_id), Error::<T, I>::AnchorNotFound);
// and not already anchored/linked
Expand Down Expand Up @@ -682,7 +682,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
.encode()
.into(),
};
println!("{:?}", update_edge);
let dest = (Parent, Parachain(other_para_id.into()));
let result = T::XcmSender::send_xcm(dest, Xcm(vec![update_edge]));
match result {
Expand Down Expand Up @@ -732,7 +731,9 @@ pub fn para_id_to_chain_id<T: Config<I>, I: 'static>(para_id: ParaId) -> T::Chai
.unwrap_or_default()
}

pub fn chain_id_to_bytes<T: Config<I>, I: 'static>(chain_id: T::ChainId) -> T::ChainId {
pub fn compute_chain_id_with_internal_type<T: Config<I>, I: 'static>(
chain_id: T::ChainId,
) -> T::ChainId {
T::ChainId::try_from(compute_chain_id_type(chain_id, T::Anchor::get_chain_type()))
.unwrap_or_default()
}
125 changes: 64 additions & 61 deletions pallets/xanchor/src/mock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,78 @@
pub mod parachain;
pub mod parachain_a;
pub mod parachain_b;
pub mod relay_chain;
pub(crate) mod test_utils;

use std::ops::Mul;

use frame_support::traits::{GenesisBuild, OnInitialize};
use codec::{Decode, Encode};
pub use webb_primitives::{AccountId, types::ElementTrait};
use frame_support::ord_parameter_types;
use polkadot_parachain::primitives::Id as ParaId;
use sp_runtime::traits::AccountIdConversion;
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain};

use frame_support::{ Deserialize, Serialize };

ord_parameter_types! {
pub const AccountOne: AccountId = sp_runtime::AccountId32::new([1u8; 32]);
pub const AccountTwo: AccountId = sp_runtime::AccountId32::new([2u8; 32]);
pub const AccountThree: AccountId = sp_runtime::AccountId32::new([3u8; 32]);
pub const AccountFour: AccountId = sp_runtime::AccountId32::new([4u8; 32]);
pub const AccountFive: AccountId = sp_runtime::AccountId32::new([5u8; 32]);
pub const AccountSix: AccountId = sp_runtime::AccountId32::new([6u8; 32]);
}

#[derive(
Debug,
Encode,
Decode,
Default,
Copy,
Clone,
PartialEq,
Eq,
scale_info::TypeInfo,
Serialize,
Deserialize,
)]
pub struct Element([u8; 32]);

impl Element {
pub const fn zero() -> Self {
Element([0; 32])
}
}

impl ElementTrait for Element {
fn to_bytes(&self) -> &[u8] {
&self.0
}

fn from_bytes(input: &[u8]) -> Self {
let mut buf = [0u8; 32];
buf.copy_from_slice(input);
Self(buf)
}
}

pub const INITIAL_BALANCE: u128 = 1_000_000_000;
pub const PARAID_A: u32 = 2000;
pub const PARAID_B: u32 = 3000;

decl_test_parachain! {
pub struct ParaA {
Runtime = parachain::Runtime,
XcmpMessageHandler = parachain::MsgQueue,
DmpMessageHandler = parachain::MsgQueue,
new_ext = para_ext(PARAID_A),
Runtime = parachain1::Runtime,
XcmpMessageHandler = parachain_a::MsgQueue,
DmpMessageHandler = parachain_a::MsgQueue,
new_ext = parachain_a::para_ext(PARAID_A),
}
}

decl_test_parachain! {
pub struct ParaB {
Runtime = parachain::Runtime,
XcmpMessageHandler = parachain::MsgQueue,
DmpMessageHandler = parachain::MsgQueue,
new_ext = para_ext(PARAID_B),
Runtime = parachain2::Runtime,
XcmpMessageHandler = parachain_b::MsgQueue,
DmpMessageHandler = parachain_b::MsgQueue,
new_ext = parachain_b::para_ext(PARAID_B),
}
}

Expand All @@ -52,49 +98,19 @@ pub fn para_account_id(id: u32) -> relay_chain::AccountId {
ParaId::from(id).into_account()
}

pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
use parachain::{MsgQueue, Runtime};

let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(parachain::AccountOne::get(), INITIAL_BALANCE.mul(1u128)),
(parachain::AccountTwo::get(), INITIAL_BALANCE.mul(2u128)),
(parachain::AccountThree::get(), INITIAL_BALANCE.mul(3u128)),
(parachain::AccountFour::get(), INITIAL_BALANCE.mul(4u128)),
(parachain::AccountFive::get(), INITIAL_BALANCE.mul(5u128)),
(parachain::AccountSix::get(), INITIAL_BALANCE.mul(6u128)),
(para_account_id(PARAID_A), INITIAL_BALANCE),
(para_account_id(PARAID_B), INITIAL_BALANCE),
],
}
.assimilate_storage(&mut t)
.unwrap();
pallet_democracy::GenesisConfig::<Runtime>::default()
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
next_block();
MsgQueue::set_para_id(para_id.into());
});
ext
}

pub fn relay_ext() -> sp_io::TestExternalities {
use relay_chain::{Runtime, System};

let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(parachain::AccountOne::get(), INITIAL_BALANCE),
(parachain::AccountTwo::get(), INITIAL_BALANCE),
(parachain::AccountThree::get(), INITIAL_BALANCE),
(parachain::AccountFour::get(), INITIAL_BALANCE),
(parachain::AccountFive::get(), INITIAL_BALANCE),
(parachain::AccountSix::get(), INITIAL_BALANCE),
(AccountOne::get(), INITIAL_BALANCE),
(AccountTwo::get(), INITIAL_BALANCE),
(AccountThree::get(), INITIAL_BALANCE),
(AccountFour::get(), INITIAL_BALANCE),
(AccountFive::get(), INITIAL_BALANCE),
(AccountSix::get(), INITIAL_BALANCE),
(para_account_id(PARAID_A), INITIAL_BALANCE),
(para_account_id(PARAID_B), INITIAL_BALANCE),
],
Expand All @@ -108,16 +124,3 @@ pub fn relay_ext() -> sp_io::TestExternalities {
}

pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;

pub fn next_block() {
parachain::System::set_block_number(parachain::System::block_number() + 1);
parachain::Scheduler::on_initialize(parachain::System::block_number());
parachain::Democracy::on_initialize(parachain::System::block_number());
}

pub fn fast_forward_to(n: u64) {
while parachain::System::block_number() < n {
next_block();
}
}
Loading