Skip to content

Commit e563d29

Browse files
committed
ETCM-167: Test that we encode to the same bits.
1 parent 31f430d commit e563d29

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/test/scala/io/iohk/ethereum/network/discovery/codecs/RLPCodecsSpec.scala

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ class RLPCodecsSpec extends AnyFlatSpec {
110110
// Test the original Mantis types to make sure we're on the right track.
111111
EIP8TestVectors.foreach { case EIP8TestVector(description, data, _) =>
112112
it should s"decode a ${description} into an original Mantis type" in {
113-
import io.iohk.ethereum.network.discovery.Packet
114113
import akka.util.ByteString
114+
import io.iohk.ethereum.network.discovery.Packet
115+
115116
val bits = BitVector.fromHex(data).get
116117
val bytes = ByteString(bits.toByteArray)
117118
val packet = Packet(bytes)
@@ -120,18 +121,27 @@ class RLPCodecsSpec extends AnyFlatSpec {
120121
}
121122

122123
// Test the RLP decoders in isolation, without crypto.
123-
EIP8TestVectors.take(5).foreach { case EIP8TestVector(description, data, test) =>
124+
EIP8TestVectors.foreach { case EIP8TestVector(description, data, test) =>
124125
it should s"decode/encode a ${description}" in {
125126
val bits = BitVector.fromHex(data).get
126127
val packet = Codec[Packet].decodeValue(bits).require
127128
val payload = Codec[Payload].decodeValue(packet.data).require
129+
128130
test(payload)
131+
132+
val encoded = Codec[Payload].encode(payload).require
133+
// The packet type should match.
134+
encoded.take(8) shouldBe packet.data.take(8)
135+
// The first couple of bytes encode the length,
136+
// which in the test vector is different ecause it has random data appended to it.
137+
// Check RLP.encodeLength for details, here I'm just skipping some and see if the rest matches.
138+
encoded.drop(32) shouldBe packet.data.take(encoded.length).drop(32)
129139
}
130140
}
131141

132142
// Test the whole Packet unpack function, with RLP and crypto.
133143
EIP8TestVectors.foreach { case EIP8TestVector(description, data, test) =>
134-
ignore should s"unpack a ${description}" in {
144+
ignore should s"unpack/pack a ${description}" in {
135145
val privateKey = PrivateKey(
136146
BitVector.fromHex("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291").get
137147
)
@@ -143,6 +153,14 @@ class RLPCodecsSpec extends AnyFlatSpec {
143153

144154
// Test again to make sure `unpack` works correctly.
145155
test(payload)
156+
157+
// See if it survives a roundtrip.
158+
val bits2 = Codec[Packet].encode(Packet.pack(payload, privateKey).require).require
159+
val packet2 = Codec[Packet].decodeValue(bits2).require
160+
val (payload2, publicKey2) = Packet.unpack(packet).require
161+
162+
payload2 shouldBe payload
163+
publicKey2 shouldBe publicKey
146164
}
147165
}
148166

0 commit comments

Comments
 (0)