1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
pub use crate::miner::Connection;
use async_std::sync::{Arc, Mutex};
use stratum_types::traits::{StratumManager, StratumParams};
use stratum_types::Result;

#[derive(Default)]
pub struct MinerList<SM: StratumManager> {
    pub miners: Mutex<Vec<Arc<Connection<SM>>>>,
}

//@todo store this in one of those fancy DashMaps instead. Then we can remove connections via IP
//addr or something.

impl<SM> MinerList<SM>
where
    SM: StratumManager,
{
    pub fn new() -> Self {
        MinerList {
            miners: Mutex::new(Vec::new()),
        }
    }

    pub async fn add_miner(&self, miner: Arc<Connection<SM>>) -> Result<()> {
        self.miners.lock().await.push(miner);
        Ok(())
    }

    pub async fn broadcast_new_job(
        &self,
        _job: <SM::StratumParams as StratumParams>::Notify,
    ) -> Result<()> {
        let miners = self.miners.lock().await;

        // info!("Broadcasting new work to miners. Job ID: {}", job.id()); @todo won't work just
        // yet Need job id function needed on notify.
        for miner in miners.iter() {
            // miner.send().await;
            //@todo might be more efficient to pass it down to the miners specifically rather than
            //put strain on the data_provider, but let's test this.
            miner.send_work().await?;
        }

        Ok(())
    }
}