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
Remove retry logic
It's overkill. Swap is about something being executed, not necessarily successful.
Although there should be logic (reserve and unreserve) to make it so that both parties *believes*
that the execution is successful.
  • Loading branch information
sorpaas committed Jun 16, 2020
commit ad361856bf637b02b5e151e9323b36f524044c27
63 changes: 14 additions & 49 deletions frame/atomic-swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use frame_support::{
};
use frame_system::{self as system, ensure_signed};
use codec::{Encode, Decode};
use sp_runtime::{RuntimeDebug, traits::Saturating};
use sp_runtime::RuntimeDebug;

/// Pending atomic swap operation.
#[derive(Clone, RuntimeDebug, Eq, PartialEq, Encode, Decode)]
Expand All @@ -43,8 +43,6 @@ pub struct PendingSwap<AccountId, Balance, BlockNumber> {
pub balance: Balance,
/// End block of the lock.
pub end_block: BlockNumber,
/// Whether the swap has already been claimed.
pub claimed: bool,
}

/// Balance type from the pallet's point of view.
Expand Down Expand Up @@ -79,8 +77,6 @@ pub trait Trait: frame_system::Trait {
/// to accept the atomic swap request if A generates the proof, and asks that B generates the
/// proof instead.
type ProofLimit: Get<u32>;
/// Block when the swap completely expires.
type ExpireDuration: Get<BlockNumberFor<Self>>;
}

decl_storage! {
Expand Down Expand Up @@ -119,11 +115,8 @@ decl_event!(
{
/// Swap created.
NewSwap(AccountId, HashedProof, PendingSwap),
/// Swap claimed.
SwapClaimed(AccountId, HashedProof, Balance),
/// Swap claim failed its execution.
/// The third parameter indicates whether retry is possible.
SwapClaimFailed(AccountId, HashedProof, bool),
/// Swap claimed. The last parameter indicates whether the execution succeeds.
SwapClaimed(AccountId, HashedProof, Balance, bool),
/// Swap cancelled.
SwapCancelled(AccountId, HashedProof),
}
Expand Down Expand Up @@ -166,7 +159,6 @@ decl_module! {
source,
balance,
end_block: frame_system::Module::<T>::block_number() + duration,
claimed: false,
};
PendingSwaps::<T>::insert(target.clone(), hashed_proof.clone(), swap.clone());

Expand Down Expand Up @@ -196,46 +188,23 @@ decl_module! {
let target = ensure_signed(origin)?;
let hashed_proof = blake2_256(&proof);

let mut swap = PendingSwaps::<T>::get(&target, hashed_proof)
let swap = PendingSwaps::<T>::get(&target, hashed_proof)
.ok_or(Error::<T>::InvalidProof)?;
swap.claimed = true;

match T::Currency::repatriate_reserved(
let succeed = T::Currency::repatriate_reserved(
&swap.source,
&target,
swap.balance,
BalanceStatus::Free,
) {
Err(_) => {
let expired = frame_system::Module::<T>::block_number() >
swap.end_block.saturating_add(T::ExpireDuration::get());
if expired {
PendingSwaps::<T>::remove(target.clone(), hashed_proof.clone());

Self::deposit_event(
RawEvent::SwapClaimFailed(target, hashed_proof, false)
);
} else {
PendingSwaps::<T>::insert(target.clone(), hashed_proof.clone(), swap);

Self::deposit_event(
RawEvent::SwapClaimFailed(target, hashed_proof, true)
);
}

Ok(())
},
Ok(_) => {
PendingSwaps::<T>::remove(target.clone(), hashed_proof.clone());

Self::deposit_event(
RawEvent::SwapClaimed(target, hashed_proof, swap.balance)
);

Ok(())
},

}
).is_ok();

PendingSwaps::<T>::remove(target.clone(), hashed_proof.clone());

Self::deposit_event(
RawEvent::SwapClaimed(target, hashed_proof, swap.balance, succeed)
);

Ok(())
}

/// Cancel an atomic swap. Only possible after the originally set duration has passed.
Expand All @@ -254,10 +223,6 @@ decl_module! {

let swap = PendingSwaps::<T>::get(&target, hashed_proof)
.ok_or(Error::<T>::NotExist)?;
ensure!(
!swap.claimed,
Error::<T>::AlreadyClaimed,
);
ensure!(
swap.source == source,
sorpaas marked this conversation as resolved.
Show resolved Hide resolved
Error::<T>::SourceMismatch,
Expand Down
1 change: 0 additions & 1 deletion frame/atomic-swap/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ impl Trait for Test {
type Event = ();
type Currency = Balances;
type ProofLimit = ProofLimit;
type ExpireDuration = ExpireDuration;
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
Expand Down