-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Minor extensions for server interoperability (#21)
# What ❔ - Simplifies generating executor configurations. - Adds byte conversions for `BlockHeaderHash`. - Makes `SyncBlocks` actor terminate successfully on context cancellation. ## Why ❔ Part of preparations for the integration with the server codebase.
- Loading branch information
Showing
21 changed files
with
228 additions
and
230 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ use utils::pipe; | |
mod config; | ||
mod io; | ||
mod metrics; | ||
pub mod testonly; | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//! Testing extensions for node executor. | ||
use crate::config::{ConsensusConfig, ExecutorConfig, GossipConfig}; | ||
use concurrency::net; | ||
use consensus::testonly::make_genesis; | ||
use network::testonly::Instance; | ||
use rand::Rng; | ||
use roles::{ | ||
node, | ||
validator::{self, Payload}, | ||
}; | ||
use std::collections::HashMap; | ||
|
||
/// Full validator configuration. | ||
#[derive(Debug)] | ||
#[non_exhaustive] | ||
pub struct FullValidatorConfig { | ||
/// Executor configuration. | ||
pub node_config: ExecutorConfig, | ||
/// Secret key of the node used for identification in the gossip network. | ||
pub node_key: node::SecretKey, | ||
/// Consensus configuration of the validator. | ||
pub consensus_config: ConsensusConfig, | ||
/// Secret key for consensus. | ||
pub validator_key: validator::SecretKey, | ||
} | ||
|
||
impl FullValidatorConfig { | ||
/// Generates a validator config for a network with a single validator. | ||
pub fn for_single_validator(rng: &mut impl Rng, genesis_block_payload: Payload) -> Self { | ||
let mut net_configs = Instance::new_configs(rng, 1, 0); | ||
assert_eq!(net_configs.len(), 1); | ||
let net_config = net_configs.pop().unwrap(); | ||
let consensus_config = net_config.consensus.unwrap(); | ||
let validator_key = consensus_config.key.clone(); | ||
let consensus_config = ConsensusConfig::from(consensus_config); | ||
|
||
let (genesis_block, validators) = | ||
make_genesis(&[validator_key.clone()], genesis_block_payload); | ||
let node_key = net_config.gossip.key.clone(); | ||
let node_config = ExecutorConfig { | ||
server_addr: *net_config.server_addr, | ||
gossip: net_config.gossip.into(), | ||
genesis_block, | ||
validators, | ||
}; | ||
|
||
Self { | ||
node_config, | ||
node_key, | ||
consensus_config, | ||
validator_key, | ||
} | ||
} | ||
|
||
/// Creates a new external node and configures this validator to accept incoming connections from it. | ||
pub fn connect_external_node(&mut self, rng: &mut impl Rng) -> ExternalNodeConfig { | ||
let external_node_config = ExternalNodeConfig::new(rng, self); | ||
self.node_config | ||
.gossip | ||
.static_inbound | ||
.insert(external_node_config.node_key.public()); | ||
external_node_config | ||
} | ||
} | ||
|
||
/// Configuration for an external node (i.e., non-validator node). | ||
#[derive(Debug)] | ||
#[non_exhaustive] | ||
pub struct ExternalNodeConfig { | ||
/// Executor configuration. | ||
pub node_config: ExecutorConfig, | ||
/// Secret key of the node used for identification in the gossip network. | ||
pub node_key: node::SecretKey, | ||
} | ||
|
||
impl ExternalNodeConfig { | ||
fn new(rng: &mut impl Rng, validator: &FullValidatorConfig) -> Self { | ||
let node_key: node::SecretKey = rng.gen(); | ||
let external_node_addr = net::tcp::testonly::reserve_listener(); | ||
let node_config = ExecutorConfig { | ||
server_addr: *external_node_addr, | ||
gossip: GossipConfig { | ||
key: node_key.public(), | ||
static_outbound: HashMap::from([( | ||
validator.node_key.public(), | ||
validator.node_config.server_addr, | ||
)]), | ||
..validator.node_config.gossip.clone() | ||
}, | ||
..validator.node_config.clone() | ||
}; | ||
|
||
Self { | ||
node_config, | ||
node_key, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.