Skip to content

Commit

Permalink
refactor receive_message() into poll_message()
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesFoundation committed Aug 19, 2024
1 parent 60b6549 commit 8f8e460
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
22 changes: 10 additions & 12 deletions stratum-v1/examples/tokio-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio::spawn(async move {
loop {
let mut c = client_rx.lock().await;
match c.receive_message().await {
match c.poll_message().await {
Ok(msg) => match msg {
Message::Configured => {
Some(Message::Configured) => {
c.send_connect(Some(String::<32>::from_str("demo").unwrap()))
.await
.unwrap();
}
Message::Connected => {
Some(Message::Connected) => {
c.send_authorize(
match pool {
"Public-Pool" => String::<64>::from_str(
Expand All @@ -69,27 +69,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await
.unwrap();
}
Message::Authorized => {
Some(Message::Authorized) => {
authorized_tx.send(true).unwrap();
}
Message::Share {
Some(Message::Share {
accepted: _,
rejected: _,
} => {
}) => {
// TODO update the display if any
}
Message::VersionMask(_mask) => {
Some(Message::VersionMask(_mask)) => {
// TODO use mask for hardware version rolling is available
}
Message::Difficulty(_diff) => {
Some(Message::Difficulty(_diff)) => {
// TODO use diff to filter ASIC reported hits
}
Message::CleanJobs => {
Some(Message::CleanJobs) => {
// TODO clean the job queue and immediately start hashing a new job
}
Message::Other => {
debug!("Received Other Message");
}
None => {}
},
Err(e) => {
error!("Client receive_message error: {:?}", e);
Expand Down
21 changes: 10 additions & 11 deletions stratum-v1/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub enum Message {
VersionMask(u32),
Difficulty(f64),
CleanJobs,
Other,
}

impl<C: Read + ReadReady + Write, const RX_BUF_SIZE: usize, const TX_BUF_SIZE: usize>
Expand Down Expand Up @@ -89,8 +88,8 @@ impl<C: Read + ReadReady + Write, const RX_BUF_SIZE: usize, const TX_BUF_SIZE: u
self.job_creator.roll()
}

pub async fn receive_message(&mut self) -> Result<Message> {
let mut msg = Message::Other;
pub async fn poll_message(&mut self) -> Result<Option<Message>> {
let mut msg = None;
let mut start = 0;
while let Some(stop) = self.rx_buf[start..self.rx_free_pos]
.iter()
Expand All @@ -111,7 +110,7 @@ impl<C: Read + ReadReady + Write, const RX_BUF_SIZE: usize, const TX_BUF_SIZE: u
self.configuration = Some(response::parse_configure(line)?);
self.reqs.remove(&id);
info!("Stratum v1 Client Configured");
msg = Message::Configured;
msg = Some(Message::Configured);
}
Some(ReqKind::Connect) => {
let conn = response::parse_connect(line)?;
Expand All @@ -121,14 +120,14 @@ impl<C: Read + ReadReady + Write, const RX_BUF_SIZE: usize, const TX_BUF_SIZE: u
self.connected = true;
self.reqs.remove(&id);
info!("Stratum v1 Client Connected");
msg = Message::Connected;
msg = Some(Message::Connected);
}
Some(ReqKind::Authorize) => {
if response::parse_authorize(line)? {
self.authorized = true;
self.reqs.remove(&id);
info!("Stratum v1 Client Authorized");
msg = Message::Authorized;
msg = Some(Message::Authorized);
}
}
Some(ReqKind::Submit) => {
Expand All @@ -154,10 +153,10 @@ impl<C: Read + ReadReady + Write, const RX_BUF_SIZE: usize, const TX_BUF_SIZE: u
Err(e) => return Err(e),
}
self.reqs.remove(&id);
msg = Message::Share {
msg = Some(Message::Share {
accepted: self.shares_accepted,
rejected: self.shares_rejected,
};
});
}
None => return Err(Error::IdNotFound(id)),
}
Expand All @@ -167,18 +166,18 @@ impl<C: Read + ReadReady + Write, const RX_BUF_SIZE: usize, const TX_BUF_SIZE: u
Notification::SetVersionMask => {
let mask = notification::parse_set_version_mask(line)?;
self.job_creator.set_version_mask(mask);
msg = Message::VersionMask(mask);
msg = Some(Message::VersionMask(mask));
info!("Set Version Mask: 0x{:x}", mask);
}
Notification::SetDifficulty => {
let diff = notification::parse_set_difficulty(line)?;
msg = Message::Difficulty(diff);
msg = Some(Message::Difficulty(diff));
info!("Set Difficulty: {}", diff);
}
Notification::Notify => {
let work = notification::parse_notify(line)?;
if work.clean_jobs {
msg = Message::CleanJobs;
msg = Some(Message::CleanJobs);
}
info!("New Work: {:?}", work);
self.job_creator.set_work(work)?;
Expand Down

0 comments on commit 8f8e460

Please sign in to comment.