Skip to content

Commit

Permalink
Forger Thread automatically detect consensus slot duration change
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoOl94 committed Aug 22, 2023
1 parent e58b515 commit 58fbbea
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions sdk/src/main/scala/io/horizen/forge/AbstractForger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ abstract class AbstractForger[
type View = CurrentView[HIS, MS, VL, MP]

private val restApiTimeoutDuration: FiniteDuration = settings.sparkzSettings.restApi.timeout
private var slotsDurationInSeconds = ConsensusParamsUtil.getConsensusSecondsInSlotsPerEpoch(params.sidechainGenesisBlockTimestamp, timeProvider.time() / 1000) * 1000

// implicit timeout for akka msg
implicit private val timeout: Timeout = Timeout(restApiTimeoutDuration)
Expand All @@ -61,7 +62,7 @@ abstract class AbstractForger[
val currentTime: Long = timeProvider.time() / 1000
val delay = TimeToEpochUtils.secondsRemainingInSlot(params.sidechainGenesisBlockTimestamp, currentTime) * 1000
newTimer.schedule(forgingInitiatorTimerTask, 0L)
newTimer.scheduleAtFixedRate(forgingInitiatorTimerTask, delay, ConsensusParamsUtil.getConsensusSecondsInSlotsPerEpoch(params.sidechainGenesisBlockTimestamp, currentTime) * 1000)
newTimer.scheduleAtFixedRate(forgingInitiatorTimerTask, delay, slotsDurationInSeconds)
timerOpt = Some(newTimer)
log.info("Automatically forging had been started")
}
Expand Down Expand Up @@ -135,7 +136,18 @@ abstract class AbstractForger[
val currentTime: Long = timeProvider.time() / 1000
val epochAndSlot = TimeToEpochUtils.timestampToEpochAndSlot(params.sidechainGenesisBlockTimestamp, currentTime)
log.info(s"Send TryForgeNextBlockForEpochAndSlot message with epoch and slot $epochAndSlot")
tryToCreateBlockForEpochAndSlot(epochAndSlot.epochNumber, epochAndSlot.slotNumber, None, Seq()) }
tryToCreateBlockForEpochAndSlot(epochAndSlot.epochNumber, epochAndSlot.slotNumber, None, Seq())
RecalculateSlotDuration()
}

protected def RecalculateSlotDuration(): Unit = {
val consensusSecondsInSlot = ConsensusParamsUtil.getConsensusSecondsInSlotsPerEpoch(params.sidechainGenesisBlockTimestamp, timeProvider.time() / 1000) * 1000
if (consensusSecondsInSlot != slotsDurationInSeconds) {
slotsDurationInSeconds = consensusSecondsInSlot
stopTimer()
startTimer()
}
}

def getForgedBlockAsFuture(epochNumber: ConsensusEpochNumber, slot: ConsensusSlotNumber, forcedTx: Iterable[TX]) : Future[ForgeResult] = {
// we should not take more time than a slot duration for forging a block.
Expand Down

0 comments on commit 58fbbea

Please sign in to comment.