Skip to content

Commit

Permalink
Merge pull request #106 from monadicus/feat-node-env-and-key
Browse files Browse the repository at this point in the history
feat(agent): pass env and node_key into node state
  • Loading branch information
voximity committed Apr 8, 2024
2 parents 8fa6761 + 87c509e commit e2a88fe
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 185 deletions.
6 changes: 3 additions & 3 deletions crates/snops-agent/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,9 @@ impl AgentService for AgentRpcServer {
};

command
// .kill_on_drop(true)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
// .stdin(Stdio::null())
.envs(&node.env)
.arg("--log")
.arg(state.cli.path.join(SNARKOS_LOG_FILE))
.arg("run")
Expand Down Expand Up @@ -497,7 +496,8 @@ impl AgentService for AgentRpcServer {
) -> Result<(), AgentError> {
info!("executing authorization...");

// TODO: maybe in the env config store a branch label for the binary so it won't be put in storage and won't overwrite itself
// TODO: maybe in the env config store a branch label for the binary so it won't
// be put in storage and won't overwrite itself

// download the snarkOS binary
api::check_binary(
Expand Down
15 changes: 9 additions & 6 deletions crates/snops-common/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::HashMap,
fmt::{Display, Write},
net::SocketAddr,
str::FromStr,
Expand Down Expand Up @@ -55,6 +56,7 @@ impl AgentState {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeState {
pub node_key: NodeKey,
pub ty: NodeType,
pub private_key: KeyState,
/// Increment the usize whenever the request is updated.
Expand All @@ -63,6 +65,7 @@ pub struct NodeState {
pub online: bool,
pub peers: Vec<AgentPeer>,
pub validators: Vec<AgentPeer>,
pub env: HashMap<String, String>,
}

/// A representation of which key to use for the agent.
Expand Down Expand Up @@ -234,8 +237,8 @@ mod strings {
}

/// for some reason bincode does not allow deserialize_any so if i want to allow
/// end users to type "top", 42, or "persist" i need to do have to copies of this
/// where one is not untagged.
/// end users to type "top", 42, or "persist" i need to do have to copies of
/// this where one is not untagged.
///
/// bincode. please.
#[derive(Debug, Copy, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
Expand All @@ -248,8 +251,8 @@ pub enum DocHeightRequest {
/// Set the height to the given block
Absolute(u32),
/// Use the same ledger as configured when the same storage was used.
/// WARNING: this may create issues if the same storage id is reused between tests
/// with different nodes.
/// WARNING: this may create issues if the same storage id is reused between
/// tests with different nodes.
#[serde(with = "strings::persist")]
Persist,
// the control plane doesn't know the heights the nodes are at
Expand All @@ -266,8 +269,8 @@ pub enum HeightRequest {
/// Set the height to the given block
Absolute(u32),
/// Use the same ledger as configured when the same storage was used.
/// WARNING: this may create issues if the same storage id is reused between tests
/// with different nodes.
/// WARNING: this may create issues if the same storage id is reused between
/// tests with different nodes.
Persist,
// the control plane doesn't know the heights the nodes are at
// TruncateHeight(u32),
Expand Down
2 changes: 1 addition & 1 deletion crates/snops/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ pub async fn initial_reconcile(env_id: usize, state: &GlobalState) -> Result<(),
};

// resolve the peers and validators
let mut node_state = node.into_state(key.ty);
let mut node_state = node.into_state(key.to_owned());
node_state.private_key = node
.key
.as_ref()
Expand Down
58 changes: 29 additions & 29 deletions crates/snops/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,42 +58,42 @@ pub enum NodeTargets {
Many(Vec<NodeTarget>),
}

struct NodeTargetsVisitor;
impl<'de> Deserialize<'de> for NodeTargets {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct NodeTargetsVisitor;

impl<'de> Visitor<'de> for NodeTargetsVisitor {
type Value = NodeTargets;
impl<'de> Visitor<'de> for NodeTargetsVisitor {
type Value = NodeTargets;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("one or more node targets")
}
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("one or more node targets")
}

fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
Ok(NodeTargets::One(FromStr::from_str(v).map_err(E::custom)?))
}
fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
Ok(NodeTargets::One(FromStr::from_str(v).map_err(E::custom)?))
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut buf = vec![];
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut buf = vec![];

while let Some(elem) = seq.next_element()? {
buf.push(NodeTarget::from_str(elem).map_err(A::Error::custom)?);
}
while let Some(elem) = seq.next_element()? {
buf.push(NodeTarget::from_str(elem).map_err(A::Error::custom)?);
}

Ok(if buf.is_empty() {
NodeTargets::None
} else {
NodeTargets::Many(buf)
})
}
}
Ok(if buf.is_empty() {
NodeTargets::None
} else {
NodeTargets::Many(buf)
})
}
}

impl<'de> Deserialize<'de> for NodeTargets {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_any(NodeTargetsVisitor)
}
}
Expand Down
64 changes: 37 additions & 27 deletions crates/snops/src/schema/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{collections::HashSet, fmt::Display, net::SocketAddr, str::FromStr};
use std::{
collections::{HashMap, HashSet},
fmt::Display,
net::SocketAddr,
str::FromStr,
};

use fixedbitset::FixedBitSet;
use indexmap::IndexMap;
Expand All @@ -7,7 +12,7 @@ use serde::{de::Visitor, Deserialize, Deserializer, Serialize};
use snops_common::{
lasso::Spur,
set::{MaskBit, MASK_PREFIX_LEN},
state::{AgentId, DocHeightRequest, KeyState, NodeState, NodeType},
state::{AgentId, DocHeightRequest, NodeState},
INTERN,
};

Expand Down Expand Up @@ -90,22 +95,25 @@ pub struct Node {
/// List of peers for the node to connect to
#[serde(default)]
pub peers: NodeTargets,

/// Environment variables to inject into the snarkOS process.
#[serde(default)]
pub env: HashMap<String, String>,
}

impl Node {
pub fn into_state(&self, ty: NodeType) -> NodeState {
pub fn into_state(&self, node_key: NodeKey) -> NodeState {
NodeState {
ty,
private_key: KeyState::None,

// TODO
ty: node_key.ty,
node_key,
private_key: Default::default(),
height: (0, self.height.into()),

online: self.online,
env: self.env.clone(),

// these are resolved later
validators: vec![],
peers: vec![],
validators: Default::default(),
peers: Default::default(),
}
}

Expand Down Expand Up @@ -142,28 +150,30 @@ pub enum KeySource {
Named(String, Option<usize>),
}

struct KeySourceVisitor;

impl<'de> Visitor<'de> for KeySourceVisitor {
type Value = KeySource;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a string that represents an aleo private key, or a file from storage")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
KeySource::from_str(v).map_err(E::custom)
}
}

impl<'de> Deserialize<'de> for KeySource {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct KeySourceVisitor;

impl<'de> Visitor<'de> for KeySourceVisitor {
type Value = KeySource;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str(
"a string that represents an aleo private key, or a file from storage",
)
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
KeySource::from_str(v).map_err(E::custom)
}
}

deserializer.deserialize_str(KeySourceVisitor)
}
}
Expand Down
46 changes: 23 additions & 23 deletions crates/snops/src/schema/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,34 +129,34 @@ impl Default for LedgerGeneration {
#[derive(Debug, Clone, Serialize)]
pub struct FilenameString(String);

struct FilenameStringVisitor;

impl<'de> Visitor<'de> for FilenameStringVisitor {
type Value = FilenameString;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a string that can be used as a filename")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if v.contains('/') {
Err(E::custom("filename string cannot have a path separator"))
} else if v == "." || v == ".." {
Err(E::custom("filename string cannot be relative"))
} else {
Ok(FilenameString(String::from(v)))
}
}
}

impl<'de> Deserialize<'de> for FilenameString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct FilenameStringVisitor;

impl<'de> Visitor<'de> for FilenameStringVisitor {
type Value = FilenameString;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a string that can be used as a filename")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if v.contains('/') {
Err(E::custom("filename string cannot have a path separator"))
} else if v == "." || v == ".." {
Err(E::custom("filename string cannot be relative"))
} else {
Ok(FilenameString(String::from(v)))
}
}
}

deserializer.deserialize_str(FilenameStringVisitor)
}
}
Expand Down
Loading

0 comments on commit e2a88fe

Please sign in to comment.