Skip to content

Commit 3664a49

Browse files
committed
[ETCM-533] refactoring based on review notes
1 parent 6b6c4d8 commit 3664a49

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

src/main/scala/io/iohk/ethereum/domain/Blockchain.scala

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,9 @@ class BlockchainImpl(
319319
if (ethCompatibleStorage) domain.EthereumUInt256Mpt.storageMpt(rootHash, storage)
320320
else domain.ArbitraryIntegerMpt.storageMpt(rootHash, storage)
321321
}
322-
val value = mpt.get(position)
323-
val proof = mpt.getProof(position)
324-
325-
(value, proof) match {
326-
case (Some(value), Some(proof)) => StorageValueProof(position, value, proof)
327-
case (None, Some(proof)) => StorageValueProof(position, proof = proof)
328-
case (Some(value), None) => StorageValueProof(position, value)
329-
case (None, None) => StorageValueProof(position)
330-
}
322+
val value: Option[BigInt] = mpt.get(position)
323+
val proof: Option[Vector[MptNode]] = mpt.getProof(position)
324+
StorageValueProof(position, value, proof)
331325
}
332326

333327
private def persistBestBlocksData(): Unit = {

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import akka.util.ByteString
44
import cats.implicits._
55
import io.iohk.ethereum.consensus.blocks.BlockGenerator
66
import io.iohk.ethereum.domain.{Account, Address, Block, Blockchain, UInt256}
7+
import io.iohk.ethereum.jsonrpc.ProofService.StorageValueProof.asRlpSerializedNode
78
import io.iohk.ethereum.jsonrpc.ProofService.{GetProofRequest, GetProofResponse, ProofAccount, StorageProof, StorageProofKey, StorageValueProof}
89
import io.iohk.ethereum.mpt.{MptNode, MptTraversals}
910
import monix.eval.Task
@@ -24,7 +25,7 @@ object ProofService {
2425

2526
case class GetProofResponse(proofAccount: ProofAccount)
2627

27-
trait StorageProof {
28+
sealed trait StorageProof {
2829
val key: StorageProofKey
2930
val value: BigInt
3031
val proof: Seq[ByteString]
@@ -38,12 +39,20 @@ object ProofService {
3839
*/
3940
case class StorageValueProof(
4041
key: StorageProofKey,
41-
value: BigInt,
42-
proof: Seq[ByteString]) extends StorageProof
42+
value: BigInt = BigInt(0),
43+
proof: Seq[ByteString] = Seq.empty[MptNode].map(asRlpSerializedNode)) extends StorageProof
4344

4445
object StorageValueProof {
45-
def apply(key: BigInt, value: BigInt = BigInt(0), proof: => Seq[MptNode] = Seq.empty[MptNode]): StorageValueProof =
46-
new StorageValueProof(StorageProofKey(key), value, proof.map(asRlpSerializedNode))
46+
def apply(position: BigInt, value: Option[BigInt], proof: Option[Vector[MptNode]]): StorageValueProof =
47+
(value, proof) match {
48+
case (Some(value), Some(proof)) => new StorageValueProof(key = StorageProofKey(position), value = value, proof = proof.map(asRlpSerializedNode))
49+
case (None, Some(proof)) => new StorageValueProof(key = StorageProofKey(position), proof = proof.map(asRlpSerializedNode))
50+
case (Some(value), None) => new StorageValueProof(key = StorageProofKey(position), value = value)
51+
case (None, None) => new StorageValueProof(key = StorageProofKey(position))
52+
}
53+
54+
def apply(position: BigInt): StorageValueProof =
55+
new StorageValueProof(key = StorageProofKey(position))
4756

4857
def asRlpSerializedNode(node: MptNode): ByteString =
4958
ByteString(MptTraversals.encodeNode(node))

src/main/scala/io/iohk/ethereum/mpt/MerklePatriciaTrie.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,14 @@ class MerklePatriciaTrie[K, V] private (private[mpt] val rootNode: Option[MptNod
8282
* @throws io.iohk.ethereum.mpt.MerklePatriciaTrie.MPTException if there is any inconsistency in how the trie is build.
8383
*/
8484
def get(key: K): Option[V] = {
85-
pathTraverse[Option[V]](None, mkKeyNibbles(key)) { case (_, node) =>
86-
node match {
87-
case Some(LeafNode(_, value, _, _, _)) =>
88-
Some(vSerializer.fromBytes(value.toArray[Byte]))
85+
pathTraverse[Option[V]](None, mkKeyNibbles(key)) {
86+
case (_, Some(LeafNode(_, value, _, _, _))) =>
87+
Some(vSerializer.fromBytes(value.toArray[Byte]))
8988

90-
case Some(BranchNode(_, terminator, _, _, _)) =>
91-
terminator.map(term => vSerializer.fromBytes(term.toArray[Byte]))
89+
case (_, Some(BranchNode(_, terminator, _, _, _))) =>
90+
terminator.map(term => vSerializer.fromBytes(term.toArray[Byte]))
9291

93-
case _ => None
94-
}
92+
case _ => None
9593
}.flatten
9694
}
9795

0 commit comments

Comments
 (0)