Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ETCM-198] Update fastsync to request headers only from peers with be… #761

Merged
merged 3 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/it/scala/io/iohk/ethereum/sync/FastSyncItSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ class FastSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfterAll {
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber() - peer2.testSyncConfig.pivotBlockOffset)
}
}

it should "follow the longest chains" in customTestCaseResourceM(
FakePeer.start4FakePeersRes()
) { case (peer1, peer2, peer3, peer4) =>
for {
_ <- peer2.importBlocksUntil(1000)(IdentityUpdate)
_ <- peer3.importBlocksUntil(1000)(IdentityUpdate)
_ <- peer4.importBlocksUntil(1000)(IdentityUpdate)

_ <- peer2.importBlocksUntil(2000)(IdentityUpdate)
_ <- peer3.importBlocksUntil(3000)(updateStateAtBlock(1001, endAccount = 3000))
_ <- peer4.importBlocksUntil(3000)(updateStateAtBlock(1001, endAccount = 3000))

_ <- peer1.connectToPeers(Set(peer2.node, peer3.node, peer4.node))
_ <- peer1.startFastSync().delayExecution(50.milliseconds)
_ <- peer1.waitForFastSyncFinish()
} yield {
val trie = peer1.getBestBlockTrie()
val synchronizingPeerHaveAllData = peer1.containsExpectedDataUpToAccountAtBlock(3000, 1001)
// due to the fact that function generating state is deterministic both peer3 and peer4 ends up with exactly same
// state, so peer1 can get whole trie from both of them.
assert(peer1.bl.getBestBlockNumber() == peer3.bl.getBestBlockNumber() - peer3.testSyncConfig.pivotBlockOffset)
assert(peer1.bl.getBestBlockNumber() == peer4.bl.getBestBlockNumber() - peer4.testSyncConfig.pivotBlockOffset)
assert(trie.isDefined)
assert(synchronizingPeerHaveAllData)
}
}

}

object FastSyncItSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ object FastSyncItSpecUtils {
peer3 <- start1FakePeerRes(fakePeerCustomConfig3, "Peer3")
} yield (peer1, peer2, peer3)
}
}

def start4FakePeersRes(
fakePeerCustomConfig1: FakePeerCustomConfig = defaultConfig,
fakePeerCustomConfig2: FakePeerCustomConfig = defaultConfig,
fakePeerCustomConfig3: FakePeerCustomConfig = defaultConfig,
fakePeerCustomConfig4: FakePeerCustomConfig = defaultConfig
): Resource[Task, (FakePeer, FakePeer, FakePeer, FakePeer)] = {
for {
peer1 <- start1FakePeerRes(fakePeerCustomConfig1, "Peer1")
peer2 <- start1FakePeerRes(fakePeerCustomConfig2, "Peer2")
peer3 <- start1FakePeerRes(fakePeerCustomConfig3, "Peer3")
peer4 <- start1FakePeerRes(fakePeerCustomConfig4, "Peer3")
} yield (peer1, peer2, peer3, peer4)
}
}
}
18 changes: 10 additions & 8 deletions src/main/scala/io/iohk/ethereum/blockchain/sync/FastSync.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.iohk.ethereum.blockchain.sync

import java.time.Instant

import akka.actor._
import akka.util.ByteString
import cats.data.NonEmptyList
Expand All @@ -25,7 +24,6 @@ import io.iohk.ethereum.network.p2p.messages.PV63._
import io.iohk.ethereum.utils.ByteStringUtils
import io.iohk.ethereum.utils.Config.SyncConfig
import org.bouncycastle.util.encoders.Hex

import scala.annotation.tailrec
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.{FiniteDuration, _}
Expand Down Expand Up @@ -647,24 +645,25 @@ class FastSync(
} else {
val now = Instant.now()
val peers = unassignedPeers
.filter(p => peerRequestsTime.get(p).forall(d => d.plusMillis(fastSyncThrottle.toMillis).isBefore(now)))
.filter(p => peerRequestsTime.get(p.peer).forall(d => d.plusMillis(fastSyncThrottle.toMillis).isBefore(now)))
peers
.take(maxConcurrentRequests - assignedHandlers.size)
.toSeq
.sortBy(_.ref.toString())
.sortBy(_.info.maxBlockNumber)(Ordering[BigInt].reverse)
.foreach(assignBlockchainWork)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor - why not adding the block height condition here?

}
}

def assignBlockchainWork(peer: Peer): Unit = {
def assignBlockchainWork(peerWithInfo: PeerWithInfo): Unit = {
val PeerWithInfo(peer, peerInfo) = peerWithInfo
if (syncState.receiptsQueue.nonEmpty) {
requestReceipts(peer)
} else if (syncState.blockBodiesQueue.nonEmpty) {
requestBlockBodies(peer)
} else if (
requestedHeaders.isEmpty &&
context.child(BlockHeadersHandlerName).isEmpty &&
syncState.bestBlockHeaderNumber < syncState.safeDownloadTarget
syncState.bestBlockHeaderNumber < syncState.safeDownloadTarget &&
peerInfo.maxBlockNumber >= syncState.pivotBlock.number
) {
requestBlockHeaders(peer)
}
Expand Down Expand Up @@ -737,7 +736,8 @@ class FastSync(
peerRequestsTime += (peer -> Instant.now())
}

def unassignedPeers: Set[Peer] = peersToDownloadFrom.keySet diff assignedHandlers.values.toSet
def unassignedPeers: List[PeerWithInfo] =
(peersToDownloadFrom -- assignedHandlers.values).map(PeerWithInfo.tupled).toList

def blockchainDataToDownload: Boolean =
syncState.blockChainWorkQueued || syncState.bestBlockHeaderNumber < syncState.safeDownloadTarget
Expand Down Expand Up @@ -768,6 +768,8 @@ class FastSync(

object FastSync {

case class PeerWithInfo(peer: Peer, info: PeerInfo)

// scalastyle:off parameter.number
def props(
fastSyncStateStorage: FastSyncStateStorage,
Expand Down