|
1 | 1 | package io.iohk.ethereum.network.discovery.codecs
|
2 | 2 |
|
3 |
| -import io.iohk.scalanet.discovery.ethereum.Node |
| 3 | +import io.iohk.scalanet.discovery.crypto.{PublicKey, Signature} |
| 4 | +import io.iohk.scalanet.discovery.ethereum.{Node, EthereumNodeRecord} |
4 | 5 | import io.iohk.scalanet.discovery.ethereum.v4.Payload
|
5 |
| -import io.iohk.ethereum.rlp.RLPCodec |
| 6 | +import io.iohk.scalanet.discovery.hash.Hash |
| 7 | +import io.iohk.ethereum.rlp |
| 8 | +import io.iohk.ethereum.rlp.{RLPCodec, RLPList} |
| 9 | +import io.iohk.ethereum.rlp.RLPCodec.Ops |
6 | 10 | import io.iohk.ethereum.rlp.RLPImplicits._
|
7 | 11 | import io.iohk.ethereum.rlp.RLPImplicitConversions._
|
8 | 12 | import io.iohk.ethereum.rlp.RLPImplicitDerivations._
|
9 | 13 | import scodec.Codec
|
| 14 | +import scodec.bits.{BitVector, ByteVector} |
10 | 15 | import java.net.InetAddress
|
| 16 | +import scala.collection.SortedMap |
11 | 17 |
|
12 | 18 | /** RLP codecs based on https://github.com/ethereum/devp2p/blob/master/discv4.md */
|
13 | 19 | object RLPCodecs {
|
14 | 20 |
|
15 | 21 | implicit val policy: DerivationPolicy = DerivationPolicy(omitTrailingOptionals = true)
|
16 | 22 |
|
17 | 23 | implicit val inetAddressRLPCodec: RLPCodec[InetAddress] =
|
18 |
| - RLPCodec.instance[InetAddress]( |
19 |
| - ip => ip.getAddress, |
20 |
| - // Implicit conversion to `Array[Byte]` |
21 |
| - { case rlp => InetAddress.getByAddress(rlp) } |
22 |
| - ) |
| 24 | + implicitly[RLPCodec[Array[Byte]]].xmap(InetAddress.getByAddress(_), _.getAddress) |
| 25 | + |
| 26 | + implicit val bitVectorRLPCodec: RLPCodec[BitVector] = |
| 27 | + implicitly[RLPCodec[Array[Byte]]].xmap(BitVector(_), _.toByteArray) |
| 28 | + |
| 29 | + implicit val byteVectorRLPCodec: RLPCodec[ByteVector] = |
| 30 | + implicitly[RLPCodec[Array[Byte]]].xmap(ByteVector(_), _.toArray) |
| 31 | + |
| 32 | + implicit val hashRLPCodec: RLPCodec[Hash] = |
| 33 | + implicitly[RLPCodec[BitVector]].xmap(Hash(_), identity) |
| 34 | + |
| 35 | + implicit val publicKeyRLPCodec: RLPCodec[PublicKey] = |
| 36 | + implicitly[RLPCodec[BitVector]].xmap(PublicKey(_), identity) |
| 37 | + |
| 38 | + implicit val signatureRLPCodec: RLPCodec[Signature] = |
| 39 | + implicitly[RLPCodec[BitVector]].xmap(Signature(_), identity) |
23 | 40 |
|
24 | 41 | implicit val nodeAddressRLPCodec: RLPCodec[Node.Address] =
|
25 |
| - RLPCodec[Node.Address]( |
26 |
| - deriveLabelledGenericRLPListEncoder, |
27 |
| - deriveLabelledGenericRLPListDecoder |
| 42 | + deriveLabelledGenericRLPCodec |
| 43 | + |
| 44 | + implicit val nodeRLPCodec: RLPCodec[Node] = |
| 45 | + deriveLabelledGenericRLPCodec |
| 46 | + |
| 47 | + // https://github.com/ethereum/devp2p/blob/master/enr.md#rlp-encoding |
| 48 | + // `record = [signature, seq, k, v, ...]` |
| 49 | + implicit val enrRLPCodec: RLPCodec[EthereumNodeRecord] = |
| 50 | + RLPCodec.instance( |
| 51 | + { case EthereumNodeRecord(signature, EthereumNodeRecord.Content(seq, attrs)) => |
| 52 | + val kvs = attrs |
| 53 | + .map { case (k, v) => |
| 54 | + RLPList(k.toArray, v.toArray) |
| 55 | + } |
| 56 | + .foldRight(RLPList())(_ ++ _) |
| 57 | + |
| 58 | + signature.toByteArray :: seq :: kvs |
| 59 | + }, |
| 60 | + { case RLPList(signature, seq, kvs @ _*) => |
| 61 | + val attrs = kvs |
| 62 | + .grouped(2) |
| 63 | + .collect { case Seq(k, v) => |
| 64 | + rlp.decode[ByteVector](k) -> rlp.decode[ByteVector](v) |
| 65 | + } |
| 66 | + .toSeq |
| 67 | + |
| 68 | + // TODO: Should have a constructor for key-value pairs. |
| 69 | + import EthereumNodeRecord.byteOrdering |
| 70 | + |
| 71 | + EthereumNodeRecord( |
| 72 | + rlp.decode[Signature](signature), |
| 73 | + EthereumNodeRecord.Content( |
| 74 | + seq, |
| 75 | + SortedMap(attrs: _*) |
| 76 | + ) |
| 77 | + ) |
| 78 | + } |
28 | 79 | )
|
29 | 80 |
|
30 | 81 | implicit val pingRLPCodec: RLPCodec[Payload.Ping] =
|
31 |
| - RLPCodec[Payload.Ping]( |
32 |
| - deriveLabelledGenericRLPListEncoder, |
33 |
| - deriveLabelledGenericRLPListDecoder |
34 |
| - ) |
| 82 | + deriveLabelledGenericRLPCodec |
| 83 | + |
| 84 | + implicit val pongRLPCodec: RLPCodec[Payload.Pong] = |
| 85 | + deriveLabelledGenericRLPCodec |
| 86 | + |
| 87 | + implicit val findNodeRLPCodec: RLPCodec[Payload.FindNode] = |
| 88 | + deriveLabelledGenericRLPCodec |
| 89 | + |
| 90 | + implicit val neighborsRLPCodec: RLPCodec[Payload.Neighbors] = |
| 91 | + deriveLabelledGenericRLPCodec |
| 92 | + |
| 93 | + implicit val enrRequestRLPCodec: RLPCodec[Payload.ENRRequest] = |
| 94 | + deriveLabelledGenericRLPCodec |
| 95 | + |
| 96 | + implicit val enrResponseRLPCodec: RLPCodec[Payload.ENRResponse] = |
| 97 | + deriveLabelledGenericRLPCodec |
35 | 98 |
|
36 | 99 | implicit def payloadCodec: Codec[Payload] = ???
|
37 | 100 | }
|
0 commit comments