Skip to content

Commit

Permalink
ETCM-167: Combine encoder and decoder into codec.
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Oct 28, 2020
1 parent 8aebfca commit a8964b3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
Expand Up @@ -2,7 +2,7 @@ package io.iohk.ethereum.network.discovery.codecs

import io.iohk.scalanet.discovery.ethereum.Node
import io.iohk.scalanet.discovery.ethereum.v4.Payload
import io.iohk.ethereum.rlp.{RLPList, RLPEncodeable, RLPCodec, RLPEncoder, RLPDecoder}
import io.iohk.ethereum.rlp.{RLPList, RLPCodec}
import io.iohk.ethereum.rlp.RLPImplicits._
import io.iohk.ethereum.rlp.RLPImplicitConversions._
import io.iohk.ethereum.rlp.RLPImplicitDerivations._
Expand All @@ -15,7 +15,7 @@ object RLPCodecs {
implicit val policy: DerivationPolicy = DerivationPolicy(omitTrailingOptionals = true)

implicit val nodeAddressRLPCodec: RLPCodec[Node.Address] =
RLPCodec[Node.Address](
RLPCodec.instance[Node.Address](
{ case Node.Address(ip, udpPort, tcpPort) =>
RLPList(ip.getAddress, udpPort, tcpPort)
},
Expand All @@ -24,16 +24,11 @@ object RLPCodecs {
}
)

implicit val `Option[RLPEncodeable] => Option[Long]` : Option[RLPEncodeable] => Option[Long] =
fromOptionalEncodeable[Long]

implicit val nodeAddressFromEncodeable = fromEncodeable[Node.Address](_)

implicit val pingRLPEncoder: RLPEncoder[Payload.Ping] =
deriveLabelledGenericRLPListEncoder

implicit val pingRLPDecoder: RLPDecoder[Payload.Ping] =
deriveLabelledGenericRLPListDecoder
implicit val pingRLPCodec: RLPCodec[Payload.Ping] =
RLPCodec[Payload.Ping](
deriveLabelledGenericRLPListEncoder,
deriveLabelledGenericRLPListDecoder
)

implicit def payloadCodec: Codec[Payload] = ???
}
8 changes: 7 additions & 1 deletion src/main/scala/io/iohk/ethereum/rlp/package.scala
Expand Up @@ -80,7 +80,7 @@ package object rlp {
type RLPCodec[T] = RLPEncoder[T] with RLPDecoder[T]

object RLPCodec {
def apply[T](enc: T => RLPEncodeable, dec: PartialFunction[RLPEncodeable, T])(implicit
def instance[T](enc: T => RLPEncodeable, dec: PartialFunction[RLPEncodeable, T])(implicit
ct: ClassTag[T]
): RLPCodec[T] =
new RLPEncoder[T] with RLPDecoder[T] {
Expand All @@ -91,5 +91,11 @@ package object rlp {
if (dec.isDefinedAt(rlp)) dec(rlp)
else throw new RuntimeException(s"Cannot decode ${ct.getClass.getSimpleName} from RLP.")
}

def apply[T](enc: RLPEncoder[T], dec: RLPDecoder[T]): RLPCodec[T] =
new RLPEncoder[T] with RLPDecoder[T] {
override def encode(obj: T): RLPEncodeable = enc.encode(obj)
override def decode(rlp: RLPEncodeable): T = dec.decode(rlp)
}
}
}

0 comments on commit a8964b3

Please sign in to comment.