Skip to content

Commit

Permalink
move impl Upgrade from types to impls module (#1702)
Browse files Browse the repository at this point in the history
  • Loading branch information
imabdulbasit authored Jul 12, 2024
2 parents 5547a9b + 0272891 commit a997f5f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
4 changes: 2 additions & 2 deletions sequencer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub async fn init_node<P: PersistenceOptions, Ver: StaticVersionType + 'static>(
.upgrades
.get(&<SeqTypes as NodeType>::Upgrade::VERSION)
{
upgrade.set_hotshot_config(&mut config.config);
upgrade.set_hotshot_config_parameters(&mut config.config);
}

// If the `Libp2p` bootstrap nodes were supplied via the command line, override those
Expand Down Expand Up @@ -444,7 +444,7 @@ pub mod testing {

pub fn build(mut self) -> TestConfig<NUM_NODES> {
if let Some(upgrade) = self.upgrades.get(&<SeqTypes as NodeType>::Upgrade::VERSION) {
upgrade.set_hotshot_config(&mut self.config)
upgrade.set_hotshot_config_parameters(&mut self.config)
}

TestConfig {
Expand Down
40 changes: 38 additions & 2 deletions types/src/v0/impls/instance_state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::{collections::BTreeMap, sync::Arc};

use hotshot_types::traits::{node_implementation::NodeType, states::InstanceState};
use hotshot_types::{
traits::{node_implementation::NodeType, states::InstanceState},
HotShotConfig,
};
use vbs::version::{StaticVersionType, Version};

use crate::{
v0::traits::StateCatchup, ChainConfig, L1Client, NodeState, SeqTypes, Upgrade, ValidatedState,
v0::traits::StateCatchup, ChainConfig, L1Client, NodeState, PubKey, SeqTypes, Timestamp,
Upgrade, UpgradeMode, ValidatedState,
};

impl NodeState {
Expand Down Expand Up @@ -77,6 +81,38 @@ impl Default for NodeState {

impl InstanceState for NodeState {}

impl Upgrade {
pub fn set_hotshot_config_parameters(&self, config: &mut HotShotConfig<PubKey>) {
match &self.mode {
UpgradeMode::View(v) => {
config.start_proposing_view = v.start_proposing_view;
config.stop_proposing_view = v.stop_proposing_view;
config.start_voting_view = v.start_voting_view.unwrap_or(0);
config.stop_voting_view = v.stop_voting_view.unwrap_or(u64::MAX);
config.start_proposing_time = 0;
config.stop_proposing_time = u64::MAX;
config.start_voting_time = 0;
config.stop_voting_time = u64::MAX;
}
UpgradeMode::Time(t) => {
config.start_proposing_time = t.start_proposing_time.unix_timestamp();
config.stop_proposing_time = t.stop_proposing_time.unix_timestamp();
config.start_voting_time = t.start_voting_time.unwrap_or_default().unix_timestamp();
// this should not panic because Timestamp::max() constructs the maximum possible Unix timestamp
// using i64::MAX
config.stop_voting_time = t
.stop_voting_time
.unwrap_or(Timestamp::max().expect("overflow"))
.unix_timestamp();
config.start_proposing_view = 0;
config.stop_proposing_view = u64::MAX;
config.start_voting_view = 0;
config.stop_voting_view = u64::MAX;
}
}
}
}

#[cfg(any(test, feature = "testing"))]
pub mod mock {
use std::collections::HashMap;
Expand Down
36 changes: 1 addition & 35 deletions types/src/v0/v0_1/instance_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ use std::collections::BTreeMap;

use std::sync::Arc;

use hotshot_types::HotShotConfig;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

use crate::{
v0::traits::StateCatchup, ChainConfig, GenesisHeader, L1BlockInfo, PubKey, Timestamp,
ValidatedState,
v0::traits::StateCatchup, ChainConfig, GenesisHeader, L1BlockInfo, Timestamp, ValidatedState,
};
use vbs::version::Version;

Expand Down Expand Up @@ -69,38 +67,6 @@ pub struct Upgrade {
pub upgrade_type: UpgradeType,
}

impl Upgrade {
pub fn set_hotshot_config(&self, config: &mut HotShotConfig<PubKey>) {
match &self.mode {
UpgradeMode::View(v) => {
config.start_proposing_view = v.start_proposing_view;
config.stop_proposing_view = v.stop_proposing_view;
config.start_voting_view = v.start_voting_view.unwrap_or(0);
config.stop_voting_view = v.stop_voting_view.unwrap_or(u64::MAX);
config.start_proposing_time = 0;
config.stop_proposing_time = u64::MAX;
config.start_voting_time = 0;
config.stop_voting_time = u64::MAX;
}
UpgradeMode::Time(t) => {
config.start_proposing_time = t.start_proposing_time.unix_timestamp();
config.stop_proposing_time = t.stop_proposing_time.unix_timestamp();
config.start_voting_time = t.start_voting_time.unwrap_or_default().unix_timestamp();
// this should not panic because Timestamp::max() constructs the maximum possible Unix timestamp
// using i64::MAX
config.stop_voting_time = t
.stop_voting_time
.unwrap_or(Timestamp::max().expect("overflow"))
.unix_timestamp();
config.start_proposing_view = 0;
config.stop_proposing_view = u64::MAX;
config.start_voting_view = 0;
config.stop_voting_view = u64::MAX;
}
}
}
}

/// Represents the immutable state of a node.
///
/// For mutable state, use `ValidatedState`.
Expand Down

0 comments on commit a997f5f

Please sign in to comment.