Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dev] fix dev sleep command do not update on chain timestamp #3477

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 76 additions & 23 deletions miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use starcoin_service_registry::{
};
use std::sync::Arc;
use std::time::Duration;
use types::block::BlockTemplate;
use types::system_events::GenerateSleepBlockEvent;

mod create_block_template;
pub mod generate_block_event_pacemaker;
Expand Down Expand Up @@ -126,11 +128,13 @@ impl ServiceFactory<MinerService> for MinerService {
impl ActorService for MinerService {
fn started(&mut self, ctx: &mut ServiceContext<Self>) -> Result<()> {
ctx.subscribe::<GenerateBlockEvent>();
ctx.subscribe::<GenerateSleepBlockEvent>();
Ok(())
}

fn stopped(&mut self, ctx: &mut ServiceContext<Self>) -> Result<()> {
ctx.unsubscribe::<GenerateBlockEvent>();
ctx.unsubscribe::<GenerateSleepBlockEvent>();
Ok(())
}
}
Expand Down Expand Up @@ -172,32 +176,50 @@ impl MinerService {
debug!("The flag disable_mint_empty_block is true and no txn in pool, so skip mint empty block.");
Ok(())
} else {
debug!("Mint block template: {:?}", block_template);
let difficulty = block_template.difficulty;
let strategy = block_template.strategy;
let number = block_template.number;
let parent_hash = block_template.parent_hash;
let task = MintTask::new(block_template, self.metrics.clone());
let mining_blob = task.minting_blob.clone();
if let Some(current_task) = self.current_task.as_ref() {
debug!(
"force set mint task, current_task: {:?}, new_task: {:?}",
current_task, task
);
}
self.current_task = Some(task);
ctx.broadcast(MintBlockEvent::new(
parent_hash,
strategy,
mining_blob,
difficulty,
number,
None,
));
Ok(())
self.dispatch_mint_block_event(ctx, block_template)
}
}

pub fn dispatch_sleep_task(&mut self, ctx: &mut ServiceContext<MinerService>) -> Result<()> {
//create block template should block_on for avoid mint same block template.
let response = block_on(async {
self.create_block_template_service
.send(BlockTemplateRequest)
.await?
})?;
self.dispatch_mint_block_event(ctx, response.template)
}

fn dispatch_mint_block_event(
&mut self,
ctx: &mut ServiceContext<MinerService>,
block_template: BlockTemplate,
) -> Result<()> {
debug!("Mint block template: {:?}", block_template);
let difficulty = block_template.difficulty;
let strategy = block_template.strategy;
let number = block_template.number;
let parent_hash = block_template.parent_hash;
let task = MintTask::new(block_template, self.metrics.clone());
let mining_blob = task.minting_blob.clone();
if let Some(current_task) = self.current_task.as_ref() {
debug!(
"force set mint task, current_task: {:?}, new_task: {:?}",
current_task, task
);
}
self.current_task = Some(task);
ctx.broadcast(MintBlockEvent::new(
parent_hash,
strategy,
mining_blob,
difficulty,
number,
None,
));
Ok(())
}

pub fn finish_task(
&mut self,
nonce: u32,
Expand Down Expand Up @@ -271,3 +293,34 @@ impl EventHandler<Self, GenerateBlockEvent> for MinerService {
}
}
}

impl EventHandler<Self, GenerateSleepBlockEvent> for MinerService {
fn handle_event(
&mut self,
event: GenerateSleepBlockEvent,
ctx: &mut ServiceContext<MinerService>,
) {
debug!("Handle GenerateSleepBlockEvent:{:?}", event);
if !event.force && self.is_minting() {
debug!("Miner has mint job so just ignore this event.");
return;
}
if self.config.miner.disable_miner_client() && self.client_subscribers_num == 0 {
debug!("No miner client connected, ignore GenerateSleepBlockEvent.");
// Once Miner client connect, we should dispatch task.
ctx.run_later(Duration::from_secs(2), |ctx| {
ctx.notify(GenerateSleepBlockEvent::new(false));
});
return;
}
if let Err(err) = self.dispatch_sleep_task(ctx) {
warn!(
"Failed to process generate sleep block event:{}, delay to trigger a new event.",
err
);
ctx.run_later(Duration::from_secs(2), |ctx| {
ctx.notify(GenerateSleepBlockEvent::new(false));
});
}
}
}
4 changes: 2 additions & 2 deletions rpc/server/src/module/debug_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use starcoin_rpc_api::debug::DebugApi;
use starcoin_rpc_api::types::FactoryAction;
use starcoin_service_registry::bus::{Bus, BusService};
use starcoin_service_registry::ServiceRef;
use starcoin_types::system_events::GenerateBlockEvent;
use starcoin_types::system_events::GenerateSleepBlockEvent;
use std::str::FromStr;
use std::sync::Arc;

Expand Down Expand Up @@ -74,7 +74,7 @@ impl DebugApi for DebugRpcImpl {
}
self.config.net().time_service().sleep(time);
self.bus
.broadcast(GenerateBlockEvent::new(true))
.broadcast(GenerateSleepBlockEvent::new(true))
.map_err(|_e| jsonrpc_core::Error::internal_error())?;
Ok(())
}
Expand Down
12 changes: 12 additions & 0 deletions types/src/system_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ impl GenerateBlockEvent {
}
}

#[derive(Clone, Debug)]
pub struct GenerateSleepBlockEvent {
/// Force break current minting, and Generate new block.
pub force: bool,
}

impl GenerateSleepBlockEvent {
pub fn new(force: bool) -> Self {
Self { force }
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct MintBlockEvent {
pub parent_hash: HashValue,
Expand Down
2 changes: 1 addition & 1 deletion vm/types/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl TimeService for RealTimeService {
let now = self.now_millis();
if value.milliseconds > now && value.milliseconds - now > 150000 {
warn!(
"Local time {} is behind on chain time {} too match.",
"Local time {} is behind on chain time {} too much.",
now / 1000,
value.seconds()
);
Expand Down