Skip to content

Commit b929992

Browse files
authored
Include PPoW info in BlockResponse (#867)
1 parent aa5ec2c commit b929992

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

project/Dependencies.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ object Dependencies {
4747
val boopickle = Seq("io.suzaku" %% "boopickle" % "1.3.3")
4848

4949
val rocksDb = Seq(
50+
// use "5.18.3" for older macOS
5051
"org.rocksdb" % "rocksdbjni" % "6.11.4"
5152
)
5253

src/main/protobuf/extvm

src/main/scala/io/iohk/ethereum/consensus/ethash/RestrictedEthashSigner.scala

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,26 @@ object RestrictedEthashSigner {
1111

1212
def validateSignature(blockHeader: BlockHeader, allowedMiners: Set[ByteString]): Boolean = {
1313
val signature = blockHeader.extraData.takeRight(ECDSASignature.EncodedLength)
14-
val blockHeaderWithoutSig = blockHeader.dropRightNExtraDataBytes(ECDSASignature.EncodedLength)
15-
val encodedBlockHeader = getEncodedWithoutNonce(blockHeaderWithoutSig)
16-
val headerHash = crypto.kec256(encodedBlockHeader)
17-
val maybePubKey = for {
18-
sig <- ECDSASignature.fromBytes(signature)
19-
pubKeyBytes <- sig.publicKey(headerHash)
20-
} yield ByteString.fromArrayUnsafe(pubKeyBytes)
14+
val headerHash = hashHeaderForSigning(blockHeader)
15+
val maybePubKey = ECDSASignature.fromBytes(signature).flatMap(_.publicKey(headerHash))
2116

2217
maybePubKey.exists(allowedMiners.contains)
2318
}
2419

2520
def signHeader(blockHeader: BlockHeader, keyPair: AsymmetricCipherKeyPair): BlockHeader = {
26-
val encoded = getEncodedWithoutNonce(blockHeader)
27-
val hash = crypto.kec256(encoded)
28-
val signed = ECDSASignature.sign(hash, keyPair)
21+
val hash = hashHeaderForSigning(blockHeader)
22+
val signed = ECDSASignature.sign(hash.toArray, keyPair)
2923
val sigBytes = signed.toBytes
3024
blockHeader.withAdditionalExtraData(sigBytes)
3125
}
3226

27+
def hashHeaderForSigning(blockHeader: BlockHeader): ByteString = {
28+
val blockHeaderWithoutSig =
29+
if (blockHeader.extraData.length >= ECDSASignature.EncodedLength)
30+
blockHeader.dropRightNExtraDataBytes(ECDSASignature.EncodedLength)
31+
else blockHeader
32+
val encodedBlockHeader = getEncodedWithoutNonce(blockHeaderWithoutSig)
33+
ByteString(crypto.kec256(encodedBlockHeader))
34+
}
35+
3336
}

src/main/scala/io/iohk/ethereum/extvm/VMServer.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ class VMServer(messageHandler: MessageHandler) extends Logger {
7070

7171
private def awaitHello(): Unit = {
7272
val helloMsg = messageHandler.awaitMessage[msg.Hello]
73-
require(helloMsg.version == ApiVersionProvider.version)
74-
require(helloMsg.config.isEthereumConfig)
73+
require(
74+
helloMsg.version == ApiVersionProvider.version,
75+
s"Wrong Hello message version. Expected ${ApiVersionProvider.version} but was ${helloMsg.version}"
76+
)
77+
require(helloMsg.config.isEthereumConfig, "Hello message ethereum config must be true")
7578
defaultBlockchainConfig = constructBlockchainConfig(helloMsg.config.ethereumConfig.get)
7679
}
7780

src/main/scala/io/iohk/ethereum/jsonrpc/BlockResponse.scala

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package io.iohk.ethereum.jsonrpc
22

33
import akka.util.ByteString
44
import cats.implicits._
5+
import io.iohk.ethereum.consensus.ethash.RestrictedEthashSigner
56
import io.iohk.ethereum.crypto.ECDSASignature
67
import io.iohk.ethereum.domain.{Block, BlockBody, BlockHeader, ChainWeight}
8+
import io.iohk.ethereum.utils.ByteStringUtils
79

810
case class CheckpointResponse(signatures: Seq[ECDSASignature], signers: Seq[ByteString])
911

12+
//scalastyle:off method.length
1013
case class BlockResponse(
1114
number: BigInt,
1215
hash: Option[ByteString],
@@ -29,7 +32,9 @@ case class BlockResponse(
2932
checkpoint: Option[CheckpointResponse],
3033
treasuryOptOut: Option[Boolean],
3134
transactions: Either[Seq[ByteString], Seq[TransactionResponse]],
32-
uncles: Seq[ByteString]
35+
uncles: Seq[ByteString],
36+
signature: String,
37+
signer: String
3338
) {
3439
val chainWeight: Option[ChainWeight] = for {
3540
lcn <- lastCheckpointNumber
@@ -39,6 +44,8 @@ case class BlockResponse(
3944

4045
object BlockResponse {
4146

47+
val NotAvailable = "N/A"
48+
4249
def apply(
4350
block: Block,
4451
weight: Option[ChainWeight] = None,
@@ -61,6 +68,17 @@ object BlockResponse {
6168

6269
val (lcn, td) = weight.map(_.asTuple).separate
6370

71+
val signature =
72+
if (block.header.extraData.length >= ECDSASignature.EncodedLength)
73+
ECDSASignature.fromBytes(block.header.extraData.takeRight(ECDSASignature.EncodedLength))
74+
else None
75+
76+
val signatureStr = signature.map(_.toBytes).map(ByteStringUtils.hash2string).getOrElse(NotAvailable)
77+
val signerStr = signature
78+
.flatMap(_.publicKey(RestrictedEthashSigner.hashHeaderForSigning(block.header)))
79+
.map(ByteStringUtils.hash2string)
80+
.getOrElse(NotAvailable)
81+
6482
BlockResponse(
6583
number = block.header.number,
6684
hash = if (pendingBlock) None else Some(block.header.hash),
@@ -83,7 +101,9 @@ object BlockResponse {
83101
checkpoint = checkpoint,
84102
treasuryOptOut = block.header.treasuryOptOut,
85103
transactions = transactions,
86-
uncles = block.body.uncleNodesList.map(_.hash)
104+
uncles = block.body.uncleNodesList.map(_.hash),
105+
signature = signatureStr,
106+
signer = signerStr
87107
)
88108
}
89109

src/test/scala/io/iohk/ethereum/extvm/VMServerSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class VMServerSpec extends AnyFlatSpec with Matchers with MockFactory {
110110
accountStartNonce = blockchainConfig.accountStartNonce
111111
)
112112
val ethereumConfigMsg = msg.Hello.Config.EthereumConfig(ethereumConfig)
113-
val helloMsg = msg.Hello(version = "1.1", config = ethereumConfigMsg)
113+
val helloMsg = msg.Hello(version = "2.0", config = ethereumConfigMsg)
114114

115115
val messageHandler = mock[MessageHandler]
116116
val vmServer = new VMServer(messageHandler)

0 commit comments

Comments
 (0)