Implement the client half of SV2 as framed messages: the 6-byte header (extension_type U16, msg_type U8, msg_length U24), SetupConnection and its success reply, OpenStandardMiningChannel and its success reply, NewMiningJob with both the active and future ntime forms, SetNewPrevHash and SubmitSharesStandard. Field layouts and message identifiers come from the published specification (03-Protocol-Overview 3.2/3.6, 08-Message-Types, 05-Mining-Protocol 5.3) and were checked against it rather than recalled. Decoding is strict: truncated payloads, bad option markers, bad UTF-8 and a non-finite or negative hashrate are protocol errors, never defaults.
This commit is contained in:
@@ -22,6 +22,7 @@ pub mod stratum_client;
|
||||
pub mod stratum_session;
|
||||
pub mod stratum_submit;
|
||||
pub mod stratum_v1;
|
||||
pub mod stratum_v2;
|
||||
pub mod stratum_wire;
|
||||
pub mod stratum_work;
|
||||
pub mod tuning;
|
||||
|
||||
@@ -0,0 +1,631 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Stratum V2 mining-protocol codec.
|
||||
//!
|
||||
//! Covers the client half of the mining sub-protocol: connection setup, opening a standard
|
||||
//! channel, and the job/share messages that follow. Layouts follow the published specification
|
||||
//! (framing in 03-Protocol-Overview 3.2, message types in 08-Message-Types, payloads in
|
||||
//! 05-Mining-Protocol 5.3).
|
||||
//!
|
||||
//! Everything is little-endian. `U256` is carried as the raw 32 bytes of the little-endian
|
||||
//! integer, which for a SHA-256 digest is the digest's own byte order.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
pub const EXTENSION_TYPE_CORE: u16 = 0;
|
||||
|
||||
pub const SETUP_CONNECTION: u8 = 0x00;
|
||||
pub const SETUP_CONNECTION_SUCCESS: u8 = 0x01;
|
||||
pub const SETUP_CONNECTION_ERROR: u8 = 0x02;
|
||||
pub const OPEN_STANDARD_MINING_CHANNEL: u8 = 0x10;
|
||||
pub const OPEN_STANDARD_MINING_CHANNEL_SUCCESS: u8 = 0x11;
|
||||
pub const OPEN_MINING_CHANNEL_ERROR: u8 = 0x12;
|
||||
pub const NEW_MINING_JOB: u8 = 0x15;
|
||||
pub const SUBMIT_SHARES_STANDARD: u8 = 0x1a;
|
||||
pub const SUBMIT_SHARES_SUCCESS: u8 = 0x1c;
|
||||
pub const SUBMIT_SHARES_ERROR: u8 = 0x1d;
|
||||
pub const SET_NEW_PREV_HASH: u8 = 0x20;
|
||||
pub const SET_TARGET: u8 = 0x21;
|
||||
|
||||
const HEADER_LEN: usize = 6;
|
||||
const MAX_FRAME: usize = 1 << 20;
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum V2Error {
|
||||
Truncated,
|
||||
Length,
|
||||
String,
|
||||
NonFiniteRate,
|
||||
UnknownMessage(u8),
|
||||
}
|
||||
|
||||
impl fmt::Display for V2Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Truncated => f.write_str("stratum v2 message is truncated"),
|
||||
Self::Length => f.write_str("stratum v2 length field is invalid"),
|
||||
Self::String => f.write_str("stratum v2 string field is invalid"),
|
||||
Self::NonFiniteRate => {
|
||||
f.write_str("stratum v2 hashrate is not a finite positive number")
|
||||
}
|
||||
Self::UnknownMessage(kind) => {
|
||||
write!(f, "unsupported stratum v2 message type {kind:#x}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for V2Error {}
|
||||
|
||||
/// One framed message: 6-byte header plus payload.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct Frame {
|
||||
pub extension_type: u16,
|
||||
pub msg_type: u8,
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
pub fn new(msg_type: u8, payload: Vec<u8>) -> Self {
|
||||
Self {
|
||||
extension_type: EXTENSION_TYPE_CORE,
|
||||
msg_type,
|
||||
payload,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode(&self, out: &mut Vec<u8>) {
|
||||
out.extend_from_slice(&self.extension_type.to_le_bytes());
|
||||
out.push(self.msg_type);
|
||||
let length = self.payload.len() as u32;
|
||||
out.extend_from_slice(&length.to_le_bytes()[..3]);
|
||||
out.extend_from_slice(&self.payload);
|
||||
}
|
||||
|
||||
/// Decode one frame from `buf`, returning it and how many bytes it consumed.
|
||||
pub fn decode(buf: &[u8]) -> Result<(Self, usize), V2Error> {
|
||||
if buf.len() < HEADER_LEN {
|
||||
return Err(V2Error::Truncated);
|
||||
}
|
||||
let extension_type = u16::from_le_bytes([buf[0], buf[1]]);
|
||||
let msg_type = buf[2];
|
||||
let length = u32::from_le_bytes([buf[3], buf[4], buf[5], 0]) as usize;
|
||||
if length > MAX_FRAME {
|
||||
return Err(V2Error::Length);
|
||||
}
|
||||
let end = HEADER_LEN + length;
|
||||
if buf.len() < end {
|
||||
return Err(V2Error::Truncated);
|
||||
}
|
||||
Ok((
|
||||
Self {
|
||||
extension_type,
|
||||
msg_type,
|
||||
payload: buf[HEADER_LEN..end].to_vec(),
|
||||
},
|
||||
end,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// `SetupConnection` for the mining sub-protocol.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct SetupConnection {
|
||||
pub protocol: u8,
|
||||
pub min_version: u16,
|
||||
pub max_version: u16,
|
||||
pub flags: u32,
|
||||
pub endpoint_host: String,
|
||||
pub endpoint_port: u16,
|
||||
pub vendor: String,
|
||||
pub hardware_version: String,
|
||||
pub firmware: String,
|
||||
pub device_id: String,
|
||||
}
|
||||
|
||||
impl SetupConnection {
|
||||
pub fn encode(&self) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
out.push(self.protocol);
|
||||
put_u16(&mut out, self.min_version);
|
||||
put_u16(&mut out, self.max_version);
|
||||
put_u32(&mut out, self.flags);
|
||||
put_str(&mut out, &self.endpoint_host);
|
||||
put_u16(&mut out, self.endpoint_port);
|
||||
put_str(&mut out, &self.vendor);
|
||||
put_str(&mut out, &self.hardware_version);
|
||||
put_str(&mut out, &self.firmware);
|
||||
put_str(&mut out, &self.device_id);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode(payload: &[u8]) -> Result<Self, V2Error> {
|
||||
let mut reader = Reader::new(payload);
|
||||
Ok(Self {
|
||||
protocol: reader.u8()?,
|
||||
min_version: reader.u16()?,
|
||||
max_version: reader.u16()?,
|
||||
flags: reader.u32()?,
|
||||
endpoint_host: reader.string()?,
|
||||
endpoint_port: reader.u16()?,
|
||||
vendor: reader.string()?,
|
||||
hardware_version: reader.string()?,
|
||||
firmware: reader.string()?,
|
||||
device_id: reader.string()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// `SetupConnection.Success`.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct SetupConnectionSuccess {
|
||||
pub used_version: u16,
|
||||
pub flags: u32,
|
||||
}
|
||||
|
||||
impl SetupConnectionSuccess {
|
||||
pub fn encode(&self) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
put_u16(&mut out, self.used_version);
|
||||
put_u32(&mut out, self.flags);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode(payload: &[u8]) -> Result<Self, V2Error> {
|
||||
let mut reader = Reader::new(payload);
|
||||
Ok(Self {
|
||||
used_version: reader.u16()?,
|
||||
flags: reader.u32()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// `OpenStandardMiningChannel`.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct OpenStandardMiningChannel {
|
||||
pub request_id: u32,
|
||||
pub user_identity: String,
|
||||
pub nominal_hash_rate: f32,
|
||||
pub max_target: [u8; 32],
|
||||
}
|
||||
|
||||
impl OpenStandardMiningChannel {
|
||||
pub fn encode(&self) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
put_u32(&mut out, self.request_id);
|
||||
put_str(&mut out, &self.user_identity);
|
||||
out.extend_from_slice(&self.nominal_hash_rate.to_le_bytes());
|
||||
out.extend_from_slice(&self.max_target);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode(payload: &[u8]) -> Result<Self, V2Error> {
|
||||
let mut reader = Reader::new(payload);
|
||||
let request_id = reader.u32()?;
|
||||
let user_identity = reader.string()?;
|
||||
let nominal_hash_rate = reader.f32()?;
|
||||
if !nominal_hash_rate.is_finite() || nominal_hash_rate < 0.0 {
|
||||
return Err(V2Error::NonFiniteRate);
|
||||
}
|
||||
Ok(Self {
|
||||
request_id,
|
||||
user_identity,
|
||||
nominal_hash_rate,
|
||||
max_target: reader.u256()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// `OpenStandardMiningChannel.Success`.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct OpenStandardMiningChannelSuccess {
|
||||
pub request_id: u32,
|
||||
pub channel_id: u32,
|
||||
pub target: [u8; 32],
|
||||
pub extranonce_prefix: Vec<u8>,
|
||||
pub group_channel_id: u32,
|
||||
}
|
||||
|
||||
impl OpenStandardMiningChannelSuccess {
|
||||
pub fn encode(&self) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
put_u32(&mut out, self.request_id);
|
||||
put_u32(&mut out, self.channel_id);
|
||||
out.extend_from_slice(&self.target);
|
||||
put_bytes(&mut out, &self.extranonce_prefix);
|
||||
put_u32(&mut out, self.group_channel_id);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode(payload: &[u8]) -> Result<Self, V2Error> {
|
||||
let mut reader = Reader::new(payload);
|
||||
Ok(Self {
|
||||
request_id: reader.u32()?,
|
||||
channel_id: reader.u32()?,
|
||||
target: reader.u256()?,
|
||||
extranonce_prefix: reader.bytes(32)?,
|
||||
group_channel_id: reader.u32()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// `NewMiningJob` on a standard channel.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct NewMiningJob {
|
||||
pub channel_id: u32,
|
||||
pub job_id: u32,
|
||||
/// `None` means a future job, activated by the matching `SetNewPrevHash`.
|
||||
pub ntime_start: Option<u32>,
|
||||
pub version: u32,
|
||||
pub merkle_root: [u8; 32],
|
||||
}
|
||||
|
||||
impl NewMiningJob {
|
||||
pub fn encode(&self) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
put_u32(&mut out, self.channel_id);
|
||||
put_u32(&mut out, self.job_id);
|
||||
match self.ntime_start {
|
||||
Some(ntime) => {
|
||||
out.push(1);
|
||||
put_u32(&mut out, ntime);
|
||||
}
|
||||
None => out.push(0),
|
||||
}
|
||||
put_u32(&mut out, self.version);
|
||||
out.extend_from_slice(&self.merkle_root);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode(payload: &[u8]) -> Result<Self, V2Error> {
|
||||
let mut reader = Reader::new(payload);
|
||||
let channel_id = reader.u32()?;
|
||||
let job_id = reader.u32()?;
|
||||
let ntime_start = reader.option_u32()?;
|
||||
Ok(Self {
|
||||
channel_id,
|
||||
job_id,
|
||||
ntime_start,
|
||||
version: reader.u32()?,
|
||||
merkle_root: reader.u256()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// `SetNewPrevHash`.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct SetNewPrevHash {
|
||||
pub channel_id: u32,
|
||||
pub job_id: u32,
|
||||
pub prev_hash: [u8; 32],
|
||||
pub ntime_start: u32,
|
||||
pub nbits: u32,
|
||||
}
|
||||
|
||||
impl SetNewPrevHash {
|
||||
pub fn encode(&self) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
put_u32(&mut out, self.channel_id);
|
||||
put_u32(&mut out, self.job_id);
|
||||
out.extend_from_slice(&self.prev_hash);
|
||||
put_u32(&mut out, self.ntime_start);
|
||||
put_u32(&mut out, self.nbits);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode(payload: &[u8]) -> Result<Self, V2Error> {
|
||||
let mut reader = Reader::new(payload);
|
||||
Ok(Self {
|
||||
channel_id: reader.u32()?,
|
||||
job_id: reader.u32()?,
|
||||
prev_hash: reader.u256()?,
|
||||
ntime_start: reader.u32()?,
|
||||
nbits: reader.u32()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// `SubmitSharesStandard`.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct SubmitSharesStandard {
|
||||
pub channel_id: u32,
|
||||
pub sequence_number: u32,
|
||||
pub job_id: u32,
|
||||
pub nonce: u32,
|
||||
pub ntime: u32,
|
||||
pub version: u32,
|
||||
}
|
||||
|
||||
impl SubmitSharesStandard {
|
||||
pub fn encode(&self) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
put_u32(&mut out, self.channel_id);
|
||||
put_u32(&mut out, self.sequence_number);
|
||||
put_u32(&mut out, self.job_id);
|
||||
put_u32(&mut out, self.nonce);
|
||||
put_u32(&mut out, self.ntime);
|
||||
put_u32(&mut out, self.version);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode(payload: &[u8]) -> Result<Self, V2Error> {
|
||||
let mut reader = Reader::new(payload);
|
||||
Ok(Self {
|
||||
channel_id: reader.u32()?,
|
||||
sequence_number: reader.u32()?,
|
||||
job_id: reader.u32()?,
|
||||
nonce: reader.u32()?,
|
||||
ntime: reader.u32()?,
|
||||
version: reader.u32()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn put_u16(out: &mut Vec<u8>, value: u16) {
|
||||
out.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
|
||||
fn put_u32(out: &mut Vec<u8>, value: u32) {
|
||||
out.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
|
||||
fn put_str(out: &mut Vec<u8>, text: &str) {
|
||||
let bytes = text.as_bytes();
|
||||
let length = bytes.len().min(255);
|
||||
out.push(length as u8);
|
||||
out.extend_from_slice(&bytes[..length]);
|
||||
}
|
||||
|
||||
fn put_bytes(out: &mut Vec<u8>, bytes: &[u8]) {
|
||||
let length = bytes.len().min(255);
|
||||
out.push(length as u8);
|
||||
out.extend_from_slice(&bytes[..length]);
|
||||
}
|
||||
|
||||
struct Reader<'a> {
|
||||
buf: &'a [u8],
|
||||
at: usize,
|
||||
}
|
||||
|
||||
impl<'a> Reader<'a> {
|
||||
fn new(buf: &'a [u8]) -> Self {
|
||||
Self { buf, at: 0 }
|
||||
}
|
||||
|
||||
fn take(&mut self, count: usize) -> Result<&'a [u8], V2Error> {
|
||||
let end = self.at.checked_add(count).ok_or(V2Error::Length)?;
|
||||
if end > self.buf.len() {
|
||||
return Err(V2Error::Truncated);
|
||||
}
|
||||
let slice = &self.buf[self.at..end];
|
||||
self.at = end;
|
||||
Ok(slice)
|
||||
}
|
||||
|
||||
fn u8(&mut self) -> Result<u8, V2Error> {
|
||||
Ok(self.take(1)?[0])
|
||||
}
|
||||
|
||||
fn u16(&mut self) -> Result<u16, V2Error> {
|
||||
let bytes = self.take(2)?;
|
||||
Ok(u16::from_le_bytes([bytes[0], bytes[1]]))
|
||||
}
|
||||
|
||||
fn u32(&mut self) -> Result<u32, V2Error> {
|
||||
let bytes = self.take(4)?;
|
||||
Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
|
||||
}
|
||||
|
||||
fn f32(&mut self) -> Result<f32, V2Error> {
|
||||
Ok(f32::from_bits(self.u32()?))
|
||||
}
|
||||
|
||||
fn u256(&mut self) -> Result<[u8; 32], V2Error> {
|
||||
let bytes = self.take(32)?;
|
||||
let mut out = [0_u8; 32];
|
||||
out.copy_from_slice(bytes);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn string(&mut self) -> Result<String, V2Error> {
|
||||
let length = self.u8()? as usize;
|
||||
let bytes = self.take(length)?;
|
||||
String::from_utf8(bytes.to_vec()).map_err(|_| V2Error::String)
|
||||
}
|
||||
|
||||
fn bytes(&mut self, max: usize) -> Result<Vec<u8>, V2Error> {
|
||||
let length = self.u8()? as usize;
|
||||
if length > max {
|
||||
return Err(V2Error::Length);
|
||||
}
|
||||
Ok(self.take(length)?.to_vec())
|
||||
}
|
||||
|
||||
fn option_u32(&mut self) -> Result<Option<u32>, V2Error> {
|
||||
match self.u8()? {
|
||||
0 => Ok(None),
|
||||
1 => Ok(Some(self.u32()?)),
|
||||
_ => Err(V2Error::Length),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn hash(byte: u8) -> [u8; 32] {
|
||||
[byte; 32]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frame_header_matches_the_specification() {
|
||||
let mut wire = Vec::new();
|
||||
Frame::new(SETUP_CONNECTION, vec![1, 2, 3]).encode(&mut wire);
|
||||
assert_eq!(&wire[..3], &[0x00, 0x00, 0x00]);
|
||||
assert_eq!(&wire[3..6], &[0x03, 0x00, 0x00]);
|
||||
let (frame, consumed) = Frame::decode(&wire).expect("frame");
|
||||
assert_eq!(consumed, wire.len());
|
||||
assert_eq!(frame.msg_type, SETUP_CONNECTION);
|
||||
assert_eq!(frame.payload, vec![1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_truncated_frame_is_rejected() {
|
||||
assert_eq!(Frame::decode(&[0, 0, 0x00]), Err(V2Error::Truncated));
|
||||
let mut wire = Vec::new();
|
||||
Frame::new(SETUP_CONNECTION, vec![9; 10]).encode(&mut wire);
|
||||
assert_eq!(Frame::decode(&wire[..8]), Err(V2Error::Truncated));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn setup_connection_round_trips() {
|
||||
let message = SetupConnection {
|
||||
protocol: 0,
|
||||
min_version: 2,
|
||||
max_version: 2,
|
||||
flags: 0b1011,
|
||||
endpoint_host: "pool.example".into(),
|
||||
endpoint_port: 3333,
|
||||
vendor: "alkane".into(),
|
||||
hardware_version: "s19jpro-bm1362".into(),
|
||||
firmware: "alkane-os/0.1.0".into(),
|
||||
device_id: "bench-169".into(),
|
||||
};
|
||||
let wire = message.encode();
|
||||
assert_eq!(SetupConnection::decode(&wire).expect("decode"), message);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn setup_connection_success_round_trips() {
|
||||
let message = SetupConnectionSuccess {
|
||||
used_version: 2,
|
||||
flags: 0,
|
||||
};
|
||||
assert_eq!(
|
||||
SetupConnectionSuccess::decode(&message.encode()).expect("decode"),
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_standard_channel_round_trips() {
|
||||
let message = OpenStandardMiningChannel {
|
||||
request_id: 7,
|
||||
user_identity: "bc1q.example".into(),
|
||||
nominal_hash_rate: 95_000_000_000_000.0,
|
||||
max_target: hash(0xff),
|
||||
};
|
||||
assert_eq!(
|
||||
OpenStandardMiningChannel::decode(&message.encode()).expect("decode"),
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_non_finite_hashrate_is_a_protocol_error() {
|
||||
let message = OpenStandardMiningChannel {
|
||||
request_id: 1,
|
||||
user_identity: "x".into(),
|
||||
nominal_hash_rate: f32::NAN,
|
||||
max_target: hash(0),
|
||||
};
|
||||
assert_eq!(
|
||||
OpenStandardMiningChannel::decode(&message.encode()),
|
||||
Err(V2Error::NonFiniteRate)
|
||||
);
|
||||
let negative = OpenStandardMiningChannel {
|
||||
nominal_hash_rate: -1.0,
|
||||
..message
|
||||
};
|
||||
assert_eq!(
|
||||
OpenStandardMiningChannel::decode(&negative.encode()),
|
||||
Err(V2Error::NonFiniteRate)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn channel_success_carries_the_extranonce_prefix() {
|
||||
let message = OpenStandardMiningChannelSuccess {
|
||||
request_id: 7,
|
||||
channel_id: 11,
|
||||
target: hash(0x7f),
|
||||
extranonce_prefix: vec![0xde, 0xad, 0xbe, 0xef],
|
||||
group_channel_id: 0,
|
||||
};
|
||||
assert_eq!(
|
||||
OpenStandardMiningChannelSuccess::decode(&message.encode()).expect("decode"),
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_job_round_trips_both_ntime_forms() {
|
||||
let active = NewMiningJob {
|
||||
channel_id: 1,
|
||||
job_id: 2,
|
||||
ntime_start: Some(1_700_000_000),
|
||||
version: 0x2000_0000,
|
||||
merkle_root: hash(0x11),
|
||||
};
|
||||
assert_eq!(
|
||||
NewMiningJob::decode(&active.encode()).expect("decode"),
|
||||
active
|
||||
);
|
||||
let future = NewMiningJob {
|
||||
ntime_start: None,
|
||||
..active
|
||||
};
|
||||
assert_eq!(
|
||||
NewMiningJob::decode(&future.encode()).expect("decode"),
|
||||
future
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_new_prev_hash_round_trips() {
|
||||
let message = SetNewPrevHash {
|
||||
channel_id: 1,
|
||||
job_id: 2,
|
||||
prev_hash: hash(0x22),
|
||||
ntime_start: 1_700_000_100,
|
||||
nbits: 0x1707_0000,
|
||||
};
|
||||
assert_eq!(
|
||||
SetNewPrevHash::decode(&message.encode()).expect("decode"),
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn submit_shares_round_trips() {
|
||||
let message = SubmitSharesStandard {
|
||||
channel_id: 3,
|
||||
sequence_number: 99,
|
||||
job_id: 4,
|
||||
nonce: 0xdead_beef,
|
||||
ntime: 1_700_000_200,
|
||||
version: 0x2000_0000,
|
||||
};
|
||||
assert_eq!(
|
||||
SubmitSharesStandard::decode(&message.encode()).expect("decode"),
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_short_payload_is_rejected_rather_than_padded() {
|
||||
let wire = SubmitSharesStandard {
|
||||
channel_id: 3,
|
||||
sequence_number: 99,
|
||||
job_id: 4,
|
||||
nonce: 0,
|
||||
ntime: 0,
|
||||
version: 0,
|
||||
}
|
||||
.encode();
|
||||
assert_eq!(
|
||||
SubmitSharesStandard::decode(&wire[..8]),
|
||||
Err(V2Error::Truncated)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user