Skip to content

Commit

Permalink
Add nits
Browse files Browse the repository at this point in the history
  • Loading branch information
Shourya742 committed Jul 14, 2024
1 parent 8d10d6d commit cd20a5b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ mod test {
#[test]
fn test_check_flag() {
let protocol = crate::Protocol::MiningProtocol;
let flag_avaiable = 0b_0000_0000_0000_0000_0000_0000_0000_0000;
let flag_available = 0b_0000_0000_0000_0000_0000_0000_0000_0000;
let flag_required = 0b_0000_0000_0000_0000_0000_0000_0000_0001;
assert!(SetupConnection::check_flags(
protocol,
flag_avaiable,
flag_available,
flag_required
));

Expand Down
84 changes: 39 additions & 45 deletions roles/jd-server/src/lib/job_declarator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,16 +453,15 @@ impl JobDeclarator {
if let Ok((receiver, sender, _, _)) =
Connection::new(stream, HandshakeRole::Responder(responder)).await
{
let address = addr.unwrap();
let mut incoming = match receiver.recv().await {
Ok(EitherFrame::Sv2(s)) => {
debug!("Got sv2 message: {:?}", s);
s
}
Ok(EitherFrame::HandShake(s)) => {
error!(
"Got unexpected handshake message from upstream: {:?} at {}",
s, address
"Received unexpected handshake message from upstream: {:?} at {:?}",
s, addr
);
continue;
}
Expand All @@ -473,56 +472,51 @@ impl JobDeclarator {
};

let payload = incoming.payload();
let flag = payload[5] as u32;

let is_valid =
SetupConnection::check_flags(Protocol::JobDeclarationProtocol, flag, 1);
if let Ok(value) = binary_sv2::from_bytes::<SetupConnection>(payload) {
let flag = value.flags;
let is_valid = SetupConnection::check_flags(Protocol::JobDeclarationProtocol, flag, 1);

let sv2_frame: StdFrame = if is_valid {
let success_message = SetupConnectionSuccess {
used_version: 2,
flags: 0b_0000_0000_0000_0000_0000_0000_0000_0001,
};
info!("Sending success message for proxy");
JdsMessages::Common(success_message.into())
.try_into()
.unwrap()
} else {
let error_message = SetupConnectionError {
flags: flag,
error_code: "unsupported-feature-flags"
.to_string()
.into_bytes()
let sv2_frame: StdFrame = if is_valid {
let success_message = SetupConnectionSuccess {
used_version: 2,
flags: 0b_0000_0000_0000_0000_0000_0000_0000_0001,
};
info!("Sending success message for proxy");
JdsMessages::Common(success_message.into())
.try_into()
.expect("Failed to convert setup connection response message to standard frame")
} else {
let error_message = SetupConnectionError {
flags: flag,
error_code: "unsupported-feature-flags".to_string().into_bytes().try_into().unwrap(),
};
info!("Sending error message for proxy");
JdsMessages::Common(error_message.into())
.try_into()
.unwrap(),
.expect("Failed to convert setup connection response message to standard frame")
};
info!("Sending error message for proxy");
JdsMessages::Common(error_message.into())
.try_into()
.unwrap()
};

let sv2_frame = sv2_frame.into();
sender.send(sv2_frame).await.unwrap();
sender.send(sv2_frame.into()).await.unwrap();

if !is_valid {
continue;
}
if !is_valid {
continue;
}

let jddownstream = Arc::new(Mutex::new(JobDeclaratorDownstream::new(
receiver.clone(),
sender.clone(),
&config,
mempool.clone(),
// each downstream has its own sender (multi producer single consumer)
sender_add_txs_to_mempool.clone(),
)));
let jddownstream = Arc::new(Mutex::new(JobDeclaratorDownstream::new(
receiver.clone(),
sender.clone(),
&config,
mempool.clone(),
sender_add_txs_to_mempool.clone(), // each downstream has its own sender (multi producer single consumer)
)));

JobDeclaratorDownstream::start(
jddownstream,
status_tx.clone(),
new_block_sender.clone(),
);
JobDeclaratorDownstream::start(
jddownstream,
status_tx.clone(),
new_block_sender.clone(),
);
}
} else {
error!("Can not connect {:?}", addr);
}
Expand Down
20 changes: 20 additions & 0 deletions test/config/jds-setup-connection-flag-test/jds-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SRI Pool config
authority_public_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"
authority_secret_key = "mkDLTBBRxdBv998612qipDYoTK3YUrqLe8uWw7gu3iXbSrn2n"
cert_validity_sec = 3600

# list of compressed or uncompressed pubkeys for coinbase payout (only supports 1 item in the array at this point)
coinbase_outputs = [
{ output_script_type = "P2WPKH", output_script_value = "036adc3bdf21e6f9a0f0fb0066bf517e5b7909ed1563d6958a10993849a7554075" },
]

listen_jd_address = "127.0.0.1:34264"

core_rpc_url = ""
core_rpc_port = 18332
core_rpc_user = ""
core_rpc_pass = ""
# Time interval used for JDS mempool update
[mempool_update_interval]
unit = "secs"
value = 1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": "2",
"doc": [
"This test does",
"Soft mock of JD",
"Soft mock of JDC",
"Connect to JDS",
"Receive Setup Connection Error as incorrect bits are set"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"jd_server",
"--",
"-c",
"../test/config/jds-do-not-fail-on-wrong-txdatasucc/jds-config.toml"
"../test/config/jds-setup-connection-flag-test/jds-config.toml"
],
"conditions": {
"WithConditions": {
Expand All @@ -40,7 +40,7 @@
"command": "cargo",
"args": [
"run",
"../../test/message-generator/mock/job-declarator-mock-invalid-flag.json"
"../../test/message-generator/mock/jdc-mock-jds-setup-connection-mock-invalid-flag.json"
],
"conditions": {
"WithConditions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cd roles
cargo llvm-cov --no-report -p pool_sv2

cd ../utils/message-generator/
cargo build

RUST_LOG=debug cargo run ../../test/message-generator/test/jds-setup-connection-flag-test/jds-setup-connection-flag-test.json || { echo 'mg test failed' ; exit 1; }

sleep 10

This file was deleted.

0 comments on commit cd20a5b

Please sign in to comment.