Skip to content

Commit

Permalink
[ETCM-533] refactoring based on review notes
Browse files Browse the repository at this point in the history
  • Loading branch information
bsuieric committed Jan 26, 2021
1 parent 465b721 commit 3364789
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
12 changes: 3 additions & 9 deletions src/main/scala/io/iohk/ethereum/domain/Blockchain.scala
Expand Up @@ -319,15 +319,9 @@ class BlockchainImpl(
if (ethCompatibleStorage) domain.EthereumUInt256Mpt.storageMpt(rootHash, storage)
else domain.ArbitraryIntegerMpt.storageMpt(rootHash, storage)
}
val value = mpt.get(position)
val proof = mpt.getProof(position)

(value, proof) match {
case (Some(value), Some(proof)) => StorageValueProof(position, value, proof)
case (None, Some(proof)) => StorageValueProof(position, proof = proof)
case (Some(value), None) => StorageValueProof(position, value)
case (None, None) => StorageValueProof(position)
}
val value: Option[BigInt] = mpt.get(position)
val proof: Option[Vector[MptNode]] = mpt.getProof(position)
StorageValueProof(position, value, proof)
}

private def persistBestBlocksData(): Unit = {
Expand Down
19 changes: 14 additions & 5 deletions src/main/scala/io/iohk/ethereum/jsonrpc/EthProofService.scala
Expand Up @@ -4,6 +4,7 @@ import akka.util.ByteString
import cats.implicits._
import io.iohk.ethereum.consensus.blocks.BlockGenerator
import io.iohk.ethereum.domain.{Account, Address, Block, Blockchain, UInt256}
import io.iohk.ethereum.jsonrpc.ProofService.StorageValueProof.asRlpSerializedNode
import io.iohk.ethereum.jsonrpc.ProofService.{GetProofRequest, GetProofResponse, ProofAccount, StorageProof, StorageProofKey, StorageValueProof}
import io.iohk.ethereum.mpt.{MptNode, MptTraversals}
import monix.eval.Task
Expand All @@ -24,7 +25,7 @@ object ProofService {

case class GetProofResponse(proofAccount: ProofAccount)

trait StorageProof {
sealed trait StorageProof {
val key: StorageProofKey
val value: BigInt
val proof: Seq[ByteString]
Expand All @@ -38,12 +39,20 @@ object ProofService {
*/
case class StorageValueProof(
key: StorageProofKey,
value: BigInt,
proof: Seq[ByteString]) extends StorageProof
value: BigInt = BigInt(0),
proof: Seq[ByteString] = Seq.empty[MptNode].map(asRlpSerializedNode)) extends StorageProof

object StorageValueProof {
def apply(key: BigInt, value: BigInt = BigInt(0), proof: => Seq[MptNode] = Seq.empty[MptNode]): StorageValueProof =
new StorageValueProof(StorageProofKey(key), value, proof.map(asRlpSerializedNode))
def apply(position: BigInt, value: Option[BigInt], proof: Option[Vector[MptNode]]): StorageValueProof =
(value, proof) match {
case (Some(value), Some(proof)) => new StorageValueProof(key = StorageProofKey(position), value = value, proof = proof.map(asRlpSerializedNode))
case (None, Some(proof)) => new StorageValueProof(key = StorageProofKey(position), proof = proof.map(asRlpSerializedNode))
case (Some(value), None) => new StorageValueProof(key = StorageProofKey(position), value = value)
case (None, None) => new StorageValueProof(key = StorageProofKey(position))
}

def apply(position: BigInt): StorageValueProof =
new StorageValueProof(key = StorageProofKey(position))

def asRlpSerializedNode(node: MptNode): ByteString =
ByteString(MptTraversals.encodeNode(node))
Expand Down
14 changes: 6 additions & 8 deletions src/main/scala/io/iohk/ethereum/mpt/MerklePatriciaTrie.scala
Expand Up @@ -82,16 +82,14 @@ class MerklePatriciaTrie[K, V] private (private[mpt] val rootNode: Option[MptNod
* @throws io.iohk.ethereum.mpt.MerklePatriciaTrie.MPTException if there is any inconsistency in how the trie is build.
*/
def get(key: K): Option[V] = {
pathTraverse[Option[V]](None, mkKeyNibbles(key)) { case (_, node) =>
node match {
case Some(LeafNode(_, value, _, _, _)) =>
Some(vSerializer.fromBytes(value.toArray[Byte]))
pathTraverse[Option[V]](None, mkKeyNibbles(key)) {
case (_, Some(LeafNode(_, value, _, _, _))) =>
Some(vSerializer.fromBytes(value.toArray[Byte]))

case Some(BranchNode(_, terminator, _, _, _)) =>
terminator.map(term => vSerializer.fromBytes(term.toArray[Byte]))
case (_, Some(BranchNode(_, terminator, _, _, _))) =>
terminator.map(term => vSerializer.fromBytes(term.toArray[Byte]))

case _ => None
}
case _ => None
}.flatten
}

Expand Down

0 comments on commit 3364789

Please sign in to comment.