From a16a4385f8979fac04a476a23f6e7a90ff7a2d7b Mon Sep 17 00:00:00 2001 From: Noah <2+noah@noreply.gitea.bluefla.re> Date: Thu, 22 Oct 2026 11:03:41 -0600 Subject: [PATCH] Drive an SV2 session against a socket pool end to end The codec and session had only ever been tested in memory. This runs the real path over TCP: the client handshakes, opens a channel, receives SetNewPrevHash and an active NewMiningJob, submits a share and consumes the acknowledgement, against a mock pool that speaks the same frames. It also pins the ordering the protocol depends on: the job is interpreted only after the prev hash, and the submit sequence starts at one. --- src/stratum_link.rs | 132 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/src/stratum_link.rs b/src/stratum_link.rs index fe31a01..9e398ae 100644 --- a/src/stratum_link.rs +++ b/src/stratum_link.rs @@ -250,4 +250,136 @@ mod tests { None ); } + + fn mock_pool() -> std::net::SocketAddr { + use crate::stratum_v2::{ + NewMiningJob, OpenStandardMiningChannelSuccess, SetNewPrevHash, SetupConnectionSuccess, + OPEN_STANDARD_MINING_CHANNEL, OPEN_STANDARD_MINING_CHANNEL_SUCCESS, SETUP_CONNECTION, + SETUP_CONNECTION_SUCCESS, SET_NEW_PREV_HASH, SUBMIT_SHARES_SUCCESS, + }; + let listener = TcpListener::bind("127.0.0.1:0").expect("bind"); + let address = listener.local_addr().expect("addr"); + // Publish the address through a channel so the test can connect after binding. + let (tx, rx) = std::sync::mpsc::channel(); + tx.send(address).expect("send address"); + std::thread::spawn(move || { + let (stream, _) = listener.accept().expect("accept"); + let mut link = TcpLink::from_stream(stream).expect("link"); + let setup = link.recv().expect("setup"); + assert_eq!(setup.msg_type, SETUP_CONNECTION); + link.send(&Frame::new( + SETUP_CONNECTION_SUCCESS, + SetupConnectionSuccess { + used_version: 2, + flags: 0, + } + .encode(), + )) + .expect("setup success"); + let open = link.recv().expect("open"); + assert_eq!(open.msg_type, OPEN_STANDARD_MINING_CHANNEL); + link.send(&Frame::new( + OPEN_STANDARD_MINING_CHANNEL_SUCCESS, + OpenStandardMiningChannelSuccess { + request_id: 1, + channel_id: 7, + target: [0x7f; 32], + extranonce_prefix: vec![9, 9], + group_channel_id: 0, + } + .encode(), + )) + .expect("channel success"); + link.send(&Frame::new( + SET_NEW_PREV_HASH, + SetNewPrevHash { + channel_id: 7, + job_id: 41, + prev_hash: [0x11; 32], + ntime_start: 1_700_000_000, + nbits: 0x1707_0000, + } + .encode(), + )) + .expect("prev hash"); + link.send(&Frame::new( + NEW_MINING_JOB_TYPE, + NewMiningJob { + channel_id: 7, + job_id: 42, + ntime_start: Some(1_700_000_100), + version: 0x2000_0000, + merkle_root: [0x22; 32], + } + .encode(), + )) + .expect("job"); + let share = link.recv().expect("share"); + assert_eq!(share.msg_type, SUBMIT_SHARES_STANDARD_TYPE); + let mut body = Vec::new(); + body.extend_from_slice(&7_u32.to_le_bytes()); + body.extend_from_slice(&1_u32.to_le_bytes()); + body.extend_from_slice(&1_u32.to_le_bytes()); + body.extend_from_slice(&0_u64.to_le_bytes()); + link.send(&Frame::new(SUBMIT_SHARES_SUCCESS, body)) + .expect("share success"); + }); + rx.recv().expect("address") + } + + const NEW_MINING_JOB_TYPE: u8 = 0x15; + const SUBMIT_SHARES_STANDARD_TYPE: u8 = 0x1a; + + #[test] + fn a_session_runs_against_a_socket_pool() { + use crate::stratum_v2_session::{handshake, V2Config, V2Event, V2Session}; + let address = mock_pool(); + let mut link = TcpLink::connect(address, Duration::from_secs(3)).expect("connect"); + let config = V2Config { + host: "pool.test".into(), + port: 3333, + flags: 0, + user_identity: "worker".into(), + nominal_hash_rate: 1.0e14, + max_target: [0xff; 32], + vendor: "alkane".into(), + hardware: "bench".into(), + firmware: "alkane-os".into(), + device_id: "b1".into(), + }; + let channel = handshake(&mut link, &config).expect("handshake"); + assert_eq!(channel.channel_id, 7); + assert_eq!(channel.extranonce_prefix, vec![9, 9]); + + let mut session = V2Session::new(channel); + let prev = session + .on_frame(&link.recv().expect("prev")) + .expect("prev event"); + assert!(matches!(prev, V2Event::PrevHash { job_id: 41, .. })); + let job = session + .on_frame(&link.recv().expect("job")) + .expect("job event"); + match job { + V2Event::Job(parsed) => { + assert_eq!(parsed.job_id, 42); + assert_eq!(parsed.prev_hash, Some([0x11; 32])); + assert_eq!(parsed.ntime_start, Some(1_700_000_100)); + } + other => panic!("unexpected event: {other:?}"), + } + let sequence = session + .submit(&mut link, 42, 0xdead_beef, 1_700_000_100, 0x2000_0000) + .expect("submit"); + assert_eq!(sequence, 1); + let ack = session + .on_frame(&link.recv().expect("ack")) + .expect("ack event"); + assert_eq!( + ack, + V2Event::ShareAccepted { + last_sequence: 1, + accepted: 1 + } + ); + } }