Skip to content

Commit

Permalink
removing known_nodes in favor of using known_nodes_with_stake
Browse files Browse the repository at this point in the history
  • Loading branch information
dailinsubjam committed Sep 13, 2023
1 parent cd9539d commit cdfc395
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 59 deletions.
6 changes: 3 additions & 3 deletions .vscode/settings.json.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Sets features for Rust Analyzer, since some features are required to be set for compilation
{
"rust-analyzer.cargo.features": [
"full-ci"
],
"rust-analyzer.cargo.extraEnv": {
"RUSTFLAGS": "--cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"async-std\""
},
}
6 changes: 6 additions & 0 deletions crates/hotshot-signature-key/src/bn254/bn254_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ impl SignatureKey for BN254Pub {
}
}

fn get_public_key(entry: &Self::StakeTableEntry) -> Self {
Self {
pub_key: entry.stake_key,
}
}

fn get_public_parameter(
stake_entries: Vec<Self::StakeTableEntry>,
threshold: U256,
Expand Down
4 changes: 2 additions & 2 deletions crates/hotshot/examples/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn load_config_from_file<TYPES: NodeType>(
> = config_toml.into();

// Generate network's public keys
config.config.known_nodes = (0..config.config.total_nodes.get())
let known_nodes = (0..config.config.total_nodes.get())
.map(|node_id| {
TYPES::SignatureKey::generated_from_seed_indexed(
config.seed,
Expand All @@ -63,7 +63,7 @@ pub fn load_config_from_file<TYPES: NodeType>(
.collect();

config.config.known_nodes_with_stake = (0..config.config.total_nodes.get())
.map(|node_id| config.config.known_nodes[node_id].get_stake_table_entry(1u64))
.map(|node_id| known_nodes[node_id].get_stake_table_entry(1u64))
.collect();

config
Expand Down
2 changes: 0 additions & 2 deletions crates/hotshot/examples/infra/modDA.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ pub trait RunDA<
// Get KeyPair for certificate Aggregation
let (pk, sk) =
TYPES::SignatureKey::generated_from_seed_indexed(config.seed, config.node_index);
let known_nodes = config.config.known_nodes.clone();
let known_nodes_with_stake = config.config.known_nodes_with_stake.clone();
let entry = pk.get_stake_table_entry(1u64);

Expand All @@ -240,7 +239,6 @@ pub trait RunDA<

let exchanges = NODE::Exchanges::create(
known_nodes_with_stake.clone(),
known_nodes.clone(),
(quorum_election_config, committee_election_config),
(
quorum_network.clone(),
Expand Down
3 changes: 0 additions & 3 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ pub struct SystemContextInner<TYPES: NodeType, I: NodeImplementation<TYPES>> {

/// Configuration items for this hotshot instance
config: HotShotConfig<
TYPES::SignatureKey,
<TYPES::SignatureKey as SignatureKey>::StakeTableEntry,
TYPES::ElectionConfigType,
>,
Expand Down Expand Up @@ -178,7 +177,6 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> SystemContext<TYPES, I> {
private_key: <TYPES::SignatureKey as SignatureKey>::PrivateKey,
nonce: u64,
config: HotShotConfig<
TYPES::SignatureKey,
<TYPES::SignatureKey as SignatureKey>::StakeTableEntry,
TYPES::ElectionConfigType,
>,
Expand Down Expand Up @@ -405,7 +403,6 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> SystemContext<TYPES, I> {
private_key: <TYPES::SignatureKey as SignatureKey>::PrivateKey,
node_id: u64,
config: HotShotConfig<
TYPES::SignatureKey,
<TYPES::SignatureKey as SignatureKey>::StakeTableEntry,
TYPES::ElectionConfigType,
>,
Expand Down
35 changes: 15 additions & 20 deletions crates/hotshot/src/traits/election/static_committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ use tracing::debug;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GeneralStaticCommittee<T, LEAF: LeafType<NodeType = T>, PUBKEY: SignatureKey> {
/// All the nodes participating
nodes: Vec<PUBKEY>,
/// All the nodes participating and their stake
nodes_with_stake: Vec<PUBKEY::StakeTableEntry>,
/// The nodes on the static committee
committee_nodes: Vec<PUBKEY>,
/// The nodes on the static committee and their stake
committee_nodes_with_stake: Vec<PUBKEY::StakeTableEntry>,
/// Node type phantom
Expand All @@ -41,11 +37,9 @@ impl<T, LEAF: LeafType<NodeType = T>, PUBKEY: SignatureKey>
{
/// Creates a new dummy elector
#[must_use]
pub fn new(nodes: Vec<PUBKEY>, nodes_with_stake: Vec<PUBKEY::StakeTableEntry>) -> Self {
pub fn new(_nodes: Vec<PUBKEY>, nodes_with_stake: Vec<PUBKEY::StakeTableEntry>) -> Self {
Self {
nodes: nodes.clone(),
nodes_with_stake: nodes_with_stake.clone(),
committee_nodes: nodes,
committee_nodes_with_stake: nodes_with_stake,
_type_phantom: PhantomData,
_leaf_phantom: PhantomData,
Expand Down Expand Up @@ -107,8 +101,9 @@ where

/// Index the vector of public keys with the current view number
fn get_leader(&self, view_number: TYPES::Time) -> PUBKEY {
let index = (*view_number % self.nodes.len() as u64) as usize;
self.nodes[index].clone()
let index = (*view_number % self.nodes_with_stake.len() as u64) as usize;
let res = self.nodes_with_stake[index].clone();
TYPES::SignatureKey::get_public_key(&res)
}

/// Simply make the partial signature
Expand All @@ -119,7 +114,8 @@ where
) -> std::result::Result<Option<StaticVoteToken<PUBKEY>>, ElectionError> {
// TODO ED Below
let pub_key = PUBKEY::from_private(private_key);
if !self.committee_nodes.contains(&pub_key) {
let entry = pub_key.get_stake_table_entry(1u64);
if !self.committee_nodes_with_stake.contains(&entry) {
return Ok(None);
}
let mut message: Vec<u8> = vec![];
Expand All @@ -137,7 +133,8 @@ where
) -> Result<Checked<TYPES::VoteTokenType>, ElectionError> {
match token {
Checked::Valid(t) | Checked::Unchecked(t) => {
if self.committee_nodes.contains(&pub_key) {
let entry = pub_key.get_stake_table_entry(1u64);
if self.committee_nodes_with_stake.contains(&entry) {
Ok(Checked::Valid(t))
} else {
Ok(Checked::Inval(t))
Expand All @@ -153,40 +150,38 @@ where

fn create_election(
keys_qc: Vec<PUBKEY::StakeTableEntry>,
keys: Vec<PUBKEY>,
config: TYPES::ElectionConfigType,
) -> Self {
let mut committee_nodes = keys.clone();
let mut committee_nodes_with_stake = keys_qc.clone();
committee_nodes.truncate(config.num_nodes.try_into().unwrap());
debug!("Election Membership Size: {}", config.num_nodes);
committee_nodes_with_stake.truncate(config.num_nodes.try_into().unwrap());
Self {
nodes_with_stake: keys_qc,
nodes: keys,
committee_nodes,
committee_nodes_with_stake,
_type_phantom: PhantomData,
_leaf_phantom: PhantomData,
}
}

fn total_nodes(&self) -> usize {
self.committee_nodes.len()
self.committee_nodes_with_stake.len()
}

fn success_threshold(&self) -> NonZeroU64 {
NonZeroU64::new(((self.committee_nodes.len() as u64 * 2) / 3) + 1).unwrap()
NonZeroU64::new(((self.committee_nodes_with_stake.len() as u64 * 2) / 3) + 1).unwrap()
}

fn failure_threshold(&self) -> NonZeroU64 {
NonZeroU64::new(((self.committee_nodes.len() as u64) / 3) + 1).unwrap()
NonZeroU64::new(((self.committee_nodes_with_stake.len() as u64) / 3) + 1).unwrap()
}

fn get_committee(
&self,
_view_number: <TYPES as NodeType>::Time,
) -> std::collections::BTreeSet<<TYPES as NodeType>::SignatureKey> {
self.committee_nodes.clone().into_iter().collect()
// Transfer from committee_nodes_with_stake to pure committee_nodes
(0..(self.committee_nodes_with_stake.len()))
.map(|node_id| <TYPES as NodeType>::SignatureKey::get_public_key(&self.committee_nodes_with_stake[node_id]))
.collect()
}
}
9 changes: 6 additions & 3 deletions crates/orchestrator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
num::NonZeroUsize,
time::Duration,
};
use std::marker::PhantomData;
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct Libp2pConfig {
pub bootstrap_nodes: Vec<(SocketAddr, Vec<u8>)>,
Expand Down Expand Up @@ -65,9 +66,10 @@ pub struct NetworkConfig<KEY, ENTRY, ELECTIONCONFIG> {
pub key_type_name: String,
pub election_config_type_name: String,
pub libp2p_config: Option<Libp2pConfig>,
pub config: HotShotConfig<KEY, ENTRY, ELECTIONCONFIG>,
pub config: HotShotConfig<ENTRY, ELECTIONCONFIG>,
pub web_server_config: Option<WebServerConfig>,
pub da_web_server_config: Option<WebServerConfig>,
_key_type_phantom: PhantomData<KEY>,
}

impl<K, ENTRY, E> Default for NetworkConfig<K, ENTRY, E> {
Expand All @@ -85,6 +87,7 @@ impl<K, ENTRY, E> Default for NetworkConfig<K, ENTRY, E> {
election_config_type_name: std::any::type_name::<E>().to_string(),
web_server_config: None,
da_web_server_config: None,
_key_type_phantom: PhantomData,
}
}
}
Expand Down Expand Up @@ -152,6 +155,7 @@ impl<K, ENTRY, E> From<NetworkConfigFile> for NetworkConfig<K, ENTRY, E> {
start_delay_seconds: val.start_delay_seconds,
web_server_config: val.web_server_config,
da_web_server_config: val.da_web_server_config,
_key_type_phantom: PhantomData,
}
}
}
Expand Down Expand Up @@ -183,14 +187,13 @@ pub struct HotShotConfigFile {
pub propose_max_round_time: Duration,
}

impl<K, ENTRY, E> From<HotShotConfigFile> for HotShotConfig<K, ENTRY, E> {
impl<ENTRY, E> From<HotShotConfigFile> for HotShotConfig<ENTRY, E> {
fn from(val: HotShotConfigFile) -> Self {
HotShotConfig {
execution_type: ExecutionType::Continuous,
total_nodes: val.total_nodes,
max_transactions: val.max_transactions,
min_transactions: val.min_transactions,
known_nodes: Vec::new(),
known_nodes_with_stake: Vec::new(),
da_committee_size: val.committee_nodes,
next_view_timeout: val.next_view_timeout,
Expand Down
2 changes: 1 addition & 1 deletion crates/orchestrator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ where
fn post_ready(&mut self) -> Result<(), ServerError> {
self.nodes_connected += 1;
println!("Nodes connected: {}", self.nodes_connected);
if self.nodes_connected >= self.config.config.known_nodes.len().try_into().unwrap() {
if self.nodes_connected >= self.config.config.known_nodes_with_stake.len().try_into().unwrap() {
self.start = true;
}
Ok(())
Expand Down
2 changes: 0 additions & 2 deletions crates/testing/src/task_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub async fn build_system_handle(
>>::block_genesis())
.unwrap();

let known_nodes = config.known_nodes.clone();
let known_nodes_with_stake = config.known_nodes_with_stake.clone();
let private_key = <BN254Pub as SignatureKey>::generated_from_seed_indexed([0u8; 32], node_id).1;
let public_key = <SequencingTestTypes as NodeType>::SignatureKey::from_private(&private_key);
Expand All @@ -67,7 +66,6 @@ pub async fn build_system_handle(
let exchanges =
<SequencingMemoryImpl as NodeImplementation<SequencingTestTypes>>::Exchanges::create(
known_nodes_with_stake.clone(),
known_nodes.clone(),
(quorum_election_config, committee_election_config),
networks,
public_key,
Expand Down
3 changes: 1 addition & 2 deletions crates/testing/src/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ impl TestMetadata {
num_bootstrap: num_bootstrap_nodes,
min_transactions,
max_transactions: NonZeroUsize::new(99999).unwrap(),
known_nodes,
known_nodes_with_stake,
da_committee_size,
next_view_timeout: 500,
Expand Down Expand Up @@ -247,7 +246,7 @@ impl TestMetadata {
} = timing_data;
let mod_config =
// TODO this should really be using the timing config struct
|a: &mut HotShotConfig<TYPES::SignatureKey, <TYPES::SignatureKey as SignatureKey>::StakeTableEntry, TYPES::ElectionConfigType>| {
|a: &mut HotShotConfig<<TYPES::SignatureKey as SignatureKey>::StakeTableEntry, TYPES::ElectionConfigType>| {
a.next_view_timeout = next_view_timeout;
a.timeout_ratio = timeout_ratio;
a.round_start_delay = round_start_delay;
Expand Down
2 changes: 0 additions & 2 deletions crates/testing/src/test_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ where
pub storage: Generator<<I as NodeImplementation<TYPES>>::Storage>,
/// configuration used to generate each hotshot node
pub config: HotShotConfig<
TYPES::SignatureKey,
<TYPES::SignatureKey as SignatureKey>::StakeTableEntry,
TYPES::ElectionConfigType,
>,
Expand Down Expand Up @@ -198,7 +197,6 @@ impl<TYPES: NodeType, I: TestableNodeImplementation<TYPES>> TestLauncher<TYPES,
mut self,
mut f: impl FnMut(
&mut HotShotConfig<
TYPES::SignatureKey,
<TYPES::SignatureKey as SignatureKey>::StakeTableEntry,
TYPES::ElectionConfigType,
>,
Expand Down
3 changes: 0 additions & 3 deletions crates/testing/src/test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ where
storage: I::Storage,
initializer: HotShotInitializer<TYPES, I::Leaf>,
config: HotShotConfig<
TYPES::SignatureKey,
<TYPES::SignatureKey as SignatureKey>::StakeTableEntry,
TYPES::ElectionConfigType,
>,
Expand All @@ -250,7 +249,6 @@ where
let node_id = self.next_node_id;
self.next_node_id += 1;

let known_nodes = config.known_nodes.clone();
let known_nodes_with_stake = config.known_nodes_with_stake.clone();
// Generate key pair for certificate aggregation
let private_key = TYPES::SignatureKey::generated_from_seed_indexed([0u8; 32], node_id).1;
Expand All @@ -265,7 +263,6 @@ where
let committee_election_config = I::committee_election_config_generator();
let exchanges = I::Exchanges::create(
known_nodes_with_stake.clone(),
known_nodes.clone(),
(
quorum_election_config,
committee_election_config(config.da_committee_size as u64),
Expand Down
4 changes: 1 addition & 3 deletions crates/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub enum ExecutionType {

/// Holds configuration for a `HotShot`
#[derive(Clone, custom_debug::Debug, serde::Serialize, serde::Deserialize)]
pub struct HotShotConfig<K, ENTRY, ELECTIONCONFIG> {
pub struct HotShotConfig<ENTRY, ELECTIONCONFIG> {
/// Whether to run one view or continuous views
pub execution_type: ExecutionType,
/// Total number of nodes in the network
Expand All @@ -45,8 +45,6 @@ pub struct HotShotConfig<K, ENTRY, ELECTIONCONFIG> {
pub min_transactions: usize,
/// Maximum transactions per block
pub max_transactions: NonZeroUsize,
/// List of known node's public keys, including own, sorted by nonce ()
pub known_nodes: Vec<K>,
/// List of known node's public keys and stake value for certificate aggregation, serving as public parameter
pub known_nodes_with_stake: Vec<ENTRY>,
/// List of DA committee nodes for static DA committe
Expand Down
Loading

0 comments on commit cdfc395

Please sign in to comment.