Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pallet reward inflation #277

Merged
merged 60 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
e019e26
reward trait
nanocryk Sep 18, 2023
14aa1c1
reward distribution code
nanocryk Sep 18, 2023
3d1793e
tests (wip)
nanocryk Sep 18, 2023
9fb4f66
reward distribution tests
nanocryk Sep 19, 2023
36583d3
fix test + remove warning
nanocryk Sep 20, 2023
d83f92e
chore: create pallet inflation-rewards and primitive tp-maths
librelois Sep 21, 2023
862f19b
chore: move trait DistributeRewards in tp-traits
librelois Sep 21, 2023
8463feb
ref: move AuthorNotingHook in tp-traits
librelois Sep 25, 2023
8c1448c
ref: pooled-staking: use Credit for rewards
librelois Sep 25, 2023
1f1dd5a
reword pallet inflation rewards
librelois Sep 25, 2023
875953b
ref: use bounded vec for container chains
librelois Sep 28, 2023
47a5a8f
fix existing rust tests compilation
librelois Sep 28, 2023
c686196
reward orchestrator chain
librelois Sep 28, 2023
3368ae2
Merge branch 'master' into elois-pallet-rewards
librelois Sep 28, 2023
88a55fc
add rust tests for pallet inflation-rewards
librelois Sep 28, 2023
a11b3df
fmt
librelois Sep 28, 2023
ef72074
remove dead code
nanocryk Sep 29, 2023
f09ff87
Merge remote-tracking branch 'origin/master' into jeremy-staking-rewa…
nanocryk Sep 29, 2023
263adff
note container author only if block increase
librelois Sep 29, 2023
e5fff7c
Merge branch 'jeremy-staking-rewards-distribution' into elois-pallet-…
librelois Sep 29, 2023
3565bb4
fmt
librelois Oct 2, 2023
b2d102e
Merge remote-tracking branch 'origin/master' into elois-pallet-rewards
nanocryk Oct 11, 2023
595246d
cleanup merge
nanocryk Oct 11, 2023
d777ccc
reward author if it increases
girazoki Oct 13, 2023
a97b3a2
Re-design on-container-author-noted to simplify it for now
girazoki Oct 13, 2023
1188dc7
Merge remote-tracking branch 'origin/master' into elois-pallet-rewards
girazoki Oct 13, 2023
ab0dce4
adapt to 1.1.0
girazoki Oct 13, 2023
8bb84f2
fmt
girazoki Oct 13, 2023
e25be7d
fix merge mistakes and benchmarking
girazoki Oct 13, 2023
25b0ae5
types and unitest error
girazoki Oct 13, 2023
49d1028
take invulnerables into account
nanocryk Oct 13, 2023
8db89fb
fmt
nanocryk Oct 13, 2023
bb5eb07
let's use collatorAssignment to fethc the author account
girazoki Oct 13, 2023
cf86006
hack around how to get the collator account to be rewarded
girazoki Oct 13, 2023
a1a4e6a
fix rust tests
girazoki Oct 13, 2023
74e42e5
TODO: important!
girazoki Oct 13, 2023
38b8fb2
Merge remote-tracking branch 'origin/master' into HEAD
girazoki Oct 17, 2023
dc42ba4
events for inflation rewards and benchmark for rewards
girazoki Oct 18, 2023
9ce62e0
add runtime event to mock
girazoki Oct 18, 2023
8eec4c5
substitute extractFee for extractFeeAuthor
girazoki Oct 18, 2023
b49d627
fix ts-tests and local interfaceS
girazoki Oct 18, 2023
c24d1c5
fix linting and fmt
girazoki Oct 18, 2023
6f82135
first ts-tests dedicated to inflation
girazoki Oct 18, 2023
cc79907
add new parafchain bond account
girazoki Oct 19, 2023
55ad901
add more test
girazoki Oct 19, 2023
9109b24
fmt lint
girazoki Oct 19, 2023
daa03d0
Merge remote-tracking branch 'origin/master' into elois-pallet-rewards
nanocryk Oct 20, 2023
347a928
wire in container rewarding
girazoki Oct 20, 2023
96d4668
fmt
girazoki Oct 20, 2023
5d51452
add more test, including container-chain reward
girazoki Oct 20, 2023
bf3d9c8
try-runtime
girazoki Oct 20, 2023
c264068
staking rewards tests, not through invlunerables
girazoki Oct 23, 2023
e84ee7d
test fixed
girazoki Oct 24, 2023
4fb9ae2
a couple of tests for staking
girazoki Oct 26, 2023
3be9f61
make distribute_rewards transactional (#299)
nanocryk Oct 27, 2023
c570d15
reward the author of the first container block
librelois Oct 30, 2023
238b5ee
PR feedback
nanocryk Oct 30, 2023
cda81e9
compile
girazoki Oct 30, 2023
323584f
Inflation rewards integration tests (#295)
nanocryk Oct 31, 2023
be84d2d
a few todos added and cleaning the remaining feedback
girazoki Oct 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ authors = [ "Moondance Labs" ]
repository = "https://github.com/moondance-labs/tanssi"

[workspace.dependencies]

# Members
pallet-author-noting = { path = "pallets/author-noting", default-features = false }
pallet-author-noting-runtime-api = { path = "pallets/author-noting/rpc/runtime-api", default-features = false }
Expand All @@ -28,6 +29,7 @@ pallet-authority-mapping = { path = "pallets/authority-mapping", default-feature
pallet-collator-assignment = { path = "pallets/collator-assignment", default-features = false }
pallet-collator-assignment-runtime-api = { path = "pallets/collator-assignment/rpc/runtime-api", default-features = false }
pallet-configuration = { path = "pallets/configuration", 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 }
pallet-pooled-staking = { path = "pallets/pooled-staking", default-features = false }
Expand All @@ -52,6 +54,7 @@ tp-collator-assignment = { path = "primitives/collator-assignment", default-feat
tp-consensus = { path = "primitives/consensus", default-features = false }
tp-container-chain-genesis-data = { path = "primitives/container-chain-genesis-data", default-features = false }
tp-core = { path = "primitives/core", default-features = false }
tp-maths = { path = "primitives/maths", default-features = false }
tp-traits = { path = "primitives/traits", default-features = false }

# Moonkit (wasm)
Expand Down Expand Up @@ -217,6 +220,7 @@ fc-rpc-core = { git = "https://github.com/moondance-labs/frontier", branch = "ta
fc-storage = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }

# General (wasm)
bounded-collections = { version = "0.1.8", default-features = false }
hex-literal = { version = "0.3.4" }
log = { version = "0.4.17", default-features = false }
serde = { version = "1.0.152", default-features = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ use {
super::*,
crate::ContainerChainAuthoritiesInherentData,
async_trait::async_trait,
cumulus_primitives_core::relay_chain::BlockId,
cumulus_primitives_core::{
relay_chain::{
CommittedCandidateReceipt, HeadData, OccupiedCoreAssumption, SessionIndex, ValidatorId,
BlockId, CommittedCandidateReceipt, HeadData, OccupiedCoreAssumption, SessionIndex,
ValidatorId,
},
InboundDownwardMessage, InboundHrmpMessage, ParaId, PersistedValidationData,
},
cumulus_relay_chain_interface::{PHash, PHeader, RelayChainInterface, RelayChainResult},
futures::Stream,
polkadot_overseer::Handle,
sc_client_api::HeaderBackend,
sc_client_api::{StorageKey, StorageProvider},
sc_client_api::{HeaderBackend, StorageKey, StorageProvider},
sp_inherents::{InherentData, InherentDataProvider},
sp_state_machine::{prove_read, StorageValue},
std::{collections::BTreeMap, pin::Pin, sync::Arc},
Expand Down
16 changes: 8 additions & 8 deletions container-chains/templates/frontier/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ use {
sc_network::config::FullNetworkConfiguration,
};
// std
use futures::FutureExt;
use sc_client_api::Backend;
use sc_transaction_pool_api::OffchainTransactionPoolFactory;
use std::{
collections::BTreeMap,
sync::{Arc, Mutex},
time::Duration,
};
use {
cumulus_client_cli::CollatorOptions,
cumulus_primitives_parachain_inherent::{
MockValidationDataInherentDataProvider, MockXcmConfig,
},
fc_consensus::FrontierBlockImport,
futures::FutureExt,
nimbus_primitives::NimbusId,
sc_client_api::Backend,
sc_transaction_pool_api::OffchainTransactionPoolFactory,
sp_consensus_aura::SlotDuration,
sp_core::Pair,
std::{
collections::BTreeMap,
sync::{Arc, Mutex},
time::Duration,
},
};
// Local Runtime Types
use {
Expand Down
2 changes: 1 addition & 1 deletion container-chains/templates/simple/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use {
};

// Substrate Imports
use futures::FutureExt;
use {
futures::FutureExt,
sc_client_api::Backend,
sc_consensus::ImportQueue,
sc_executor::NativeElseWasmExecutor,
Expand Down
3 changes: 1 addition & 2 deletions node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.

use futures::FutureExt;
use sc_transaction_pool_api::OffchainTransactionPoolFactory;
#[allow(deprecated)]
use {
crate::{
Expand Down Expand Up @@ -82,6 +80,7 @@ use {
},
tokio::sync::mpsc::{unbounded_channel, UnboundedSender},
};
use {futures::FutureExt, sc_transaction_pool_api::OffchainTransactionPoolFactory};

type FullBackend = TFullBackend<Block>;
type MaybeSelectChain = Option<sc_consensus::LongestChain<FullBackend, Block>>;
Expand Down
1 change: 1 addition & 0 deletions pallets/author-noting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tp-core = { workspace = true }
tp-traits = { workspace = true }

[dev-dependencies]
bounded-collections = { workspace = true }
hex-literal = { workspace = true }
polkadot-parachain-primitives = { workspace = true }
polkadot-primitives = { workspace = true }
Expand Down
39 changes: 34 additions & 5 deletions pallets/author-noting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use {
sp_runtime::{traits::Header, DispatchResult, RuntimeString},
tp_author_noting_inherent::INHERENT_IDENTIFIER,
tp_core::well_known_keys::PARAS_HEADS_INDEX,
tp_traits::{GetContainerChainAuthor, GetCurrentContainerChains},
tp_traits::{AuthorNotingHook, GetContainerChainAuthor, GetCurrentContainerChains},
};

#[cfg(test)]
Expand Down Expand Up @@ -81,6 +81,11 @@ pub mod pallet {

type RelayChainStateProvider: cumulus_pallet_parachain_system::RelaychainStateProvider;

/// An entry-point for higher-level logic to react to containers chains authoring.
///
/// Typically, this can be a hook to reward block authors.
type AuthorNotingHook: AuthorNotingHook<Self::AccountId>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -140,7 +145,7 @@ pub mod pallet {
);

let registered_para_ids = T::ContainerChains::current_container_chains();
let total_weight =
let mut total_weight =
T::WeightInfo::set_latest_author_data(registered_para_ids.len() as u32);

// We do this first to make sure we dont do 2 reads (parachains and relay state)
Expand All @@ -151,15 +156,39 @@ pub mod pallet {
relay_storage_proof,
} = data;

let relay_storage_root =
T::RelayChainStateProvider::current_relay_chain_state().state_root;
let relay_chain_state = T::RelayChainStateProvider::current_relay_chain_state();
let relay_storage_root = relay_chain_state.state_root;
let relay_storage_rooted_proof =
GenericStateProof::new(relay_storage_root, relay_storage_proof)
.expect("Invalid relay chain state proof");

for para_id in registered_para_ids {
match Self::fetch_block_info_from_proof(&relay_storage_rooted_proof, para_id) {
Ok(block_info) => LatestAuthor::<T>::insert(para_id, block_info),
Ok(block_info) => {
LatestAuthor::<T>::mutate(
para_id,
|maybe_old_block_info: &mut Option<ContainerChainBlockInfo<T>>| {
if let Some(ref mut old_block_info) = maybe_old_block_info {
if block_info.block_number > old_block_info.block_number {
// We only reward author if the block increases
total_weight = total_weight.saturating_add(
T::AuthorNotingHook::on_container_author_noted(
&block_info.author,
block_info.block_number,
para_id,
),
);
let _ = core::mem::replace(old_block_info, block_info);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't care about the old value you don't need mem::replace

Suggested change
let _ = core::mem::replace(old_block_info, block_info);
*old_block_info = block_info;

}
} else {
let _ = core::mem::replace(
maybe_old_block_info,
girazoki marked this conversation as resolved.
Show resolved Hide resolved
Some(block_info),
);
}
},
);
}
Err(e) => log::warn!(
"Author-noting error {:?} found in para {:?}",
e,
Expand Down
19 changes: 12 additions & 7 deletions pallets/author-noting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use {
crate::{self as author_noting_pallet, Config},
bounded_collections::bounded_vec,
cumulus_pallet_parachain_system::{RelayChainState, RelaychainStateProvider},
cumulus_primitives_core::ParaId,
frame_support::{
Expand All @@ -25,14 +26,15 @@ use {
ConstU32, ConstU64, Everything, OnFinalize, OnInitialize, UnfilteredDispatchable,
},
},
frame_system::pallet_prelude::BlockNumberFor,
frame_system::RawOrigin,
frame_system::{pallet_prelude::BlockNumberFor, RawOrigin},
parity_scale_codec::{Decode, Encode},
polkadot_parachain_primitives::primitives::RelayChainBlockNumber,
polkadot_primitives::Slot,
sp_core::H256,
sp_runtime::traits::{BlakeTwo256, IdentityLookup},
sp_runtime::BuildStorage,
sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BoundedVec, BuildStorage,
},
sp_state_machine::StorageProof,
test_relay_sproof_builder::ParaHeaderSproofBuilder,
};
Expand Down Expand Up @@ -116,13 +118,13 @@ impl mock_data::Config for Test {}
#[derive(Clone, Encode, Decode, PartialEq, sp_core::RuntimeDebug, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct Mocks {
pub container_chains: Vec<ParaId>,
pub container_chains: BoundedVec<ParaId, ConstU32<5>>,
}

impl Default for Mocks {
fn default() -> Self {
Self {
container_chains: vec![1001.into()],
container_chains: bounded_vec![1001.into()],
}
}
}
Expand All @@ -141,7 +143,9 @@ impl tp_traits::GetContainerChainAuthor<AccountId> for MockAuthorFetcher {
pub struct MockContainerChainGetter;

impl tp_traits::GetCurrentContainerChains for MockContainerChainGetter {
fn current_container_chains() -> Vec<ParaId> {
type MaxContainerChains = ConstU32<5>;

fn current_container_chains() -> BoundedVec<ParaId, Self::MaxContainerChains> {
MockData::mock().container_chains
}

Expand Down Expand Up @@ -181,6 +185,7 @@ impl Config for Test {
type ContainerChainAuthor = MockAuthorFetcher;
type SelfParaId = ParachainId;
type ContainerChains = MockContainerChainGetter;
type AuthorNotingHook = ();
type RelayChainStateProvider = MockRelayStateProvider;
}

Expand Down
3 changes: 2 additions & 1 deletion pallets/author-noting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use {
crate::{mock::*, ContainerChainBlockInfo, Event},
bounded_collections::bounded_vec,
cumulus_primitives_core::ParaId,
frame_support::{
assert_ok,
Expand Down Expand Up @@ -166,7 +167,7 @@ fn test_author_id_insertion_many_paras() {
// Writing to this pallet storage will only change the sproofs of the next block,
// not the ones of the current block
MockData::mutate(|m| {
m.container_chains = vec![1001.into(), 1002.into()];
m.container_chains = bounded_vec![1001.into(), 1002.into()];
});
assert_eq!(
AuthorNoting::latest_author(ParaId::from(1001)),
Expand Down
9 changes: 8 additions & 1 deletion pallets/collator-assignment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub mod pallet {
// which guarantees that at least one full session has passed before any changes are applied.
type HostConfiguration: GetHostConfiguration<Self::SessionIndex>;
type ContainerChains: GetSessionContainerChains<Self::SessionIndex>;
type SelfParaId: Get<ParaId>;
type ShouldRotateAllCollators: ShouldRotateAllCollators<Self::SessionIndex>;
type GetRandomnessForNextBlock: GetRandomnessForNextBlock<BlockNumberFor<Self>>;
type RemoveInvulnerables: RemoveInvulnerables<Self::AccountId>;
Expand Down Expand Up @@ -411,9 +412,15 @@ pub mod pallet {
}

impl<T: Config> GetContainerChainAuthor<T::AccountId> for Pallet<T> {
// TODO: pending collator container chain if the block is a session change!
fn author_for_slot(slot: Slot, para_id: ParaId) -> Option<T::AccountId> {
let assigned_collators = Pallet::<T>::collator_container_chain();
let collators = assigned_collators.container_chains.get(&para_id)?;
let collators = if para_id == T::SelfParaId::get() {
Some(&assigned_collators.orchestrator_chain)
} else {
assigned_collators.container_chains.get(&para_id)
}?;

if collators.is_empty() {
// Avoid division by zero below
return None;
Expand Down
Loading
Loading