mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 07:58:02 +00:00
Pallet: Atomic Swap (#6349)
* Init atomic swap pallet * Implement module swap operations * Add successful swap test * Bump node spec_version * Fix storage name * Add ProofLimit parameter to prevent proof size being too large * Add missing events * Basic weight support * Add basic docs * Mark swap on claim This handles the additional case if `repatriate_reserved` fails. * Add additional expire handler * Update frame/atomic-swap/src/lib.rs Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> * Add docs on ProofLimit * Fix test * Return Ok(()) even when the transfer fails Because we need to mark the swap as claimed no matter what. * 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. * succeed -> succeeded * Add docs on duration -- revealer should use duration shorter than counterparty * Missing trait type Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Generated
+15
@@ -3845,6 +3845,21 @@ dependencies = [
|
||||
"sp-std",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pallet-atomic-swap"
|
||||
version = "2.0.0-rc3"
|
||||
dependencies = [
|
||||
"frame-support",
|
||||
"frame-system",
|
||||
"pallet-balances",
|
||||
"parity-scale-codec",
|
||||
"serde",
|
||||
"sp-core",
|
||||
"sp-io",
|
||||
"sp-runtime",
|
||||
"sp-std",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pallet-aura"
|
||||
version = "2.0.0-rc3"
|
||||
|
||||
@@ -60,6 +60,7 @@ members = [
|
||||
"utils/wasm-builder-runner",
|
||||
"frame/assets",
|
||||
"frame/aura",
|
||||
"frame/atomic-swap",
|
||||
"frame/authority-discovery",
|
||||
"frame/authorship",
|
||||
"frame/babe",
|
||||
|
||||
@@ -97,8 +97,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
// and set impl_version to 0. If only runtime
|
||||
// implementation changes and behavior does not, then leave spec_version as
|
||||
// is and increment impl_version.
|
||||
spec_version: 252,
|
||||
impl_version: 1,
|
||||
spec_version: 253,
|
||||
impl_version: 0,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
transaction_version: 1,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
[package]
|
||||
name = "pallet-atomic-swap"
|
||||
version = "2.0.0-rc3"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://substrate.dev"
|
||||
repository = "https://github.com/paritytech/substrate/"
|
||||
description = "FRAME atomic swap pallet"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.101", optional = true }
|
||||
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false }
|
||||
frame-support = { version = "2.0.0-rc3", default-features = false, path = "../support" }
|
||||
frame-system = { version = "2.0.0-rc3", default-features = false, path = "../system" }
|
||||
sp-runtime = { version = "2.0.0-rc3", default-features = false, path = "../../primitives/runtime" }
|
||||
sp-std = { version = "2.0.0-rc3", default-features = false, path = "../../primitives/std" }
|
||||
sp-io = { version = "2.0.0-rc3", default-features = false, path = "../../primitives/io" }
|
||||
sp-core = { version = "2.0.0-rc3", default-features = false, path = "../../primitives/core" }
|
||||
|
||||
[dev-dependencies]
|
||||
pallet-balances = { version = "2.0.0-rc3", default-features = false, path = "../balances" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"serde",
|
||||
"codec/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
"sp-io/std",
|
||||
"sp-core/std",
|
||||
"pallet-balances/std",
|
||||
]
|
||||
@@ -0,0 +1,248 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! # Atomic swap support pallet
|
||||
|
||||
// Ensure we're `no_std` when compiling for Wasm.
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
mod tests;
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use sp_io::hashing::blake2_256;
|
||||
use frame_support::{
|
||||
decl_module, decl_storage, decl_event, decl_error, ensure,
|
||||
traits::{Get, Currency, ReservableCurrency, BalanceStatus},
|
||||
weights::Weight,
|
||||
dispatch::DispatchResult,
|
||||
};
|
||||
use frame_system::{self as system, ensure_signed};
|
||||
use codec::{Encode, Decode};
|
||||
use sp_runtime::RuntimeDebug;
|
||||
|
||||
/// Pending atomic swap operation.
|
||||
#[derive(Clone, RuntimeDebug, Eq, PartialEq, Encode, Decode)]
|
||||
pub struct PendingSwap<AccountId, Balance, BlockNumber> {
|
||||
/// Source of the swap.
|
||||
pub source: AccountId,
|
||||
/// Balance value of the swap.
|
||||
pub balance: Balance,
|
||||
/// End block of the lock.
|
||||
pub end_block: BlockNumber,
|
||||
}
|
||||
|
||||
/// Balance type from the pallet's point of view.
|
||||
pub type BalanceFor<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
|
||||
|
||||
/// AccountId type from the pallet's point of view.
|
||||
pub type AccountIdFor<T> = <T as frame_system::Trait>::AccountId;
|
||||
|
||||
/// BlockNumber type from the pallet's point of view.
|
||||
pub type BlockNumberFor<T> = <T as frame_system::Trait>::BlockNumber;
|
||||
|
||||
/// PendingSwap type from the pallet's point of view.
|
||||
pub type PendingSwapFor<T> = PendingSwap<AccountIdFor<T>, BalanceFor<T>, BlockNumberFor<T>>;
|
||||
|
||||
/// Hashed proof type.
|
||||
pub type HashedProof = [u8; 32];
|
||||
|
||||
/// Atomic swap's pallet configuration trait.
|
||||
pub trait Trait: frame_system::Trait {
|
||||
/// The overarching event type.
|
||||
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
|
||||
/// The currency mechanism.
|
||||
type Currency: ReservableCurrency<Self::AccountId>;
|
||||
/// Limit of proof size.
|
||||
///
|
||||
/// Atomic swap is only atomic if once the proof is revealed, both parties can submit the proofs
|
||||
/// on-chain. If A is the one that generates the proof, then it requires that either:
|
||||
/// - A's blockchain has the same proof length limit as B's blockchain.
|
||||
/// - Or A's blockchain has shorter proof length limit as B's blockchain.
|
||||
///
|
||||
/// If B sees A is on a blockchain with larger proof length limit, then it should kindly refuse
|
||||
/// to accept the atomic swap request if A generates the proof, and asks that B generates the
|
||||
/// proof instead.
|
||||
type ProofLimit: Get<u32>;
|
||||
}
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Trait> as AtomicSwap {
|
||||
pub PendingSwaps: double_map
|
||||
hasher(twox_64_concat) T::AccountId, hasher(blake2_128_concat) HashedProof
|
||||
=> Option<PendingSwapFor<T>>;
|
||||
}
|
||||
}
|
||||
|
||||
decl_error! {
|
||||
pub enum Error for Module<T: Trait> {
|
||||
/// Swap already exists.
|
||||
AlreadyExist,
|
||||
/// Swap proof is invalid.
|
||||
InvalidProof,
|
||||
/// Proof is too large.
|
||||
ProofTooLarge,
|
||||
/// Source does not match.
|
||||
SourceMismatch,
|
||||
/// Swap has already been claimed.
|
||||
AlreadyClaimed,
|
||||
/// Swap does not exist.
|
||||
NotExist,
|
||||
/// Duration has not yet passed for the swap to be cancelled.
|
||||
DurationNotPassed,
|
||||
}
|
||||
}
|
||||
|
||||
decl_event!(
|
||||
/// Event of atomic swap pallet.
|
||||
pub enum Event<T> where
|
||||
Balance = BalanceFor<T>,
|
||||
AccountId = AccountIdFor<T>,
|
||||
PendingSwap = PendingSwapFor<T>,
|
||||
{
|
||||
/// Swap created.
|
||||
NewSwap(AccountId, HashedProof, PendingSwap),
|
||||
/// Swap claimed. The last parameter indicates whether the execution succeeds.
|
||||
SwapClaimed(AccountId, HashedProof, Balance, bool),
|
||||
/// Swap cancelled.
|
||||
SwapCancelled(AccountId, HashedProof),
|
||||
}
|
||||
);
|
||||
|
||||
decl_module! {
|
||||
/// Module definition of atomic swap pallet.
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
type Error = Error<T>;
|
||||
|
||||
fn deposit_event() = default;
|
||||
|
||||
/// Register a new atomic swap, declaring an intention to send funds from origin to target
|
||||
/// on the current blockchain. The target can claim the fund using the revealed proof. If
|
||||
/// the fund is not claimed after `duration` blocks, then the sender can cancel the swap.
|
||||
///
|
||||
/// The dispatch origin for this call must be _Signed_.
|
||||
///
|
||||
/// - `target`: Receiver of the atomic swap.
|
||||
/// - `hashed_proof`: The blake2_256 hash of the secret proof.
|
||||
/// - `balance`: Funds to be sent from origin.
|
||||
/// - `duration`: Locked duration of the atomic swap. For safety reasons, it is recommended
|
||||
/// that the revealer uses a shorter duration than the counterparty, to prevent the
|
||||
/// situation where the revealer reveals the proof too late around the end block.
|
||||
#[weight = T::DbWeight::get().reads_writes(1, 1).saturating_add(40_000_000)]
|
||||
fn create_swap(
|
||||
origin,
|
||||
target: AccountIdFor<T>,
|
||||
hashed_proof: HashedProof,
|
||||
balance: BalanceFor<T>,
|
||||
duration: BlockNumberFor<T>,
|
||||
) {
|
||||
let source = ensure_signed(origin)?;
|
||||
ensure!(
|
||||
!PendingSwaps::<T>::contains_key(&target, hashed_proof),
|
||||
Error::<T>::AlreadyExist
|
||||
);
|
||||
|
||||
T::Currency::reserve(&source, balance)?;
|
||||
|
||||
let swap = PendingSwap {
|
||||
source,
|
||||
balance,
|
||||
end_block: frame_system::Module::<T>::block_number() + duration,
|
||||
};
|
||||
PendingSwaps::<T>::insert(target.clone(), hashed_proof.clone(), swap.clone());
|
||||
|
||||
Self::deposit_event(
|
||||
RawEvent::NewSwap(target, hashed_proof, swap)
|
||||
);
|
||||
}
|
||||
|
||||
/// Claim an atomic swap.
|
||||
///
|
||||
/// The dispatch origin for this call must be _Signed_.
|
||||
///
|
||||
/// - `proof`: Revealed proof of the claim.
|
||||
#[weight = T::DbWeight::get().reads_writes(2, 2)
|
||||
.saturating_add(40_000_000)
|
||||
.saturating_add((proof.len() as Weight).saturating_mul(100))
|
||||
]
|
||||
fn claim_swap(
|
||||
origin,
|
||||
proof: Vec<u8>,
|
||||
) -> DispatchResult {
|
||||
ensure!(
|
||||
proof.len() <= T::ProofLimit::get() as usize,
|
||||
Error::<T>::ProofTooLarge,
|
||||
);
|
||||
|
||||
let target = ensure_signed(origin)?;
|
||||
let hashed_proof = blake2_256(&proof);
|
||||
|
||||
let swap = PendingSwaps::<T>::get(&target, hashed_proof)
|
||||
.ok_or(Error::<T>::InvalidProof)?;
|
||||
|
||||
let succeeded = T::Currency::repatriate_reserved(
|
||||
&swap.source,
|
||||
&target,
|
||||
swap.balance,
|
||||
BalanceStatus::Free,
|
||||
).is_ok();
|
||||
|
||||
PendingSwaps::<T>::remove(target.clone(), hashed_proof.clone());
|
||||
|
||||
Self::deposit_event(
|
||||
RawEvent::SwapClaimed(target, hashed_proof, swap.balance, succeeded)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Cancel an atomic swap. Only possible after the originally set duration has passed.
|
||||
///
|
||||
/// The dispatch origin for this call must be _Signed_.
|
||||
///
|
||||
/// - `target`: Target of the original atomic swap.
|
||||
/// - `hashed_proof`: Hashed proof of the original atomic swap.
|
||||
#[weight = T::DbWeight::get().reads_writes(1, 1).saturating_add(40_000_000)]
|
||||
fn cancel_swap(
|
||||
origin,
|
||||
target: AccountIdFor<T>,
|
||||
hashed_proof: HashedProof,
|
||||
) {
|
||||
let source = ensure_signed(origin)?;
|
||||
|
||||
let swap = PendingSwaps::<T>::get(&target, hashed_proof)
|
||||
.ok_or(Error::<T>::NotExist)?;
|
||||
ensure!(
|
||||
swap.source == source,
|
||||
Error::<T>::SourceMismatch,
|
||||
);
|
||||
ensure!(
|
||||
frame_system::Module::<T>::block_number() >= swap.end_block,
|
||||
Error::<T>::DurationNotPassed,
|
||||
);
|
||||
|
||||
T::Currency::unreserve(
|
||||
&swap.source,
|
||||
swap.balance,
|
||||
);
|
||||
PendingSwaps::<T>::remove(&target, hashed_proof.clone());
|
||||
|
||||
Self::deposit_event(
|
||||
RawEvent::SwapCancelled(target, hashed_proof)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use super::*;
|
||||
|
||||
use frame_support::{
|
||||
impl_outer_origin, parameter_types, weights::Weight,
|
||||
};
|
||||
use sp_core::H256;
|
||||
// The testing primitives are very useful for avoiding having to work with signatures
|
||||
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
|
||||
use sp_runtime::{
|
||||
Perbill,
|
||||
testing::Header,
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
};
|
||||
|
||||
impl_outer_origin! {
|
||||
pub enum Origin for Test where system = frame_system {}
|
||||
}
|
||||
|
||||
// For testing the pallet, we construct most of a mock runtime. This means
|
||||
// first constructing a configuration type (`Test`) which `impl`s each of the
|
||||
// configuration traits of pallets we want to use.
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct Test;
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u64 = 250;
|
||||
pub const MaximumBlockWeight: Weight = 1024;
|
||||
pub const MaximumBlockLength: u32 = 2 * 1024;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::one();
|
||||
}
|
||||
impl frame_system::Trait for Test {
|
||||
type BaseCallFilter = ();
|
||||
type Origin = Origin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Hash = H256;
|
||||
type Call = ();
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type Event = ();
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type MaximumBlockWeight = MaximumBlockWeight;
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
type ModuleToIndex = ();
|
||||
type AccountData = pallet_balances::AccountData<u64>;
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
}
|
||||
parameter_types! {
|
||||
pub const ExistentialDeposit: u64 = 1;
|
||||
}
|
||||
impl pallet_balances::Trait for Test {
|
||||
type Balance = u64;
|
||||
type DustRemoval = ();
|
||||
type Event = ();
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
type AccountStore = System;
|
||||
}
|
||||
parameter_types! {
|
||||
pub const ProofLimit: u32 = 1024;
|
||||
pub const ExpireDuration: u64 = 100;
|
||||
}
|
||||
impl Trait for Test {
|
||||
type Event = ();
|
||||
type Currency = Balances;
|
||||
type ProofLimit = ProofLimit;
|
||||
}
|
||||
type System = frame_system::Module<Test>;
|
||||
type Balances = pallet_balances::Module<Test>;
|
||||
type AtomicSwap = Module<Test>;
|
||||
|
||||
const A: u64 = 1;
|
||||
const B: u64 = 2;
|
||||
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().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_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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user