Skip to content

Commit

Permalink
Add dependencies available in block store before adding block itself
Browse files Browse the repository at this point in the history
  • Loading branch information
nzpr committed Mar 10, 2020
1 parent 2e870e1 commit 21741ce
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions casper/src/main/scala/coop/rchain/casper/engine/Running.scala
Expand Up @@ -345,8 +345,35 @@ class Running[F[_]: Sync: BlockStore: CommUtil: TransportLayer: ConnectionsCell:
}
}
} yield ()
} >>
casper.addBlock(b)
} >> processUnfinishedParentsAndAddBlock(b)
}

/**
* Add parents that are already in block store and then block itself
* @param b block
* @return Result of block processing
*/
private def processUnfinishedParentsAndAddBlock(b: BlockMessage): F[ValidBlockProcessing] =
processUnfinishedParents(b) >> casper.addBlock(b)

/**
* Adds parents that are in block store but not in DAG yet
* @param b block
* @return F[Unit]
*/
private def processUnfinishedParents(b: BlockMessage): F[Unit] = {
import cats.instances.list._
for {
// Parents that are in block store but not in DAG yet and not being currently handled
unfinishedParents <- b.header.parentsHashList.traverse { hash =>
for {
block <- repeatedCasperMessage(hash)
.ifM(none[BlockMessage].pure[F], BlockStore[F].get(hash))
} yield block
}
// Try to add block to DAG. We don't care of results, just make an attempt.
_ <- unfinishedParents.flatten.traverse_(processUnfinishedParentsAndAddBlock)
} yield ()
}

override def init: F[Unit] = theInit
Expand Down

0 comments on commit 21741ce

Please sign in to comment.