Skip to content

Commit

Permalink
fix MG rust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorbax committed Jun 6, 2024
1 parent 5f1bb02 commit 19221ff
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 160 deletions.
354 changes: 194 additions & 160 deletions utils/message-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,15 @@ mod test {
use roles_logic_sv2::{
mining_sv2::{
CloseChannel, NewExtendedMiningJob, OpenExtendedMiningChannel,
OpenExtendedMiningChannelSuccess, SetCustomMiningJob, SetTarget,
OpenExtendedMiningChannelSuccess, SetCustomMiningJob, SetTarget, NewMiningJob,
},
parsers::Mining,
job_declaration_sv2::DeclareMiningJob,
common_messages_sv2::{SetupConnection, Protocol},
parsers::{CommonMessages, Mining},
};
use std::convert::TryInto;
use std::{io::Write, convert::TryInto};
use tokio::join;
use key_utils::Secp256k1PublicKey;

// The following test see that the composition serialise fist and deserialize
// second is the identity function (on an example message)
Expand All @@ -472,7 +475,6 @@ mod test {
let message_as_string = serde_json::to_string(&message_as_serde_value).unwrap();
let message: AnyMessage<'_> = serde_json::from_str(&message_as_string).unwrap();
let m_ = into_static(message);
let message_as_string_ = serde_json::to_string(&m_).unwrap();

let message_ = match message_ {
AnyMessage::Mining(m) => m,
Expand Down Expand Up @@ -572,6 +574,7 @@ mod test {
assert!(message_new == message);
}

#[test]
fn test_serialize_and_deserialize_5_scmj() {
let message = SetCustomMiningJob {
channel_id: 1,
Expand All @@ -597,33 +600,52 @@ mod test {
assert!(message_new == message);
}

//DeclareMiningJob in Declaration Protocol
// TODO! MAKE THIS TEST COMPILE AND PASS!
//fn test_serialize_and_deserialize_6_dmj() {
// let message = DeclareMiningJob {
// request_id: 1,
// mining_job_token: binary_sv2::B0255::try_from(vec![3, 0, 0, 0]).unwrap(),
// version: 2,
// coinbase_tx_version: 2,
// coinbase_prefix: todo!(),
// coinbase_tx_input_n_sequence: 1,
// coinbase_tx_value_remaining: 1,
// coinbase_tx_outputs: binary_sv2::B064K::try_from(vec![0, 1, 1]).unwrap(),
// coinbase_tx_locktime: 1,
// min_extranonce_size: 1,
// tx_short_hash_nonce: 1,
// tx_short_hash_list: binary_sv2::Seq064K::new(vec![binary_sv2::ShortTxId::try_from(
// [1; 32],
// )]),
// tx_hash_list_hash: todo!(),
// excess_data: todo!(),
// };
// let message_as_serde_value = serde_json::to_value(message.clone()).unwrap();
// let message_as_string = serde_json::to_string(&message_as_serde_value).unwrap();
// let message_new: DeclareMiningJob = serde_json::from_str(&message_as_string).unwrap();

// assert!(message_new == message);
//}


#[test]
fn test_serialize_and_deserialize_6_dmj() {
let short_tx_id_inner_: Vec<u8> = vec![0, 1, 2, 3, 4, 5];
let short_tx_id_inner = short_tx_id_inner_.as_slice();
let short_tx_id = binary_sv2::ShortTxId::try_from(short_tx_id_inner).unwrap();
let prefix_inner = vec![0_u8; 32];
let prefix_inner_as_ref = prefix_inner.as_slice();
let message = DeclareMiningJob {
request_id: 1,
mining_job_token: binary_sv2::B0255::try_from(vec![3, 0, 0, 0]).unwrap(),
version: 2,
coinbase_prefix: binary_sv2::B064K::try_from(prefix_inner_as_ref).unwrap(),
coinbase_suffix: binary_sv2::B064K::try_from(prefix_inner_as_ref).unwrap(),
tx_short_hash_nonce: 1,
tx_short_hash_list: binary_sv2::Seq064K::new(vec![short_tx_id]).unwrap(),
tx_hash_list_hash: binary_sv2::U256::try_from(vec![1_u8; 32]).unwrap(),
excess_data: binary_sv2::B064K::try_from(prefix_inner_as_ref).unwrap(),
};
let message_as_serde_value = serde_json::to_value(message.clone()).unwrap();
dbg!(&message_as_serde_value);
let message_as_string = serde_json::to_string(&message_as_serde_value).unwrap();
let message_new: DeclareMiningJob = serde_json::from_str(&message_as_string).unwrap();

assert!(message_new == message);
}

//{"Mining":{"NewMiningJob":{"channel_id":1,"job_id":2,"min_ntime":[0,[]],"version":536870912,"merkle_root":[141,193,6,239,151,79,49,202,237,183,207,121,56,40,107,153,235,208,66,215,81,186,3,16,215,110,130,182,37,198,98,251]}}}

#[test]
fn test_serialize_and_deserialize_7_nmj() {
let inner_: Vec<u8> = vec![141,193,6,239,151,79,49,202,237,183,207,121,56,40,107,153,235,208,66,215,81,186,3,16,215,110,130,182,37,198,98,251];
let inner = inner_.as_slice();
let message = NewMiningJob {
channel_id: 1,
job_id: 2,
min_ntime: binary_sv2::Sv2Option::try_from(vec![]).unwrap(),
version: 536870912,
merkle_root: binary_sv2::B032::try_from(inner).unwrap(),
};
let message_as_serde_value = serde_json::to_value(message.clone()).unwrap();
let message_as_string = serde_json::to_string(&message_as_serde_value).unwrap();
let message_new: NewMiningJob = serde_json::from_str(&message_as_string).unwrap();
assert!(message_new == message);
}

#[tokio::test]
async fn it_send_and_receive() {
Expand Down Expand Up @@ -710,133 +732,145 @@ mod test {
assert!(true)
}

//#[tokio::test]
//async fn it_initialize_a_pool_and_connect_to_it() {
// //let mut bitcoind = os_command(
// // "./test/bin/bitcoind",
// // vec!["--regtest", "--datadir=./test/appdata/bitcoin_data/"],
// // ExternalCommandConditions::new_with_timer_secs(10)
// // .continue_if_std_out_have("sv2 thread start")
// // .fail_if_anything_on_std_err(),
// //)
// //.await;
// //let mut child = os_command(
// // "./test/bin/bitcoin-cli",
// // vec![
// // "--regtest",
// // "--datadir=./test/appdata/bitcoin_data/",
// // "generatetoaddress",
// // "16",
// // "bcrt1qttuwhmpa7a0ls5kr3ye6pjc24ng685jvdrksxx",
// // ],
// // ExternalCommandConditions::None,
// //)
// //.await;
// //child.unwrap().wait().await.unwrap();
// let mut pool = os_command(
// "cargo",
// vec![
// "llvm-cov",
// "--no-report",
// "run",
// "-p",
// "pool_sv2",
// "--",
// "-c",
// "./test/config/pool-config-sri-tp.toml",
// ],
// ExternalCommandConditions::new_with_timer_secs(60)
// .continue_if_std_out_have("Listening for encrypted connection on: 127.0.0.1:34254"),
// )
// .await;

// let setup_connection = CommonMessages::SetupConnection(SetupConnection {
// protocol: Protocol::MiningProtocol,
// min_version: 2,
// max_version: 2,
// flags: 0,
// endpoint_host: "".to_string().try_into().unwrap(),
// endpoint_port: 0,
// vendor: "".to_string().try_into().unwrap(),
// hardware_version: "".to_string().try_into().unwrap(),
// firmware: "".to_string().try_into().unwrap(),
// device_id: "".to_string().try_into().unwrap(),
// });

// let frame = Sv2Frame::from_message(
// setup_connection.clone(),
// const_sv2::MESSAGE_TYPE_SETUP_CONNECTION,
// 0,
// true,
// )
// .unwrap();

// let frame = EitherFrame::Sv2(frame);

// let pool_address = SocketAddr::new("127.0.0.1".parse().unwrap(), 34254);
// let pub_key: EncodedEd25519PublicKey = "2di19GHYQnAZJmEpoUeP7C3Eg9TCcksHr23rZCC83dvUiZgiDL"
// .to_string()
// .try_into()
// .unwrap();
// tokio::time::sleep(std::time::Duration::from_secs(1)).await;
// let (recv_from_pool, send_to_pool) = setup_as_downstream(pool_address, Some(pub_key)).await;
// send_to_pool.send(frame.try_into().unwrap()).await.unwrap();
// match recv_from_pool.recv().await.unwrap() {
// EitherFrame::Sv2(a) => {
// assert!(true)
// }
// _ => assert!(false),
// }
// let mut child = os_command(
// "rm",
// vec!["-rf", "./test/appdata/bitcoin_data/regtest"],
// ExternalCommandConditions::None,
// )
// .await;
// child.unwrap().wait().await.unwrap();

// // TODO not panic in network utils but return an handler
// //pool.kill().unwrap();
// //bitcoind.kill().await.unwrap();
// assert!(true)
//}

//#[tokio::test]
//async fn it_test_against_remote_endpoint() {
// let proxy = match os_command(
// "cargo",
// vec![
// "run",
// "-p",
// "mining-proxy",
// "--",
// "-c",
// "./test/config/ant-pool-config.toml",
// ],
// ExternalCommandConditions::new_with_timer_secs(10)
// .continue_if_std_out_have("PROXY INITIALIZED")
// .warn_no_panic(),
// )
// .await
// {
// Some(child) => child,
// None => {
// write!(
// &mut std::io::stdout(),
// "WARNING: remote not avaiable it_test_against_remote_endpoint not executed"
// )
// .unwrap();
// return;
// }
// };
// //loop {}
// let _ = os_command(
// "cargo",
// vec!["run", "-p", "mining-device"],
// ExternalCommandConditions::new_with_timer_secs(10)
// .continue_if_std_out_have("channel opened with"),
// )
// .await;
// assert!(true)
//}
#[tokio::test]
// for testing purposes, it may be useful to leave the code for running bitcoind
async fn it_initialize_a_pool_and_connect_to_it() {
//dont clean the following commented code
/*
let mut bitcoind = os_command(
"./test/bin/bitcoind",
vec!["--regtest", "--datadir=./test/appdata/bitcoin_data/"],
ExternalCommandConditions::new_with_timer_secs(10)
.continue_if_std_out_have("sv2 thread start")
.fail_if_anything_on_std_err(),
)
.await;
let mut child = os_command(
"./test/bin/bitcoin-cli",
vec![
"--regtest",
"--datadir=./test/appdata/bitcoin_data/",
"generatetoaddress",
"16",
"bcrt1qttuwhmpa7a0ls5kr3ye6pjc24ng685jvdrksxx",
],
ExternalCommandConditions::None,
)
.await;
child.unwrap().wait().await.unwrap();
*/
let pool = os_command(
"cargo",
vec![
"llvm-cov",
"--no-report",
"run",
"-p",
"pool_sv2",
"--",
"-c",
"../test/config/pool-config-sri-tp.toml",
],
ExternalCommandConditions::new_with_timer_secs(60)
.continue_if_std_out_have("Listening for encrypted connection on: 127.0.0.1:34254"),
)
.await;
//wait that the pool initializes
tokio::time::sleep(std::time::Duration::from_secs(4)).await;

let setup_connection = CommonMessages::SetupConnection(SetupConnection {
protocol: Protocol::MiningProtocol,
min_version: 2,
max_version: 2,
flags: 0,
endpoint_host: "".to_string().try_into().unwrap(),
endpoint_port: 0,
vendor: "".to_string().try_into().unwrap(),
hardware_version: "".to_string().try_into().unwrap(),
firmware: "".to_string().try_into().unwrap(),
device_id: "".to_string().try_into().unwrap(),
});

let frame = Sv2Frame::from_message(
setup_connection.clone(),
const_sv2::MESSAGE_TYPE_SETUP_CONNECTION,
0,
true,
)
.unwrap();

let frame = EitherFrame::Sv2(frame);

let pool_address = SocketAddr::new("127.0.0.1".parse().unwrap(), 34254);
let pub_key: Secp256k1PublicKey = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"
.to_string()
.try_into()
.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
let (recv_from_pool, send_to_pool) = setup_as_downstream(pool_address, Some(pub_key)).await;
send_to_pool.send(frame.try_into().unwrap()).await.unwrap();
match recv_from_pool.recv().await.unwrap() {
EitherFrame::Sv2(_a) => {
assert!(true)
}
_ => assert!(false),
}
// dont clean the following commented code
/*
let mut child = os_command(
"rm",
vec!["-rf", "./test/appdata/bitcoin_data/regtest"],
ExternalCommandConditions::None,
)
.await;
child.unwrap().wait().await.unwrap();
*/

// TODO not panic in network utils but return an handler
pool.unwrap().kill().await.unwrap();
//bitcoind.kill().await.unwrap();
assert!(true)
}

// the previous version of this test used mining-proxy, which is currently broken. Possibly
// this is due to the lack of elligator swift supporto for the mining-proxy, and in general the
// handshake needs maintainance
#[tokio::test]
async fn it_test_against_remote_endpoint() {
let _proxy = match os_command(
"cargo",
vec![
"run",
"-p",
"translator_sv2",
"--",
"-c",
"translator/config-examples/tproxy-config-hosted-pool-example.toml"
],
ExternalCommandConditions::new_with_timer_secs(10)
.continue_if_std_out_have("PROXY INITIALIZED")
.warn_no_panic(),
)
.await
{
Some(child) => child,
None => {
write!(
&mut std::io::stdout(),
"WARNING: remote not avaiable it_test_against_remote_endpoint not executed"
)
.unwrap();
return;
}
};
//loop {}
let _ = os_command(
"cargo",
vec!["run", "-p", "sv1-mining-device"],
ExternalCommandConditions::new_with_timer_secs(10)
.continue_if_std_out_have("channel opened with"),
)
.await;
assert!(true)
}
}
1 change: 1 addition & 0 deletions utils/message-generator/src/parser/sv1_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ mod test {
params: json!(["username", "password"]),
},
id: "authorize".to_string(),
replace_fields: Some(vec![]),
};

assert_eq!(m1.message.params, m2.message.params);
Expand Down

0 comments on commit 19221ff

Please sign in to comment.