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

Include PPoW info in BlockResponse #867

Merged
merged 6 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ object Dependencies {
val boopickle = Seq("io.suzaku" %% "boopickle" % "1.3.3")

val rocksDb = Seq(
// use "5.18.3" for older macOS
"org.rocksdb" % "rocksdbjni" % "6.11.4"
)

Expand Down
2 changes: 1 addition & 1 deletion src/main/protobuf/extvm
Submodule extvm updated 2 files
+1 −1 VERSION
+1 −1 msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@ object RestrictedEthashSigner {

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

maybePubKey.exists(allowedMiners.contains)
}

def signHeader(blockHeader: BlockHeader, keyPair: AsymmetricCipherKeyPair): BlockHeader = {
val encoded = getEncodedWithoutNonce(blockHeader)
val hash = crypto.kec256(encoded)
val signed = ECDSASignature.sign(hash, keyPair)
val hash = hashHeaderForSigning(blockHeader)
val signed = ECDSASignature.sign(hash.toArray, keyPair)
val sigBytes = signed.toBytes
blockHeader.withAdditionalExtraData(sigBytes)
}

def hashHeaderForSigning(blockHeader: BlockHeader): ByteString = {
val blockHeaderWithoutSig =
if (blockHeader.extraData.length >= ECDSASignature.EncodedLength)
blockHeader.dropRightNExtraDataBytes(ECDSASignature.EncodedLength)
else blockHeader
val encodedBlockHeader = getEncodedWithoutNonce(blockHeaderWithoutSig)
ByteString(crypto.kec256(encodedBlockHeader))
}

}
7 changes: 5 additions & 2 deletions src/main/scala/io/iohk/ethereum/extvm/VMServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ class VMServer(messageHandler: MessageHandler) extends Logger {

private def awaitHello(): Unit = {
val helloMsg = messageHandler.awaitMessage[msg.Hello]
require(helloMsg.version == ApiVersionProvider.version)
require(helloMsg.config.isEthereumConfig)
require(
helloMsg.version == ApiVersionProvider.version,
s"Wrong Hello message version. Expected ${ApiVersionProvider.version} but was ${helloMsg.version}"
)
require(helloMsg.config.isEthereumConfig, "Hello message ethereum config must be true")
defaultBlockchainConfig = constructBlockchainConfig(helloMsg.config.ethereumConfig.get)
}

Expand Down
24 changes: 22 additions & 2 deletions src/main/scala/io/iohk/ethereum/jsonrpc/BlockResponse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package io.iohk.ethereum.jsonrpc

import akka.util.ByteString
import cats.implicits._
import io.iohk.ethereum.consensus.ethash.RestrictedEthashSigner
import io.iohk.ethereum.crypto.ECDSASignature
import io.iohk.ethereum.domain.{Block, BlockBody, BlockHeader, ChainWeight}
import io.iohk.ethereum.utils.ByteStringUtils

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

//scalastyle:off method.length
case class BlockResponse(
number: BigInt,
hash: Option[ByteString],
Expand All @@ -29,7 +32,9 @@ case class BlockResponse(
checkpoint: Option[CheckpointResponse],
treasuryOptOut: Option[Boolean],
transactions: Either[Seq[ByteString], Seq[TransactionResponse]],
uncles: Seq[ByteString]
uncles: Seq[ByteString],
signature: String,
signer: String
) {
val chainWeight: Option[ChainWeight] = for {
lcn <- lastCheckpointNumber
Expand All @@ -39,6 +44,8 @@ case class BlockResponse(

object BlockResponse {

val NotAvailable = "N/A"

def apply(
block: Block,
weight: Option[ChainWeight] = None,
Expand All @@ -61,6 +68,17 @@ object BlockResponse {

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

val signature =
if (block.header.extraData.length >= ECDSASignature.EncodedLength)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor comment, when we have the condition (block.header.extraData.length = ECDSASignature.EncodedLength)
The following line:
block.header.extraData.takeRight(ECDSASignature.EncodedLength) is not required, right?
Anyway, this code is clear and understandable.

ECDSASignature.fromBytes(block.header.extraData.takeRight(ECDSASignature.EncodedLength))
else None

val signatureStr = signature.map(_.toBytes).map(ByteStringUtils.hash2string).getOrElse(NotAvailable)
val signerStr = signature
.flatMap(_.publicKey(RestrictedEthashSigner.hashHeaderForSigning(block.header)))
.map(ByteStringUtils.hash2string)
.getOrElse(NotAvailable)

BlockResponse(
number = block.header.number,
hash = if (pendingBlock) None else Some(block.header.hash),
Expand All @@ -83,7 +101,9 @@ object BlockResponse {
checkpoint = checkpoint,
treasuryOptOut = block.header.treasuryOptOut,
transactions = transactions,
uncles = block.body.uncleNodesList.map(_.hash)
uncles = block.body.uncleNodesList.map(_.hash),
signature = signatureStr,
signer = signerStr
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/io/iohk/ethereum/extvm/VMServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class VMServerSpec extends AnyFlatSpec with Matchers with MockFactory {
accountStartNonce = blockchainConfig.accountStartNonce
)
val ethereumConfigMsg = msg.Hello.Config.EthereumConfig(ethereumConfig)
val helloMsg = msg.Hello(version = "1.1", config = ethereumConfigMsg)
val helloMsg = msg.Hello(version = "2.0", config = ethereumConfigMsg)

val messageHandler = mock[MessageHandler]
val vmServer = new VMServer(messageHandler)
Expand Down