Skip to content

Commit 2b8c75b

Browse files
author
Igor Grahovac
committed
ETCM-697: Transaction get log hash implemented
1 parent 1a430fd commit 2b8c75b

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ case class JsonRpcController(
247247
handle[RewindToBlockRequest, RewindToBlockResponse](testService.rewindToBlock, req)
248248
case req @ JsonRpcRequest(_, "test_importRawBlock", _, _) =>
249249
handle[ImportRawBlockRequest, ImportRawBlockResponse](testService.importRawBlock, req)
250+
case req @ JsonRpcRequest(_, "test_getLogHash", _, _) =>
251+
handle[GetLogHashRequest, GetLogHashResponse](testService.getLogHash, req)
250252
case req @ JsonRpcRequest(_, "miner_setEtherbase", _, _) =>
251253
handle[SetEtherbaseRequest, SetEtherbaseResponse](testService.setEtherbase, req)
252254
case req @ JsonRpcRequest(_, "debug_accountRange", _, _) =>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,17 @@ object TestJsonMethodsImplicits extends JsonMethodsImplicits {
216216

217217
override def encodeJson(t: StorageRangeResponse): JValue = Extraction.decompose(t)
218218
}
219+
220+
implicit val test_getLogHash = new JsonMethodDecoder[GetLogHashRequest] with JsonEncoder[GetLogHashResponse] {
221+
def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetLogHashRequest] =
222+
params match {
223+
case Some(JArray(JString(transactionHashString) :: Nil)) =>
224+
for {
225+
transactionHash <- extractHash(transactionHashString)
226+
} yield GetLogHashRequest(transactionHash)
227+
case _ => Left(InvalidParams())
228+
}
229+
230+
override def encodeJson(t: GetLogHashResponse): JValue = encodeAsHex(t.logHash)
231+
}
219232
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import akka.util.{ByteString, Timeout}
55
import io.iohk.ethereum.blockchain.data.{GenesisAccount, GenesisData, GenesisDataLoader}
66
import io.iohk.ethereum.consensus.ConsensusConfig
77
import io.iohk.ethereum.consensus.blocks._
8-
import io.iohk.ethereum.crypto
8+
import io.iohk.ethereum.{crypto, rlp}
99
import io.iohk.ethereum.domain.Block._
1010
import io.iohk.ethereum.domain.{Address, Block, BlockchainImpl, UInt256}
1111
import io.iohk.ethereum.ledger._
@@ -17,7 +17,8 @@ import monix.eval.Task
1717
import monix.execution.Scheduler
1818
import org.bouncycastle.util.encoders.Hex
1919
import io.iohk.ethereum.jsonrpc.JsonMethodsImplicits._
20-
import io.iohk.ethereum.ledger.InMemoryWorldStateProxy.persistState
20+
21+
import io.iohk.ethereum.rlp.RLPList
2122

2223
import scala.concurrent.duration._
2324
import scala.util.{Failure, Success, Try}
@@ -98,6 +99,9 @@ object TestService {
9899

99100
case class StorageRangeRequest(parameters: StorageRangeParams)
100101
case class StorageRangeResponse(complete: Boolean, storage: Map[ByteString, StorageEntry])
102+
103+
case class GetLogHashRequest(transactionHash: ByteString)
104+
case class GetLogHashResponse(logHash: ByteString)
101105
}
102106

103107
class TestService(
@@ -340,4 +344,21 @@ class TestService(
340344
)
341345
)
342346
}
347+
348+
def getLogHash(request: GetLogHashRequest): ServiceResponse[GetLogHashResponse] = {
349+
import io.iohk.ethereum.network.p2p.messages.PV63.TxLogEntryImplicits._
350+
351+
val result = for {
352+
transactionLocation <- blockchain.getTransactionLocation(request.transactionHash)
353+
block <- blockchain.getBlockByHash(transactionLocation.blockHash)
354+
_ <- block.body.transactionList.lift(transactionLocation.txIndex)
355+
receipts <- blockchain.getReceiptsByHash(block.header.hash)
356+
logs = receipts.flatMap(receipt => receipt.logs)
357+
rlpList: RLPList = RLPList(logs.map(_.toRLPEncodable).toList: _*)
358+
} yield ByteString(crypto.kec256(rlp.encode(rlpList)))
359+
360+
result
361+
.map(rlpHash => Task.now(Right(GetLogHashResponse(rlpHash))))
362+
.getOrElse(Task.now(Right(GetLogHashResponse(ByteString(crypto.kec256(rlp.encode(RLPList(List(): _*))))))))
363+
}
343364
}

src/main/scala/io/iohk/ethereum/ledger/BlockPreparator.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class BlockPreparator(
6060
block: Block,
6161
worldStateProxy: InMemoryWorldStateProxy
6262
): InMemoryWorldStateProxy = {
63-
if (!Config.testmode) {
63+
if (Config.testmode) {
64+
worldStateProxy
65+
} else {
6466
val blockNumber = block.header.number
6567

6668
val minerRewardForBlock = blockRewardCalculator.calculateMiningRewardForBlock(blockNumber)
@@ -104,8 +106,7 @@ class BlockPreparator(
104106
log.debug(s"Paying block $blockNumber reward of $ommerReward to ommer with account address $ommerAddress")
105107
increaseAccountBalance(ommerAddress, UInt256(ommerReward))(ws)
106108
}
107-
} else
108-
worldStateProxy
109+
}
109110
}
110111

111112
/**

src/main/scala/io/iohk/ethereum/testmode/TestmodeConsensus.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class TestmodeConsensus(
6565
}
6666
}
6767

68-
override def validators: Validators = ValidatorsExecutor.apply(blockchainConfig, Protocol.Ethash)
68+
override def validators: Validators = ValidatorsExecutor.apply(blockchainConfig, Protocol.MockedPow)
6969

7070
override val blockPreparator: BlockPreparator = new BlockPreparator(
7171
vm = vm,

0 commit comments

Comments
 (0)