Skip to content

Commit

Permalink
Merge branch 'phase/release2_0' into feature/ec-588_more_efficient_se…
Browse files Browse the repository at this point in the history
…rializer_for_ReceiptStorage
  • Loading branch information
Agnieszka Kowal committed Oct 23, 2018
2 parents 8778629 + 3891aac commit e998892
Show file tree
Hide file tree
Showing 29 changed files with 2,329 additions and 1,954 deletions.
22 changes: 15 additions & 7 deletions src/it/scala/io/iohk/ethereum/txExecTest/ContractTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package io.iohk.ethereum.txExecTest
import java.util.concurrent.Executors

import io.iohk.ethereum.domain.Receipt
import io.iohk.ethereum.ledger.{Ledger, LedgerImpl}
import io.iohk.ethereum.ledger._
import io.iohk.ethereum.txExecTest.util.FixtureProvider
import io.iohk.ethereum.utils.Config.SyncConfig
import io.iohk.ethereum.utils.{BlockchainConfig, Config}
import org.scalatest.{FlatSpec, Matchers}
import io.iohk.ethereum.utils.{ BlockchainConfig, Config }
import org.scalatest.{ FlatSpec, Matchers }

import scala.concurrent.ExecutionContext

Expand All @@ -24,7 +24,9 @@ class ContractTest extends FlatSpec with Matchers {
val testBlockchainStorages = FixtureProvider.prepareStorages(0, fixtures)

//block only with ether transfers
new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus, ec).executeBlock(fixtures.blockByNumber(1)) shouldBe noErrors
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
blockExecution.executeBlock(fixtures.blockByNumber(1)) shouldBe noErrors
}

it should "deploy contract" in new ScenarioSetup {
Expand All @@ -33,7 +35,9 @@ class ContractTest extends FlatSpec with Matchers {
val testBlockchainStorages = FixtureProvider.prepareStorages(1, fixtures)

//contract creation
new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus, ec).executeBlock(fixtures.blockByNumber(2)) shouldBe noErrors
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
blockExecution.executeBlock(fixtures.blockByNumber(2)) shouldBe noErrors
}

it should "execute contract call" in new ScenarioSetup {
Expand All @@ -42,7 +46,9 @@ class ContractTest extends FlatSpec with Matchers {
val testBlockchainStorages = FixtureProvider.prepareStorages(2, fixtures)

//block with ether transfers and contract call
new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus, ec).executeBlock(fixtures.blockByNumber(3)) shouldBe noErrors
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
blockExecution.executeBlock(fixtures.blockByNumber(3)) shouldBe noErrors
}

it should "execute contract that pays 2 accounts" in new ScenarioSetup {
Expand All @@ -51,6 +57,8 @@ class ContractTest extends FlatSpec with Matchers {
val testBlockchainStorages = FixtureProvider.prepareStorages(2, fixtures)

//block contains contract paying 2 accounts
new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus, ec).executeBlock(fixtures.blockByNumber(3)) shouldBe noErrors
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
blockExecution.executeBlock(fixtures.blockByNumber(3)) shouldBe noErrors
}
}
14 changes: 7 additions & 7 deletions src/it/scala/io/iohk/ethereum/txExecTest/ECIP1017Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package io.iohk.ethereum.txExecTest

import java.util.concurrent.Executors

import io.iohk.ethereum.domain.{BlockchainImpl, Receipt, UInt256}
import io.iohk.ethereum.ledger.{Ledger, LedgerImpl}
import io.iohk.ethereum.domain.{ BlockchainImpl, Receipt, UInt256 }
import io.iohk.ethereum.ledger._
import io.iohk.ethereum.txExecTest.util.FixtureProvider
import io.iohk.ethereum.utils.{BlockchainConfig, DaoForkConfig, MonetaryPolicyConfig}
import org.scalatest.{FlatSpec, Matchers}
import io.iohk.ethereum.utils.{ BlockchainConfig, DaoForkConfig, MonetaryPolicyConfig }
import org.scalatest.{ FlatSpec, Matchers }

import scala.concurrent.ExecutionContext

Expand Down Expand Up @@ -64,9 +64,9 @@ class ECIP1017Test extends FlatSpec with Matchers {
(startBlock to endBlock) foreach { blockToExecute =>
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
val blockchain = BlockchainImpl(storages)
val ledger = new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus, ec)

ledger.executeBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
blockExecution.executeBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/it/scala/io/iohk/ethereum/txExecTest/ForksTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package io.iohk.ethereum.txExecTest

import java.util.concurrent.Executors

import io.iohk.ethereum.domain.{BlockchainImpl, Receipt, UInt256}
import io.iohk.ethereum.ledger.LedgerImpl
import io.iohk.ethereum.domain.{ BlockchainImpl, Receipt, UInt256 }
import io.iohk.ethereum.ledger.{ BlockExecution, BlockQueue, BlockValidation }
import io.iohk.ethereum.txExecTest.util.FixtureProvider
import io.iohk.ethereum.utils.{BlockchainConfig, DaoForkConfig, MonetaryPolicyConfig}
import org.scalatest.{FlatSpec, Matchers}
import io.iohk.ethereum.utils.{ BlockchainConfig, DaoForkConfig, MonetaryPolicyConfig }
import org.scalatest.{ FlatSpec, Matchers }

import scala.concurrent.ExecutionContext

Expand Down Expand Up @@ -53,9 +53,9 @@ class ForksTest extends FlatSpec with Matchers {
(startBlock to endBlock) foreach { blockToExecute =>
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
val blockchain = BlockchainImpl(storages)
val ledger = new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus, ec)

ledger.executeBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
blockExecution.executeBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ trait Validators {
getNBlocksBack: GetNBlocksBack
): Either[ValidationBeforeExecError, BlockExecutionSuccess]

/**
* This function validates that the various results from execution are consistent with the block. This includes:
* - Validating the resulting stateRootHash
* - Doing BlockValidator.validateBlockReceipts validations involving the receipts
* - Validating the resulting gas used
*
* @note This method was originally provided by the [[io.iohk.ethereum.ledger.Ledger Ledger]].
*
* @param block to validate
* @param stateRootHash from the resulting state trie after executing the txs from the block
* @param receipts associated with the execution of each of the tx from the block
* @param gasUsed, accumulated gas used for the execution of the txs from the block
* @return None if valid else a message with what went wrong
*/
/** This function validates that the various results from execution are consistent with the block. This includes:
* - Validating the resulting stateRootHash
* - Doing BlockValidator.validateBlockReceipts validations involving the receipts
* - Validating the resulting gas used
*
* @note This method was originally provided by the [[io.iohk.ethereum.ledger.Ledger]].
*
* @param block to validate
* @param stateRootHash from the resulting state trie after executing the txs from the block
* @param receipts associated with the execution of each of the tx from the block
* @param gasUsed accumulated gas used for the execution of the txs from the block
* @return None if valid else a message with what went wrong
*/
def validateBlockAfterExecution(
block: Block,
stateRootHash: ByteString,
Expand Down
66 changes: 30 additions & 36 deletions src/main/scala/io/iohk/ethereum/jsonrpc/EthService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package io.iohk.ethereum.jsonrpc
import java.time.Duration
import java.util.Date
import java.util.concurrent.atomic.AtomicReference
import java.util.function.UnaryOperator

import akka.actor.ActorRef
import akka.pattern.ask
import akka.util.{ByteString, Timeout}
import akka.util.{ ByteString, Timeout }
import io.iohk.ethereum.blockchain.sync.RegularSync
import io.iohk.ethereum.consensus.ConsensusConfig
import io.iohk.ethereum.crypto._
import io.iohk.ethereum.db.storage.AppStateStorage
import io.iohk.ethereum.db.storage.TransactionMappingStorage.TransactionLocation
import io.iohk.ethereum.domain.{BlockHeader, SignedTransaction, UInt256, _}
import io.iohk.ethereum.jsonrpc.FilterManager.{FilterChanges, FilterLogs, LogFilterLogs, TxLog}
import io.iohk.ethereum.domain.{ BlockHeader, SignedTransaction, UInt256, _ }
import io.iohk.ethereum.jsonrpc.FilterManager.{ FilterChanges, FilterLogs, LogFilterLogs, TxLog }
import io.iohk.ethereum.jsonrpc.JsonRpcController.JsonRpcConfig
import io.iohk.ethereum.keystore.KeyStore
import io.iohk.ethereum.ledger.{InMemoryWorldStateProxy, Ledger}
import io.iohk.ethereum.ledger.{ InMemoryWorldStateProxy, Ledger, StxLedger }
import io.iohk.ethereum.ommers.OmmersPool
import io.iohk.ethereum.rlp
import io.iohk.ethereum.rlp.RLPImplicitConversions._
Expand All @@ -32,8 +31,8 @@ import org.bouncycastle.util.encoders.Hex
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import scala.util.{Failure, Success, Try}
import scala.language.existentials
import scala.util.{ Failure, Success, Try }

// scalastyle:off number.of.methods number.of.types file.size.limit
object EthService {
Expand Down Expand Up @@ -190,6 +189,7 @@ class EthService(
blockchain: Blockchain,
appStateStorage: AppStateStorage,
ledger: Ledger,
stxLedger: StxLedger,
keyStore: KeyStore,
pendingTransactionsManager: ActorRef,
syncingController: ActorRef,
Expand All @@ -213,7 +213,7 @@ class EthService(
private[this] def consensusConfig: ConsensusConfig = fullConsensusConfig.generic

private[this] def ifEthash[Req, Res](req: Req)(f: Req Res): ServiceResponse[Res] = {
@inline def F[A](x: A) = Future.successful(x)
@inline def F[A](x: A): Future[A] = Future.successful(x)
consensus.ifEthash[ServiceResponse[Res]](_ F(Right(f(req))))(F(Left(JsonRpcErrors.ConsensusIsNotEthash)))
}

Expand Down Expand Up @@ -404,11 +404,9 @@ class EthService(
def submitHashRate(req: SubmitHashRateRequest): ServiceResponse[SubmitHashRateResponse] =
ifEthash(req) { req
reportActive()
hashRate.updateAndGet(new UnaryOperator[Map[ByteString, (BigInt, Date)]] {
override def apply(t: Map[ByteString, (BigInt, Date)]): Map[ByteString, (BigInt, Date)] = {
val now = new Date
removeObsoleteHashrates(now, t + (req.id -> (req.hashRate, now)))
}
hashRate.updateAndGet((t: Map[ByteString, (BigInt, Date)]) => {
val now = new Date
removeObsoleteHashrates(now, t + (req.id -> (req.hashRate, now)))
})

SubmitHashRateResponse(true)
Expand All @@ -433,29 +431,25 @@ class EthService(
}

def getMining(req: GetMiningRequest): ServiceResponse[GetMiningResponse] =
ifEthash(req) { req
val isMining = lastActive.updateAndGet(new UnaryOperator[Option[Date]] {
override def apply(e: Option[Date]): Option[Date] = {
e.filter {
time => Duration.between(time.toInstant, (new Date).toInstant).toMillis < jsonRpcConfig.minerActiveTimeout.toMillis
}
ifEthash(req) { _
val isMining = lastActive.updateAndGet((e: Option[Date]) => {
e.filter{
time => Duration.between(time.toInstant, (new Date).toInstant).toMillis < jsonRpcConfig.minerActiveTimeout.toMillis
}
}).isDefined

GetMiningResponse(isMining)
}

private def reportActive() = {
private def reportActive(): Option[Date] = {
val now = new Date()
lastActive.updateAndGet(_ Some(now))
}

def getHashRate(req: GetHashRateRequest): ServiceResponse[GetHashRateResponse] =
ifEthash(req) { req
val hashRates: Map[ByteString, (BigInt, Date)] = hashRate.updateAndGet(new UnaryOperator[Map[ByteString, (BigInt, Date)]] {
override def apply(t: Map[ByteString, (BigInt, Date)]): Map[ByteString, (BigInt, Date)] = {
removeObsoleteHashrates(new Date, t)
}
ifEthash(req) { _
val hashRates: Map[ByteString, (BigInt, Date)] = hashRate.updateAndGet((t: Map[ByteString, (BigInt, Date)]) => {
removeObsoleteHashrates(new Date, t)
})

//sum all reported hashRates
Expand All @@ -472,7 +466,7 @@ class EthService(
def getWork(req: GetWorkRequest): ServiceResponse[GetWorkResponse] =
consensus.ifEthash(ethash {
reportActive()
import io.iohk.ethereum.consensus.ethash.EthashUtils.{seed, epoch}
import io.iohk.ethereum.consensus.ethash.EthashUtils.{ epoch, seed }

val bestBlock = blockchain.getBestBlock()
getOmmersFromPool(bestBlock.header.number + 1).zip(getTransactionsFromPool).map {
Expand All @@ -495,7 +489,7 @@ class EthService(
private def getOmmersFromPool(blockNumber: BigInt): Future[OmmersPool.Ommers] =
consensus.ifEthash(ethash {
val miningConfig = ethash.config.specific
implicit val timeout = Timeout(miningConfig.ommerPoolQueryTimeout)
implicit val timeout: Timeout = Timeout(miningConfig.ommerPoolQueryTimeout)

(ommersPool ? OmmersPool.GetOmmers(blockNumber)).mapTo[OmmersPool.Ommers]
.recover { case ex =>
Expand All @@ -506,7 +500,7 @@ class EthService(

// TODO This seems to be re-implemented elsewhere, probably move to a better place? Also generalize the error message.
private def getTransactionsFromPool: Future[PendingTransactionsResponse] = {
implicit val timeout = Timeout(getTransactionFromPoolTimeout)
implicit val timeout: Timeout = Timeout(getTransactionFromPoolTimeout)

(pendingTransactionsManager ? PendingTransactionsManager.GetPendingTransactions).mapTo[PendingTransactionsResponse]
.recover { case ex =>
Expand Down Expand Up @@ -573,7 +567,7 @@ class EthService(

def call(req: CallRequest): ServiceResponse[CallResponse] = {
Future {
doCall(req)(ledger.simulateTransaction).map(r => CallResponse(r.vmReturnData))
doCall(req)(stxLedger.simulateTransaction).map(r => CallResponse(r.vmReturnData))
}
}

Expand All @@ -597,7 +591,7 @@ class EthService(

def estimateGas(req: CallRequest): ServiceResponse[EstimateGasResponse] = {
Future {
doCall(req)(ledger.binarySearchGasEstimation).map(gasUsed => EstimateGasResponse(gasUsed))
doCall(req)(stxLedger.binarySearchGasEstimation).map(gasUsed => EstimateGasResponse(gasUsed))
}
}

Expand Down Expand Up @@ -680,7 +674,7 @@ class EthService(
}

def newFilter(req: NewFilterRequest): ServiceResponse[NewFilterResponse] = {
implicit val timeout = Timeout(filterConfig.filterManagerQueryTimeout)
implicit val timeout: Timeout = Timeout(filterConfig.filterManagerQueryTimeout)

import req.filter._
(filterManager ? FilterManager.NewLogFilter(fromBlock, toBlock, address, topics)).mapTo[FilterManager.NewFilterResponse].map { resp =>
Expand All @@ -689,47 +683,47 @@ class EthService(
}

def newBlockFilter(req: NewBlockFilterRequest): ServiceResponse[NewFilterResponse] = {
implicit val timeout = Timeout(filterConfig.filterManagerQueryTimeout)
implicit val timeout: Timeout = Timeout(filterConfig.filterManagerQueryTimeout)

(filterManager ? FilterManager.NewBlockFilter).mapTo[FilterManager.NewFilterResponse].map { resp =>
Right(NewFilterResponse(resp.id))
}
}

def newPendingTransactionFilter(req: NewPendingTransactionFilterRequest): ServiceResponse[NewFilterResponse] = {
implicit val timeout = Timeout(filterConfig.filterManagerQueryTimeout)
implicit val timeout: Timeout = Timeout(filterConfig.filterManagerQueryTimeout)

(filterManager ? FilterManager.NewPendingTransactionFilter).mapTo[FilterManager.NewFilterResponse].map { resp =>
Right(NewFilterResponse(resp.id))
}
}

def uninstallFilter(req: UninstallFilterRequest): ServiceResponse[UninstallFilterResponse] = {
implicit val timeout = Timeout(filterConfig.filterManagerQueryTimeout)
implicit val timeout: Timeout = Timeout(filterConfig.filterManagerQueryTimeout)

(filterManager ? FilterManager.UninstallFilter(req.filterId)).map { _ =>
Right(UninstallFilterResponse(success = true))
}
}

def getFilterChanges(req: GetFilterChangesRequest): ServiceResponse[GetFilterChangesResponse] = {
implicit val timeout = Timeout(filterConfig.filterManagerQueryTimeout)
implicit val timeout: Timeout = Timeout(filterConfig.filterManagerQueryTimeout)

(filterManager ? FilterManager.GetFilterChanges(req.filterId)).mapTo[FilterManager.FilterChanges].map { filterChanges =>
Right(GetFilterChangesResponse(filterChanges))
}
}

def getFilterLogs(req: GetFilterLogsRequest): ServiceResponse[GetFilterLogsResponse] = {
implicit val timeout = Timeout(filterConfig.filterManagerQueryTimeout)
implicit val timeout: Timeout = Timeout(filterConfig.filterManagerQueryTimeout)

(filterManager ? FilterManager.GetFilterLogs(req.filterId)).mapTo[FilterManager.FilterLogs].map { filterLogs =>
Right(GetFilterLogsResponse(filterLogs))
}
}

def getLogs(req: GetLogsRequest): ServiceResponse[GetLogsResponse] = {
implicit val timeout = Timeout(filterConfig.filterManagerQueryTimeout)
implicit val timeout: Timeout = Timeout(filterConfig.filterManagerQueryTimeout)
import req.filter._

(filterManager ? FilterManager.GetLogs(fromBlock, toBlock, address, topics)).mapTo[FilterManager.LogFilterLogs].map { filterLogs =>
Expand Down

0 comments on commit e998892

Please sign in to comment.