Skip to content

Commit

Permalink
feat(runtime): Add a builtin actor for staking operations (#3843)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekovalev authored Jul 11, 2024
1 parent b90deda commit 1fdd1e0
Show file tree
Hide file tree
Showing 21 changed files with 1,917 additions and 141 deletions.
31 changes: 30 additions & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ members = [
"examples/wat",
"galloc",
"gbuiltins/bls381",
"gbuiltins/staking",
"gcli",
"gclient",
"gcore",
Expand Down Expand Up @@ -202,6 +203,7 @@ common = { package = "gear-common", path = "common", default-features = false }
core-processor = { package = "gear-core-processor", path = "core-processor", default-features = false }
galloc = { path = "galloc" }
gbuiltin-bls381 = { path = "gbuiltins/bls381", default-features = false }
gbuiltin-staking = { path = "gbuiltins/staking" }
gcore = { path = "gcore" }
gcli = { path = "gcli" }
gclient = { path = "gclient" }
Expand Down Expand Up @@ -444,6 +446,7 @@ demo-reserve-gas = { path = "examples/reserve-gas", default-features = false }
demo-rwlock = { path = "examples/rwlock" }
demo-send-from-reservation = { path = "examples/send-from-reservation" }
demo-signal-entry = { path = "examples/signal-entry", default-features = false }
demo-staking-broker = { path = "examples/staking-broker" }
demo-state-rollback = { path = "examples/state-rollback" }
demo-sync-duplicate = { path = "examples/sync-duplicate" }
demo-vec = { path = "examples/vec" }
Expand Down
131 changes: 131 additions & 0 deletions common/src/pallet_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,134 @@ macro_rules! impl_pallet_authorship_inner {
$crate::impl_pallet_authorship_inner!($runtime, $($( $rest )*)?);
};
}

#[macro_export]
macro_rules! impl_pallet_staking {
($( $tokens:tt )*) => {
#[allow(dead_code)]
pub struct DummyEraPayout;
impl pallet_staking::EraPayout<u128> for DummyEraPayout {
fn era_payout(
_total_staked: u128,
total_issuance: u128,
_era_duration_millis: u64,
) -> (u128, u128) {
// At each era have 1% `total_issuance` increase
(Permill::from_percent(1) * total_issuance, 0)
}
}

type DataProviderInfo = (
AccountId,
BlockNumber,
pallet_staking::Pallet<Test>,
ConstU32<100>,
);

#[allow(dead_code)]
type StakingConfigEraPayout = DummyEraPayout;
#[allow(dead_code)]
type StakingConfigSlash = ();
#[allow(dead_code)]
type StakingConfigReward = ();
#[allow(dead_code)]
type StakingConfigNextNewSession = ();
#[allow(dead_code)]
type StakingConfigElectionProvider =
frame_election_provider_support::NoElection<DataProviderInfo>;
#[allow(dead_code)]
type StakingConfigGenesisElectionProvider =
frame_election_provider_support::NoElection<DataProviderInfo>;

mod pallet_tests_staking_config_impl {
use super::*;

$crate::impl_pallet_staking_inner!($( $tokens )*);
}
};
}

#[macro_export]
macro_rules! impl_pallet_staking_inner {
($runtime:ty$(,)?) => {
parameter_types! {
// 6 sessions in an era
pub const SessionsPerEra: u32 = 6;
// 8 eras for unbonding
pub const BondingDuration: u32 = 8;
pub const SlashDeferDuration: u32 = 7;
pub const MaxExposurePageSize: u32 = 512;
pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17);
pub const HistoryDepth: u32 = 84;
pub const MaxNominations: u32 = 16;
}

impl pallet_staking::Config for Test {
type Currency = Balances;
type UnixTime = Timestamp;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
type CurrencyToVote = ();
type ElectionProvider = StakingConfigElectionProvider;
type GenesisElectionProvider = StakingConfigGenesisElectionProvider;
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = StakingConfigSlash;
type Reward = StakingConfigReward;
type SessionsPerEra = SessionsPerEra;
type BondingDuration = BondingDuration;
type SlashDeferDuration = SlashDeferDuration;
type AdminOrigin = frame_system::EnsureRoot<AccountId>;
type SessionInterface = ();
type EraPayout = StakingConfigEraPayout;
type NextNewSession = StakingConfigNextNewSession;
type MaxExposurePageSize = MaxExposurePageSize;
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type NominationsQuota = pallet_staking::FixedNominationsQuota<16>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = HistoryDepth;
type EventListeners = ();
type WeightInfo = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
}
};

($runtime:ty, EraPayout = $era_payout:ty $(, $( $rest:tt )*)?) => {
type StakingConfigEraPayout = $era_payout;

$crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
};

($runtime:ty, Slash = $slash:ty $(, $( $rest:tt )*)?) => {
type StakingConfigSlash = $slash;

$crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
};

($runtime:ty, Reward = $reward:ty $(, $( $rest:tt )*)?) => {
type StakingConfigReward = $reward;

$crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
};

($runtime:ty, NextNewSession = $next_new_session:ty $(, $( $rest:tt )*)?) => {
type StakingConfigNextNewSession = $next_new_session;

$crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
};

($runtime:ty, ElectionProvider = $election_provider:ty $(, $( $rest:tt )*)?) => {
type StakingConfigElectionProvider = $election_provider;

$crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
};

(
$runtime:ty, GenesisElectionProvider = $genesis_election_provider:ty $(, $( $rest:tt )*)?
) => {
type StakingConfigGenesisElectionProvider = $genesis_election_provider;

$crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
};
}
24 changes: 24 additions & 0 deletions examples/staking-broker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "demo-staking-broker"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
gbuiltin-staking.workspace = true
gstd.workspace = true
hashbrown.workspace = true
hex.workspace = true
hex-literal.workspace = true
parity-scale-codec.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true

[features]
debug = ["gstd/debug"]
std = []
default = ["std"]
21 changes: 21 additions & 0 deletions examples/staking-broker/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is part of Gear.

// Copyright (C) 2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

fn main() {
gear_wasm_builder::build();
}
33 changes: 33 additions & 0 deletions examples/staking-broker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This file is part of Gear.
//
// Copyright (C) 2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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

#[cfg(feature = "std")]
mod code {
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
}

#[cfg(feature = "std")]
pub use code::WASM_BINARY_OPT as WASM_BINARY;

#[cfg(not(feature = "std"))]
pub const WASM_BINARY: &[u8] = &[];

#[cfg(not(feature = "std"))]
pub mod wasm;
Loading

0 comments on commit 1fdd1e0

Please sign in to comment.