Skip to content

Commit

Permalink
fix upgrade: set time parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
imabdulbasit committed Jul 12, 2024
1 parent 6181610 commit 7cbdaab
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 109 deletions.
5 changes: 2 additions & 3 deletions data/genesis/demo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ timestamp = "1970-01-01T00:00:00Z"

[[upgrade]]
version = "0.2"
view = 5
propose_window = 10

start_proposing_view = 5
stop_proposing_view = 15

[upgrade.chain_config]
chain_id = 999999999
Expand Down
52 changes: 21 additions & 31 deletions sequencer/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl<N: ConnectedNetwork<PubKey>, Ver: StaticVersionType + 'static, P: Sequencer

#[cfg(any(test, feature = "testing"))]
pub mod test_helpers {
use std::{collections::BTreeMap, time::Duration};
use std::time::Duration;

use async_compatibility_layer::logging::{setup_backtrace, setup_logging};
use async_std::task::sleep;
Expand All @@ -360,7 +360,7 @@ pub mod test_helpers {
use espresso_types::{
mock::MockStateCatchup,
v0::traits::{PersistenceOptions, StateCatchup},
NamespaceId, Upgrade, ValidatedState,
NamespaceId, ValidatedState,
};
use ethers::{prelude::Address, utils::Anvil};
use futures::{
Expand All @@ -378,7 +378,6 @@ pub mod test_helpers {
use portpicker::pick_unused_port;
use surf_disco::Client;
use tide_disco::error::ServerError;
use vbs::version::Version;

use super::*;
use crate::{
Expand Down Expand Up @@ -503,15 +502,6 @@ pub mod test_helpers {
}
}

#[derive(Clone, Debug)]
pub struct TestNetworkUpgrades {
pub upgrades: BTreeMap<Version, Upgrade>,
pub start_proposing_view: u64,
pub stop_proposing_view: u64,
pub start_voting_view: u64,
pub stop_voting_view: u64,
}

impl<P: PersistenceOptions, const NUM_NODES: usize> TestNetwork<P, { NUM_NODES }> {
pub async fn new<C: StateCatchup + 'static>(
cfg: TestNetworkConfig<{ NUM_NODES }, P, C>,
Expand All @@ -529,7 +519,7 @@ pub mod test_helpers {
.map(|(i, (state, persistence, catchup))| {
let opt = cfg.api_config.clone();
let cfg = &cfg.network_config;
let upgrades_map = cfg.upgrades().map(|e| e.upgrades).unwrap_or_default();
let upgrades_map = cfg.upgrades();
async move {
if i == 0 {
opt.serve(
Expand Down Expand Up @@ -1049,7 +1039,8 @@ mod test {
use committable::{Commitment, Committable};
use es_version::{SequencerVersion, SEQUENCER_VERSION};
use espresso_types::{
mock::MockStateCatchup, FeeAccount, FeeAmount, Header, Upgrade, UpgradeType, ValidatedState,
mock::MockStateCatchup, v0_1::UpgradeMode, FeeAccount, FeeAmount, Header, Upgrade,
UpgradeType, ValidatedState,
};
use ethers::utils::Anvil;
use futures::{
Expand All @@ -1063,14 +1054,14 @@ mod test {
};
use hotshot_types::{
event::LeafInfo,
traits::{metrics::NoMetrics, node_implementation::ConsensusTime},
traits::{metrics::NoMetrics, node_implementation::{ConsensusTime, NodeType}},
};
use jf_merkle_tree::prelude::{MerkleProof, Sha3Node};
use portpicker::pick_unused_port;
use surf_disco::Client;
use test_helpers::{
catchup_test_helper, state_signature_test_helper, status_test_helper, submit_test_helper,
TestNetwork, TestNetworkConfigBuilder, TestNetworkUpgrades,
TestNetwork, TestNetworkConfigBuilder,
};
use tide_disco::{app::AppHealth, error::ServerError, healthcheck::HealthStatus};
use vbs::version::Version;
Expand Down Expand Up @@ -1446,28 +1437,27 @@ mod test {
base_fee: 1.into(),
..Default::default()
};
let mut map = std::collections::BTreeMap::new();
let start_proposing_view = 5;
let propose_window = 10;
map.insert(
Version { major: 0, minor: 2 },
let mut upgrades = std::collections::BTreeMap::new();

upgrades.insert(
<SeqTypes as NodeType>::Upgrade::VERSION,
Upgrade {
start_proposing_view,
propose_window,
start_voting_time: None,
stop_voting_time: None,
start_proposing_time: 0,
stop_proposing_time: u64::MAX,
start_voting_view: None,
stop_voting_view: None,
start_proposing_view: 1,
stop_proposing_view: 10,
mode: UpgradeMode::View,
upgrade_type: UpgradeType::ChainConfig {
chain_config: chain_config_upgrade,
},
},
);

let stop_voting_view = 100;
let upgrades = TestNetworkUpgrades {
upgrades: map,
start_proposing_view,
stop_proposing_view: start_proposing_view + propose_window,
start_voting_view: 1,
stop_voting_view,
};
let stop_voting_view = u64::MAX;

const NUM_NODES: usize = 5;
let config = TestNetworkConfigBuilder::<NUM_NODES, _, _>::with_num_nodes()
Expand Down
204 changes: 166 additions & 38 deletions sequencer/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,71 @@ mod upgrade_serialization {

use std::{collections::BTreeMap, fmt};

use espresso_types::{Upgrade, UpgradeType};
use espresso_types::{v0_1::UpgradeMode, Upgrade, UpgradeType};
use serde::{
de::{SeqAccess, Visitor},
ser::SerializeSeq,
Deserialize, Deserializer, Serializer,
Deserialize, Deserializer, Serialize, Serializer,
};
use vbs::version::Version;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct UpgradeTimeParams {
pub start_proposing_time: u64,
pub stop_proposing_time: u64,
pub start_voting_time: Option<u64>,
pub stop_voting_time: Option<u64>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct UpgradeViewParams {
pub start_proposing_view: u64,
pub stop_proposing_view: u64,
pub start_voting_view: Option<u64>,
pub stop_voting_view: Option<u64>,
}

/// Represents the specific type of upgrade.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum UpgradeParameters {
Time(UpgradeTimeParams),
View(UpgradeViewParams),
}

#[derive(Deserialize)]
struct UpgradeFields {
version: String,
#[serde(flatten)]
params: UpgradeParameters,
#[serde(flatten)]
upgrade_type: UpgradeType,
}

pub fn serialize<S>(map: &BTreeMap<Version, Upgrade>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(Some(map.len()))?;
for (version, upgrade) in map {
seq.serialize_element(&(
version.to_string(),
upgrade.start_proposing_view,
upgrade.propose_window,
upgrade.upgrade_type.clone(),
))?;
match upgrade.mode {
UpgradeMode::View => seq.serialize_element(&(
version.to_string(),
upgrade.start_proposing_view,
upgrade.stop_proposing_view,
upgrade.start_voting_view,
upgrade.stop_voting_view,
upgrade.upgrade_type.clone(),
))?,
UpgradeMode::Time => seq.serialize_element(&(
version.to_string(),
upgrade.start_proposing_time,
upgrade.stop_proposing_time,
upgrade.start_voting_time,
upgrade.stop_voting_time,
upgrade.upgrade_type.clone(),
))?,
}
}
seq.end()
}
Expand All @@ -94,15 +139,6 @@ mod upgrade_serialization {
{
let mut map = BTreeMap::new();

#[derive(Deserialize)]
struct UpgradeFields {
version: String,
view: u64,
propose_window: u64,
#[serde(flatten)]
upgrade_type: UpgradeType,
}

while let Some(fields) = seq.next_element::<UpgradeFields>()? {
// add try_from in Version
let version: Vec<_> = fields.version.split('.').collect();
Expand All @@ -112,14 +148,38 @@ mod upgrade_serialization {
minor: version[1].parse().expect("invalid version"),
};

map.insert(
version,
Upgrade {
start_proposing_view: fields.view,
propose_window: fields.propose_window,
upgrade_type: fields.upgrade_type,
},
);
match fields.params {
UpgradeParameters::Time(t) => map.insert(
version,
Upgrade {
start_voting_time: t.start_voting_time,
stop_voting_time: t.stop_voting_time,
start_proposing_time: t.start_proposing_time,
stop_proposing_time: t.stop_proposing_time,
start_voting_view: None,
stop_voting_view: None,
start_proposing_view: 0,
stop_proposing_view: u64::MAX,
mode: UpgradeMode::Time,
upgrade_type: fields.upgrade_type,
},
),
UpgradeParameters::View(v) => map.insert(
version,
Upgrade {
start_voting_time: None,
stop_voting_time: None,
start_proposing_time: 0,
stop_proposing_time: u64::MAX,
start_voting_view: v.start_voting_view,
stop_voting_view: v.stop_voting_view,
start_proposing_view: v.start_proposing_view,
stop_proposing_view: v.stop_proposing_view,
mode: UpgradeMode::View,
upgrade_type: fields.upgrade_type,
},
),
};
}

Ok(map)
Expand Down Expand Up @@ -148,7 +208,7 @@ impl Genesis {

#[cfg(test)]
mod test {
use espresso_types::{L1BlockInfo, Timestamp};
use espresso_types::{v0_1::UpgradeMode, L1BlockInfo, Timestamp, UpgradeType};
use ethers::prelude::{Address, H160, H256};
use sequencer_utils::ser::FromStringOrInteger;
use toml::toml;
Expand Down Expand Up @@ -179,18 +239,6 @@ mod test {
number = 64
timestamp = "0x123def"
hash = "0x80f5dd11f2bdda2814cb1ad94ef30a47de02cf28ad68c89e104c00c4e51bb7a5"

[[upgrade]]
version = "1.0"
view = 1
propose_window = 10

[upgrade.chain_config]
chain_id = 12345
max_block_size = 30000
base_fee = 1
fee_recipient = "0x0000000000000000000000000000000000000000"
fee_contract = "0x0000000000000000000000000000000000000000"
}
.to_string();

Expand Down Expand Up @@ -335,4 +383,84 @@ mod test {
}
)
}

#[test]
fn test_genesis_toml_upgrade() {
// without optional fields
// with view settings
let toml = toml! {
[stake_table]
capacity = 10

[chain_config]
chain_id = 12345
max_block_size = 30000
base_fee = 1
fee_recipient = "0x0000000000000000000000000000000000000000"
fee_contract = "0x0000000000000000000000000000000000000000"

[header]
timestamp = 123456

[accounts]
"0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f" = 100000
"0x0000000000000000000000000000000000000000" = 42

[l1_finalized]
number = 64
timestamp = "0x123def"
hash = "0x80f5dd11f2bdda2814cb1ad94ef30a47de02cf28ad68c89e104c00c4e51bb7a5"

[[upgrade]]
version = "0.2"
start_proposing_view = 1
stop_proposing_view = 10

[upgrade.chain_config]
chain_id = 12345
max_block_size = 30000
base_fee = 1
fee_recipient = "0x0000000000000000000000000000000000000000"
fee_contract = "0x0000000000000000000000000000000000000000"
}
.to_string();

let genesis: Genesis = toml::from_str(&toml).unwrap_or_else(|err| panic!("{err:#}"));

let (version, genesis_upgrade) = genesis.upgrades.last_key_value().unwrap();

assert_eq!(*version, Version { major: 0, minor: 2 });

let upgrade = Upgrade {
start_voting_time: None,
stop_voting_time: None,
start_proposing_time: 0,
stop_proposing_time: u64::MAX,
start_voting_view: None,
stop_voting_view: None,
start_proposing_view: 1,
stop_proposing_view: 10,
mode: UpgradeMode::View,
upgrade_type: UpgradeType::ChainConfig {
chain_config: genesis.chain_config,
},
};

assert_eq!(*genesis_upgrade, upgrade);

let mut upgrades = BTreeMap::new();
upgrades.insert(Version { major: 0, minor: 2 }, upgrade);

let genesis = Genesis {
chain_config: genesis.chain_config,
stake_table: genesis.stake_table,
accounts: genesis.accounts,
l1_finalized: genesis.l1_finalized,
header: genesis.header,
upgrades,
};

let toml_from_genesis = toml::to_string(&genesis).unwrap();
assert_eq!(toml, toml_from_genesis);
}
}
Loading

0 comments on commit 7cbdaab

Please sign in to comment.