stratum_types/
traits.rs

1use crate::miner::MinerInfo;
2use crate::stratum_error::StratumError;
3use crate::Result;
4use async_trait::async_trait;
5use serde::{de::DeserializeOwned, Serialize};
6use std::fmt::Debug;
7use uuid::Uuid;
8
9pub trait SubscribeResult: DeserializeOwned + Serialize + Sync + Send + Clone + Debug {
10    fn id(&self) -> Option<Uuid>;
11    fn sid(&self) -> String;
12}
13
14pub trait AuthorizeResult: DeserializeOwned + Serialize + Sync + Send + Clone + Debug {
15    fn authorized(&self) -> bool;
16    fn id(&self) -> String;
17    fn worker_id(&self) -> Option<Uuid>;
18    fn worker_name(&self) -> Option<String>;
19}
20
21pub trait Authorize: DeserializeOwned + Serialize + Sync + Send + Clone + Debug {
22    fn username(&self) -> String;
23    fn client(&self) -> String;
24    fn worker_name(&self) -> Option<String>;
25}
26
27pub trait Notify: DeserializeOwned + Serialize + Sync + Send + Clone + Debug {
28    fn get_difficulty(&self) -> f64;
29}
30
31pub trait Subscribe: DeserializeOwned + Serialize + Sync + Send + Clone + Debug {
32    fn set_sid(&mut self, sid: &str);
33}
34
35pub trait Submit: DeserializeOwned + Serialize + Sync + Send + Clone + Debug {
36    fn username(&self) -> String;
37}
38
39pub trait StratumParams {
40    type Submit: Submit;
41    type Notify: Notify;
42}
43
44pub trait PoolParams {
45    type Authorize: Authorize;
46    type AuthorizeResult: AuthorizeResult;
47    type Subscribe: Subscribe;
48    type SubscribeResult: SubscribeResult;
49}
50
51#[async_trait]
52pub trait StratumManager: Sync + Send + Clone {
53    type StratumParams: StratumParams + DeserializeOwned + Serialize + Clone;
54    type PoolParams: PoolParams + DeserializeOwned + Serialize + Clone;
55
56    type AuthManager: AuthManager<
57        Authorize = <Self::PoolParams as PoolParams>::Authorize,
58        AuthorizeResult = <Self::PoolParams as PoolParams>::AuthorizeResult,
59        Subscribe = <Self::PoolParams as PoolParams>::Subscribe,
60        SubscribeResult = <Self::PoolParams as PoolParams>::SubscribeResult,
61    >;
62    type DataProvider: DataProvider<Job = <Self::StratumParams as StratumParams>::Notify>;
63    type BlockValidator: BlockValidator<
64        Job = <Self::StratumParams as StratumParams>::Notify,
65        Submit = <Self::StratumParams as StratumParams>::Submit,
66    >;
67}
68
69//@todo note to self. Ideally, we actually switch this entire implementation to send a pointer of
70//the actual miner to this trait. That way we can just pull stats from the miner itself, but this
71//will be in a later minor/major version.
72#[async_trait]
73pub trait AuthManager: Sync + Send + Clone {
74    type Authorize: Authorize;
75    type AuthorizeResult: AuthorizeResult;
76    type Subscribe: Subscribe;
77    type SubscribeResult: SubscribeResult;
78
79    async fn authorize(
80        &self,
81        info: MinerInfo,
82        auth: &Self::Authorize,
83        classic: bool,
84    ) -> std::result::Result<Self::AuthorizeResult, StratumError>;
85
86    async fn authorize_username(
87        &self,
88        info: MinerInfo,
89        auth: &str,
90    ) -> std::result::Result<Self::AuthorizeResult, StratumError>;
91
92    async fn subscribe(
93        &self,
94        info: MinerInfo,
95        subscribe_info: &Self::Subscribe,
96        classic: bool,
97    ) -> std::result::Result<Self::SubscribeResult, StratumError>;
98}
99
100#[async_trait]
101pub trait DataProvider: Sync + Send + Clone {
102    type Job: Notify;
103
104    //Init allows the data provider to do whatever it needs before starting. In the case of an RPC
105    //client, this would mean making the initial connection to the rpc server and getting the first
106    //block template.
107    async fn init(&self) -> Result<()>;
108
109    async fn get_job(&self, classic: bool) -> Self::Job;
110
111    //@todo polls for new templates. If it returns true, we then there is a new block template
112    //available. Might want to optimize this slightly.
113    async fn poll_template(&self) -> std::result::Result<bool, StratumError>;
114
115    //@todo review this.
116    // async fn get_tx_data(&self, timestamp: u64) -> Result<Vec<Self::TransactionData>>;
117}
118
119#[async_trait]
120pub trait BlockValidator: Sync + Send + Clone {
121    type Submit: Submit;
122    type Job: DeserializeOwned + Serialize + Sync + Send + Clone + Debug;
123
124    //Init allows the data provider to do whatever it needs before starting. In the case of an RPC
125    //client, this would mean making the initial connection to the rpc server and getting the first
126    //block template.
127    async fn init(&self) -> Result<()>;
128
129    //Handles validating share, and submitting
130    async fn validate_share(
131        &self,
132        info: MinerInfo,
133        share: Self::Submit,
134        classic: bool,
135    ) -> std::result::Result<bool, StratumError>;
136}