Skip to content

Commit

Permalink
External operator slashes info pallet (#727)
Browse files Browse the repository at this point in the history
* Add pallet_external_validators, remove ValidatorManager

* Rename extrinsics and add tests

* Fix tests

* Fix benchmarks

* typescript-api

* first version stored

* Add traits

* slowly add stuff

* Some PR comments

* Change era every n sessions

* confirmed and unconfirmed, tests

* rename pallet

* WIP integration test and hooks

* good tests for once

* Benchmark new extrinsics

* Migrate queued keys instead of current validators

* typescript-api

* Add era index to hook

* impl for tuples

* Make tests pass

* adapt

* continue progress

* remove comments

* Add integration tests

* fixes but pallet incorporated to runtime

* added sessionINterface

* add offence handler

* current era and active era

* typescript-api

* add invulnerables

* Add one typescript test

* a few more fixes

* more tests

* console log

* benchmarks

* benchmarks generated

* a little bit of cleanup

* a bit more cleanup

* zepter

* toml-maid

* refactor tests

* run_to_block not working

* a few fixes2

* Unit tests still not working

* Fix unit tests

* test babe slashes

* slashes tests ts

* Start era 0 in session 0

* more slashing tests

* one more test

* Rewrite pallet logic, copy it from pallet_staking

* Test hook calls

* testing finished

* typescript-api

* Add era session start to trait

* test cleanup

* fmt and typescript api

* a few fixes here and there

* change a few things

* pr comments

* wip

* typescript-api

* typescript-api

* new tests

* typescript api

* try now

* Test migration

* fix last test

* clippy

* slashes formatting

* ts api

* api

* fix clippy

* typescript api new

* wip

* fmt

* first pr review fixes

* more pr feedback

* more fmt and pr fixes

* remove weight

* add doc

* add correct typescript api

---------

Co-authored-by: Tomasz Polaczyk <[email protected]>
  • Loading branch information
girazoki and tmpolaczyk authored Nov 5, 2024
1 parent fafdcc9 commit f8b66f3
Show file tree
Hide file tree
Showing 30 changed files with 3,654 additions and 846 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pallet-collator-assignment-runtime-api = { path = "pallets/collator-assignment/r
pallet-configuration = { path = "pallets/configuration", default-features = false }
pallet-data-preservers = { path = "pallets/data-preservers", default-features = false }
pallet-data-preservers-runtime-api = { path = "pallets/data-preservers/runtime-api", default-features = false }
pallet-external-validator-slashes = { path = "pallets/external-validator-slashes", default-features = false }
pallet-external-validators = { path = "pallets/external-validators", default-features = false }
pallet-inflation-rewards = { path = "pallets/inflation-rewards", default-features = false }
pallet-initializer = { path = "pallets/initializer", default-features = false }
Expand Down Expand Up @@ -325,6 +326,7 @@ fc-storage = { git = "https://github.com/moondance-labs/frontier", branch = "tan

# General (wasm)
bounded-collections = { version = "0.1.8", default-features = false }
finality-grandpa = { version = "0.16.2", default-features = false }
hex-literal = { version = "0.3.4" }
impl-trait-for-tuples = "0.2.2"
impls = "1.0.3"
Expand Down
68 changes: 68 additions & 0 deletions pallets/external-validator-slashes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[package]
name = "pallet-external-validator-slashes"
authors = { workspace = true }
description = "External validator info slashes"
edition = "2021"
license = "GPL-3.0-only"
version = "0.1.0"

[package.metadata.docs.rs]
targets = [ "x86_64-unknown-linux-gnu" ]

[lints]
workspace = true

[dependencies]
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
log = { workspace = true }
pallet-session = { workspace = true }
pallet-staking = { workspace = true }
parity-scale-codec = { workspace = true, features = [ "derive", "max-encoded-len" ] }
scale-info = { workspace = true }
sp-runtime = { workspace = true }
sp-staking = { workspace = true }
sp-std = { workspace = true }
tp-traits = { workspace = true }

[dev-dependencies]
sp-core = { workspace = true }
sp-io = { workspace = true }

[features]
default = [ "std" ]
std = [
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-session/std",
"pallet-staking/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-staking/std",
"sp-std/std",
"tp-traits/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"scale-info/std",
"sp-runtime/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
"tp-traits/runtime-benchmarks",
]

try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-session/try-runtime",
"pallet-staking/try-runtime",
"sp-runtime/try-runtime",
]
95 changes: 95 additions & 0 deletions pallets/external-validator-slashes/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (C) Moondance Labs Ltd.
// This file is part of Tanssi.

// Tanssi 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.

// Tanssi 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 Tanssi. If not, see <http://www.gnu.org/licenses/>

//! Benchmarking setup for pallet-external-validator-slashes

use super::*;

#[allow(unused)]
use crate::Pallet as ExternalValidatorSlashes;
use {
frame_benchmarking::{v2::*, BenchmarkError},
frame_system::RawOrigin,
pallet_session::{self as session},
sp_runtime::traits::TrailingZeroInput,
sp_std::prelude::*,
};

const MAX_SLASHES: u32 = 1000;

#[allow(clippy::multiple_bound_locations)]
#[benchmarks(where T: session::Config)]
mod benchmarks {
use super::*;

#[benchmark]
fn cancel_deferred_slash(s: Linear<1, MAX_SLASHES>) -> Result<(), BenchmarkError> {
let mut existing_slashes = Vec::new();
let era = T::EraIndexProvider::active_era().index;
let dummy = || T::AccountId::decode(&mut TrailingZeroInput::zeroes()).unwrap();
for _ in 0..MAX_SLASHES {
existing_slashes.push(Slash::<T::AccountId, T::SlashId>::default_from(dummy()));
}
Slashes::<T>::insert(
era.saturating_add(T::SlashDeferDuration::get())
.saturating_add(One::one()),
&existing_slashes,
);
let slash_indices: Vec<u32> = (0..s).collect();

#[extrinsic_call]
_(
RawOrigin::Root,
era.saturating_add(T::SlashDeferDuration::get())
.saturating_add(One::one()),
slash_indices,
);

assert_eq!(
Slashes::<T>::get(
&era.saturating_add(T::SlashDeferDuration::get())
.saturating_add(One::one())
)
.len(),
(MAX_SLASHES - s) as usize
);
Ok(())
}

#[benchmark]
fn force_inject_slash() -> Result<(), BenchmarkError> {
let era = T::EraIndexProvider::active_era().index;
let dummy = || T::AccountId::decode(&mut TrailingZeroInput::zeroes()).unwrap();
#[extrinsic_call]
_(RawOrigin::Root, era, dummy(), Perbill::from_percent(50));

assert_eq!(
Slashes::<T>::get(
&era.saturating_add(T::SlashDeferDuration::get())
.saturating_add(One::one())
)
.len(),
1_usize
);
Ok(())
}

impl_benchmark_test_suite!(
ExternalValidatorSlashes,
crate::mock::new_test_ext(),
crate::mock::Test,
);
}
Loading

0 comments on commit f8b66f3

Please sign in to comment.