Skip to content

Commit

Permalink
Merge rchain#2061
Browse files Browse the repository at this point in the history
2061: Replay deploys from a block with the corresponding timestamp r=KentShikama a=KentShikama

Previously we were injecting the current time to replay
deploys for parents blocks.

### JIRA ticket:
<sup>_Create it if there isn't one already._</sup>

https://rchain.atlassian.net/browse/RCHAIN-1122

### Notes
<sup>_Optional. Add any notes on caveats, approaches you tried that didn't work, or anything else._</sup>



### Please make sure that this PR:
- [x] is at most 200 lines of code (excluding tests),
- [x] meets [RChain development coding standards](https://rchain.atlassian.net/wiki/spaces/DOC/pages/28082177/Coding+Standards),
- [x] includes tests for all added features,
- [x] has a reviewer assigned,
- [x] has [all commits signed](https://rchain.atlassian.net/wiki/spaces/DOC/pages/498630673/How+to+sign+commits+to+rchain+rchain).

### [Bors](https://bors.tech/) cheat-sheet:

- `bors r+` runs integration tests and merges the PR (if it's approved),
- `bors try` runs integration tests for the PR,
- `bors delegate+` enables non-maintainer PR authors to run the above.


Co-authored-by: Kent Shikama <kent@kentshikama.com>
  • Loading branch information
bors[bot] and KentShikama committed Jan 4, 2019
2 parents 51d9323 + b43b484 commit 853acc3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ object InterpreterUtil {
possiblePreStateHash <- computeParentsPostState[F](
parents,
dag,
runtimeManager,
timestamp
runtimeManager
)
_ <- Log[F].info(s"Computed parents post state for ${PrettyPrinter.buildString(b)}.")
result <- processPossiblePreStateHash[F](
Expand Down Expand Up @@ -155,7 +154,7 @@ object InterpreterUtil {
implicit scheduler: Scheduler
): F[Either[Throwable, (StateHash, StateHash, Seq[InternalProcessedDeploy])]] =
for {
possiblePreStateHash <- computeParentsPostState[F](parents, dag, runtimeManager, time)
possiblePreStateHash <- computeParentsPostState[F](parents, dag, runtimeManager)
} yield
possiblePreStateHash match {
case Right(preStateHash) =>
Expand All @@ -169,8 +168,7 @@ object InterpreterUtil {
private def computeParentsPostState[F[_]: Sync: BlockStore: ToAbstractContext](
parents: Seq[BlockMessage],
dag: BlockDagRepresentation[F],
runtimeManager: RuntimeManager,
time: Option[Long]
runtimeManager: RuntimeManager
): F[Either[Throwable, StateHash]] = {
val parentTuplespaces = parents.flatMap(p => ProtoUtil.tuplespace(p).map(p -> _))

Expand All @@ -187,7 +185,6 @@ object InterpreterUtil {
parents,
dag,
runtimeManager,
time,
initStateHash
)
}
Expand All @@ -199,29 +196,41 @@ object InterpreterUtil {
parents: Seq[BlockMessage],
dag: BlockDagRepresentation[F],
runtimeManager: RuntimeManager,
time: Option[Long],
initStateHash: StateHash
): F[Either[Throwable, StateHash]] =
for {
blockHashesToApply <- findMultiParentsBlockHashesForReplay(parents, dag)
blocksToApply <- blockHashesToApply.traverse(b => ProtoUtil.unsafeGetBlock[F](b.blockHash))
deploys = blocksToApply.flatMap(_.getBody.deploys.flatMap(ProcessedDeployUtil.toInternal))
replayResult <- ToAbstractContext[F].fromTask(
runtimeManager
.replayComputeState(initStateHash, deploys, time)
)
} yield
replayResult match {
case result @ Right(_) => result.leftCast[Throwable]
case Left((_, status)) =>
val parentHashes =
parents.map(p => Base16.encode(p.blockHash.toByteArray).take(8))
Left(
new Exception(
s"Failed status while computing post state of $parentHashes: $status"
)
)
}
replayResult <- blocksToApply.toList.foldM(Right(initStateHash).leftCast[Throwable]) {
(acc, block) =>
acc match {
case Right(stateHash) =>
val deploys =
block.getBody.deploys.flatMap(ProcessedDeployUtil.toInternal)
val time = Some(block.header.get.timestamp)
for {
replayResult <- ToAbstractContext[F].fromTask(
runtimeManager
.replayComputeState(stateHash, deploys, time)
)
} yield
replayResult match {
case result @ Right(_) => result.leftCast[Throwable]
case Left((_, status)) =>
val parentHashes =
parents.map(
p => Base16.encode(p.blockHash.toByteArray).take(8)
)
Left(
new Exception(
s"Failed status while computing post state of $parentHashes: $status"
)
)
}
case Left(_) => acc.pure[F]
}
}
} yield replayResult

private[rholang] def findMultiParentsBlockHashesForReplay[F[_]: Monad](
parents: Seq[BlockMessage],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class HashSetCasperTest extends FlatSpec with Matchers {
} yield result
}

it should "not produce UnusedCommEvent while merging non conflicting blocks in the presence of conflicting ones" ignore effectTest {
it should "not produce UnusedCommEvent while merging non conflicting blocks in the presence of conflicting ones" in effectTest {
def defineDeploy(source: String, t: Long) =
ProtoUtil.sourceDeploy(
source,
Expand Down

0 comments on commit 853acc3

Please sign in to comment.