Skip to content

Commit

Permalink
add examples to understand the process flow with workspace addition
Browse files Browse the repository at this point in the history
  • Loading branch information
Shourya742 committed Jan 14, 2025
1 parent 09fe7be commit 89e0c58
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 1 deletion.
3 changes: 2 additions & 1 deletion utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ members = [
"error-handling",
"key-utils",
"bip32-key-derivation",
]
"sv2_serde_json"
, "sv2_serde_json_macros"]

exclude = [
"message-generator",
Expand Down
63 changes: 63 additions & 0 deletions utils/sv2_serde_json/examples/derive_macro_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use sv2_serde_json::value::{FromJsonValue, ToJsonValue};
use sv2_serde_json_macros::{DeJson, SerJson};

#[derive(SerJson, DeJson, Debug, Clone)]
struct Person {
name: String,
age: i64,
is_student: bool,
here: Vec<String>,
check: sv2_serde_json::value::Value,
optional_check: Option<sv2_serde_json::value::Value>,
non_optional_check: Option<String>,
}

#[derive(SerJson, DeJson, Debug)]
struct Person2 {
person: Person,
optional_struct: Option<Person>,
}

#[derive(SerJson, DeJson, Debug)]
enum Status {
Active,
Inactive,
Pending,
Here(Person),
}

fn main() {
let person = Person {
name: "Alice".to_string(),
age: 25,
is_student: true,
here: vec!["HEre".to_string()],
check: sv2_serde_json::value::Value::Null,
optional_check: Some(sv2_serde_json::value::Value::Null),
non_optional_check: Some("Hello".to_string()),
};

let person2 = Person2 {
person: person.clone(),
optional_struct: Some(person.clone()),
};

let person_json = person.to_json_value();
println!("Serialized: {:?}", person_json.to_json_string());

let deserialized_person = Person::from_json_value(&person_json).unwrap();
println!("Deserialized: {:?}", deserialized_person);

let person2_json = person2.to_json_value();
println!("Serialized: {:?}", person2_json.to_json_string());

let deserialized_person2 = Person2::from_json_value(&person2_json).unwrap();
println!("Deserialized: {:?}", deserialized_person2);

let status = Status::Active;
let status_json = status.to_json_value();
println!("Serialized Enum: {:?}", status_json);

let deserialized_status = Status::from_json_value(&status_json);
println!("Deserialized Enum: {:?}", deserialized_status);
}
11 changes: 11 additions & 0 deletions utils/sv2_serde_json/examples/reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// examples/json.rs

use std::fs::File;
use sv2_serde_json::parser::JsonParser;

fn main() {
let file = File::open("./examples/test.json").unwrap();
let parser = JsonParser::parse(file).unwrap();

dbg!(parser);
}
19 changes: 19 additions & 0 deletions utils/sv2_serde_json/examples/reader_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::{fs::File, io::Write};

use sv2_serde_json::{parser::JsonParser, value::Value};

fn write_value_to_file(value: &Value, file_path: &str) -> std::io::Result<()> {
let json_bytes = value.to_json_bytes();
let mut file = File::create(file_path)?;
file.write_all(&json_bytes)?;
Ok(())
}

fn main() {
let file = File::open("./examples/test.json").unwrap();
let parser = JsonParser::parse(file).unwrap();

let _ = write_value_to_file(&parser, "./examples/result.json");

dbg!(parser);
}
1 change: 1 addition & 0 deletions utils/sv2_serde_json/examples/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"cleanup_commands":[],"version":"2","frame_builders":[{"type":"automatic","message_id":"setup_connection_success"},{"message_id":"setup_connection","type":"automatic"},{"type":"automatic","message_id":"open_standard_mining_channel"}],"role":"client","downstream":{"port":34254,"pub_key":"2di19GHYQnAZJmEpoUeP7C3Eg9TCcksHr23rZCC83dvUiZgiDL","ip":"0.0.0.0"},"mining_messages":[{"id":"open_standard_mining_channel","message":{"request_id":89,"nominal_hash_rate":10,"max_target":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],"user_identity":"","type":"OpenStandardMiningChannel"}}],"actions":[{"message_ids":["setup_connection"],"actiondoc":"","results":[{"type":"match_message_type","value":"0x01"}],"role":"client"},{"message_ids":["open_standard_mining_channel"],"role":"client","actiondoc":"","results":[{"type":"match_message_field","value":["MiningProtocol","OpenStandardMiningChannelSuccess",[["request_id",{"U32":89}],["extranonce_prefix",{"B032":[[32],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]]}]]]},{"type":"match_message_type","value":"0x20"},{"type":"match_message_type","value":"0x1f"}]}],"execution_commands":[],"doc":["What this test does:","1) launch SRI pool and wait for initialization","2) connect to the pool as a Downstream that uses noise","3) send SetupConnection to the pool","4) check that SetupConnectionSuccess is received","5) send OpenStandardMiningChannel with request id 89","6) check that NewExtendedJob is received","7) check that NewPrevHash is received","8) check that OpenStandardMiningChannelSuccess with request 89 is received"],"common_messages":[{"message":{"hardware_version":"","min_version":2,"flags":0,"type":"SetupConnection","endpoint_host":"","endpoint_port":0,"protocol":0,"vendor":"","max_version":2,"firmware":"","device_id":""},"id":"setup_connection"},{"message":{"used_version":2,"type":"SetupConnectionSuccess","flags":0},"id":"setup_connection_success"}],"setup_commands":[{"command":"cargo","conditions":{"WithConditions":{"warn_no_panic":false,"conditions":[{"output_string":"Listening for encrypted connection on: 0.0.0.0:34254","condition":true,"output_location":"StdOut"}],"timer_secs":60}},"args":["run","-p","pool","--","-c","./test/config/pool-config-sri-tp.toml"]}]}
148 changes: 148 additions & 0 deletions utils/sv2_serde_json/examples/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"version": "2",
"doc": [
"What this test does:",
"1) launch SRI pool and wait for initialization",
"2) connect to the pool as a Downstream that uses noise",
"3) send SetupConnection to the pool",
"4) check that SetupConnectionSuccess is received",
"5) send OpenStandardMiningChannel with request id 89",
"6) check that NewExtendedJob is received",
"7) check that NewPrevHash is received",
"8) check that OpenStandardMiningChannelSuccess with request 89 is received"
],
"common_messages": [
{
"message": {
"type": "SetupConnection",
"protocol": 0,
"min_version": 2,
"max_version": 2,
"flags": 0,
"endpoint_host": "",
"endpoint_port": 0,
"vendor": "",
"hardware_version": "",
"firmware": "",
"device_id": ""
},
"id": "setup_connection"
},
{
"message": {
"type": "SetupConnectionSuccess",
"flags": 0,
"used_version": 2
},
"id": "setup_connection_success"
}
],
"mining_messages": [
{
"message": {
"type": "OpenStandardMiningChannel",
"request_id": 89,
"user_identity": "",
"nominal_hash_rate": 10,
"max_target": [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
},
"id": "open_standard_mining_channel"
}
],
"frame_builders": [
{
"type": "automatic",
"message_id": "setup_connection_success"
},
{
"type": "automatic",
"message_id": "setup_connection"
},
{
"type": "automatic",
"message_id": "open_standard_mining_channel"
}
],
"actions": [
{
"message_ids": ["setup_connection"],
"role": "client",
"results": [
{
"type": "match_message_type",
"value": "0x01"
}
],
"actiondoc": ""
},
{
"message_ids": ["open_standard_mining_channel"],
"role": "client",
"results": [
{
"type": "match_message_field",
"value": [
"MiningProtocol",
"OpenStandardMiningChannelSuccess",
[
[
"request_id",
{"U32": 89}
],
[
"extranonce_prefix",
{"B032": [[32], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]]}
]
]
]
},
{
"type": "match_message_type",
"value": "0x20"
},
{
"type": "match_message_type",
"value": "0x1f"
}
],
"actiondoc": ""
}

],
"setup_commands": [
{
"command": "cargo",
"args": [
"run",
"-p",
"pool",
"--",
"-c",
"./test/config/pool-config-sri-tp.toml"
],
"conditions": {
"WithConditions": {
"conditions": [
{
"output_string": "Listening for encrypted connection on: 0.0.0.0:34254",
"output_location": "StdOut",
"condition": true
}
],
"timer_secs": 60,
"warn_no_panic": false
}
}
}
],
"execution_commands": [
],
"cleanup_commands": [
],
"role": "client",
"downstream": {
"ip": "0.0.0.0",
"port": 34254,
"pub_key": "2di19GHYQnAZJmEpoUeP7C3Eg9TCcksHr23rZCC83dvUiZgiDL"
}
}

0 comments on commit 89e0c58

Please sign in to comment.