Skip to content

Commit 4438dfb

Browse files
[ETCM-355] Introduce ProtocolFamily
This also fixes a bug in NewBlock message creation, where only protocol version was taken into account
1 parent 43c8eef commit 4438dfb

18 files changed

+69
-15
lines changed

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockBroadcast.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import io.iohk.ethereum.network.p2p.messages.BaseETH6XMessages
1717
import io.iohk.ethereum.network.p2p.messages.ETC64
1818
import io.iohk.ethereum.network.p2p.messages.ETH62
1919
import io.iohk.ethereum.network.p2p.messages.ETH62.BlockHash
20-
import io.iohk.ethereum.network.p2p.messages.ProtocolVersions
20+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily._
2121

2222
class BlockBroadcast(val etcPeerManager: ActorRef) {
2323

@@ -45,10 +45,12 @@ class BlockBroadcast(val etcPeerManager: ActorRef) {
4545

4646
private def broadcastNewBlock(blockToBroadcast: BlockToBroadcast, peers: Map[PeerId, PeerWithInfo]): Unit =
4747
obtainRandomPeerSubset(peers.values.map(_.peer).toSet).foreach { peer =>
48-
val message: MessageSerializable =
49-
if (peers(peer.id).peerInfo.remoteStatus.protocolVersion.toByte == ProtocolVersions.ETC64.version)
50-
blockToBroadcast.as64
51-
else blockToBroadcast.as63
48+
val remoteStatus = peers(peer.id).peerInfo.remoteStatus
49+
50+
val message: MessageSerializable = remoteStatus.protocolFamily match {
51+
case ETH => blockToBroadcast.as63
52+
case ETC => blockToBroadcast.as64
53+
}
5254
etcPeerManager ! EtcPeerManagerActor.SendMessage(message, peer.id)
5355
}
5456

src/main/scala/io/iohk/ethereum/network/EtcPeerManagerActor.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import io.iohk.ethereum.network.p2p.messages.ETH62.BlockHeaders
2727
import io.iohk.ethereum.network.p2p.messages.ETH62.GetBlockHeaders
2828
import io.iohk.ethereum.network.p2p.messages.ETH62.NewBlockHashes
2929
import io.iohk.ethereum.network.p2p.messages.ETH64
30+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily
31+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily.ETC
32+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily.ETH
3033
import io.iohk.ethereum.network.p2p.messages.WireProtocol.Disconnect
3134
import io.iohk.ethereum.utils.ByteStringUtils
3235

@@ -240,6 +243,7 @@ object EtcPeerManagerActor {
240243
* (they are different versions of Status msg)
241244
*/
242245
case class RemoteStatus(
246+
protocolFamily: ProtocolFamily,
243247
protocolVersion: Int,
244248
networkId: Int,
245249
chainWeight: ChainWeight,
@@ -248,6 +252,7 @@ object EtcPeerManagerActor {
248252
) {
249253
override def toString: String =
250254
s"RemoteStatus { " +
255+
s"protocolFamily: $protocolFamily, " +
251256
s"protocolVersion: $protocolVersion, " +
252257
s"networkId: $networkId, " +
253258
s"chainWeight: $chainWeight, " +
@@ -259,6 +264,7 @@ object EtcPeerManagerActor {
259264
object RemoteStatus {
260265
def apply(status: ETH64.Status): RemoteStatus =
261266
RemoteStatus(
267+
ETH,
262268
status.protocolVersion,
263269
status.networkId,
264270
ChainWeight.totalDifficultyOnly(status.totalDifficulty),
@@ -267,10 +273,18 @@ object EtcPeerManagerActor {
267273
)
268274

269275
def apply(status: ETC64.Status): RemoteStatus =
270-
RemoteStatus(status.protocolVersion, status.networkId, status.chainWeight, status.bestHash, status.genesisHash)
276+
RemoteStatus(
277+
ETC,
278+
status.protocolVersion,
279+
status.networkId,
280+
status.chainWeight,
281+
status.bestHash,
282+
status.genesisHash
283+
)
271284

272285
def apply(status: BaseETH6XMessages.Status): RemoteStatus =
273286
RemoteStatus(
287+
ETH,
274288
status.protocolVersion,
275289
status.networkId,
276290
ChainWeight.totalDifficultyOnly(status.totalDifficulty),

src/main/scala/io/iohk/ethereum/network/handshaker/EtcHelloExchangeState.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ case class EtcHelloExchangeState(handshakerConfiguration: EtcHandshakerConfigura
3131
log.debug("Protocol handshake finished with peer ({})", hello)
3232
// FIXME in principle this should be already negotiated
3333
Capability.negotiate(hello.capabilities.toList, handshakerConfiguration.blockchainConfig.capabilities) match {
34-
case Some(ProtocolVersions.ETC64) => {
34+
case Some(ProtocolVersions.ETC64) =>
3535
log.debug("Negotiated protocol version with client {} is etc/64", hello.clientId)
3636
EtcNodeStatus64ExchangeState(handshakerConfiguration)
37-
}
38-
case Some(ProtocolVersions.ETH63) => {
37+
case Some(ProtocolVersions.ETH63) =>
3938
log.debug("Negotiated protocol version with client {} is eth/63", hello.clientId)
4039
EthNodeStatus63ExchangeState(handshakerConfiguration)
41-
}
42-
case Some(ProtocolVersions.ETH64) => {
40+
case Some(ProtocolVersions.ETH64) =>
4341
log.debug("Negotiated protocol version with client {} is eth/64", hello.clientId)
4442
EthNodeStatus64ExchangeState(handshakerConfiguration)
45-
}
4643
case _ =>
4744
log.debug(
4845
s"Connected peer does not support {} / {} / {} protocol. Disconnecting.",

src/main/scala/io/iohk/ethereum/network/p2p/messages/package.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package io.iohk.ethereum.network.p2p
22

3+
package messages {
4+
sealed trait ProtocolFamily
5+
object ProtocolFamily {
6+
final case object ETH extends ProtocolFamily
7+
final case object ETC extends ProtocolFamily
8+
}
9+
}
10+
311
package object messages {
412
object ProtocolVersions {
513
val ETH61: Capability = Capability("eth", 61.toByte)

src/test/scala/io/iohk/ethereum/blockchain/sync/BlockBroadcastSpec.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import io.iohk.ethereum.network.p2p.messages.BaseETH6XMessages
2727
import io.iohk.ethereum.network.p2p.messages.ETC64.NewBlock
2828
import io.iohk.ethereum.network.p2p.messages.ETH62
2929
import io.iohk.ethereum.network.p2p.messages.ETH62.NewBlockHashes
30+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily
3031
import io.iohk.ethereum.network.p2p.messages.ProtocolVersions
3132

3233
class BlockBroadcastSpec
@@ -61,7 +62,9 @@ class BlockBroadcastSpec
6162
val blockHeader: BlockHeader = baseBlockHeader.copy(number = initialPeerInfo.maxBlockNumber - 3)
6263
val newBlockNewHashes = NewBlockHashes(Seq(ETH62.BlockHash(blockHeader.hash, blockHeader.number)))
6364
val peerInfo = initialPeerInfo
64-
.copy(remoteStatus = peerStatus.copy(protocolVersion = ProtocolVersions.ETH63.version))
65+
.copy(remoteStatus =
66+
peerStatus.copy(protocolFamily = ProtocolFamily.ETH, protocolVersion = ProtocolVersions.ETH63.version)
67+
)
6568
.withChainWeight(ChainWeight.totalDifficultyOnly(initialPeerInfo.chainWeight.totalDifficulty))
6669
val newBlock =
6770
BaseETH6XMessages.NewBlock(Block(blockHeader, BlockBody(Nil, Nil)), peerInfo.chainWeight.totalDifficulty + 2)
@@ -176,6 +179,7 @@ class BlockBroadcastSpec
176179
val baseBlockHeader = Fixtures.Blocks.Block3125369.header
177180

178181
val peerStatus: RemoteStatus = RemoteStatus(
182+
protocolFamily = ProtocolFamily.ETC,
179183
protocolVersion = ProtocolVersions.ETC64.version,
180184
networkId = 1,
181185
chainWeight = ChainWeight(10, 10000),

src/test/scala/io/iohk/ethereum/blockchain/sync/PeersClientSpec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import io.iohk.ethereum.network.EtcPeerManagerActor.PeerInfo
1616
import io.iohk.ethereum.network.EtcPeerManagerActor.RemoteStatus
1717
import io.iohk.ethereum.network.Peer
1818
import io.iohk.ethereum.network.PeerId
19+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily
1920
import io.iohk.ethereum.network.p2p.messages.ProtocolVersions
2021

2122
class PeersClientSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyChecks {
@@ -80,6 +81,7 @@ class PeersClientSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyC
8081
val peer3: Peer = Peer(PeerId("peer3"), new InetSocketAddress("127.0.0.1", 3), TestProbe().ref, false)
8182

8283
private val peerStatus = RemoteStatus(
84+
protocolFamily = ProtocolFamily.ETH,
8385
protocolVersion = ProtocolVersions.ETH63.version,
8486
networkId = 1,
8587
chainWeight = ChainWeight(0, 0),

src/test/scala/io/iohk/ethereum/blockchain/sync/PivotBlockSelectorSpec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import io.iohk.ethereum.network.p2p.Message
3838
import io.iohk.ethereum.network.p2p.messages.BaseETH6XMessages.NewBlock
3939
import io.iohk.ethereum.network.p2p.messages.Codes
4040
import io.iohk.ethereum.network.p2p.messages.ETH62._
41+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily
4142
import io.iohk.ethereum.network.p2p.messages.ProtocolVersions
4243
import io.iohk.ethereum.utils.Config.SyncConfig
4344

@@ -587,6 +588,7 @@ class PivotBlockSelectorSpec
587588

588589
val peer1Status: RemoteStatus =
589590
RemoteStatus(
591+
ProtocolFamily.ETC,
590592
ProtocolVersions.ETC64.version,
591593
1,
592594
ChainWeight.totalDifficultyOnly(20),

src/test/scala/io/iohk/ethereum/blockchain/sync/StateSyncSpec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import io.iohk.ethereum.network.PeerEventBusActor.PeerEvent.MessageFromPeer
4141
import io.iohk.ethereum.network.PeerId
4242
import io.iohk.ethereum.network.p2p.messages.ETH63.GetNodeData.GetNodeDataEnc
4343
import io.iohk.ethereum.network.p2p.messages.ETH63.NodeData
44+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily
4445
import io.iohk.ethereum.network.p2p.messages.ProtocolVersions
4546
import io.iohk.ethereum.utils.Config
4647

@@ -131,6 +132,7 @@ class StateSyncSpec
131132
val syncInit: TestProbe = TestProbe()
132133

133134
val peerStatus: RemoteStatus = RemoteStatus(
135+
protocolFamily = ProtocolFamily.ETH,
134136
protocolVersion = ProtocolVersions.ETH63.version,
135137
networkId = 1,
136138
chainWeight = ChainWeight.totalDifficultyOnly(10000),

src/test/scala/io/iohk/ethereum/blockchain/sync/TestSyncPeers.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.iohk.ethereum.network.EtcPeerManagerActor.PeerInfo
1010
import io.iohk.ethereum.network.EtcPeerManagerActor.RemoteStatus
1111
import io.iohk.ethereum.network.Peer
1212
import io.iohk.ethereum.network.PeerId
13+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily
1314
import io.iohk.ethereum.network.p2p.messages.ProtocolVersions
1415

1516
trait TestSyncPeers { self: TestSyncConfig =>
@@ -25,6 +26,7 @@ trait TestSyncPeers { self: TestSyncConfig =>
2526

2627
val peer1Status: RemoteStatus =
2728
RemoteStatus(
29+
ProtocolFamily.ETC,
2830
ProtocolVersions.ETC64.version,
2931
1,
3032
ChainWeight.totalDifficultyOnly(20),

src/test/scala/io/iohk/ethereum/blockchain/sync/fast/FastSyncBranchResolverActorSpec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import io.iohk.ethereum.network.PeerEventBusActor.PeerEvent.MessageFromPeer
4444
import io.iohk.ethereum.network.PeerId
4545
import io.iohk.ethereum.network.p2p.messages.ETH62.BlockHeaders
4646
import io.iohk.ethereum.network.p2p.messages.ETH62.GetBlockHeaders
47+
import io.iohk.ethereum.network.p2p.messages.ProtocolFamily
4748
import io.iohk.ethereum.network.p2p.messages.ProtocolVersions
4849
import io.iohk.ethereum.utils.Logger
4950

@@ -264,6 +265,7 @@ class FastSyncBranchResolverActorSpec
264265
def getPeerInfo(peer: Peer, protocolVersion: Int = ProtocolVersions.ETC64.version): PeerInfo = {
265266
val status =
266267
RemoteStatus(
268+
ProtocolFamily.ETC,
267269
protocolVersion,
268270
1,
269271
ChainWeight.totalDifficultyOnly(1),

0 commit comments

Comments
 (0)