Skip to content

Commit b5f03cd

Browse files
committed
ETCM-167: Require a derivation policy to enable the omission of trailing optionals.
1 parent d3e4a43 commit b5f03cd

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import java.net.InetAddress
1212
/** RLP codecs based on https://github.com/ethereum/devp2p/blob/master/discv4.md */
1313
object RLPCodecs {
1414

15+
implicit val policy: DerivationPolicy = DerivationPolicy(omitTrailingOptionals = true)
16+
1517
implicit val nodeAddressRLPCodec: RLPCodec[Node.Address] =
1618
RLPCodec[Node.Address](
1719
{ case Node.Address(ip, udpPort, tcpPort) =>

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import shapeless.labelled.FieldType
66
/** Automatically derive RLP codecs for case classes. */
77
object RLPImplicitDerivations {
88

9+
case class DerivationPolicy(
10+
// Whether to treat optional fields at the end of the list like
11+
// they can be omitted from the RLP list, or inserted as a value,
12+
// as opposed to a list of 0 or 1 items.
13+
omitTrailingOptionals: Boolean
14+
)
15+
916
/** Support introspecting on what happened during encoding the tail. */
1017
case class FieldInfo(isOptional: Boolean)
1118

@@ -41,14 +48,15 @@ object RLPImplicitDerivations {
4148
implicit def deriveOptionHListRLPListEncoder[K, H, T <: HList](implicit
4249
hEncoder: Lazy[RLPEncoder[H]],
4350
tEncoder: Lazy[RLPListEncoder[T]],
44-
ev: H <:< Option[_]
51+
ev: H <:< Option[_],
52+
policy: DerivationPolicy
4553
): RLPListEncoder[FieldType[K, H] :: T] = {
4654
val hInfo = FieldInfo(isOptional = true)
4755
// Create an encoder that takes a list of field values.
4856
RLPListEncoder { case head :: tail =>
4957
val (tRLP, tInfos) = tEncoder.value.encodeList(tail)
5058
val htRLP =
51-
if (tInfos.forall(_.isOptional)) {
59+
if (policy.omitTrailingOptionals && tInfos.forall(_.isOptional)) {
5260
// This is still a trailing optional field, so we can insert it as a value or omit it.
5361
hEncoder.value.encode(head) match {
5462
case RLPList(hRLP) =>

0 commit comments

Comments
 (0)