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

Refactor block validation logic - effects #3808

Draft
wants to merge 35 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f541f9a
blockSignature function moved to BlockValidationLogic
stanislavlyalin Aug 8, 2022
179a7b5
formatOfFields function moved to BlockValidationLogic
stanislavlyalin Aug 8, 2022
ee167f4
version function moved to BlockValidationLogic
stanislavlyalin Aug 8, 2022
41176d5
futureTransaction function moved to BlockValidationLogic
stanislavlyalin Aug 8, 2022
c8d471c
transactionExpiration function moved to BlockValidationLogic
stanislavlyalin Aug 8, 2022
cc5dfef
deploysShardIdentifier function moved to BlockValidationLogic
stanislavlyalin Aug 8, 2022
beebdee
blockHash function moved to BlockValidationLogic
stanislavlyalin Aug 8, 2022
5972692
Removed unused
stanislavlyalin Aug 9, 2022
c1a1116
phloPrice function moved to BlockValidationLogic
stanislavlyalin Aug 9, 2022
0417ab5
Rearranged validation functions
stanislavlyalin Aug 9, 2022
be920f5
Version validation test moved to BlockValidationLogicSpec
stanislavlyalin Aug 9, 2022
0af307a
Block hash validation test moved to BlockValidationLogicSpec
stanislavlyalin Aug 9, 2022
31cd7dc
Block fields validation test moved to BlockValidationLogicSpec
stanislavlyalin Aug 9, 2022
19f56a4
Block signature validation tests moved to BlockValidationLogicSpec
stanislavlyalin Aug 9, 2022
6b5dc7f
Future deploy validation tests moved to BlockValidationLogicSpec
stanislavlyalin Aug 9, 2022
95e4a12
Deploy expiration validation tests moved to BlockValidationLogicSpec
stanislavlyalin Aug 9, 2022
48a557e
Internal function `ignore` marked private
stanislavlyalin Aug 9, 2022
962b06a
Fixed test name
stanislavlyalin Aug 9, 2022
91a6f8e
Simplified validation functions. They returns just Boolean
stanislavlyalin Aug 17, 2022
9194775
Fixed calling validation functions. Added related tests
stanislavlyalin Aug 18, 2022
e345109
Test for `formatOfFields` made generative
stanislavlyalin Aug 18, 2022
9f5a5a8
Test for block signature made generative
stanislavlyalin Aug 19, 2022
d5b040d
Test for future deploy made generative
stanislavlyalin Aug 19, 2022
2f63371
Fixed review issue: `f` evaluated always, `errorStatus` only if `f` r…
stanislavlyalin Aug 23, 2022
ddf248c
Tests for block's signature algorithm made generative
stanislavlyalin Aug 23, 2022
1375491
Refactored first test of ValidateTest with MonixTaskTest and Mockito.…
stanislavlyalin Aug 9, 2022
29eb860
Refactored two more tests
stanislavlyalin Aug 10, 2022
34026eb
Extended signature algorithm test
stanislavlyalin Aug 24, 2022
d46c9b5
Removed genesisContext and genesis
stanislavlyalin Aug 25, 2022
43be59a
Refactored createChain function
stanislavlyalin Aug 25, 2022
6cba86e
Removed signedBlock function
stanislavlyalin Aug 26, 2022
7f8e2f2
Refactored one more test
stanislavlyalin Aug 26, 2022
75e34d3
Refactored sequence number validation tests
stanislavlyalin Aug 26, 2022
c53c56d
Refactored repeat deploy validation tests
stanislavlyalin Aug 26, 2022
e494319
Refactored block summary validation tests
stanislavlyalin Aug 26, 2022
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
Prev Previous commit
Next Next commit
blockHash function moved to BlockValidationLogic
  • Loading branch information
stanislavlyalin committed Aug 10, 2022
commit beebdeee57a9929d76b5bd09fce209603d68316b
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coop.rchain.casper
import cats.syntax.all._
import coop.rchain.casper.Validate.signatureVerifiers
import coop.rchain.casper.protocol.BlockMessage
import coop.rchain.casper.util.ProtoUtil
import coop.rchain.models.BlockVersion
import coop.rchain.models.syntax._

Expand Down Expand Up @@ -55,4 +56,6 @@ object BlockValidationLogic {
BlockStatus.invalidDeployShardId.asLeft[ValidBlock]
}
}

def blockHash(b: BlockMessage): Boolean = b.blockHash == ProtoUtil.hashBlock(b)
}
20 changes: 1 addition & 19 deletions casper/src/main/scala/coop/rchain/casper/Validate.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package coop.rchain.casper

import cats.Monad
import cats.data.EitherT
import cats.effect.{Concurrent, Sync}
import cats.syntax.all._
import cats.{Applicative, Monad}
import com.google.protobuf.ByteString
import coop.rchain.blockstorage.BlockStore
import coop.rchain.blockstorage.BlockStore.BlockStore
Expand Down Expand Up @@ -171,24 +171,6 @@ object Validate {
}
} yield status

def blockHash[F[_]: Applicative: Log](b: BlockMessage): F[Boolean] = {
val blockHashComputed = ProtoUtil.hashBlock(b)
if (b.blockHash == blockHashComputed)
true.pure
else {
val computedHashString = PrettyPrinter.buildString(blockHashComputed)
val hashString = PrettyPrinter.buildString(b.blockHash)
for {
_ <- Log[F].warn(
ignore(
b,
s"block hash $hashString does not match to computed value $computedHashString."
)
)
} yield false
}
}

/**
* Justification regression check.
* Compares justifications that has been already used by sender and recorded in the DAG with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import coop.rchain.blockstorage.BlockStore.BlockStore
import coop.rchain.blockstorage.dag.BlockDagStorage
import coop.rchain.casper.protocol.BlockMessage
import coop.rchain.casper.syntax._
import coop.rchain.casper.{BlockValidationLogic, PrettyPrinter, Validate}
import coop.rchain.casper.{BlockValidationLogic, PrettyPrinter}
import coop.rchain.models.BlockHash.BlockHash
import coop.rchain.shared.Log
import coop.rchain.shared.syntax._
Expand Down Expand Up @@ -205,14 +205,14 @@ object BlockReceiver {
// TODO: 1. validation and logging in these checks should be separated
// 2. logging of these block should indicate that information cannot be trusted
// e.g. if block hash is invalid it cannot represent identity of a block
val validFormat = BlockValidationLogic.formatOfFields(b).pure
val validHash = Validate.blockHash(b)
val validSig = BlockValidationLogic.blockSignature(b).pure
val validFormat = BlockValidationLogic.formatOfFields(b)
val validHash = BlockValidationLogic.blockHash(b)
val validSig = BlockValidationLogic.blockSignature(b)
// TODO: check sender to be valid bonded validator
// - not always possible because now are new blocks downloaded from DAG tips
// which in case of epoch change sender can be unknown
// TODO: check valid version (possibly part of hash checking)
validFormat &&^ validShard &&^ validHash &&^ validSig
validShard &&^ (validFormat && validHash && validSig).pure
}

// Check if block should be stored
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package coop.rchain.casper.engine

import cats.effect.concurrent.{Deferred, Ref}
import cats.effect.{Concurrent, Timer}
import cats.effect.{Concurrent, Sync, Timer}
import cats.syntax.all._
import coop.rchain.blockstorage.BlockStore
import coop.rchain.blockstorage.BlockStore.BlockStore
Expand Down Expand Up @@ -143,7 +143,7 @@ class NodeSyncing[F[_]
BlockStore[F].contains(_),
BlockStore[F].getUnsafe,
BlockStore[F].put(_, _),
Validate.blockHash[F]
block => Sync[F].delay(BlockValidationLogic.blockHash(block))
)

// Request tuple space state for Last Finalized State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,14 @@ class ValidateTest
val blockValidHash = block.copy(blockHash = hash)

// Test valid block hash
val hashValid = Validate.blockHash[Task](blockValidHash).runSyncUnsafe()
val hashValid = BlockValidationLogic.blockHash(blockValidHash)

hashValid shouldBe true

val blockInValidHash = block.copy(blockHash = ByteString.copyFromUtf8("123"))

// Test invalid block hash
val hashInValid = Validate.blockHash[Task](blockInValidHash).runSyncUnsafe()
val hashInValid = BlockValidationLogic.blockHash(blockInValidHash)

hashInValid shouldBe false
}
Expand Down