Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.

Commit a5ba6db

Browse files
committed
Merge branch 'handle-db-issues-explicitly' into status-tracker
2 parents 7d4a525 + 7b84e7d commit a5ba6db

File tree

5 files changed

+45
-35
lines changed

5 files changed

+45
-35
lines changed

examples/src/main/scala/examples/hybrid/history/HybridHistory.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,16 +474,16 @@ class HybridHistory(val storage: HistoryStorage,
474474
chainBack(storage.bestPosBlock, isGenesis).get.map(_._2).map(encoder.encodeId).mkString(",")
475475
}
476476

477-
override def reportModifierIsValid(modifier: HybridBlock): HybridHistory = {
477+
override def reportModifierIsValid(modifier: HybridBlock): Try[HybridHistory] = Try {
478478
storage.updateValidity(modifier, Valid)
479479
storage.update(modifier, None, isBest = true)
480480

481481
new HybridHistory(storage, settings, validators, statsLogger, timeProvider)
482482
}
483483

484484
override def reportModifierIsInvalid(modifier: HybridBlock,
485-
progressInfo: ProgressInfo[HybridBlock]): (HybridHistory,
486-
ProgressInfo[HybridBlock]) = {
485+
progressInfo: ProgressInfo[HybridBlock]): Try[(HybridHistory,
486+
ProgressInfo[HybridBlock])] = Try {
487487
storage.updateValidity(modifier, Invalid)
488488

489489
new HybridHistory(storage, settings, validators, statsLogger, timeProvider) ->

src/main/scala/scorex/core/NodeViewHolder.scala

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ trait NodeViewHolder[TX <: Transaction, PMOD <: PersistentNodeViewModifier]
163163
}
164164
}
165165

166-
private def requestDownloads(pi: ProgressInfo[PMOD]): Unit =
166+
protected def requestDownloads(pi: ProgressInfo[PMOD]): Unit =
167167
pi.toDownload.foreach { case (tid, id) =>
168168
context.system.eventStream.publish(DownloadRequest(tid, id))
169169
}
@@ -206,7 +206,7 @@ trait NodeViewHolder[TX <: Transaction, PMOD <: PersistentNodeViewModifier]
206206
**/
207207

208208
@tailrec
209-
private def updateState(history: HIS,
209+
protected final def updateState(history: HIS,
210210
state: MS,
211211
progressInfo: ProgressInfo[PMOD],
212212
suffixApplied: IndexedSeq[PMOD]): (HIS, Try[MS], Seq[PMOD]) = {
@@ -222,14 +222,18 @@ trait NodeViewHolder[TX <: Transaction, PMOD <: PersistentNodeViewModifier]
222222

223223
stateToApplyTry match {
224224
case Success(stateToApply) =>
225-
val stateUpdateInfo = applyState(history, stateToApply, suffixTrimmed, progressInfo)
226-
227-
stateUpdateInfo.failedMod match {
228-
case Some(_) =>
229-
@SuppressWarnings(Array("org.wartremover.warts.OptionPartial"))
230-
val alternativeProgressInfo = stateUpdateInfo.alternativeProgressInfo.get
231-
updateState(stateUpdateInfo.history, stateUpdateInfo.state, alternativeProgressInfo, stateUpdateInfo.suffix)
232-
case None => (stateUpdateInfo.history, Success(stateUpdateInfo.state), stateUpdateInfo.suffix)
225+
applyState(history, stateToApply, suffixTrimmed, progressInfo) match {
226+
case Success(stateUpdateInfo) =>
227+
stateUpdateInfo.failedMod match {
228+
case Some(_) =>
229+
@SuppressWarnings(Array("org.wartremover.warts.OptionPartial"))
230+
val alternativeProgressInfo = stateUpdateInfo.alternativeProgressInfo.get
231+
updateState(stateUpdateInfo.history, stateUpdateInfo.state, alternativeProgressInfo, stateUpdateInfo.suffix)
232+
case None =>
233+
(stateUpdateInfo.history, Success(stateUpdateInfo.state), stateUpdateInfo.suffix)
234+
}
235+
case Failure(ex) =>
236+
(history, Failure(ex), suffixTrimmed)
233237
}
234238
case Failure(e) =>
235239
log.error("Rollback failed: ", e)
@@ -239,24 +243,30 @@ trait NodeViewHolder[TX <: Transaction, PMOD <: PersistentNodeViewModifier]
239243
}
240244
}
241245

242-
protected def applyState(history: HIS,
246+
private def applyState(history: HIS,
243247
stateToApply: MS,
244248
suffixTrimmed: IndexedSeq[PMOD],
245-
progressInfo: ProgressInfo[PMOD]): UpdateInformation = {
249+
progressInfo: ProgressInfo[PMOD]): Try[UpdateInformation] = {
246250
val updateInfoSample = UpdateInformation(history, stateToApply, None, None, suffixTrimmed)
247-
progressInfo.toApply.foldLeft(updateInfoSample) { case (updateInfo, modToApply) =>
248-
if (updateInfo.failedMod.isEmpty) {
249-
updateInfo.state.applyModifier(modToApply) match {
250-
case Success(stateAfterApply) =>
251-
val newHis = history.reportModifierIsValid(modToApply)
252-
context.system.eventStream.publish(SemanticallySuccessfulModifier(modToApply))
253-
UpdateInformation(newHis, stateAfterApply, None, None, updateInfo.suffix :+ modToApply)
254-
case Failure(e) =>
255-
val (newHis, newProgressInfo) = history.reportModifierIsInvalid(modToApply, progressInfo)
256-
context.system.eventStream.publish(SemanticallyFailedModification(modToApply, e))
257-
UpdateInformation(newHis, updateInfo.state, Some(modToApply), Some(newProgressInfo), updateInfo.suffix)
258-
}
259-
} else updateInfo
251+
progressInfo.toApply.foldLeft[Try[UpdateInformation]](Success(updateInfoSample)) {
252+
case (f@Failure(ex), _) =>
253+
log.error("Reporting modifier failed", ex)
254+
f
255+
case (success@Success(updateInfo), modToApply) =>
256+
if (updateInfo.failedMod.isEmpty) {
257+
updateInfo.state.applyModifier(modToApply) match {
258+
case Success(stateAfterApply) =>
259+
history.reportModifierIsValid(modToApply).map { newHis =>
260+
context.system.eventStream.publish(SemanticallySuccessfulModifier(modToApply))
261+
UpdateInformation(newHis, stateAfterApply, None, None, updateInfo.suffix :+ modToApply)
262+
}
263+
case Failure(e) =>
264+
history.reportModifierIsInvalid(modToApply, progressInfo).map { case (newHis, newProgressInfo) =>
265+
context.system.eventStream.publish(SemanticallyFailedModification(modToApply, e))
266+
UpdateInformation(newHis, updateInfo.state, Some(modToApply), Some(newProgressInfo), updateInfo.suffix)
267+
}
268+
}
269+
} else success
260270
}
261271
}
262272

@@ -294,8 +304,8 @@ trait NodeViewHolder[TX <: Transaction, PMOD <: PersistentNodeViewModifier]
294304

295305
case Failure(e) =>
296306
log.warn(s"Can`t apply persistent modifier (id: ${pmod.encodedId}, contents: $pmod) to minimal state", e)
307+
// not publishing SemanticallyFailedModification as this is an internal error
297308
updateNodeView(updatedHistory = Some(newHistory))
298-
context.system.eventStream.publish(SemanticallyFailedModification(pmod, e))
299309
}
300310
} else {
301311
requestDownloads(progressInfo)

src/main/scala/scorex/core/consensus/History.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ trait History[PM <: PersistentNodeViewModifier, SI <: SyncInfo, HT <: History[PM
3131
* Report that modifier is valid from point of view of the state component
3232
*
3333
* @param modifier - valid modifier
34-
* @return modified history
34+
* @return modified history, or failure if reporting was not successful
3535
*/
36-
def reportModifierIsValid(modifier: PM): HT
36+
def reportModifierIsValid(modifier: PM): Try[HT]
3737

3838
/**
3939
* Report that modifier is invalid from other nodeViewHolder components point of view
4040
*
4141
* @param modifier - invalid modifier
4242
* @param progressInfo - what suffix failed to be applied because of an invalid modifier
43-
* @return modified history and new progress info
43+
* @return modified history and new progress info, or failure if reporting was not successful
4444
*/
45-
def reportModifierIsInvalid(modifier: PM, progressInfo: ProgressInfo[PM]): (HT, ProgressInfo[PM])
45+
def reportModifierIsInvalid(modifier: PM, progressInfo: ProgressInfo[PM]): Try[(HT, ProgressInfo[PM])]
4646

4747

4848
/**

src/main/scala/scorex/core/network/NetworkController.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class NetworkController(settings: NetworkSettings,
203203
filterConnections(Broadcast, Version.initial).foreach { connectedPeer =>
204204
connectedPeer.handlerRef ! CloseConnection
205205
}
206-
self ! Unbind
206+
tcpManager ! Unbind
207207
context stop self
208208
}
209209

testkit/src/main/scala/scorex/testkit/properties/HistoryTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ trait HistoryTests[TX <: Transaction, PM <: PersistentNodeViewModifier, SI <: Sy
5353
property(propertyNameGenerator("report semantically validation after appending valid modifier")) {
5454
forAll(generatorWithValidModifier) { case (h, m) =>
5555
h.append(m)
56-
h.reportModifierIsValid(m)
56+
h.reportModifierIsValid(m).get
5757
h.isSemanticallyValid(m.id) shouldBe Valid
5858
}
5959
}

0 commit comments

Comments
 (0)