Skip to content

Commit a8964b3

Browse files
committed
ETCM-167: Combine encoder and decoder into codec.
1 parent 8aebfca commit a8964b3

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/main/scala/io/iohk/ethereum/network/discovery/codecs/RLPCodecs.scala

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package io.iohk.ethereum.network.discovery.codecs
22

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

1717
implicit val nodeAddressRLPCodec: RLPCodec[Node.Address] =
18-
RLPCodec[Node.Address](
18+
RLPCodec.instance[Node.Address](
1919
{ case Node.Address(ip, udpPort, tcpPort) =>
2020
RLPList(ip.getAddress, udpPort, tcpPort)
2121
},
@@ -24,16 +24,11 @@ object RLPCodecs {
2424
}
2525
)
2626

27-
implicit val `Option[RLPEncodeable] => Option[Long]` : Option[RLPEncodeable] => Option[Long] =
28-
fromOptionalEncodeable[Long]
29-
30-
implicit val nodeAddressFromEncodeable = fromEncodeable[Node.Address](_)
31-
32-
implicit val pingRLPEncoder: RLPEncoder[Payload.Ping] =
33-
deriveLabelledGenericRLPListEncoder
34-
35-
implicit val pingRLPDecoder: RLPDecoder[Payload.Ping] =
36-
deriveLabelledGenericRLPListDecoder
27+
implicit val pingRLPCodec: RLPCodec[Payload.Ping] =
28+
RLPCodec[Payload.Ping](
29+
deriveLabelledGenericRLPListEncoder,
30+
deriveLabelledGenericRLPListDecoder
31+
)
3732

3833
implicit def payloadCodec: Codec[Payload] = ???
3934
}

src/main/scala/io/iohk/ethereum/rlp/package.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ package object rlp {
8080
type RLPCodec[T] = RLPEncoder[T] with RLPDecoder[T]
8181

8282
object RLPCodec {
83-
def apply[T](enc: T => RLPEncodeable, dec: PartialFunction[RLPEncodeable, T])(implicit
83+
def instance[T](enc: T => RLPEncodeable, dec: PartialFunction[RLPEncodeable, T])(implicit
8484
ct: ClassTag[T]
8585
): RLPCodec[T] =
8686
new RLPEncoder[T] with RLPDecoder[T] {
@@ -91,5 +91,11 @@ package object rlp {
9191
if (dec.isDefinedAt(rlp)) dec(rlp)
9292
else throw new RuntimeException(s"Cannot decode ${ct.getClass.getSimpleName} from RLP.")
9393
}
94+
95+
def apply[T](enc: RLPEncoder[T], dec: RLPDecoder[T]): RLPCodec[T] =
96+
new RLPEncoder[T] with RLPDecoder[T] {
97+
override def encode(obj: T): RLPEncodeable = enc.encode(obj)
98+
override def decode(rlp: RLPEncodeable): T = dec.decode(rlp)
99+
}
94100
}
95101
}

0 commit comments

Comments
 (0)