Skip to content

Commit d6daa34

Browse files
[ETCM-354] Roundtrip ForkIds
1 parent 1e8cdbf commit d6daa34

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/main/scala/io/iohk/ethereum/forkid/ForkId.scala

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import io.iohk.ethereum.utils.BigIntExtensionMethods._
99
import io.iohk.ethereum.utils.ByteUtils._
1010
import io.iohk.ethereum.rlp._
1111

12+
import RLPImplicitConversions._
13+
1214
case class ForkId(hash: BigInt, next: Option[BigInt])
1315

1416
object ForkId {
@@ -44,7 +46,6 @@ object ForkId {
4446

4547
implicit class ForkIdEnc(forkId: ForkId) extends RLPSerializable {
4648
import RLPImplicits._
47-
import RLPImplicitConversions._
4849

4950
import io.iohk.ethereum.utils.ByteUtils._
5051
override def toRLPEncodable: RLPEncodeable = {
@@ -55,4 +56,14 @@ object ForkId {
5556

5657
}
5758

59+
implicit val forkIdEnc = new RLPDecoder[ForkId] {
60+
61+
def decode(rlp: RLPEncodeable): ForkId = rlp match {
62+
case RLPList(hash, next) => {
63+
val i = bigIntFromEncodeable(next)
64+
ForkId(bigIntFromEncodeable(hash), if (i == 0) None else Some(i))
65+
}
66+
case _ => throw new RuntimeException("Error when decoding ForkId")
67+
}
68+
}
5869
}

src/test/scala/io/iohk/ethereum/forkid/ForkIdSpec.scala

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import org.scalatest.wordspec.AnyWordSpec
99
import org.scalatest.matchers.should._
1010
import org.bouncycastle.util.encoders.Hex
1111

12+
import io.iohk.ethereum.rlp._
13+
import io.iohk.ethereum.rlp.RLPImplicits._
14+
15+
1216
class ForkIdSpec extends AnyWordSpec with Matchers {
1317

1418
val config = blockchains
@@ -79,22 +83,22 @@ class ForkIdSpec extends AnyWordSpec with Matchers {
7983
create(11700000) shouldBe ForkId(0xB20AFE12L, None)
8084
}
8185

82-
8386
// Here’s a couple of tests to verify the proper RLP encoding (since FORK_HASH is a 4 byte binary but FORK_NEXT is an 8 byte quantity):
8487
"be correctly encoded via rlp" in {
85-
import ForkId._
86-
import io.iohk.ethereum.rlp._
87-
import io.iohk.ethereum.rlp.RLPImplicits._
88-
89-
encode(ForkId(0, None).toRLPEncodable) shouldBe Hex.decode("c6840000000080")
90-
encode(ForkId(0xdeadbeefL, Some(0xBADDCAFEL)).toRLPEncodable) shouldBe Hex.decode("ca84deadbeef84baddcafe")
88+
roundTrip(ForkId(0, None), "c6840000000080")
89+
roundTrip(ForkId(0xdeadbeefL, Some(0xBADDCAFEL)), "ca84deadbeef84baddcafe")
9190

9291
val maxUInt64 = (BigInt(0x7FFFFFFFFFFFFFFFL) << 1) + 1
9392
maxUInt64.toByteArray shouldBe Array(0, -1, -1, -1, -1, -1, -1, -1, -1)
9493
val maxUInt32 = BigInt(0xFFFFFFFFL)
9594
maxUInt32.toByteArray shouldBe Array(0, -1, -1, -1, -1)
9695

97-
encode(ForkId(maxUInt32, Some(maxUInt64)).toRLPEncodable) shouldBe Hex.decode("ce84ffffffff88ffffffffffffffff")
96+
roundTrip(ForkId(maxUInt32, Some(maxUInt64)), "ce84ffffffff88ffffffffffffffff")
9897
}
9998
}
99+
100+
private def roundTrip(forkId: ForkId, hex: String) = {
101+
encode(forkId.toRLPEncodable) shouldBe Hex.decode(hex)
102+
decode[ForkId](Hex.decode(hex)) shouldBe forkId
103+
}
100104
}

0 commit comments

Comments
 (0)