Skip to content

Commit 06bd48d

Browse files
author
Christos KK Loverdos
committed
Make difficulty consensus-polymorphic (part III)
Add a test
1 parent fec4c6b commit 06bd48d

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package io.iohk.ethereum.consensus.atomixraft.difficulty
2+
3+
import akka.util.ByteString
4+
import io.iohk.ethereum.blockchain.sync.ScenarioSetup
5+
import io.iohk.ethereum.consensus.atomixraft.validators.AtomixRaftBlockHeaderValidator
6+
import io.iohk.ethereum.consensus.validators.BlockHeaderValid
7+
import io.iohk.ethereum.domain.BlockHeader
8+
import org.scalatest.prop.PropertyChecks
9+
import org.scalatest.{FlatSpec, Matchers}
10+
import org.spongycastle.util.encoders.Hex
11+
12+
class DifficultySpec extends FlatSpec with Matchers with PropertyChecks {
13+
private val blockHeader = BlockHeader(
14+
parentHash = ByteString(Hex.decode("d882d5c210bab4cb7ef0b9f3dc2130cb680959afcd9a8f9bf83ee6f13e2f9da3")),
15+
ommersHash = ByteString(Hex.decode("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")),
16+
beneficiary = ByteString(Hex.decode("95f484419881c6e9b6de7fb3f8ad03763bd49a89")),
17+
stateRoot = ByteString(Hex.decode("634a2b20c9e02afdda7157afe384306c5acc4fb9c09b45dc0203c0fbb2fed0e6")),
18+
transactionsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
19+
receiptsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
20+
logsBloom = ByteString(Hex.decode("00" * 256)),
21+
difficulty = 0,
22+
number = 20,
23+
gasLimit = 131620495,
24+
gasUsed = 0,
25+
unixTimestamp = 1486752441,
26+
extraData = ByteString(Hex.decode("d783010507846765746887676f312e372e33856c696e7578")),
27+
mixHash = ByteString(Hex.decode("6bc729364c9b682cfa923ba9480367ebdfa2a9bca2a652fe975e8d5958f696dd")),
28+
nonce = ByteString(Hex.decode("797a8f3a494f937b"))
29+
)
30+
31+
private val parentHeader = BlockHeader(
32+
parentHash = ByteString(Hex.decode("677a5fb51d52321b03552e3c667f602cc489d15fc1d7824445aee6d94a9db2e7")),
33+
ommersHash = ByteString(Hex.decode("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")),
34+
beneficiary = ByteString(Hex.decode("95f484419881c6e9b6de7fb3f8ad03763bd49a89")),
35+
stateRoot = ByteString(Hex.decode("cddeeb071e2f69ad765406fb7c96c0cd42ddfc6ec54535822b564906f9e38e44")),
36+
transactionsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
37+
receiptsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
38+
logsBloom = ByteString(Hex.decode("00" * 256)),
39+
difficulty = 0,
40+
number = 19,
41+
gasLimit = 131749155,
42+
gasUsed = 0,
43+
unixTimestamp = 1486752440,
44+
extraData = ByteString(Hex.decode("d783010507846765746887676f312e372e33856c696e7578")),
45+
mixHash = ByteString(Hex.decode("7f9ac1ddeafff0f926ed9887b8cf7d50c3f919d902e618b957022c46c8b404a6")),
46+
nonce = ByteString(Hex.decode("3fc7bc671f7cee70"))
47+
)
48+
49+
it should "validate difficulty for AtomixRaft block header" in new ScenarioSetup {
50+
val blockHeaderValidator = new AtomixRaftBlockHeaderValidator(blockchainConfig)
51+
val calculator = AtomixRaftDifficulty
52+
53+
val blockNumber: BigInt = parentHeader.number + 1
54+
val blockTimestamp: Long = parentHeader.unixTimestamp + 6
55+
56+
val difficulty = calculator.calculateDifficulty(blockNumber, blockTimestamp, parentHeader)
57+
val result = blockHeaderValidator.validate(blockHeader, parentHeader)
58+
59+
result shouldBe Right(BlockHeaderValid)
60+
difficulty shouldBe 0
61+
}
62+
}

0 commit comments

Comments
 (0)