Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Pallet: Atomic Swap #6349

Merged
merged 21 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add successful swap test
  • Loading branch information
sorpaas committed Jun 14, 2020
commit b08c1eaec35c85504c68e3d74d81775725fde58b
2 changes: 1 addition & 1 deletion frame/atomic-swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod tests;
use sp_std::prelude::*;
use sp_io::hashing::blake2_256;
use frame_support::{
dispatch::DispatchResult, decl_module, decl_storage, decl_event, decl_error, ensure,
decl_module, decl_storage, decl_event, decl_error, ensure,
traits::{Currency, ReservableCurrency, BalanceStatus},
};
use frame_system::{self as system, ensure_signed};
Expand Down
79 changes: 69 additions & 10 deletions frame/atomic-swap/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use super::*;

use frame_support::{
assert_ok, impl_outer_origin, parameter_types, weights::{Weight, DispatchInfo, GetDispatchInfo},
traits::{OnInitialize, OnFinalize}
impl_outer_origin, parameter_types, weights::Weight,
};
use sp_core::H256;
// The testing primitives are very useful for avoiding having to work with signatures
Expand Down Expand Up @@ -71,20 +70,80 @@ impl Trait for Test {
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Example = Module<Test>;
type AtomicSwap = Module<Test>;

const A: u64 = 1;
const B: u64 = 2;

// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
// We use default for brevity, but you can configure as desired if needed.
pallet_balances::GenesisConfig::<Test>::default().assimilate_storage(&mut t).unwrap();
let genesis = pallet_balances::GenesisConfig::<Test> {
balances: vec![
(A, 100),
(B, 200),
],
};
genesis.assimilate_storage(&mut t).unwrap();
t.into()
}

#[test]
fn two_party_swap() {
new_test_ext().execute_with(|| {
unimplemented!()
fn two_party_successful_swap() {
let mut chain1 = new_test_ext();
let mut chain2 = new_test_ext();

// A generates a random proof. Keep it secret.
let proof: [u8; 2] = [4, 2];
// The hashed proof is the blake2_256 hash of the proof. This is public.
let hashed_proof = blake2_256(&proof);

// A creates the swap on chain1.
chain1.execute_with(|| {
AtomicSwap::create_swap(
Origin::signed(A),
B,
hashed_proof.clone(),
50,
1000,
).unwrap();

assert_eq!(Balances::free_balance(A), 100 - 50);
assert_eq!(Balances::free_balance(B), 200);
});

// B creates the swap on chain2.
chain2.execute_with(|| {
AtomicSwap::create_swap(
Origin::signed(B),
A,
hashed_proof.clone(),
75,
1000,
).unwrap();

assert_eq!(Balances::free_balance(A), 100);
assert_eq!(Balances::free_balance(B), 200 - 75);
});

// A reveals the proof and claims the swap on chain2.
chain2.execute_with(|| {
AtomicSwap::claim_swap(
Origin::signed(A),
proof.to_vec(),
).unwrap();

assert_eq!(Balances::free_balance(A), 100 + 75);
assert_eq!(Balances::free_balance(B), 200 - 75);
});

// B use the revealed proof to claim the swap on chain1.
chain1.execute_with(|| {
AtomicSwap::claim_swap(
Origin::signed(B),
proof.to_vec(),
).unwrap();

assert_eq!(Balances::free_balance(A), 100 - 50);
assert_eq!(Balances::free_balance(B), 200 + 50);
});
}