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

Added support for Uncompressed Key #910

Merged
merged 21 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 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 pallets/thea/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ thea-primitives = { path = "../../primitives/thea", default-features = false }
sp-application-crypto = { workspace = true, default-features = false }
sp-io = { workspace = true, default-features = false }
hex = { workspace = true, default-features = false, features = ["alloc"] }
libsecp256k1 = { version = "0.7.1", default-features = false}

[dev-dependencies]
pallet-assets = { workspace = true, features = ["std"] }
Expand Down Expand Up @@ -53,6 +54,7 @@ std = [
"pallet-balances/std",
"sp-core/std",
"frame-benchmarking?/std",
"libsecp256k1/std"
]
runtime-benchmarks = [
"pallet-balances/runtime-benchmarks",
Expand Down
2 changes: 1 addition & 1 deletion pallets/thea/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ benchmarks! {

add_thea_network {
let network: u8 = 2;
}: _(RawOrigin::Root, network, 20, 100*UNIT_BALANCE, 1000*UNIT_BALANCE)
}: _(RawOrigin::Root, network, false, 20, 100*UNIT_BALANCE, 1000*UNIT_BALANCE)
verify {
let active_list = <ActiveNetworks<T>>::get();
assert!(active_list.contains(&network));
Expand Down
56 changes: 43 additions & 13 deletions pallets/thea/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use thea_primitives::{
types::{Message, PayloadType},
Network, ValidatorSet, GENESIS_AUTHORITY_SET_ID,
};
use thea_primitives::types::KeyType;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
Expand Down Expand Up @@ -479,14 +480,15 @@ pub mod pallet {
pub fn add_thea_network(
origin: OriginFor<T>,
network: Network,
requires_uncompressed_key: bool,
fork_period: u32,
min_stake: u128,
fisherman_stake: u128,
) -> DispatchResult {
ensure_root(origin)?;
<NetworkConfig<T>>::insert(
network,
thea_primitives::types::NetworkConfig { fork_period, min_stake, fisherman_stake },
thea_primitives::types::NetworkConfig::new(fork_period, min_stake, fisherman_stake, requires_uncompressed_key),
);
<ActiveNetworks<T>>::mutate(|list| {
list.insert(network);
Expand Down Expand Up @@ -746,19 +748,47 @@ impl<T: Config> Pallet<T> {
// This last message should be signed by the outgoing set
// Similar to how Grandpa's session change works.
if incoming != queued {
// This should happen at the beginning of the last epoch
if let Some(validator_set) = ValidatorSet::new(queued.clone(), new_id) {
let payload = validator_set.encode();
for network in &active_networks {
let message = Self::generate_payload(
PayloadType::ScheduledRotateValidators,
*network,
payload.clone(),
);
// Update nonce
<OutgoingNonce<T>>::insert(message.network, message.nonce);
<OutgoingMessages<T>>::insert(message.network, message.nonce, message);
let mut uncompressed_keys = vec![];
for public_key in queued.clone().into_iter() {
let public_key: sp_core::ecdsa::Public = public_key.into();
if let Ok(compressed_key) = libsecp256k1::PublicKey::parse_compressed(&public_key.0) {
uncompressed_keys.push(compressed_key.serialize());
} else {
log::error!(target: "thea", "Unable to parse compressed key");
Gauthamastro marked this conversation as resolved.
Show resolved Hide resolved
return;
}
}
for network in &active_networks {
let network_config = <NetworkConfig<T>>::get(*network);
let message = match network_config.key_type {
KeyType::Uncompressed => {
if let Some(payload) = ValidatorSet::new(uncompressed_keys.clone(), new_id) {
let message = Self::generate_payload(
PayloadType::ScheduledRotateValidators,
*network,
payload.encode(),
);
message
} else {
continue;
}

}
KeyType::Compressed => {
if let Some(payload) = ValidatorSet::new(queued.clone(), new_id) {
let message = Self::generate_payload(
PayloadType::ScheduledRotateValidators,
*network,
payload.encode(),
);
message
} else {
continue;
}
}
};
<OutgoingNonce<T>>::insert(message.network, message.nonce);
<OutgoingMessages<T>>::insert(message.network, message.nonce, message);
}
<NextAuthorities<T>>::put(queued);
}
Expand Down
11 changes: 11 additions & 0 deletions pallets/thea/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ fn test_add_thea_network_full() {
Thea::add_thea_network(
RuntimeOrigin::none(),
1,
false,
20,
100 * UNIT_BALANCE,
1000 * UNIT_BALANCE
Expand All @@ -204,6 +205,7 @@ fn test_add_thea_network_full() {
Thea::add_thea_network(
RuntimeOrigin::signed(1),
1,
false,
20,
100 * UNIT_BALANCE,
1000 * UNIT_BALANCE
Expand All @@ -215,6 +217,7 @@ fn test_add_thea_network_full() {
assert_ok!(Thea::add_thea_network(
RuntimeOrigin::root(),
net,
false,
20,
100 * UNIT_BALANCE,
1000 * UNIT_BALANCE
Expand All @@ -228,6 +231,7 @@ fn test_add_thea_network_full() {
assert_ok!(Thea::add_thea_network(
RuntimeOrigin::root(),
net,
false,
20,
100 * UNIT_BALANCE,
1000 * UNIT_BALANCE
Expand All @@ -251,6 +255,7 @@ fn test_remove_thea_network_full() {
assert_ok!(Thea::add_thea_network(
RuntimeOrigin::root(),
net,
false,
20,
100 * UNIT_BALANCE,
1000 * UNIT_BALANCE
Expand All @@ -264,6 +269,7 @@ fn test_remove_thea_network_full() {
assert_ok!(Thea::add_thea_network(
RuntimeOrigin::root(),
net,
false,
20,
100 * UNIT_BALANCE,
1000 * UNIT_BALANCE
Expand Down Expand Up @@ -298,6 +304,7 @@ fn test_report_misbehaviour_happy_path() {
fork_period: 0,
min_stake: 1_000_000,
fisherman_stake: 1_000_000,
key_type: KeyType::Compressed,
};
<NetworkConfig<Test>>::insert(network, config);
let relayer = 1u64;
Expand Down Expand Up @@ -336,6 +343,7 @@ fn test_report_misbehaviour_not_enough_stake() {
fork_period: 0,
min_stake: 1_000_000_000_000_000_000_000_000_000,
fisherman_stake: 1_000_000_000_000_000_000_000_000,
key_type: KeyType::Compressed,
};
<NetworkConfig<Test>>::insert(network, config);
let relayer = 1u64;
Expand Down Expand Up @@ -370,6 +378,7 @@ fn test_handle_misbehaviour_happy_path_valid_proposal() {
fork_period: 0,
min_stake: 1_000_000,
fisherman_stake: 1_000_000,
key_type: KeyType::Compressed,
};
<NetworkConfig<Test>>::insert(network, config);
let relayer = 1u64;
Expand Down Expand Up @@ -405,6 +414,7 @@ fn test_handle_misbehaviour_happy_path_invalid_proposal() {
fork_period: 0,
min_stake: 1_000_000,
fisherman_stake: 1_000_000,
key_type: KeyType::Compressed,
};
<NetworkConfig<Test>>::insert(network, config);
let relayer = 1u64;
Expand Down Expand Up @@ -665,6 +675,7 @@ fn test_submit_incoming_message_happy_path_first_message() {
fork_period: 0,
min_stake: 1 * UNIT_BALANCE,
fisherman_stake: 1 * UNIT_BALANCE,
key_type: KeyType::Compressed,
};
<NetworkConfig<Test>>::insert(network_id, network_config);
assert_ok!(Thea::submit_incoming_message(
Expand Down
19 changes: 19 additions & 0 deletions primitives/thea/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,18 @@ impl<Signature> SignedMessage<Signature> {

pub const THEA_HOLD_REASON: [u8; 8] = *b"theaRela";

#[derive(Clone, Encode, Decode, TypeInfo, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum KeyType {
Compressed,
Uncompressed,
}

#[derive(Clone, Encode, Decode, TypeInfo, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct NetworkConfig {
pub fork_period: u32,
pub min_stake: u128,
pub fisherman_stake: u128,
pub key_type: KeyType,
}

impl Default for NetworkConfig {
Expand All @@ -138,10 +145,22 @@ impl Default for NetworkConfig {
fork_period: 20,
min_stake: 1000 * UNIT_BALANCE,
fisherman_stake: 100 * UNIT_BALANCE,
key_type: KeyType::Compressed,
}
}
}

impl NetworkConfig {
pub fn new(fork_period: u32, min_stake: u128, fisherman_stake: u128, is_uncompressed_key_req: bool) -> Self {
let key_type = if is_uncompressed_key_req {
KeyType::Uncompressed
} else {
KeyType::Compressed
};
Self { fork_period, min_stake, fisherman_stake, key_type }
}
}

#[derive(
Clone, Encode, Decode, TypeInfo, Debug, Eq, PartialEq, Ord, PartialOrd, Deserialize, Serialize,
)]
Expand Down