Skip to main content

stratum_apps/utils/
types.rs

1use stratum_core::{
2    codec_sv2::{EncodableFrame, MessageFrame, SerializedFrame},
3    parsers_sv2::{AnyMessageOwned, ParserError},
4};
5
6pub const GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS: u64 = 5;
7
8/// The SV2 protocol version supported by the apps in this workspace.
9pub const SUPPORTED_PROTOCOL_VERSION: u16 = 2;
10
11pub type TemplateId = u64;
12pub type UpstreamJobId = u32;
13pub type JobId = u32;
14pub type DownstreamId = usize;
15pub type RequestId = u32;
16pub type ChannelId = u32;
17pub type Hashrate = f32;
18pub type SharesPerMinute = f32;
19pub type SharesBatchSize = usize;
20pub type ExtensionType = u16;
21pub type MessageType = u8;
22pub type JdToken = u64;
23
24pub type Message = AnyMessageOwned;
25
26/// A frame read off the wire: a header plus the raw payload bytes behind it.
27pub type InboundFrame = SerializedFrame;
28
29/// A frame on its way out, for a sender that has either kind to send: a message the encoder will
30/// serialize, or bytes that were framed by hand, as the TLV path does.
31#[derive(Debug)]
32pub enum OutboundFrame {
33    /// A message the encoder serializes on the way out.
34    Message(MessageFrame<Message>),
35
36    /// A frame that is already serialized, written out as it is.
37    Raw(SerializedFrame),
38}
39
40impl OutboundFrame {
41    /// Frames `message` for the encoder to serialize as it writes it out.
42    pub fn from_message(message: Message) -> Result<Self, ParserError> {
43        Ok(Self::Message(message.try_into()?))
44    }
45}
46
47impl From<MessageFrame<Message>> for OutboundFrame {
48    fn from(frame: MessageFrame<Message>) -> Self {
49        Self::Message(frame)
50    }
51}
52
53impl From<SerializedFrame> for OutboundFrame {
54    fn from(frame: SerializedFrame) -> Self {
55        Self::Raw(frame)
56    }
57}
58
59impl EncodableFrame for OutboundFrame {
60    fn encoded_length(&self) -> usize {
61        match self {
62            Self::Message(frame) => frame.encoded_length(),
63            Self::Raw(frame) => frame.encoded_length(),
64        }
65    }
66
67    fn encode_into(self, dst: &mut [u8]) -> Result<(), stratum_core::framing_sv2::Error> {
68        match self {
69            Self::Message(frame) => frame.encode_into(dst),
70            Self::Raw(frame) => frame.encode_into(dst),
71        }
72    }
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
76pub struct VardiffKey {
77    pub downstream_id: DownstreamId,
78    pub channel_id: ChannelId,
79}
80
81impl From<(DownstreamId, ChannelId)> for VardiffKey {
82    fn from(value: (DownstreamId, ChannelId)) -> Self {
83        VardiffKey {
84            downstream_id: value.0,
85            channel_id: value.1,
86        }
87    }
88}
89
90/// Marker traits used to define set of action
91/// the implementor can take
92pub trait CanDisconnect {}
93pub trait CanFallback {}
94pub trait CanShutdown {}