Skip to content

Commit

Permalink
Starlight <> Ethereum: Rewards mapping (#761)
Browse files Browse the repository at this point in the history
* WIP does not compile, needs cherry pick AggregateMessageOrigin

* Add eth outbound queue and system pallets

* Use fork branch tomasz-generic-aggregate-message-origin

* Update Cargo.lock

* Fix outdated fork branches

* Fix tests and benchmarks

* Fix compilation and toml

* WIP root_test_send_msg_to_eth

* Use custom Command enum and do_process_message impl

* Create validate trait

* Fix tests

* Fix unused variable

* Fix toml

* Add integration test

* pnpm lint and fmt

* typescript-api

* Also support original Command enum

* typescript-api

* Add test using snowbridge message

* Add different origin for custom snowbridge messages vs original ones

* add initial ReportSlashes variant

* PR review

* Fix tests

* polkadot-sdk branch merged, use stable2407 again

* typescript-api

* small PoC for sending slashes message on_initialize

* minor fixes

* Test message limits: max numMsg and msgSize

* Add benchmarks

* fmt

* Fix accidental refactor

* typescript-api

* only map rewards

* PR comments

* Copy

* refactor mapping and add test

* fix match pattern

* start adding runtime-api

* more progress on runtime apis

* add test for merkle proofs

* fmt

* fix external-validators-rewards mock

* toml fmt

* rust fmt

* add more checks to rust test

* add TS test for runtime apis

* fix test

* add some docs

* add EraInflationProvider

* PR suggestion

* add benchmark for on_era_end

* add benchmark for on_era_end

* remove comment

* pr comments

* add TODO for gas cost
  • Loading branch information
Agusrodri authored Dec 10, 2024
1 parent a4aed7e commit 354bbb8
Show file tree
Hide file tree
Showing 19 changed files with 857 additions and 52 deletions.
15 changes: 15 additions & 0 deletions 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 @@ -63,6 +63,7 @@ pallet-data-preservers-runtime-api = { path = "pallets/data-preservers/runtime-a
pallet-external-validator-slashes = { path = "pallets/external-validator-slashes", default-features = false }
pallet-external-validators = { path = "pallets/external-validators", default-features = false }
pallet-external-validators-rewards = { path = "pallets/external-validators-rewards", default-features = false }
pallet-external-validators-rewards-runtime-api = { path = "pallets/external-validators-rewards/runtime-api", default-features = false }
pallet-inflation-rewards = { path = "pallets/inflation-rewards", default-features = false }
pallet-initializer = { path = "pallets/initializer", default-features = false }
pallet-invulnerables = { path = "pallets/invulnerables", default-features = false }
Expand Down Expand Up @@ -260,9 +261,11 @@ xcm-runtime-apis = { git = "https://github.com/moondance-labs/polkadot-sdk", bra

# Bridges (wasm)
alloy-sol-types = { version = "0.4.2", default-features = false }
bridge-hub-common = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2407", default-features = false }
milagro-bls = { package = "snowbridge-milagro-bls", version = "1.5.4", default-features = false }
snowbridge-beacon-primitives = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-core = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-outbound-queue-merkle-tree = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-pallet-ethereum-client = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-pallet-inbound-queue = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-pallet-inbound-queue-fixtures = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
Expand Down
13 changes: 12 additions & 1 deletion pallets/external-validators-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ targets = [ "x86_64-unknown-linux-gnu" ]
workspace = true

[dependencies]
log = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true, features = [ "derive" ] }

frame-support = { workspace = true }
frame-system = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-staking = { workspace = true }
sp-std = { workspace = true }
tp-bridge = { workspace = true }
tp-traits = { workspace = true }

frame-benchmarking = { workspace = true }
Expand All @@ -29,11 +32,13 @@ pallet-balances = { workspace = true, optional = true }
pallet-session = { workspace = true, features = [ "historical" ] }
runtime-parachains = { workspace = true }

snowbridge-core = { workspace = true }
snowbridge-outbound-queue-merkle-tree = { workspace = true }

polkadot-primitives = { workspace = true }

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

[features]
Expand All @@ -42,18 +47,22 @@ std = [
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"pallet-session/std",
"pallet-timestamp/std",
"parity-scale-codec/std",
"polkadot-primitives/std",
"runtime-parachains/std",
"scale-info/std",
"snowbridge-core/std",
"snowbridge-outbound-queue-merkle-tree/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-staking/std",
"sp-std/std",
"tp-bridge/std",
"tp-traits/std",
]
runtime-benchmarks = [
Expand All @@ -64,8 +73,10 @@ runtime-benchmarks = [
"pallet-timestamp/runtime-benchmarks",
"polkadot-primitives/runtime-benchmarks",
"runtime-parachains/runtime-benchmarks",
"snowbridge-core/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
"tp-bridge/runtime-benchmarks",
"tp-traits/runtime-benchmarks",
]

Expand Down
28 changes: 28 additions & 0 deletions pallets/external-validators-rewards/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "pallet-external-validators-rewards-runtime-api"
authors = { workspace = true }
description = "Runtime API definition of pallet-external-validators-rewards"
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]
parity-scale-codec = { workspace = true }
snowbridge-outbound-queue-merkle-tree = { workspace = true }
sp-api = { workspace = true }
sp-core = { workspace = true }

[features]
default = [ "std" ]
std = [
"parity-scale-codec/std",
"snowbridge-outbound-queue-merkle-tree/std",
"sp-api/std",
"sp-core/std",
]
32 changes: 32 additions & 0 deletions pallets/external-validators-rewards/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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/>

//! Runtime API for External Validators Rewards pallet
#![cfg_attr(not(feature = "std"), no_std)]

use snowbridge_outbound_queue_merkle_tree::MerkleProof;

sp_api::decl_runtime_apis! {
pub trait ExternalValidatorsRewardsApi<AccountId, EraIndex>
where
AccountId: parity_scale_codec::Codec,
EraIndex: parity_scale_codec::Codec,
{
fn generate_rewards_merkle_proof(account_id: AccountId, era_index: EraIndex) -> Option<MerkleProof>;
fn verify_rewards_merkle_proof(merkle_proof: MerkleProof) -> bool;
}
}
81 changes: 81 additions & 0 deletions pallets/external-validators-rewards/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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_validators_rewards
use super::*;

#[allow(unused)]
use crate::Pallet as ExternalValidatorsRewards;
use {
frame_benchmarking::{account, v2::*, BenchmarkError},
frame_support::traits::{Currency, Get},
sp_std::prelude::*,
tp_traits::OnEraEnd,
};

const SEED: u32 = 0;

fn create_funded_user<T: Config + pallet_balances::Config>(
string: &'static str,
n: u32,
balance_factor: u32,
) -> T::AccountId {
let user = account(string, n, SEED);
let balance = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::minimum_balance()
* balance_factor.into();
let _ = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::make_free_balance_be(
&user, balance,
);
user
}

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

// worst case for the end of an era.
#[benchmark]
fn on_era_end() -> Result<(), BenchmarkError> {
frame_system::Pallet::<T>::set_block_number(0u32.into());

let mut era_reward_points = EraRewardPoints::default();
era_reward_points.total = T::BackingPoints::get() * 1000;

for i in 0..1000 {
let account_id = create_funded_user::<T>("candidate", i, 100);
era_reward_points
.individual
.insert(account_id, T::BackingPoints::get());
}

<RewardPointsForEra<T>>::insert(1u32, era_reward_points);

#[block]
{
<ExternalValidatorsRewards<T> as OnEraEnd>::on_era_end(1u32);
}

Ok(())
}

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

0 comments on commit 354bbb8

Please sign in to comment.