Skip to content

Commit

Permalink
get very basic tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhuo68 committed Dec 6, 2022
1 parent 1954c97 commit c2b51ee
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

11 changes: 11 additions & 0 deletions pallets/anonymity-mining/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ scale-info = { version = "2.1.1", default-features = false, features = ["derive"
pallet-vanchor = { path = "../vanchor", default-features = false }
pallet-asset-registry = { path = "../asset-registry", default-features = false }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
pallet-hasher = { path = "../hasher", default-features = false }
pallet-mixer = { path = "../mixer", default-features = false }
pallet-mt = { path = "../mt", default-features = false }
pallet-linkable-tree = { path = "../linkable-tree", default-features = false }
pallet-vanchor-verifier = { path = "../vanchor-verifier", default-features = false }
pallet-token-wrapper = { path = "../token-wrapper", default-features = false }
pallet-key-storage = {path = "../key-storage"}
orml-currencies = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", branch = "polkadot-v0.9.30", default-features = false }
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", branch = "polkadot-v0.9.30", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", branch = "polkadot-v0.9.30", default-features = false }
Expand Down Expand Up @@ -66,6 +73,10 @@ std = [
"pallet-asset-registry/std",
"pallet-balances/std",
"pallet-vanchor/std",
"pallet-mt/std",
"pallet-vanchor-verifier/std",
"pallet-token-wrapper/std"

]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
29 changes: 28 additions & 1 deletion pallets/anonymity-mining/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ pub mod pallet {
/// VAnchor Interface
type VAnchor: VAnchorInterface<VAnchorConfigration<Self, I>>
+ VAnchorInspector<VAnchorConfigration<Self, I>>;


/// AP asset id
#[pallet::constant]
type AnonymityPointsAssetId: Get<CurrencyIdOf<Self, I>>;

/// Reward asset id
#[pallet::constant]
type RewardAssetId: Get<CurrencyIdOf<Self, I>>;

/// Native currency id
#[pallet::constant]
type NativeCurrencyId: Get<CurrencyIdOf<Self, I>>;
Expand Down Expand Up @@ -134,6 +139,28 @@ pub mod pallet {
recipient: T::AccountId,
amount: BalanceOf<T, I>,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
// Transfer tokens from pallet
//Currency::transfer(NativeCurrencyId, &Self::account_id(), &recipient, amount)?;

// Deposit AP tokens to the pallet
<T as Config<I>>::Currency::transfer(

This comment has been minimized.

Copy link
@drewstone

drewstone Dec 6, 2022

Contributor

I think we ought to actually also burn the AP instead of transferring them to the pallet. What do you think @kwhuo68 ?

T::AnonymityPointsAssetId::get(),
&recipient,
&Self::account_id(),
amount,
)?;

// TODO: calc swap conversion amount

// Pallet sends reward tokens
<T as Config<I>>::Currency::transfer(
T::RewardAssetId::get(),
&Self::account_id(),
&recipient,
amount,
)?;

Ok(().into())
}
}
Expand Down
184 changes: 173 additions & 11 deletions pallets/anonymity-mining/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
use super::*;
use crate::{self as pallet_anonymity_mining};

use frame_support::{parameter_types, traits::Nothing};
use codec::{Decode, Encode};
use frame_support::{parameter_types, traits::Nothing, PalletId};
use frame_system as system;
use orml_currencies::{BasicCurrencyAdapter, NativeCurrencyOf};
//pub use pallet_balances;
pub use pallet::*;
use serde::{Deserialize, Serialize};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};
use sp_std::convert::{TryFrom, TryInto};
pub use webb_primitives::hasher::{HasherModule, InstanceHasher};
use webb_primitives::{AccountId, BlockNumber};
use webb_primitives::{field_ops::ArkworksIntoFieldBn254, verifying::ArkworksVerifierBn254};
pub use webb_primitives::{
hasher::{HasherModule, InstanceHasher},
hashing::ethereum::Keccak256HasherBn254,
ElementTrait,
};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

pub type AccountId = u64;
pub type Balance = u128;
pub type BlockNumber = u64;
pub type CurrencyId = u32;
pub type ChainId = u64;
/// Type for storing the id of an asset.
pub type AssetId = u32;
/// Signed version of Balance
pub type Amount = i128;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test where
Expand All @@ -25,10 +42,17 @@ frame_support::construct_runtime!(
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Event<T>},
HasherPallet: pallet_hasher::{Pallet, Call, Storage, Event<T>},
VAnchorVerifier: pallet_vanchor_verifier::{Pallet, Call, Storage, Event<T>},
LinkableTree: pallet_linkable_tree::{Pallet, Call, Storage, Event<T>},
MerkleTree: pallet_mt::{Pallet, Call, Storage, Event<T>},
Currencies: orml_currencies::{Pallet, Call},
Tokens: orml_tokens::{Pallet, Storage, Call, Event<T>},
AssetRegistry: pallet_asset_registry::{Pallet, Call, Storage, Event<T>},
AnonymityMining: pallet_anonymity_mining::{Pallet, Call, Storage, Event<T>, Config<T>},
VAnchor: pallet_vanchor::{Pallet, Call, Storage, Event<T>},
TokenWrapper: pallet_token_wrapper::{Pallet, Call, Storage, Event<T>},
KeyStorage: pallet_key_storage::{Pallet, Call, Storage, Event<T>, Config<T>},
AnonymityMining: pallet_anonymity_mining::{Pallet, Call, Storage, Event<T>, Config<T>}
}
);

Expand Down Expand Up @@ -109,17 +133,74 @@ impl orml_currencies::Config for Test {
type WeightInfo = ();
}

parameter_types! {
pub const TreeDeposit: u64 = 1;
pub const LeafDepositBase: u64 = 1;
pub const LeafDepositPerByte: u64 = 1;
pub const Two: u64 = 2;
pub const MaxTreeDepth: u8 = 32;
pub const RootHistorySize: u32 = 100;
// 21663839004416932945382355908790599225266501822907911457504978515578255421292
pub const DefaultZeroElement: Element = Element([
47, 229, 76, 96, 211, 172, 171, 243, 52, 58, 53, 182, 235, 161, 93, 180, 130, 27, 52,
15, 118, 231, 65, 226, 36, 150, 133, 237, 72, 153, 175, 108,
]);
}

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

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

fn from_bytes(input: &[u8]) -> Self {
let mut buf = [0u8; 32];
buf.iter_mut().zip(input).for_each(|(a, b)| *a = *b);
Self(buf)
}
}

impl pallet_mt::Config for Test {
type Currency = Balances;
type DataDepositBase = LeafDepositBase;
type DataDepositPerByte = LeafDepositPerByte;
type DefaultZeroElement = DefaultZeroElement;
type Element = Element;
type RuntimeEvent = RuntimeEvent;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type Hasher = HasherPallet;
type LeafIndex = u32;
type MaxTreeDepth = MaxTreeDepth;
type RootHistorySize = RootHistorySize;
type RootIndex = u32;
type StringLimit = StringLimit;
type TreeDeposit = TreeDeposit;
type TreeId = u32;
type Two = Two;
type WeightInfo = ();
}

parameter_types! {
pub const NativeCurrencyId: AssetId = 0;
pub const AnonymityPointsAssetId: AssetId = 1;
pub const RewardAssetId: AssetId = 2;
pub const RegistryStringLimit: u32 = 10;
}

/// Type for storing the id of an asset.
pub type AssetId = u32;
/// Signed version of Balance
pub type Amount = i128;
pub type Balance = u128;

impl pallet_asset_registry::Config for Test {
type AssetId = webb_primitives::AssetId;
type AssetNativeLocation = ();
Expand All @@ -139,11 +220,92 @@ parameter_types! {
pub const PotId: PalletId = PalletId(*b"py/anmin");
}

impl pallet_vanchor_verifier::Config for Test {
type RuntimeEvent = RuntimeEvent;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type Verifier = ArkworksVerifierBn254;
type WeightInfo = ();
}

impl pallet_hasher::Config for Test {
type RuntimeEvent = RuntimeEvent;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type Hasher = webb_primitives::hashing::ArkworksPoseidonHasherBn254;
type WeightInfo = ();
}

parameter_types! {
pub const TokenWrapperPalletId: PalletId = PalletId(*b"py/tkwrp");
pub const WrappingFeeDivider: u128 = 100;
}

impl pallet_token_wrapper::Config for Test {
type AssetRegistry = AssetRegistry;
type Currency = Currencies;
type RuntimeEvent = RuntimeEvent;
type PalletId = TokenWrapperPalletId;
type TreasuryId = TokenWrapperPalletId;
type WeightInfo = ();
type ProposalNonce = u32;
type WrappingFeeDivider = WrappingFeeDivider;
}

parameter_types! {
pub const HistoryLength: u32 = 30;
// Substrate standalone chain ID type
pub const ChainType: [u8; 2] = [2, 0];
pub const ChainIdentifier: u32 = 5;
}

impl pallet_linkable_tree::Config for Test {
type ChainId = ChainId;
type ChainType = ChainType;
type ChainIdentifier = ChainIdentifier;
type RuntimeEvent = RuntimeEvent;
type HistoryLength = HistoryLength;
type Tree = MerkleTree;
type WeightInfo = ();
}

parameter_types! {
pub const VAnchorPalletId: PalletId = PalletId(*b"py/vanch");
pub const MaxFee: Balance = 5;
pub const MaxExtAmount: Balance = 21;
pub const MaxCurrencyId: AssetId = AssetId::MAX - 1;
}

impl pallet_vanchor::Config for Test {
type Currency = Currencies;
type EthereumHasher = Keccak256HasherBn254;
type RuntimeEvent = RuntimeEvent;
type IntoField = ArkworksIntoFieldBn254;
type LinkableTree = LinkableTree;
type NativeCurrencyId = NativeCurrencyId;
type PalletId = VAnchorPalletId;
type MaxFee = MaxFee;
type MaxExtAmount = MaxExtAmount;
type MaxCurrencyId = MaxCurrencyId;
type TokenWrapper = TokenWrapper;
type PostDepositHook = ();
type ProposalNonce = u32;
type VAnchorVerifier = VAnchorVerifier;
type KeyStorage = KeyStorage;
type WeightInfo = ();
}

impl pallet_key_storage::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

impl pallet_anonymity_mining::Config for Test {
type RuntimeEvent = RuntimeEvent;
type PotId = PotId;
type Currency = Currencies;
type VAnchor = VAnchor;
type NativeCurrencyId = NativeCurrencyId;
type AnonymityPointsAssetId = AnonymityPointsAssetId;
type RewardAssetId = RewardAssetId;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
}

Expand Down
Loading

0 comments on commit c2b51ee

Please sign in to comment.