Skip to content

Commit

Permalink
Merge branch 'main' of github.com:monadicus/snarkos-test into feat/bi…
Browse files Browse the repository at this point in the history
…nary-opt-&-compression
  • Loading branch information
gluax committed Apr 8, 2024
2 parents 2ba5303 + e2a88fe commit aae49b3
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 178 deletions.
3 changes: 1 addition & 2 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
3 changes: 3 additions & 0 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
10 changes: 9 additions & 1 deletion crates/snops/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ impl Environment {
.collect()
}

/// Deserialize (YAML) many documents into a `Vec` of documents.
pub fn deserialize_bytes(str: &[u8]) -> Result<Vec<ItemDocument>, DeserializeError> {
serde_yaml::Deserializer::from_slice(str)
.enumerate()
.map(|(i, doc)| ItemDocument::deserialize(doc).map_err(|e| DeserializeError { i, e }))
.collect()
}

/// Prepare a test. This will set the current test on the GlobalState.
///
/// **This will error if the current env is not unset before calling to
Expand Down Expand Up @@ -433,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
82 changes: 53 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 Expand Up @@ -281,3 +281,27 @@ impl NodeTargets {
}
}
}

#[cfg(test)]
mod test {
use crate::env::Environment;

#[test]
fn deserialize_specs() {
for entry in std::fs::read_dir("../../specs")
.expect("failed to read specs dir")
.map(Result::unwrap)
{
let file_name = entry.file_name();
let name = file_name.to_str().expect("failed to read spec file name");
if !name.ends_with(".yaml") && !name.ends_with(".yml") {
continue;
}

let data = std::fs::read(entry.path()).expect("failed to read spec file");
if let Err(e) = Environment::deserialize_bytes(&data) {
panic!("failed to deserialize spec file {name}: {e}")
}
}
}
}
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 aae49b3

Please sign in to comment.