2024-04-27 03:15:57 +00:00
|
|
|
use crate::miner_id::check_and_request_miner_id;
|
2024-01-03 10:24:52 +00:00
|
|
|
use crate::sealer::Sealer;
|
|
|
|
use crate::submitter::Submitter;
|
|
|
|
use crate::{config::MinerConfig, mine::PoraService, watcher::MineContextWatcher};
|
|
|
|
use network::NetworkMessage;
|
|
|
|
use std::sync::Arc;
|
2024-05-31 05:11:06 +00:00
|
|
|
use storage::config::ShardConfig;
|
2024-01-03 10:24:52 +00:00
|
|
|
use storage::log_store::Store;
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
use tokio::sync::{broadcast, RwLock};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum MinerMessage {
|
|
|
|
/// Enable / Disable Mining
|
|
|
|
ToggleMining(bool),
|
|
|
|
|
|
|
|
/// Change mining range
|
|
|
|
SetStartPosition(Option<u64>),
|
|
|
|
SetEndPosition(Option<u64>),
|
2024-05-31 05:11:06 +00:00
|
|
|
|
|
|
|
/// Change shard config
|
|
|
|
SetShardConfig(ShardConfig),
|
2024-01-03 10:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MineService;
|
|
|
|
|
|
|
|
impl MineService {
|
|
|
|
pub async fn spawn(
|
|
|
|
executor: task_executor::TaskExecutor,
|
|
|
|
_network_send: mpsc::UnboundedSender<NetworkMessage>,
|
|
|
|
config: MinerConfig,
|
|
|
|
store: Arc<RwLock<dyn Store>>,
|
|
|
|
) -> Result<broadcast::Sender<MinerMessage>, String> {
|
|
|
|
let provider = Arc::new(config.make_provider().await?);
|
|
|
|
|
|
|
|
let (msg_send, msg_recv) = broadcast::channel(1024);
|
|
|
|
|
2024-04-27 03:15:57 +00:00
|
|
|
let miner_id = check_and_request_miner_id(&config, &store, &provider).await?;
|
|
|
|
debug!("miner id setting complete.");
|
|
|
|
|
2024-01-03 10:24:52 +00:00
|
|
|
let mine_context_receiver = MineContextWatcher::spawn(
|
|
|
|
executor.clone(),
|
|
|
|
msg_recv.resubscribe(),
|
|
|
|
provider.clone(),
|
|
|
|
&config,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mine_answer_receiver = PoraService::spawn(
|
|
|
|
executor.clone(),
|
|
|
|
msg_recv.resubscribe(),
|
|
|
|
mine_context_receiver,
|
|
|
|
Arc::new(store.clone()),
|
|
|
|
&config,
|
2024-04-27 03:15:57 +00:00
|
|
|
miner_id,
|
2024-01-03 10:24:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Submitter::spawn(
|
|
|
|
executor.clone(),
|
|
|
|
mine_answer_receiver,
|
|
|
|
provider.clone(),
|
|
|
|
store.clone(),
|
|
|
|
&config,
|
|
|
|
);
|
|
|
|
|
2024-04-27 03:15:57 +00:00
|
|
|
Sealer::spawn(executor, provider, store, &config, miner_id);
|
2024-01-03 10:24:52 +00:00
|
|
|
|
|
|
|
debug!("Starting miner service");
|
|
|
|
|
|
|
|
Ok(msg_send)
|
|
|
|
}
|
|
|
|
}
|