Skip to content

Commit

Permalink
Merge branch 'phase/release2_0' into refactor/ec-320_regularsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnieszka Kowal committed Oct 1, 2018
2 parents 9dcae83 + ba71c60 commit 27f705e
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class MerklePatriciaTreeSpeedSpec extends FunSuite
}
val rootHash = Hex.toHexString(trieResult.getRootHash)

log.debug("Time taken(ms): " + (System.currentTimeMillis - start))
log.debug("Root hash obtained: " + rootHash)
log.info("Time taken(ms): " + (System.currentTimeMillis - start))
log.info("Root hash obtained: " + rootHash)

if (Symmetric) assert(rootHash.take(4) == "36f6" && rootHash.drop(rootHash.length - 4) == "93a3")
else assert(rootHash.take(4) == "da8a" && rootHash.drop(rootHash.length - 4) == "0ca4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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, LeafNode, MptNode}
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}
import io.iohk.ethereum.network.PeerEventBusActor.SubscriptionClassifier.MessageClassifier
Expand Down Expand Up @@ -111,10 +111,10 @@ class DumpChainActor(peerManager: ActorRef, peerMessageBus: ActorRef, startBlock
val nodes = NodeData(stateNodes).values.indices.map(i => NodeData(stateNodes).getMptNode(i))

val children = nodes.flatMap {
case n: BranchNode => n.children.collect { case Some(Left(h)) => h }
case ExtensionNode(_, Left(h), _, _) => Seq(h)
case n: BranchNode => n.children.collect { case HashNode(h) => h }
case ExtensionNode(_, HashNode(h), _, _) => Seq(h)
case _: LeafNode => Seq.empty
case _ => Seq.empty
case _ => Seq.empty
}

var contractChildren: Seq[ByteString] = Nil
Expand Down Expand Up @@ -142,8 +142,8 @@ class DumpChainActor(peerManager: ActorRef, peerMessageBus: ActorRef, startBlock

val cNodes = NodeData(contractNodes).values.indices.map(i => NodeData(contractNodes).getMptNode(i))
contractChildren = contractChildren ++ cNodes.flatMap {
case n: BranchNode => n.children.collect { case Some(Left(h)) => h }
case ExtensionNode(_, Left(h), _, _) => Seq(h)
case n: BranchNode => n.children.collect { case HashNode(h) => h }
case ExtensionNode(_, HashNode(h), _, _) => Seq(h)
case _ => Seq.empty
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import MptNodeEncoders._
import ReceiptImplicits._
import io.iohk.ethereum.db.cache.MapCaches
import io.iohk.ethereum.db.storage.pruning.{ArchivePruning, PruningMode}
import io.iohk.ethereum.mpt.{BranchNode, ExtensionNode, LeafNode, MptNode}
import io.iohk.ethereum.mpt.{BranchNode, ExtensionNode, HashNode, LeafNode, MptNode}
import org.bouncycastle.util.encoders.Hex

import scala.io.Source
Expand Down Expand Up @@ -63,12 +63,12 @@ object FixtureProvider {
def traverse(nodeHash: ByteString): Unit = fixtures.stateMpt.get(nodeHash).orElse(fixtures.contractMpts.get(nodeHash)) match {
case Some(m: BranchNode) =>
blockchain.nodesKeyValueStorageFor(Some(block.header.number), storages.nodeStorage).update(Nil, Seq(ByteString(m.hash) -> m.toBytes))
m.children.collect { case Some(Left(hash)) => hash}.foreach(e => traverse(e))
m.children.collect { case HashNode(hash) => hash}.foreach(e => traverse(e))

case Some(m: ExtensionNode) =>
blockchain.nodesKeyValueStorageFor(Some(block.header.number), storages.nodeStorage).update(Nil, Seq(ByteString(m.hash) -> m.toBytes))
m.next match {
case Left(hash) if hash.nonEmpty => traverse(hash)
case HashNode(hash) if hash.nonEmpty => traverse(hash)
case _ =>
}

Expand All @@ -84,7 +84,8 @@ object FixtureProvider {
}
}

case None =>
case _ =>

}

traverse(block.header.stateRoot)
Expand Down
22 changes: 13 additions & 9 deletions src/main/scala/io/iohk/ethereum/blockchain/sync/FastSync.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.iohk.ethereum.consensus.validators.Validators
import io.iohk.ethereum.crypto.kec256
import io.iohk.ethereum.db.storage.{AppStateStorage, FastSyncStateStorage}
import io.iohk.ethereum.domain._
import io.iohk.ethereum.mpt.{BranchNode, ExtensionNode, LeafNode, MptNode}
import io.iohk.ethereum.mpt.{BranchNode, ExtensionNode, HashNode, LeafNode, MptNode}
import io.iohk.ethereum.network.Peer
import io.iohk.ethereum.network.p2p.messages.PV62._
import io.iohk.ethereum.network.p2p.messages.PV63.MptNodeEncoders._
Expand Down Expand Up @@ -457,15 +457,17 @@ class FastSync(
evmRequests ++ storageRequests

case n: BranchNode =>
val hashes = n.children.collect { case Some(Left(childHash)) => childHash }
val hashes = n.children.collect { case HashNode(childHash) => childHash }
blockchain.saveFastSyncNode(ByteString(n.hash), n.toBytes, syncState.targetBlock.number)
hashes.map(e => StateMptNodeHash(e))

case n: ExtensionNode =>
blockchain.saveFastSyncNode(ByteString(n.hash), n.toBytes, syncState.targetBlock.number)
n.next.fold(
mptHash => Seq(StateMptNodeHash(mptHash)),
_ => Nil)
n.next match {
case HashNode(hashNode) => Seq(StateMptNodeHash(hashNode))
case _ => Nil
}
case _ => Nil
}

private def handleContractMptNode(mptNode: MptNode): Seq[HashType] = {
Expand All @@ -475,15 +477,17 @@ class FastSync(
Nil

case n: BranchNode =>
val hashes = n.children.collect { case Some(Left(childHash)) => childHash }
val hashes = n.children.collect { case HashNode(childHash) => childHash }
blockchain.saveFastSyncNode(ByteString(n.hash), n.toBytes, syncState.targetBlock.number)
hashes.map(e => ContractStorageMptNodeHash(e))

case n: ExtensionNode =>
blockchain.saveFastSyncNode(ByteString(n.hash), n.toBytes, syncState.targetBlock.number)
n.next.fold(
mptHash => Seq(ContractStorageMptNodeHash(mptHash)),
_ => Nil)
n.next match {
case HashNode(hashNode) => Seq(ContractStorageMptNodeHash(hashNode))
case _ => Nil
}
case _ => Nil
}
}

Expand Down
29 changes: 24 additions & 5 deletions src/main/scala/io/iohk/ethereum/mpt/HexPrefix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,44 @@ object HexPrefix {

/**
* Transforms an array of 8bit values to the corresponding array of 4bit values (hexadecimal format)
*
* Needs to be as fast possible, which requires usage of var's and mutable arrays.
* @param bytes byte[]
* @return array with each individual nibble
*
*/
def bytesToNibbles(bytes: Array[Byte]): Array[Byte] = {
bytes.foldRight[List[Byte]](List()){(elem, rec) =>
((elem >> 4) & 0xF).toByte :: (elem & 0xF).toByte :: rec}.toArray
val newArray = new Array[Byte](bytes.length * 2)
var i = 0
var n = 0
while (i < bytes.length) {
newArray(n) = ((bytes(i) >> 4) & 0xF).toByte
newArray(n + 1) = (bytes(i) & 0xF).toByte
n = n + 2
i = i + 1
}
newArray
}

/**
* Transforms an array of 4bit values (hexadecimal format) to the corresponding array of 8bit values
*
* Needs to be as fast possible, which requires usage of var's and mutable arrays.
* @param nibbles byte[]
* @return array with bytes combining pairs of nibbles
*
*/
def nibblesToBytes(nibbles: Array[Byte]): Array[Byte] = {
require(nibbles.length % 2 == 0)
nibbles.grouped(2).map{case Array(n1,n2) => (16*n1 + n2).toByte}.toArray
val newArray = new Array[Byte](nibbles.length / 2)
var i = 0
var n = 0

while (i < nibbles.length) {
val newValue = (16 * nibbles(i) + nibbles(i + 1)).toByte
newArray(n) = newValue
n = n + 1
i = i + 2
}

newArray
}
}
Loading

0 comments on commit 27f705e

Please sign in to comment.