Skip to content

[CHORE] Refactor block usage and location #669

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

Merged
merged 1 commit into from
Sep 15, 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
14 changes: 14 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,17 @@ jdkPackagerJVMArgs := Seq(
)

coverageExcludedPackages := "io\\.iohk\\.ethereum\\.extvm\\.msg.*"


addCommandAlias(
"compile-all",
""";compile
|;test:compile
|;evm:compile
|;it:compile
|;ets:compile
|;rpcTest:compile
|;snappy:compile
|;benchmark:compile
|""".stripMargin
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.iohk.ethereum.ObjectGenerators
import io.iohk.ethereum.domain.Block._
import io.iohk.ethereum.domain._
import io.iohk.ethereum.network.p2p.messages.CommonMessages.SignedTransactions._
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.utils.Logger
import org.scalacheck.Gen
import org.scalatest.FunSuite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import io.iohk.ethereum.domain._
import io.iohk.ethereum.ets.common.AccountState
import io.iohk.ethereum.ledger.Ledger.VMImpl
import io.iohk.ethereum.ledger._
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.utils.BigIntExtensionMethods._
import io.iohk.ethereum.utils.{BlockchainConfig, Config}
import org.bouncycastle.util.encoders.Hex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import java.net.URI
import akka.actor.{Actor, ActorRef, _}
import akka.util.ByteString
import io.iohk.ethereum.crypto.kec256
import io.iohk.ethereum.domain.{BlockHeader, Receipt}
import io.iohk.ethereum.domain.{BlockBody, BlockHeader, Receipt}
import io.iohk.ethereum.domain.BlockHeader._
import io.iohk.ethereum.network.{Peer, PeerManagerActor}
import io.iohk.ethereum.network.PeerActor.SendMessage
import io.iohk.ethereum.network.PeerManagerActor.{GetPeers, Peers}
Expand All @@ -15,7 +16,6 @@ import io.iohk.ethereum.network.p2p.messages.PV63._
import io.iohk.ethereum.network.p2p.messages.PV63.MptNodeEncoders._
import org.bouncycastle.util.encoders.Hex
import ReceiptImplicits._
import BlockHeaderImplicits._
import io.iohk.ethereum.mpt.{BranchNode, ExtensionNode, HashNode, LeafNode, MptNode}
import io.iohk.ethereum.network.PeerEventBusActor.PeerEvent.MessageFromPeer
import io.iohk.ethereum.network.PeerEventBusActor.{PeerSelector, Subscribe}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import io.iohk.ethereum.network.EtcPeerManagerActor.PeerInfo
import io.iohk.ethereum.network.PeerManagerActor.PeerConfiguration
import io.iohk.ethereum.network.handshaker.{EtcHandshaker, EtcHandshakerConfiguration, Handshaker}
import io.iohk.ethereum.network.p2p.EthereumMessageDecoder
import io.iohk.ethereum.network.p2p.messages.PV62
import io.iohk.ethereum.network.rlpx.RLPxConnectionHandler.RLPxConfiguration
import io.iohk.ethereum.network.{ForkResolver, PeerEventBusActor, PeerManagerActor}
import io.iohk.ethereum.nodebuilder.{AuthHandshakerBuilder, NodeKeyBuilder, SecureRandomBuilder}
Expand Down Expand Up @@ -111,13 +110,13 @@ object DumpChainApp extends App with NodeKeyBuilder with SecureRandomBuilder wit

override def getBlockHeaderByHash(hash: ByteString): Option[BlockHeader] = Some(new FakeHeader())

override def getBlockBodyByHash(hash: ByteString): Option[PV62.BlockBody] = ???
override def getBlockBodyByHash(hash: ByteString): Option[BlockBody] = ???

override def getMptNodeByHash(hash: ByteString): Option[MptNode] = ???

override def save(blockHeader: BlockHeader): Unit = ???

override def save(blockHash: ByteString, blockBody: PV62.BlockBody): Unit = ???
override def save(blockHash: ByteString, blockBody: BlockBody): Unit = ???

override def save(blockHash: ByteString, receipts: Seq[Receipt]): Unit = ???

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import akka.util.ByteString
import io.iohk.ethereum.db.dataSource.EphemDataSource
import io.iohk.ethereum.db.storage._
import io.iohk.ethereum.domain._
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody._
import io.iohk.ethereum.network.p2p.messages.PV62.BlockHeaderImplicits._
import io.iohk.ethereum.domain.BlockHeader._
import io.iohk.ethereum.domain.BlockBody._
import io.iohk.ethereum.network.p2p.messages.PV63._
import MptNodeEncoders._
import ReceiptImplicits._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.iohk.ethereum.utils.Logger
import io.iohk.ethereum.{crypto, rlp}
import io.iohk.ethereum.domain._
import io.iohk.ethereum.mpt.MerklePatriciaTrie
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.rlp.RLPImplicits._
import org.json4s.{CustomSerializer, DefaultFormats, Formats, JString, JValue}
import org.bouncycastle.util.encoders.Hex
Expand Down Expand Up @@ -135,7 +134,8 @@ class GenesisDataLoader(
unixTimestamp = BigInt(genesisData.timestamp.replace("0x", ""), 16).toLong,
extraData = genesisData.extraData,
mixHash = genesisData.mixHash.getOrElse(zeros(hashLength)),
nonce = genesisData.nonce)
nonce = genesisData.nonce
)

private def zeros(length: Int) =
ByteString(Hex.decode(List.fill(length)("0").mkString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package io.iohk.ethereum.blockchain.sync
import akka.util.ByteString
import io.iohk.ethereum.consensus.validators.Validators
import io.iohk.ethereum.consensus.validators.std.StdBlockValidator
import io.iohk.ethereum.domain.{BlockHeader, Blockchain}
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.domain.{BlockHeader, BlockBody, Blockchain}
import io.iohk.ethereum.consensus.validators.std.StdBlockValidator.BlockValid

trait SyncBlocksValidator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package io.iohk.ethereum.blockchain.sync.regular
import akka.actor.ActorRef
import akka.util.ByteString
import cats.data.NonEmptyList
import io.iohk.ethereum.domain.{Block, BlockHeader, HeadersSeq}
import io.iohk.ethereum.domain.{Block, BlockHeader, BlockBody, HeadersSeq}
import io.iohk.ethereum.network.{Peer, PeerId}
import io.iohk.ethereum.network.p2p.messages.PV62.{BlockBody, BlockHash}
import io.iohk.ethereum.network.p2p.messages.PV62.BlockHash
import BlockFetcherState._
import cats.syntax.either._
import cats.syntax.option._

import scala.collection.immutable.Queue

case class BlockFetcherState(
importer: ActorRef,
readyBlocks: Queue[Block],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait BlockGenerator {
/**
* The type of consensus-specific data used in the block generation process.
* For example, under [[io.iohk.ethereum.consensus.ethash.EthashConsensus EthashConsensus]],
* this represents the [[io.iohk.ethereum.network.p2p.messages.PV62.BlockBody#uncleNodesList ommers]].
* this represents the [[io.iohk.ethereum.domain.BlockBody#uncleNodesList ommers]].
*/
type X

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import io.iohk.ethereum.crypto.kec256
import io.iohk.ethereum.db.dataSource.EphemDataSource
import io.iohk.ethereum.db.storage.StateStorage
import io.iohk.ethereum.domain._
import io.iohk.ethereum.consensus.ethash.blocks.OmmersSeqEnc
import io.iohk.ethereum.ledger.Ledger.{BlockPreparationResult, BlockResult}
import io.iohk.ethereum.ledger.{BlockPreparator, BloomFilter}
import io.iohk.ethereum.mpt.{ByteArraySerializable, MerklePatriciaTrie}
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.network.p2p.messages.PV62.BlockHeaderImplicits._
import io.iohk.ethereum.utils.BlockchainConfig
import io.iohk.ethereum.utils.ByteUtils.or

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.iohk.ethereum.consensus.blocks
import io.iohk.ethereum.consensus.ConsensusConfig
import io.iohk.ethereum.domain._
import io.iohk.ethereum.ledger.{BlockPreparationError, BlockPreparator}
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.utils.BlockchainConfig


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.iohk.ethereum.consensus.ethash.validators.EthashValidators
import io.iohk.ethereum.crypto.kec256
import io.iohk.ethereum.domain._
import io.iohk.ethereum.ledger.{BlockPreparationError, BlockPreparator}
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.utils.BlockchainConfig


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.iohk.ethereum.consensus.ethash
import io.iohk.ethereum.consensus.ethash.validators.OmmersValidator.OmmersError
import io.iohk.ethereum.domain.BlockHeader
import io.iohk.ethereum.ledger.BlockPreparationError
import io.iohk.ethereum.rlp.{RLPEncodeable, RLPList, RLPSerializable}

package object blocks {
/**
Expand All @@ -13,5 +14,9 @@ package object blocks {
*/
final type Ommers = Seq[BlockHeader]

implicit class OmmersSeqEnc(blockHeaders: Seq[BlockHeader]) extends RLPSerializable {
override def toRLPEncodable: RLPEncodeable = RLPList(blockHeaders.map(_.toRLPEncodable): _*)
}

final case class InvalidOmmers(reason: OmmersError) extends BlockPreparationError
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.iohk.ethereum.consensus.validators

import io.iohk.ethereum.consensus.validators.std.StdBlockValidator.{BlockError, BlockValid}
import io.iohk.ethereum.domain.{BlockHeader, Receipt}
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.domain.{BlockHeader, BlockBody, Receipt}

trait BlockValidator {
def validateHeaderAndBody(blockHeader: BlockHeader, blockBody: BlockBody): Either[BlockError, BlockValid]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package io.iohk.ethereum.consensus.validators
package std

import akka.util.ByteString
import io.iohk.ethereum.consensus.ethash.blocks.OmmersSeqEnc
import io.iohk.ethereum.crypto._
import io.iohk.ethereum.domain.{Block, BlockHeader, Receipt, SignedTransaction}
import io.iohk.ethereum.domain.{Block, BlockHeader, BlockBody, Receipt, SignedTransaction}
import io.iohk.ethereum.ledger.BloomFilter
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.utils.ByteUtils.or


Expand Down Expand Up @@ -34,7 +34,6 @@ object StdBlockValidator extends BlockValidator {
* @return Block if valid, a Some otherwise
*/
private def validateOmmersHash(block: Block): Either[BlockError, BlockValid] = {
import io.iohk.ethereum.network.p2p.messages.PV62.BlockHeaderImplicits._
val encodedOmmers: Array[Byte] = block.body.uncleNodesList.toBytes
if (kec256(encodedOmmers) sameElements block.header.ommersHash) Right(BlockValid)
else Left(BlockOmmersHashError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import boopickle.Default.{ Pickle, Unpickle }
import io.iohk.ethereum.crypto.ECDSASignature
import io.iohk.ethereum.db.dataSource.DataSource
import io.iohk.ethereum.db.storage.BlockBodiesStorage.BlockBodyHash
import io.iohk.ethereum.domain.{ Address, BlockHeader, SignedTransaction, Transaction }
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.domain.{ Address, BlockHeader, BlockBody, SignedTransaction, Transaction }
import io.iohk.ethereum.utils.ByteUtils.compactPickledBytes

/**
Expand Down
4 changes: 1 addition & 3 deletions src/main/scala/io/iohk/ethereum/domain/Block.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.iohk.ethereum.domain

import akka.util.ByteString
import io.iohk.ethereum.network.p2p.messages.PV62.{BlockBody, BlockHeaderImplicits}
import io.iohk.ethereum.domain.BlockHeader._
import io.iohk.ethereum.rlp.{RLPEncodeable, RLPList, RLPSerializable, rawDecode}

/**
Expand Down Expand Up @@ -31,7 +31,6 @@ case class Block(header: BlockHeader, body: BlockBody) {
object Block {

implicit class BlockEnc(val obj: Block) extends RLPSerializable {
import BlockHeaderImplicits._
import io.iohk.ethereum.network.p2p.messages.CommonMessages.SignedTransactions._

override def toRLPEncodable: RLPEncodeable = RLPList(
Expand All @@ -42,7 +41,6 @@ object Block {
}

implicit class BlockDec(val bytes: Array[Byte]) extends AnyVal {
import BlockHeaderImplicits._
import io.iohk.ethereum.network.p2p.messages.CommonMessages.SignedTransactions._
def toBlock: Block = rawDecode(bytes) match {
case RLPList(header: RLPList, stx: RLPList, uncles: RLPList) => Block(
Expand Down
70 changes: 70 additions & 0 deletions src/main/scala/io/iohk/ethereum/domain/BlockBody.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.iohk.ethereum.domain

import io.iohk.ethereum.domain.BlockHeader._
import io.iohk.ethereum.rlp.{RLPEncodeable, RLPList, RLPSerializable, rawDecode}

case class BlockBody(transactionList: Seq[SignedTransaction], uncleNodesList: Seq[BlockHeader]) {
override def toString: String =
s"""BlockBody{
|transactionList: $transactionList
|uncleNodesList: $uncleNodesList
|}
""".stripMargin
}

object BlockBody {

val empty = BlockBody(Seq.empty, Seq.empty)

def blockBodyToRlpEncodable(blockBody: BlockBody,
signedTxToRlpEncodable: SignedTransaction => RLPEncodeable,
blockHeaderToRlpEncodable: BlockHeader => RLPEncodeable
): RLPEncodeable =
RLPList(
RLPList(blockBody.transactionList.map(signedTxToRlpEncodable): _*),
RLPList(blockBody.uncleNodesList.map(blockHeaderToRlpEncodable): _*)
)

implicit class BlockBodyEnc(msg: BlockBody) extends RLPSerializable {
override def toRLPEncodable: RLPEncodeable = {
import io.iohk.ethereum.network.p2p.messages.CommonMessages.SignedTransactions._

blockBodyToRlpEncodable(
msg,
stx => SignedTransactionEnc(stx).toRLPEncodable,
header => BlockHeaderEnc(header).toRLPEncodable
)
}
}

implicit class BlockBlodyDec(val bytes: Array[Byte]) extends AnyVal {
def toBlockBody: BlockBody = BlockBodyRLPEncodableDec(rawDecode(bytes)).toBlockBody
}

def rlpEncodableToBlockBody(rlpEncodeable: RLPEncodeable,
rlpEncodableToSignedTransaction: RLPEncodeable => SignedTransaction,
rlpEncodableToBlockHeader: RLPEncodeable => BlockHeader
): BlockBody =
rlpEncodeable match {
case RLPList((transactions: RLPList), (uncles: RLPList)) =>
BlockBody(
transactions.items.map(rlpEncodableToSignedTransaction),
uncles.items.map(rlpEncodableToBlockHeader)
)
case _ => throw new RuntimeException("Cannot decode BlockBody")
}

implicit class BlockBodyRLPEncodableDec(val rlpEncodeable: RLPEncodeable) {
def toBlockBody: BlockBody = {
import io.iohk.ethereum.network.p2p.messages.CommonMessages.SignedTransactions._

rlpEncodableToBlockBody(
rlpEncodeable,
rlp => SignedTransactionRlpEncodableDec(rlp).toSignedTransaction,
rlp => BlockheaderEncodableDec(rlp).toBlockHeader
)

}
}

}
28 changes: 26 additions & 2 deletions src/main/scala/io/iohk/ethereum/domain/BlockHeader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package io.iohk.ethereum.domain

import akka.util.ByteString
import io.iohk.ethereum.crypto.kec256
import io.iohk.ethereum.network.p2p.messages.PV62.BlockHeaderImplicits._
import io.iohk.ethereum.rlp.{RLPList, encode => rlpEncode}
import io.iohk.ethereum.rlp.RLPImplicitConversions._
import io.iohk.ethereum.rlp.RLPImplicits._
import io.iohk.ethereum.rlp.{RLPEncodeable, RLPList, RLPSerializable, rawDecode, encode => rlpEncode}
import org.bouncycastle.util.encoders.Hex

case class BlockHeader(
Expand Down Expand Up @@ -66,4 +67,27 @@ object BlockHeader {
}
rlpEncode(rlpEncoded)
}

implicit class BlockHeaderEnc(blockHeader: BlockHeader) extends RLPSerializable {
override def toRLPEncodable: RLPEncodeable = {
import blockHeader._
RLPList(parentHash, ommersHash, beneficiary, stateRoot, transactionsRoot, receiptsRoot,
logsBloom, difficulty, number, gasLimit, gasUsed, unixTimestamp, extraData, mixHash, nonce)
}
}

implicit class BlockheaderDec(val bytes: Array[Byte]) extends AnyVal {
def toBlockHeader: BlockHeader = BlockheaderEncodableDec(rawDecode(bytes)).toBlockHeader
}

implicit class BlockheaderEncodableDec(val rlpEncodeable: RLPEncodeable) extends AnyVal {
def toBlockHeader: BlockHeader = {
rlpEncodeable match {
case RLPList(parentHash, ommersHash, beneficiary, stateRoot, transactionsRoot, receiptsRoot,
logsBloom, difficulty, number, gasLimit, gasUsed, unixTimestamp, extraData, mixHash, nonce) =>
BlockHeader(parentHash, ommersHash, beneficiary, stateRoot, transactionsRoot, receiptsRoot,
logsBloom, difficulty, number, gasLimit, gasUsed, unixTimestamp, extraData, mixHash, nonce)
}
}
}
}
4 changes: 2 additions & 2 deletions src/main/scala/io/iohk/ethereum/domain/Blockchain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import io.iohk.ethereum.db.storage.pruning.PruningMode
import io.iohk.ethereum.domain
import io.iohk.ethereum.ledger.{InMemoryWorldStateProxy, InMemoryWorldStateProxyStorage}
import io.iohk.ethereum.mpt.{MerklePatriciaTrie, MptNode}
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.vm.{Storage, WorldStateProxy}

/**
* Entity to be used to persist and query Blockchain related objects (blocks, transactions, ommers)
*/
Expand Down Expand Up @@ -41,7 +41,7 @@ trait Blockchain {
* Allows to query a blockBody by block hash
*
* @param hash of the block that's being searched
* @return [[io.iohk.ethereum.network.p2p.messages.PV62.BlockBody]] if found
* @return [[io.iohk.ethereum.domain.BlockBody]] if found
*/
def getBlockBodyByHash(hash: ByteString): Option[BlockBody]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.bouncycastle.crypto.AsymmetricCipherKeyPair
import org.bouncycastle.crypto.params.ECPublicKeyParameters
import org.bouncycastle.util.encoders.Hex
import io.iohk.ethereum.network.p2p.messages.CommonMessages.SignedTransactions._
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody

import scala.concurrent.{ExecutionContext, Future}

Expand Down
Loading