Skip to content

Commit

Permalink
Add staking and deposit pre-compiles (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest committed Dec 8, 2022
1 parent 4f78cb8 commit 1da4e6c
Show file tree
Hide file tree
Showing 14 changed files with 1,184 additions and 157 deletions.
43 changes: 43 additions & 0 deletions Cargo.lock

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

110 changes: 28 additions & 82 deletions precompile/assets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,38 @@
// You should have received a copy of the GNU General Public License
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

//! Test utilities

// crates.io
use codec::{Decode, Encode, MaxEncodedLen};
// frontier
use fp_evm::{Precompile, PrecompileSet};
use pallet_evm::IdentityAddressMapping;
// parity
use frame_support::{
pallet_prelude::Weight,
traits::{ConstU32, Everything},
};
use frame_system::EnsureRoot;
use frame_support::pallet_prelude::Weight;
use sp_core::{H160, H256, U256};
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};
use sp_std::{marker::PhantomData, prelude::*};
// darwinia
use crate::*;

pub type Block = frame_system::mocking::MockBlock<TestRuntime>;
pub type Balance = u128;
pub type AssetId = u64;
pub type InternalCall = ERC20AssetsCall<TestRuntime, AssetIdConverter>;
pub type AccountId = H160;
pub type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>;

pub const TEST_ID: AssetId = 1026;

#[derive(
Eq,
PartialEq,
Ord,
PartialOrd,
Clone,
Encode,
Decode,
Debug,
MaxEncodedLen,
scale_info::TypeInfo,
)]
#[derive(Clone, Encode, Decode, Debug, MaxEncodedLen, scale_info::TypeInfo)]
pub enum Account {
Alice,
Bob,
Charlie,
Bogus,
Precompile,
}

impl Default for Account {
fn default() -> Self {
Self::Bogus
}
}

impl Into<H160> for Account {
fn into(self) -> H160 {
match self {
Account::Alice => H160::repeat_byte(0xAA),
Account::Bob => H160::repeat_byte(0xBB),
Account::Charlie => H160::repeat_byte(0xCC),
Account::Bogus => H160::repeat_byte(0xDD),
Account::Precompile => H160::from_low_u64_be(TEST_ID),
}
}
Expand All @@ -91,24 +60,21 @@ impl From<Account> for H256 {
}
}

frame_support::parameter_types! {
pub const BlockHashCount: u64 = 250;
}
impl frame_system::Config for TestRuntime {
type AccountData = pallet_balances::AccountData<Balance>;
type AccountId = AccountId;
type BaseCallFilter = Everything;
type BaseCallFilter = frame_support::traits::Everything;
type BlockHashCount = ();
type BlockLength = ();
type BlockNumber = u64;
type BlockWeights = ();
type DbWeight = ();
type Hash = H256;
type Hashing = BlakeTwo256;
type Header = Header;
type Hashing = sp_runtime::traits::BlakeTwo256;
type Header = sp_runtime::testing::Header;
type Index = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type MaxConsumers = ConstU32<16>;
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type OnKilledAccount = ();
type OnNewAccount = ();
type OnSetCode = ();
Expand All @@ -121,27 +87,20 @@ impl frame_system::Config for TestRuntime {
type Version = ();
}

frame_support::parameter_types! {
pub const MaxLocks: u32 = 10;
pub const ExistentialDeposit: u64 = 0;
}
impl pallet_balances::Config for TestRuntime {
type AccountStore = System;
type Balance = Balance;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type MaxLocks = MaxLocks;
type ExistentialDeposit = frame_support::traits::ConstU128<0>;
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

frame_support::parameter_types! {
pub const MinimumPeriod: u64 = 6000 / 2;
}
impl pallet_timestamp::Config for TestRuntime {
type MinimumPeriod = MinimumPeriod;
type MinimumPeriod = ();
type Moment = u64;
type OnTimestampSet = ();
type WeightInfo = ();
Expand Down Expand Up @@ -189,21 +148,17 @@ impl AccountToAssetId<AccountId, AssetId> for AssetIdConverter {
}

frame_support::parameter_types! {
pub const TransactionByteFee: u64 = 1;
pub const ChainId: u64 = 42;
pub const BlockGasLimit: U256 = U256::MAX;
pub const WeightPerGas: Weight = Weight::from_ref_time(20_000);
pub PrecompilesValue: TestPrecompiles<TestRuntime> = TestPrecompiles::<_>::new();
}

pub type InternalCall = ERC20AssetsCall<TestRuntime, AssetIdConverter>;

impl pallet_evm::Config for TestRuntime {
type AddressMapping = IdentityAddressMapping;
type AddressMapping = pallet_evm::IdentityAddressMapping;
type BlockGasLimit = BlockGasLimit;
type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping<Self>;
type CallOrigin = pallet_evm::EnsureAddressRoot<AccountId>;
type ChainId = ChainId;
type ChainId = frame_support::traits::ConstU64<42>;
type Currency = Balances;
type FeeCalculator = ();
type FindAuthor = ();
Expand All @@ -217,43 +172,34 @@ impl pallet_evm::Config for TestRuntime {
type WithdrawOrigin = pallet_evm::EnsureAddressNever<AccountId>;
}

frame_support::parameter_types! {
pub const AssetDeposit: Balance = 0;
pub const ApprovalDeposit: Balance = 0;
pub const AssetsStringLimit: u32 = 50;
pub const MetadataDepositBase: Balance = 0;
pub const MetadataDepositPerByte: Balance = 0;
pub const AssetAccountDeposit: Balance = 0;
}

impl pallet_assets::Config for TestRuntime {
type ApprovalDeposit = ApprovalDeposit;
type AssetAccountDeposit = AssetAccountDeposit;
type AssetDeposit = AssetDeposit;
type ApprovalDeposit = ();
type AssetAccountDeposit = ();
type AssetDeposit = ();
type AssetId = AssetId;
type Balance = Balance;
type Currency = Balances;
type Extra = ();
type ForceOrigin = EnsureRoot<AccountId>;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type Freezer = ();
type MetadataDepositBase = MetadataDepositBase;
type MetadataDepositPerByte = MetadataDepositPerByte;
type MetadataDepositBase = ();
type MetadataDepositPerByte = ();
type RuntimeEvent = RuntimeEvent;
type StringLimit = AssetsStringLimit;
type StringLimit = frame_support::traits::ConstU32<50>;
type WeightInfo = ();
}

frame_support::construct_runtime! {
pub enum TestRuntime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
Block = frame_system::mocking::MockBlock<TestRuntime>,
NodeBlock = frame_system::mocking::MockBlock<TestRuntime>,
UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Timestamp: pallet_timestamp::{Pallet, Call, Storage},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
EVM: pallet_evm::{Pallet, Call, Storage, Config, Event<T>},
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>}
System: frame_system,
Timestamp: pallet_timestamp,
Balances: pallet_balances,
EVM: pallet_evm,
Assets: pallet_assets
}
}

Expand Down
3 changes: 1 addition & 2 deletions precompile/bls12-381/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#![cfg_attr(not(feature = "std"), no_std)]

// std
// core
use core::marker::PhantomData;
// crates.io
use milagro_bls::{AggregatePublicKey, AggregateSignature, PublicKey, Signature};
Expand All @@ -28,7 +28,6 @@ use precompile_utils::prelude::*;
use sp_std::vec::Vec;

pub(crate) const VERIFY_ESTIMATED_COST: u64 = 100_000;

pub struct BLS12381<T>(PhantomData<T>);

#[precompile_utils::precompile]
Expand Down
61 changes: 61 additions & 0 deletions precompile/deposit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[package]
authors = ["Darwinia Network <hello@darwinia.network>"]
description = "State storage precompiles for EVM pallet."
edition = "2021"
homepage = "https://darwinia.network"
license = "GPL-3.0"
name = "darwinia-precompile-deposit"
readme = "README.md"
repository = "https://github.com/darwinia-network/darwinia"
version = "6.0.0"

[dependencies]
# frontier
fp-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }
pallet-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }

# darwinia
darwinia-deposit = { default-features = false, path = "../../pallet/deposit"}

# moonbeam
precompile-utils = { default-features = false, git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30" }

# substrate
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }

[dev-dependencies]
# crates.io
codec = { package = "parity-scale-codec", version = "3.2" }
scale-info = { version = "2.3", features = ["derive"] }

# moonbeam
precompile-utils = { git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30", features = ["testing"] }

# substrate
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }

[features]
default = ["std"]
std = [
# frontier
"fp-evm/std",
"pallet-evm/std",

# darwinia
"darwinia-deposit/std",

# moonbeam
"precompile-utils/std",

# substrate
"frame-support/std",
"frame-system/std",
"sp-core/std",
"sp-std/std",
]
Loading

0 comments on commit 1da4e6c

Please sign in to comment.