Skip to content

Commit

Permalink
refactor(snot): ensure storage is unified across all nodes in a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Meshiest committed Mar 23, 2024
1 parent 637ed71 commit 0f894af
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
15 changes: 13 additions & 2 deletions crates/aot/src/ledger/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use std::{fs::File, io::Write, ops::Deref, path::PathBuf, sync::Arc};
use std::{
fs::File,
io::Write,
net::{IpAddr, SocketAddr},
ops::Deref,
path::PathBuf,
sync::Arc,
};

use anyhow::Result;
use axum::{
Expand All @@ -21,6 +28,10 @@ pub struct LedgerQuery {
/// Port to listen on for incoming messages
pub port: u16,

#[arg(long, default_value = "0.0.0.0")]
// IP address to bind to
pub bind: IpAddr,

#[arg(long)]
/// When true, the POST /block endpoint will not be available
pub readonly: bool,
Expand Down Expand Up @@ -72,7 +83,7 @@ impl LedgerQuery {
.route("/block", post(Self::add_block))
.with_state(Arc::new(state));

let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", self.port)).await?;
let listener = tokio::net::TcpListener::bind(SocketAddr::new(self.bind, self.port)).await?;
tracing::info!("listening on: {:?}", listener.local_addr().unwrap());
axum::serve(listener, app).await?;

Expand Down
3 changes: 0 additions & 3 deletions crates/snot/src/schema/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ pub struct Node {
// TODO: turn this into an enum with options like `None`, `Additional(usize)`,
// `Committee(usize)`, `Named(String)`, `Literal(String)`
pub key: Option<String>,
/// The storage ID to use when starting the node.
/// TODO: move this outside of the node. this is a setting for the swarm
pub storage: String,
/// Height of ledger to inherit.
///
/// * When null, a ledger is created when the node is started.
Expand Down
33 changes: 27 additions & 6 deletions crates/snot/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ use tracing::{info, warn};
use crate::{
schema::{
nodes::{ExternalNode, Node},
storage::FilenameString,
ItemDocument, NodeTargets,
},
state::GlobalState,
};

#[derive(Debug, Clone)]
pub struct Test {
pub storage_id: FilenameString,
pub node_map: BiMap<NodeKey, TestPeer>,
pub initial_nodes: IndexMap<NodeKey, TestNode>,
// TODO: GlobalStorage.storage should maybe be here instead
Expand Down Expand Up @@ -57,7 +59,15 @@ impl Test {

let mut state_lock = state.test.write().await;

let Some(storage_id) = documents.iter().find_map(|s| match s {
ItemDocument::Storage(storage) => Some(storage.id.clone()),
_ => None,
}) else {
bail!("no storage document found in test")
};

let mut test = Test {
storage_id,
node_map: Default::default(),
initial_nodes: Default::default(),
};
Expand Down Expand Up @@ -215,7 +225,16 @@ pub async fn initial_reconcile(state: &GlobalState) -> anyhow::Result<()> {
{
let test_lock = state.test.read().await;
let test = test_lock.as_ref().unwrap();
let storage_lock = state.storage.read().await;

// get the numeric storage ID from the string storage ID
let storage_id = {
let storage_lock = state.storage.read().await;
match storage_lock.get_by_right(test.storage_id.as_str()) {
Some(id) => *id,
None => bail!("invalid storage ID specified for node"),
}
};

let pool_lock = state.pool.read().await;

// Lookup agent peers given a node key
Expand Down Expand Up @@ -264,6 +283,13 @@ pub async fn initial_reconcile(state: &GlobalState) -> anyhow::Result<()> {
// this can't really be cleverly optimized into
// a single lookup at the moment because we don't treat @local
// as a None namespace...

// TODO: ensure @local is always parsed as None, then we can
// optimize each target in this to be a direct lookup
// instead of walking through each node

// alternatively, use a more efficient data structure for
// storing node keys
test.node_map
.iter()
.filter(|(k, _)| *k != key && target.matches(k))
Expand All @@ -275,11 +301,6 @@ pub async fn initial_reconcile(state: &GlobalState) -> anyhow::Result<()> {
let TestNode::Internal(node) = node else {
continue;
};
// get the numeric storage ID from the string storage ID
let storage_id = match storage_lock.get_by_right(&node.storage) {
Some(id) => *id,
None => bail!("invalid storage ID specified for node"),
};

// get the internal agent ID from the node key
let Some(TestPeer::Internal(id)) = test.node_map.get_by_left(key) else {
Expand Down
4 changes: 0 additions & 4 deletions specs/test-4-validators.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,18 @@ nodes:
height: 0
validators: [validator/*]
peers: []
storage: base
validator/1:
key: APrivateKey1zkp2RWGDcde3efb89rjhME1VYA8QMxcxep5DShNBR6n8Yjh
height: 0
validators: [validator/*]
peers: []
storage: base
validator/2:
key: APrivateKey1zkp2GUmKbVsuc1NSj28pa1WTQuZaK5f1DQJAT6vPcHyWokG
height: 0
validators: [validator/*]
peers: []
storage: base
validator/3:
key: APrivateKey1zkpBjpEgLo4arVUkQmcLdKQMiAKGaHAQVVwmF8HQby8vdYs
height: 0
validators: [validator/*]
peers: []
storage: base

0 comments on commit 0f894af

Please sign in to comment.