Skip to content

Commit

Permalink
Don't send message to Ethereum with zero rewards (#847)
Browse files Browse the repository at this point in the history
Co-authored-by: girazoki <[email protected]>
  • Loading branch information
nanocryk and girazoki authored Feb 7, 2025
1 parent bdd3830 commit fae9632
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ fn dancelight_testnet_genesis(
x.stash.clone()
})
.collect::<Vec<_>>(),
..Default::default()
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

use {
crate::{
BlockProductionCost, CollatorAssignmentCost, ExternalValidatorSlashes, MessageQueue,
RuntimeCall,
Authorship, BlockProductionCost, CollatorAssignmentCost, ExternalValidatorSlashes,
MessageQueue, RuntimeCall,
},
babe_primitives::{
digests::{PreDigest, SecondaryPlainPreDigest},
Expand Down Expand Up @@ -250,6 +250,7 @@ pub fn start_block() -> RunSummary {

// Initialize the new block
Babe::on_initialize(System::block_number());
Authorship::on_initialize(System::block_number());
ContainerRegistrar::on_initialize(System::block_number());
ExternalValidatorSlashes::on_initialize(System::block_number());
Session::on_initialize(System::block_number());
Expand Down Expand Up @@ -280,6 +281,7 @@ pub fn end_block() {
advance_block_state_machine(RunBlockState::End(block_number));
// Finalize the block
Babe::on_finalize(System::block_number());
Authorship::on_finalize(System::block_number());
Session::on_finalize(System::block_number());
Grandpa::on_finalize(System::block_number());
TransactionPayment::on_finalize(System::block_number());
Expand Down Expand Up @@ -335,6 +337,8 @@ pub struct ExtBuilder {
balances: Vec<(AccountId, Balance)>,
// [validator, amount]
validators: Vec<(AccountId, Balance)>,
// [validator, amount]
external_validators: Vec<(AccountId, Balance)>,
// [collator, amount]
collators: Vec<(AccountId, Balance)>,
// sudo key
Expand Down Expand Up @@ -362,6 +366,7 @@ impl Default for ExtBuilder {
(AccountId::from(ALICE), 210 * UNIT),
(AccountId::from(BOB), 100 * UNIT),
],
external_validators: vec![],
collators: Default::default(),
sudo: Default::default(),
para_ids: Default::default(),
Expand Down Expand Up @@ -398,6 +403,11 @@ impl ExtBuilder {
self
}

pub fn with_external_validators(mut self, validators: Vec<(AccountId, Balance)>) -> Self {
self.external_validators = validators;
self
}

pub fn with_collators(mut self, collators: Vec<(AccountId, Balance)>) -> Self {
self.collators = collators;
self
Expand Down Expand Up @@ -614,6 +624,32 @@ impl ExtBuilder {
keys.extend(validator_keys)
}

if !self.external_validators.is_empty() {
let validator_keys: Vec<_> = self
.external_validators
.clone()
.into_iter()
.map(|(account, _balance)| {
let authority_keys =
get_authority_keys_from_seed(&account.to_string(), self.keystore.as_ref());
(
account.clone(),
account,
crate::SessionKeys {
babe: authority_keys.babe.clone(),
grandpa: authority_keys.grandpa.clone(),
para_validator: authority_keys.para_validator.clone(),
para_assignment: authority_keys.para_assignment.clone(),
authority_discovery: authority_keys.authority_discovery.clone(),
beefy: authority_keys.beefy.clone(),
nimbus: authority_keys.nimbus.clone(),
},
)
})
.collect();
keys.extend(validator_keys)
}

if !self.collators.is_empty() {
// We set invulnerables in pallet_invulnerables
let invulnerables: Vec<AccountId> = self
Expand Down Expand Up @@ -674,6 +710,11 @@ impl ExtBuilder {
.iter()
.map(|(account, _)| account.clone())
.collect(),
external_validators: self
.external_validators
.iter()
.map(|(account, _)| account.clone())
.collect(),
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,11 @@ fn external_validators_rewards_sends_message_on_era_end() {
(AccountId::from(ALICE), 210_000 * UNIT),
(AccountId::from(BOB), 100_000 * UNIT),
])
.with_validators(vec![])
.with_external_validators(vec![
(AccountId::from(ALICE), 210 * UNIT),
(AccountId::from(BOB), 100 * UNIT),
])
.build()
.execute_with(|| {
let token_location: VersionedLocation = Location::here().into();
Expand Down Expand Up @@ -829,18 +834,13 @@ fn external_validators_rewards_merkle_proofs() {
vec![AccountId::from(CHARLIE), AccountId::from(DAVE)]
);

assert!(
pallet_external_validators_rewards::RewardPointsForEra::<Runtime>::iter().count()
== 0
);

// Reward all validators in era 1
crate::RewardValidators::reward_backing(vec![ValidatorIndex(0)]);
crate::RewardValidators::reward_backing(vec![ValidatorIndex(1)]);

assert!(
pallet_external_validators_rewards::RewardPointsForEra::<Runtime>::iter().count()
== 1
assert_eq!(
pallet_external_validators_rewards::RewardPointsForEra::<Runtime>::iter().count(),
1
);

let (_era_index, era_rewards) =
Expand Down Expand Up @@ -1048,8 +1048,6 @@ fn external_validators_whitelisted_never_rewarded() {

#[test]
fn external_validators_rewards_test_command_integrity() {
use {crate::ValidatorIndex, runtime_parachains::inclusion::RewardValidators};

ExtBuilder::default()
.with_balances(vec![
(AccountId::from(ALICE), 210_000 * UNIT),
Expand Down Expand Up @@ -1139,18 +1137,10 @@ fn external_validators_rewards_test_command_integrity() {
vec![AccountId::from(CHARLIE), AccountId::from(DAVE)]
);

assert!(
pallet_external_validators_rewards::RewardPointsForEra::<Runtime>::iter().count()
== 0
);

// Reward Alice and Bob in era 1
crate::RewardValidators::reward_backing(vec![ValidatorIndex(0)]);
crate::RewardValidators::reward_backing(vec![ValidatorIndex(1)]);

assert!(
pallet_external_validators_rewards::RewardPointsForEra::<Runtime>::iter().count()
== 1
// Validators are automatically rewarded.
assert_eq!(
pallet_external_validators_rewards::RewardPointsForEra::<Runtime>::iter().count(),
1
);

let expected_inflation =
Expand Down Expand Up @@ -1180,10 +1170,16 @@ fn external_validators_rewards_test_command_integrity() {
.count();

let rewards_utils = ExternalValidatorsRewards::generate_era_rewards_utils(1, None);

let blocks_per_session: u128 = Babe::current_epoch().duration.into();
let points_per_block = 20;
let expected_total_points =
(sessions_per_era as u128) * blocks_per_session * points_per_block;

let expected_rewards_command = Command::ReportRewards {
external_idx: 1u64,
era_index: 1u32,
total_points: 40u128,
total_points: expected_total_points,
tokens_inflated: expected_inflation,
rewards_merkle_root: rewards_utils.unwrap().rewards_merkle_root,
token_id,
Expand All @@ -1209,6 +1205,15 @@ fn external_validators_rewards_are_minted_in_sovereign_account() {
(AccountId::from(ALICE), 210_000 * UNIT),
(AccountId::from(BOB), 100_000 * UNIT),
])
.with_validators(
vec![]
)
.with_external_validators(
vec![
(AccountId::from(ALICE), 210 * UNIT),
(AccountId::from(BOB), 100 * UNIT),
]
)
.build()
.execute_with(|| {
let token_location: VersionedLocation = Location::here()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

use crate::tests::common::{mock_snowbridge_message_proof, ExtBuilder};
use crate::tests::common::{mock_snowbridge_message_proof, ExtBuilder, ALICE, BOB, UNIT};
use crate::{AccountId, EthereumInboundQueue, ExternalValidators, Runtime};
use alloy_sol_types::SolEvent;
use frame_system::pallet_prelude::OriginFor;
Expand All @@ -33,7 +33,16 @@ use tp_bridge::symbiotic_message_processor::{

#[test]
fn test_inbound_queue_message_passing() {
ExtBuilder::default().build().execute_with(|| {
ExtBuilder::default()
.with_validators(
vec![]
)
.with_external_validators(
vec![
(AccountId::from(ALICE), 210 * UNIT),
(AccountId::from(BOB), 100 * UNIT),
]
).build().execute_with(|| {
let current_nonce = 1;

snowbridge_pallet_system::Channels::<Runtime>::set(PRIMARY_GOVERNANCE_CHANNEL, Some(Channel {
Expand All @@ -59,8 +68,6 @@ fn test_inbound_queue_message_passing() {
proof: dummy_proof.clone(),
}), Err(DispatchError::Other("No handler for message found")));

assert_eq!(ExternalValidators::validators(), ExternalValidators::whitelisted_validators());

let payload_validators = vec![
AccountKeyring::Charlie.to_account_id(),
AccountKeyring::Ferdie.to_account_id(),
Expand Down Expand Up @@ -91,7 +98,6 @@ fn test_inbound_queue_message_passing() {
proof: dummy_proof.clone(),
}), Ok(()));


let expected_validators = [ExternalValidators::whitelisted_validators(), payload_validators].concat();
assert_eq!(ExternalValidators::validators(), expected_validators);
});
Expand Down
27 changes: 23 additions & 4 deletions chains/orchestrator-relays/runtime/dancelight/src/tests/slashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ fn test_slashes_are_sent_to_ethereum() {
(AccountId::from(CHARLIE), 100_000 * UNIT),
(AccountId::from(DAVE), 100_000 * UNIT),
])
.with_validators(vec![])
.with_external_validators(vec![
(AccountId::from(ALICE), 210 * UNIT),
(AccountId::from(BOB), 100 * UNIT),
])
.build()
.execute_with(|| {
let token_location: VersionedLocation = Location::here().into();
Expand All @@ -422,10 +427,6 @@ fn test_slashes_are_sent_to_ethereum() {
));

run_to_block(2);
assert_ok!(ExternalValidators::remove_whitelisted(
RuntimeOrigin::root(),
AccountId::from(ALICE)
));

inject_babe_slash(&AccountId::from(ALICE).to_string());

Expand Down Expand Up @@ -552,6 +553,15 @@ fn test_slashes_are_sent_to_ethereum_accumulatedly() {
(AccountId::from(CHARLIE), 100_000 * UNIT),
(AccountId::from(DAVE), 100_000 * UNIT),
])
.with_validators(
vec![]
)
.with_external_validators(
vec![
(AccountId::from(ALICE), 210 * UNIT),
(AccountId::from(BOB), 100 * UNIT),
]
)
.build()
.execute_with(|| {
let token_location: VersionedLocation = Location::here()
Expand Down Expand Up @@ -683,6 +693,15 @@ fn test_slashes_are_sent_to_ethereum_accumulate_until_next_era() {
(AccountId::from(CHARLIE), 100_000 * UNIT),
(AccountId::from(DAVE), 100_000 * UNIT),
])
.with_validators(
vec![]
)
.with_external_validators(
vec![
(AccountId::from(ALICE), 210 * UNIT),
(AccountId::from(BOB), 100 * UNIT),
]
)
.build()
.execute_with(|| {
let token_location: VersionedLocation = Location::here()
Expand Down
Loading

0 comments on commit fae9632

Please sign in to comment.