Skip to content

Commit

Permalink
Make path more intuitive in tests (stratum-mining#486)
Browse files Browse the repository at this point in the history
Now it is possible to launch a test from the MG directory by using
    simply "cargo run [relative path of the test], without the additional
    "../". Moreover, in the frame builders relative to the common messages,
    it is possible to specify the frame with the following pattern
    [path-of-the-module]::[framebuilder].
    For example, if we want to write a test that setis a standard channel with
    the pool for HOM mining, the message-id of the frame builer is
    test/message-generator/messages/common_messages.json::setup_connection_mining_hom
  • Loading branch information
lorbax authored Mar 15, 2023
1 parent e9b3019 commit 21692e7
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 78 deletions.
2 changes: 1 addition & 1 deletion message-generator-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ cd $message_generator_dir

for entry in `ls $search_dir`; do
echo $entry
cargo run -- ../$search_dir$entry || { echo 'mg test failed' ; exit 1; }
cargo run -- $search_dir$entry || { echo 'mg test failed' ; exit 1; }
done
5 changes: 5 additions & 0 deletions mg-codecov-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
message_generator_dir="./utils/message-generator/"

cd $message_generator_dir

cargo run -- ../../test/message-generator/cov/cov_test.json
8 changes: 8 additions & 0 deletions test/message-generator/messages/common_messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
"used_version": 2
},
"id": "setup_connection_success_template_distribution"
},
{
"message": {
"type": "SetupConnectionSuccess",
"flags": 0,
"used_version": 2
},
"id": "setup_connection_success_tproxy"
}

]
Expand Down
27 changes: 14 additions & 13 deletions test/message-generator/mock/job-negotiator-mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"frame_builders": [
{
"type": "automatic",
"message_id": "../../../../test/message-generator/messages/common_messages.json::setup_connection_job_negotiator"
"message_id": "test/message-generator/messages/common_messages.json::setup_connection_job_negotiator"
}
],
"actions": [
Expand All @@ -28,19 +28,20 @@

],
"actiondoc": "This action sends SetupConnection and checks that .Success and SetCoinbase is received"
},
{
"message_ids": [],
"role": "client",
"results": [
{
"type": "match_message_type",
"value": "0x50"
}
},
{
"message_ids": [],
"role": "client",
"results": [
{
"type": "match_message_type",
"value": "0x50"
}

],
"actiondoc": "This action sends SetupConnection and checks that SetCoinbase is received"
}

],
"actiondoc": "This action sends SetupConnection and checks that SetCoinbase is received"
}
],
"setup_commands": [],
"execution_commands": [],
Expand Down
2 changes: 1 addition & 1 deletion test/message-generator/mock/template-provider-mock0.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"frame_builders": [
{
"type": "automatic",
"message_id": "../../../../test/message-generator/messages/common_messages.json::setup_connection_success_template_distribution"
"message_id": "test/message-generator/messages/common_messages.json::setup_connection_success_template_distribution"
},
{
"type": "automatic",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"doc": [
"This test does",
"Launch 2 pool on differnet address",
"Launch 2 pools on differnet address",
"Launch mining-proxy",
"Connect proxy to the 2 above pool",
"Try connect proxy to non exsistent pool proxy must not panic",
Expand Down
64 changes: 42 additions & 22 deletions test/message-generator/test/pool-sri-test-1-standard.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"frame_builders": [
{
"type": "automatic",
"message_id": "../../../../test/message-generator/messages/common_messages.json::setup_connection_mining_hom"
"message_id": "test/message-generator/messages/common_messages.json::setup_connection_mining_hom"
},
{
"type": "automatic",
Expand Down Expand Up @@ -114,7 +114,27 @@
}
],
"setup_commands": [
{
{
"command": "cargo",
"args": [
"run",
"../../test/message-generator/mock/template-provider-mock0.json"
],
"conditions": {
"WithConditions": {
"conditions": [
{
"output_string": "Running `target/debug/message_generator_sv2 ../../test/message-generator/mock/template-provider-mock0.json`",
"output_location": "StdErr",
"condition": true
}
],
"timer_secs": 240,
"warn_no_panic": false
}
}
},
{
"command": "cargo",
"args": [
"run",
Expand Down Expand Up @@ -160,26 +180,26 @@
}
}
},
{
"command": "cargo",
"args": [
"run",
"../../../test/message-generator/mock/job-negotiator-mock.json"
],
"conditions": {
"WithConditions": {
"conditions": [
{
"output_string": "Running `target/debug/message_generator_sv2 ../../../test/message-generator/mock/job-negotiator-mock.json`",
"output_location": "StdErr",
"condition": true
}
],
"timer_secs": 240,
"warn_no_panic": false
}
}
}
{
"command": "cargo",
"args": [
"run",
"../../test/message-generator/mock/job-negotiator-mock.json"
],
"conditions": {
"WithConditions": {
"conditions": [
{
"output_string": "Running `target/debug/message_generator_sv2 ../../test/message-generator/mock/job-negotiator-mock.json`",
"output_location": "StdErr",
"condition": true
}
],
"timer_secs": 240,
"warn_no_panic": false
}
}
}
],
"execution_commands": [
],
Expand Down
2 changes: 1 addition & 1 deletion test/message-generator/test/pool-sri-test-extended_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"frame_builders": [
{
"type": "automatic",
"message_id": "../../../../test/message-generator/messages/common_messages.json::setup_connection_flag_0"
"message_id": "test/message-generator/messages/common_messages.json::setup_connection_flag_0"
},
{
"type": "automatic",
Expand Down
6 changes: 3 additions & 3 deletions test/message-generator/test/pool-sri-test-extended_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"frame_builders": [
{
"type": "automatic",
"message_id": "../../../../test/message-generator/messages/common_messages.json::setup_connection_flag_0"
"message_id": "test/message-generator/messages/common_messages.json::setup_connection_flag_0"
},
{
"type": "automatic",
Expand Down Expand Up @@ -89,13 +89,13 @@
"command": "cargo",
"args": [
"run",
"../../../test/message-generator/mock/template-provider-mock0.json"
"../../test/message-generator/mock/template-provider-mock0.json"
],
"conditions": {
"WithConditions": {
"conditions": [
{
"output_string": "Running `target/debug/message_generator_sv2 ../../../test/message-generator/mock/template-provider-mock0.json`",
"output_string": "Running `target/debug/message_generator_sv2 ../../test/message-generator/mock/template-provider-mock0.json`",
"output_location": "StdErr",
"condition": true
}
Expand Down
4 changes: 2 additions & 2 deletions test/message-generator/test/standard-coverage-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@
"command": "cargo",
"args": [
"run",
"../../../test/message-generator/mock/template-provider-mock0.json"
"../../test/message-generator/mock/template-provider-mock0.json"
],
"conditions": {
"WithConditions": {
"conditions": [
{
"output_string": "Running `target/debug/message_generator_sv2 ../../../test/message-generator/mock/template-provider-mock0.json`",
"output_string": "Running `target/debug/message_generator_sv2 ../../test/message-generator/mock/template-provider-mock0.json`",
"output_location": "StdErr",
"condition": true
}
Expand Down
40 changes: 27 additions & 13 deletions utils/message-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ Little utility to execute interoperability tests between SRI and other Sv2 compl
"--datadir=PATH TO bitcoin datadir",
...
```
3. `% cargo run ../test.json`
4. If the test is in the `/test/message-geneator` folder, you have to put an additional `../` to
the test address to launch the test from `/utils/message-generator`. For example, if you want
to launch
3. `% cargo run test.json`
4. If the test is in the `/test/message-geneator` directory, you have to lauch it from the MG
directory using relative path. For example,
```
../../test/message-geneator/test/pool-sri-test-1.json
```
the following is the right command
```
cargo run ../../../test/message-geneator/test/pool-sri-test-1.json
cargo run ../../test/message-geneator/test/pool-sri-test-1.json
```

## Test execution
Expand All @@ -45,6 +40,24 @@ The message generator executes a test with the following steps:
received.
5. Cleanup Commands: Executes shell commands or bash scripts to be run on test completion, e.g. remove a `bitcoind` `datadir`.

## True tests, mocks and modules
The true tests are located in `test/message-generator/test`. Some files have the structure of a
test but in fact they are not.
For example, the file in
`test/message-generator/mock` are mocks of application, namely they are tests whose purpose is
to pretend to be an SV2 role. Currently only the TemplateProvider and the JobNegotiator are the
only roles mocked. These mocks are usually used in the true tests. For example, in the test
`test/message-generator/test/pool-sri-test-standard-1.json`
the pool has a mocked environment, i.e. the JN and TP mocks.
The files in `test/message-generator/messages` also are not true tests, but the are intended to be
modules. For example, the `common_messages.json` is the module that contains the frame builders of
the common messages for the true tests and the mocks.

True tests are made to be run and produce positive outcome.
Mocks and common messages are meant to work as a part of a true test and they are not supposed to
be run as standalone.


## Test format

Tests are written in json and must follow the below format:
Expand All @@ -66,8 +79,9 @@ Tests are written in json and must follow the below format:

### common_messages

`common_messages` is an array of messages (defined below) belonging to the common (sub)protocol. This field is optional.
Where the common subprotocol is composed by: `SetupConnection` and `SetupConnectionSuccees` and `SetupConnectionError`.
`common_messages` is an array of messages (defined below) belonging to the common (sub)protocol,
where the common subprotocol is composed by: `SetupConnection` and `SetupConnectionSuccees` and `SetupConnectionError`.
This field is optional.

### mining_messages

Expand Down Expand Up @@ -162,11 +176,11 @@ use it you have to use the syntax `<address::id>`. For example, in the test
```
{
"type": "automatic",
"message_id": "../../../../test/message-generator/messages/common_messages.json::setup_connection_success_template_distribution"
"message_id": "test/message-generator/messages/common_messages.json::setup_connection_success_template_distribution"
}
```
calls the id `setup_connection_success_template_distribution` that appears in the file
`../../../test/message-generator/messages/common_messages.json`. In the main file, the id of this
`test/message-generator/messages/common_messages.json`. In the main file, the id of this
message will be the abbreviated with `setup_connection_success_template_distribution`.


Expand Down
34 changes: 17 additions & 17 deletions utils/message-generator/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
external_commands::os_command,
net::{setup_as_downstream, setup_as_upstream},
Action, ActionResult, Command, Role, Test, Sv2Type
Action, ActionResult, Command, Role, Sv2Type, Test,
};
use async_channel::{Receiver, Sender};
use codec_sv2::{Frame, StandardEitherFrame as EitherFrame, Sv2Frame};
Expand Down Expand Up @@ -169,9 +169,8 @@ impl Executor {
ActionResult::MatchMessageField((
subprotocol,
message_type,
field_data // Vec<(String, Sv2Type)>
field_data, // Vec<(String, Sv2Type)>
)) => {

if subprotocol.as_str() == "CommonMessage" {
match (header.msg_type(),payload).try_into() {
Ok(roles_logic_sv2::parsers::CommonMessages::SetupConnection(m)) => {
Expand Down Expand Up @@ -344,7 +343,7 @@ impl Executor {
Ok(roles_logic_sv2::parsers::JobNegotiation::SetCoinbase(m)) => {
if message_type.as_str() == "SetCoinbase" {
let msg = serde_json::to_value(&m).unwrap();
check_each_field(msg,field_data);
check_each_field(msg, field_data);
}
}
Err(e) => panic!("err {:?}", e),
Expand Down Expand Up @@ -402,7 +401,6 @@ impl Executor {
);
panic!()
}

}
ActionResult::MatchMessageLen(message_len) => {
if payload.len() != *message_len {
Expand Down Expand Up @@ -456,22 +454,24 @@ impl Executor {
}
}

fn check_msg_field(
msg: serde_json::Value,
field_name: &str,
value_type: &str,
field: &Sv2Type,
) {
fn check_msg_field(msg: serde_json::Value, field_name: &str, value_type: &str, field: &Sv2Type) {
let msg = msg.as_object().unwrap();
let value = msg.get(field_name).expect("match_message_field field name is not valid").clone();
let value = msg
.get(field_name)
.expect("match_message_field field name is not valid")
.clone();
let value = serde_json::to_string(&value).unwrap();
let value = format!(r#"{{"{}":{}}}"#, value_type, value);
let value: crate::Sv2Type = serde_json::from_str(&value).unwrap();
assert!(field == &value, "match_message_field value is incorrect. Expected = {:?}, Recieved = {:?}", field, value)
assert!(
field == &value,
"match_message_field value is incorrect. Expected = {:?}, Recieved = {:?}",
field,
value
)
}

fn check_each_field(msg: serde_json::Value, field_info: &Vec<(String, Sv2Type)>) {

for field in field_info {
let value_type = serde_json::to_value(&field.1)
.unwrap()
Expand All @@ -481,7 +481,7 @@ fn check_each_field(msg: serde_json::Value, field_info: &Vec<(String, Sv2Type)>)
.next()
.unwrap()
.to_string();
check_msg_field(msg.clone(),&field.0,&value_type,&field.1)

check_msg_field(msg.clone(), &field.0, &value_type, &field.1)
}
}
}
5 changes: 4 additions & 1 deletion utils/message-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ async fn main() {
println!();
println!("EXECUTING {}", test_path);
println!();
let mut _test_path = args[1].clone();
_test_path.insert_str(0, "../");
let test_path_ = &_test_path;
// Load contents of `test.json`, then parse
let test = load_str!(test_path);
let test = load_str!(test_path_);
let test = parser::Parser::parse_test(test);
// Executes everything (the shell commands and actions)
// If the `executor` returns false, the test fails
Expand Down
2 changes: 1 addition & 1 deletion utils/message-generator/src/parser/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl ActionParser {
let sv2_type = result.get("value").unwrap().clone();
let sv2_type: (String, String, Vec<(String, Sv2Type)>) =
serde_json::from_value(sv2_type)
.expect("match_message_field values not correct");
.expect("match_message_field values not correct");
action_results.push(ActionResult::MatchMessageField(sv2_type));
}
"match_message_len" => {
Expand Down
Loading

0 comments on commit 21692e7

Please sign in to comment.