Skip to content

Commit ef658ab

Browse files
committed
ETCM-167: Use +: instead of :: on RLPList.
1 parent e28c69d commit ef658ab

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object RLPCodecs {
4545
implicit val nodeRLPCodec: RLPCodec[Node] =
4646
RLPCodec.instance[Node](
4747
{ case Node(id, address) =>
48-
RLPEncoder.encode(address).asInstanceOf[RLPList] ++ RLPList(id)
48+
RLPEncoder.encode(address).asInstanceOf[RLPList] :+ id
4949
},
5050
{
5151
case list @ RLPList(items @ _*) if items.length >= 4 =>
@@ -62,10 +62,10 @@ object RLPCodecs {
6262
{ case EthereumNodeRecord.Content(seq, attrs) =>
6363
val kvs = attrs
6464
.foldRight(RLPList()) { case ((k, v), kvs) =>
65-
k.toArray :: v.toArray :: kvs
65+
k.toArray +: v.toArray +: kvs
6666
}
6767

68-
seq :: kvs
68+
seq +: kvs
6969
},
7070
{ case RLPList(seq, kvs @ _*) =>
7171
val attrs = kvs
@@ -89,7 +89,8 @@ object RLPCodecs {
8989
implicit val enrRLPCodec: RLPCodec[EthereumNodeRecord] =
9090
RLPCodec.instance(
9191
{ case EthereumNodeRecord(signature, content) =>
92-
signature.toByteArray :: RLPEncoder.encode(content).asInstanceOf[RLPList]
92+
val contentList = RLPEncoder.encode(content).asInstanceOf[RLPList]
93+
signature.toByteArray +: contentList
9394
},
9495
{ case RLPList(signature, content @ _*) =>
9596
EthereumNodeRecord(

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,27 @@ object RLPImplicitDerivations {
4040

4141
/** Specialized decoder for case classes that only accepts RLPList for input. */
4242
trait RLPListDecoder[T] extends RLPDecoder[T] {
43+
protected def ct: ClassTag[T]
4344
def decodeList(items: List[RLPEncodeable]): (T, List[FieldInfo])
4445

4546
override def decode(rlp: RLPEncodeable): T =
4647
rlp match {
4748
case list: RLPList =>
4849
decodeList(list.items.toList)._1
4950
case _ =>
50-
throw RLPException(s"Expected to decode an RLPList", rlp)
51+
throw RLPException(s"Cannot decode ${ct.runtimeClass.getSimpleName}: expected an RLPList.", rlp)
5152
}
5253
}
5354
object RLPListDecoder {
54-
def apply[T](f: List[RLPEncodeable] => (T, List[FieldInfo])): RLPListDecoder[T] =
55+
def apply[T: ClassTag](f: List[RLPEncodeable] => (T, List[FieldInfo])): RLPListDecoder[T] =
5556
new RLPListDecoder[T] {
57+
override val ct = implicitly[ClassTag[T]]
5658
override def decodeList(items: List[RLPEncodeable]) = f(items)
5759
}
5860
}
5961

6062
private def decodeError[T](subject: String, error: String, maybeEncodeable: Option[RLPEncodeable] = None): T =
61-
throw RLPException(s"error decoding $subject: $error", maybeEncodeable)
63+
throw RLPException(s"Cannot decode $subject: $error", maybeEncodeable)
6264

6365
private def tryDecode[T](subject: => String, encodeable: RLPEncodeable)(f: RLPEncodeable => T): T = {
6466
try {
@@ -99,15 +101,15 @@ object RLPImplicitDerivations {
99101
// This is still a trailing optional field, so we can insert it as a value or omit it.
100102
hEncoder.value.encode(head) match {
101103
case RLPList(hRLP) =>
102-
hRLP :: tRLP
104+
hRLP +: tRLP
103105
case RLPList() if tRLP.items.isEmpty =>
104106
tRLP
105107
case hRLP =>
106-
hRLP :: tRLP
108+
hRLP +: tRLP
107109
}
108110
} else {
109111
// We're no longer in a trailing position, so insert it as a list of 0 or 1 items.
110-
hEncoder.value.encode(head) :: tRLP
112+
hEncoder.value.encode(head) +: tRLP
111113
}
112114

113115
htRLP -> (hInfo :: tInfos)
@@ -125,7 +127,7 @@ object RLPImplicitDerivations {
125127
RLPListEncoder { case head :: tail =>
126128
val hRLP = hEncoder.value.encode(head)
127129
val (tRLP, tInfos) = tEncoder.value.encodeList(tail)
128-
(hRLP :: tRLP, hInfo :: tInfos)
130+
(hRLP +: tRLP, hInfo :: tInfos)
129131
}
130132
}
131133

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ package object rlp {
1515
sealed trait RLPEncodeable
1616

1717
case class RLPList(items: RLPEncodeable*) extends RLPEncodeable {
18-
def ::(item: RLPEncodeable): RLPList =
18+
def +:(item: RLPEncodeable): RLPList =
1919
RLPList((item +: items): _*)
2020

21+
def :+(item: RLPEncodeable): RLPList =
22+
RLPList((items :+ item): _*)
23+
2124
def ++(other: RLPList): RLPList =
2225
RLPList((items ++ other.items): _*)
2326
}
@@ -96,7 +99,7 @@ package object rlp {
9699

97100
override def decode(rlp: RLPEncodeable): T =
98101
if (dec.isDefinedAt(rlp)) dec(rlp)
99-
else throw RLPException(s"Cannot decode type ${ct.runtimeClass.getSimpleName} from RLP.", rlp)
102+
else throw RLPException(s"Cannot decode type ${ct.runtimeClass.getSimpleName} from unexpected RLP.", rlp)
100103
}
101104

102105
def apply[T](enc: RLPEncoder[T], dec: RLPDecoder[T]): RLPCodec[T] =

0 commit comments

Comments
 (0)