mirror of
https://git.ghostchain.io/proxmio/ghost-node.git
synced 2025-12-27 03:09:56 +00:00
authorities stored based on the session hash map
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
@@ -2,18 +2,19 @@
|
||||
|
||||
use super::*;
|
||||
use crate::mock::*;
|
||||
|
||||
use frame_support::{assert_err, assert_ok, dispatch};
|
||||
use sp_core::offchain::{
|
||||
testing,
|
||||
testing::{TestOffchainExt, TestTransactionPoolExt},
|
||||
OffchainWorkerExt, OffchainDbExt, TransactionPoolExt,
|
||||
};
|
||||
use sp_runtime::testing::UintAuthorityId;
|
||||
use sp_runtime::{DispatchError, testing::UintAuthorityId};
|
||||
|
||||
use ghost_networks::BridgedInflationCurve;
|
||||
use pallet_staking::EraPayout;
|
||||
|
||||
const MAX_DEVIATION_DEPTH: u32 = 10;
|
||||
const MAX_DEVIATION_DEPTH: u64 = 10;
|
||||
|
||||
fn prepare_evm_network(
|
||||
maybe_network_id: Option<u32>,
|
||||
@@ -39,6 +40,34 @@ fn prepare_evm_network(
|
||||
network_data
|
||||
}
|
||||
|
||||
fn do_clap_from_first_authority(
|
||||
session_index: u32,
|
||||
network_id: u32,
|
||||
authority: u64,
|
||||
) -> dispatch::DispatchResult {
|
||||
let (transaction_hash, receiver, amount) = get_mocked_metadata();
|
||||
let clap = Clap {
|
||||
block_number: 420,
|
||||
removed: false,
|
||||
transaction_hash,
|
||||
session_index,
|
||||
authority_index: 0,
|
||||
network_id,
|
||||
receiver,
|
||||
amount,
|
||||
};
|
||||
let authority = UintAuthorityId::from(authority);
|
||||
let signature = authority.sign(&clap.encode()).unwrap();
|
||||
|
||||
SlowClap::pre_dispatch(&crate::Call::slow_clap {
|
||||
clap: clap.clone(),
|
||||
signature: signature.clone(),
|
||||
})
|
||||
.map_err(|e| <&'static str>::from(e))?;
|
||||
|
||||
SlowClap::slow_clap(RuntimeOrigin::none(), clap, signature)
|
||||
}
|
||||
|
||||
fn do_clap_from(
|
||||
session_index: u32,
|
||||
network_id: u32,
|
||||
@@ -325,23 +354,63 @@ fn should_throw_error_if_session_index_is_not_current() {
|
||||
let (network_id, transaction_hash, unique_transaction_hash) =
|
||||
generate_unique_hash(None, None, None, None);
|
||||
|
||||
let bad_signer = 777;
|
||||
let bad_signer_id = 5;
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
let session_index = advance_session_and_get_index();
|
||||
let mut session_and_indexes = Vec::new();
|
||||
|
||||
for deviation in 1..MAX_DEVIATION_DEPTH {
|
||||
let session_index_up = session_index.saturating_add(deviation);
|
||||
let session_index_down = session_index.saturating_sub(deviation);
|
||||
let storage_key_up = (session_index_up, transaction_hash, unique_transaction_hash);
|
||||
let storage_key_down = (session_index_down, transaction_hash, unique_transaction_hash);
|
||||
advance_session_with_authority(deviation);
|
||||
session_and_indexes.push((deviation, Session::current_index()));
|
||||
}
|
||||
|
||||
for chunk in session_and_indexes.chunks(3).into_iter() {
|
||||
let authority_curr = chunk[1].0;
|
||||
let authority_prev = chunk[0].0;
|
||||
let authority_next = chunk[2].0;
|
||||
|
||||
let session_index_curr = chunk[1].1;
|
||||
let session_index_prev = chunk[0].1;
|
||||
let session_index_next = chunk[2].1;
|
||||
|
||||
let storage_key_curr = (session_index_curr, transaction_hash, unique_transaction_hash);
|
||||
let storage_key_prev = (session_index_prev, transaction_hash, unique_transaction_hash);
|
||||
let storage_key_next = (session_index_next, transaction_hash, unique_transaction_hash);
|
||||
|
||||
assert_claps_info_correct(&storage_key_curr, &session_index_curr, 0);
|
||||
assert_claps_info_correct(&storage_key_prev, &session_index_prev, 0);
|
||||
assert_claps_info_correct(&storage_key_next, &session_index_next, 0);
|
||||
|
||||
assert_invalid_signing_address(session_index_curr, network_id, bad_signer_id);
|
||||
assert_invalid_signing_address(session_index_prev, network_id, bad_signer_id);
|
||||
assert_invalid_signing_address(session_index_prev, network_id, bad_signer_id);
|
||||
|
||||
assert_transaction_has_bad_signature(session_index_curr, network_id, bad_signer);
|
||||
assert_transaction_has_bad_signature(session_index_prev, network_id, bad_signer);
|
||||
assert_transaction_has_bad_signature(session_index_prev, network_id, bad_signer);
|
||||
|
||||
assert_transaction_has_bad_signature(session_index_curr, network_id, authority_prev);
|
||||
assert_transaction_has_bad_signature(session_index_curr, network_id, authority_next);
|
||||
|
||||
assert_transaction_has_bad_signature(session_index_prev, network_id, authority_curr);
|
||||
assert_transaction_has_bad_signature(session_index_prev, network_id, authority_next);
|
||||
|
||||
assert_transaction_has_bad_signature(session_index_next, network_id, authority_prev);
|
||||
assert_transaction_has_bad_signature(session_index_next, network_id, authority_curr);
|
||||
|
||||
assert_claps_info_correct(&storage_key_curr, &session_index_curr, 0);
|
||||
assert_claps_info_correct(&storage_key_prev, &session_index_prev, 0);
|
||||
assert_claps_info_correct(&storage_key_next, &session_index_next, 0);
|
||||
|
||||
assert_ok!(do_clap_from_first_authority(session_index_curr, network_id, authority_curr));
|
||||
assert_ok!(do_clap_from_first_authority(session_index_prev, network_id, authority_prev));
|
||||
assert_ok!(do_clap_from_first_authority(session_index_next, network_id, authority_next));
|
||||
|
||||
assert_claps_info_correct(&storage_key_curr, &session_index_curr, 1);
|
||||
assert_claps_info_correct(&storage_key_prev, &session_index_prev, 1);
|
||||
assert_claps_info_correct(&storage_key_next, &session_index_next, 1);
|
||||
|
||||
assert_claps_info_correct(&storage_key_up, &session_index, 0);
|
||||
assert_claps_info_correct(&storage_key_down, &session_index, 0);
|
||||
assert_err!(do_clap_from(session_index_up, network_id, 0, false),
|
||||
Error::<Runtime>::ClapForWrongSession);
|
||||
assert_err!(do_clap_from(session_index_down, network_id, 0, false),
|
||||
Error::<Runtime>::ClapForWrongSession);
|
||||
assert_claps_info_correct(&storage_key_up, &session_index, 0);
|
||||
assert_claps_info_correct(&storage_key_down, &session_index, 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -768,6 +837,24 @@ fn assert_claps_info_correct(
|
||||
assert_eq!(pallet::ClapsInSession::<Runtime>::get(session_index).len(), index);
|
||||
}
|
||||
|
||||
fn assert_transaction_has_bad_signature(
|
||||
session_index: u32,
|
||||
network_id: u32,
|
||||
authority: u64,
|
||||
) {
|
||||
assert_err!(do_clap_from_first_authority(session_index, network_id, authority),
|
||||
DispatchError::Other("Transaction has a bad signature"));
|
||||
}
|
||||
|
||||
fn assert_invalid_signing_address(
|
||||
session_index: u32,
|
||||
network_id: u32,
|
||||
authority_index: u32,
|
||||
) {
|
||||
assert_err!(do_clap_from(session_index, network_id, authority_index, false),
|
||||
DispatchError::Other("Invalid signing address"));
|
||||
}
|
||||
|
||||
fn get_rpc_endpoint() -> Vec<u8> {
|
||||
b"https://rpc.endpoint.network.com".to_vec()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user