-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ETCM-355] Introduce ETH64 message format
- Loading branch information
1 parent
e3f1c38
commit e72902b
Showing
4 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/scala/io/iohk/ethereum/network/p2p/messages/ETH64.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.iohk.ethereum.network.p2p.messages | ||
|
||
import akka.util.ByteString | ||
import io.iohk.ethereum.domain._ | ||
import io.iohk.ethereum.forkid.ForkId | ||
import io.iohk.ethereum.forkid.ForkId._ | ||
import io.iohk.ethereum.mpt.{MptNode, MptTraversals} | ||
import io.iohk.ethereum.network.p2p.{Message, MessageSerializableImplicit} | ||
import io.iohk.ethereum.rlp.RLPImplicitConversions._ | ||
import io.iohk.ethereum.rlp.RLPImplicits._ | ||
import io.iohk.ethereum.rlp._ | ||
import org.bouncycastle.util.encoders.Hex | ||
|
||
object ETH64 { | ||
|
||
case class Status( | ||
protocolVersion: Int, | ||
networkId: Int, | ||
totalDifficulty: BigInt, | ||
bestHash: ByteString, | ||
genesisHash: ByteString, | ||
forkId: ForkId | ||
) extends Message { | ||
|
||
override def toString: String = | ||
s"Status { " + | ||
s"code: $code, " + | ||
s"protocolVersion: $protocolVersion, " + | ||
s"networkId: $networkId, " + | ||
s"totalDifficulty: $totalDifficulty, " + | ||
s"bestHash: ${Hex.toHexString(bestHash.toArray[Byte])}, " + | ||
s"genesisHash: ${Hex.toHexString(genesisHash.toArray[Byte])}," + | ||
s"forkId: $forkId," + | ||
s"}" | ||
|
||
override def toShortString: String = toString | ||
override def code: Int = Codes.StatusCode | ||
} | ||
|
||
object Status { | ||
implicit class StatusEnc(val underlyingMsg: Status) | ||
extends MessageSerializableImplicit[Status](underlyingMsg) | ||
with RLPSerializable { | ||
override def code: Int = Codes.StatusCode | ||
|
||
override def toRLPEncodable: RLPEncodeable = { | ||
import msg._ | ||
RLPList(protocolVersion, networkId, totalDifficulty, bestHash, genesisHash, forkId.toRLPEncodable) | ||
} | ||
} | ||
|
||
implicit class StatusDec(val bytes: Array[Byte]) extends AnyVal { | ||
def toStatus: Status = rawDecode(bytes) match { | ||
case RLPList( | ||
protocolVersion, | ||
networkId, | ||
totalDifficulty, | ||
bestHash, | ||
genesisHash, | ||
forkId | ||
) => | ||
Status( | ||
protocolVersion, | ||
networkId, | ||
totalDifficulty, | ||
bestHash, | ||
genesisHash, | ||
decode[ForkId](forkId) | ||
) | ||
|
||
case _ => throw new RuntimeException("Cannot decode Status") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters