|
| 1 | +package io.iohk.ethereum.forkid |
| 2 | + |
| 3 | +import akka.util.ByteString |
| 4 | +import io.iohk.ethereum.forkid.ForkId._ |
| 5 | +import io.iohk.ethereum.utils.ForkBlockNumbers |
| 6 | +import io.iohk.ethereum.utils.Config._ |
| 7 | + |
| 8 | +import org.scalatest.wordspec.AnyWordSpec |
| 9 | +import org.scalatest.matchers.should._ |
| 10 | +import org.bouncycastle.util.encoders.Hex |
| 11 | + |
| 12 | +import io.iohk.ethereum.rlp._ |
| 13 | +import io.iohk.ethereum.rlp.RLPImplicits._ |
| 14 | + |
| 15 | + |
| 16 | +class ForkIdSpec extends AnyWordSpec with Matchers { |
| 17 | + |
| 18 | + val config = blockchains |
| 19 | + |
| 20 | + "ForkId" must { |
| 21 | + "gatherForks for all chain configurations without errors" in { |
| 22 | + config.blockchains.map { case (name, conf) => (name, gatherForks(conf)) } |
| 23 | + } |
| 24 | + "gatherForks for the etc chain correctly" in { |
| 25 | + val res = config.blockchains.map { case (name, conf) => (name, gatherForks(conf)) } |
| 26 | + res("etc") shouldBe List(1150000, 2500000, 3000000, 5000000, 5900000, 8772000, 9573000, 10500839, 11700000) |
| 27 | + } |
| 28 | + |
| 29 | + "gatherForks for the eth chain correctly" in { |
| 30 | + val res = config.blockchains.map { case (name, conf) => (name, gatherForks(conf)) } |
| 31 | + res("eth") shouldBe List(1150000, 1920000, 2463000, 2675000, 4370000, 7280000, 9069000) |
| 32 | + } |
| 33 | + |
| 34 | + "create correct ForkId for ETH mainnet blocks" in { |
| 35 | + val ethConf = config.blockchains("eth") |
| 36 | + val ethGenesisHash = ByteString(Hex.decode("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")) |
| 37 | + def create(head: BigInt) = ForkId.create(ethGenesisHash, ethConf)(head) |
| 38 | + |
| 39 | + create(0) shouldBe ForkId(0xfc64ec04L, Some(1150000)) // Unsynced |
| 40 | + create(1149999) shouldBe ForkId(0xfc64ec04L, Some(1150000)) // Last Frontier block |
| 41 | + create(1150000) shouldBe ForkId(0x97c2c34cL, Some(1920000)) // First Homestead block |
| 42 | + create(1919999) shouldBe ForkId(0x97c2c34cL, Some(1920000)) // Last Homestead block |
| 43 | + create(1920000) shouldBe ForkId(0x91d1f948L, Some(2463000)) // First DAO block |
| 44 | + create(2462999) shouldBe ForkId(0x91d1f948L, Some(2463000)) // Last DAO block |
| 45 | + create(2463000) shouldBe ForkId(0x7a64da13L, Some(2675000)) // First Tangerine block |
| 46 | + create(2674999) shouldBe ForkId(0x7a64da13L, Some(2675000)) // Last Tangerine block |
| 47 | + create(2675000) shouldBe ForkId(0x3edd5b10L, Some(4370000)) // First Spurious block |
| 48 | + create(4369999) shouldBe ForkId(0x3edd5b10L, Some(4370000)) // Last Spurious block |
| 49 | + create(4370000) shouldBe ForkId(0xa00bc324L, Some(7280000)) // First Byzantium block |
| 50 | + create(7279999) shouldBe ForkId(0xa00bc324L, Some(7280000)) // Last Byzantium block |
| 51 | + create(7280000) shouldBe ForkId(0x668db0afL, Some(9069000)) // First and last Constantinople, first Petersburg block |
| 52 | + create(9068999) shouldBe ForkId(0x668db0afL, Some(9069000)) // Last Petersburg block |
| 53 | + // TODO: Add Muir Glacier and Berlin |
| 54 | + create(9069000) shouldBe ForkId(0x879d6e30L, None) // First Istanbul block |
| 55 | + create(12644529) shouldBe ForkId(0x879d6e30L, None) // Today Istanbul block |
| 56 | + } |
| 57 | + |
| 58 | + "create correct ForkId for ETC mainnet blocks" in { |
| 59 | + val etcConf = config.blockchains("etc") |
| 60 | + val etcGenesisHash = ByteString(Hex.decode("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")) |
| 61 | + def create(head: BigInt) = ForkId.create(etcGenesisHash, etcConf)(head) |
| 62 | + |
| 63 | + create(0) shouldBe ForkId(0xfc64ec04L, Some(1150000)) // Unsynced |
| 64 | + create(1149999) shouldBe ForkId(0xfc64ec04L, Some(1150000)) // Last Frontier block |
| 65 | + create(1150000) shouldBe ForkId(0x97c2c34cL, Some(2500000)) // First Homestead block |
| 66 | + create(1919999) shouldBe ForkId(0x97c2c34cL, Some(2500000)) // Last Homestead block |
| 67 | + create(2500000) shouldBe ForkId(0xdb06803fL, Some(3000000)) |
| 68 | + create(3000000-1) shouldBe ForkId(0xdb06803fL, Some(3000000)) |
| 69 | + create(3000000) shouldBe ForkId(0xaff4bed4L, Some(5000000)) |
| 70 | + create(5000000-1) shouldBe ForkId(0xaff4bed4L, Some(5000000)) |
| 71 | + create(5000000) shouldBe ForkId(0xf79a63c0L, Some(5900000)) |
| 72 | + create(5900000-1) shouldBe ForkId(0xf79a63c0L, Some(5900000)) |
| 73 | + create(5900000) shouldBe ForkId(0x744899d6L, Some(8772000)) |
| 74 | + create(8772000-1) shouldBe ForkId(0x744899d6L, Some(8772000)) |
| 75 | + create(8772000) shouldBe ForkId(0x518b59c6L, Some(9573000)) |
| 76 | + create(9573000-1) shouldBe ForkId(0x518b59c6L, Some(9573000)) |
| 77 | + create(9573000) shouldBe ForkId(0x7ba22882L, Some(10500839)) |
| 78 | + create(10500839-1) shouldBe ForkId(0x7ba22882L, Some(10500839)) |
| 79 | + create(10500839) shouldBe ForkId(0x9007bfccL, Some(11700000)) |
| 80 | + create(11700000-1) shouldBe ForkId(0x9007bfccL, Some(11700000)) |
| 81 | + create(11700000) shouldBe ForkId(0xdb63a1caL, None) |
| 82 | + } |
| 83 | + |
| 84 | + "create correct ForkId for mordor blocks" in { |
| 85 | + val mordorConf = config.blockchains("mordor") |
| 86 | + val mordorGenesisHash = ByteString(Hex.decode("a68ebde7932eccb177d38d55dcc6461a019dd795a681e59b5a3e4f3a7259a3f1")) |
| 87 | + def create(head: BigInt) = ForkId.create(mordorGenesisHash, mordorConf)(head) |
| 88 | + |
| 89 | + create(0) shouldBe ForkId(0x175782aaL, Some(301243)) // Unsynced |
| 90 | + create(301242) shouldBe ForkId(0x175782aaL, Some(301243)) |
| 91 | + create(301243) shouldBe ForkId(0x604f6ee1L, Some(999983)) |
| 92 | + create(999982) shouldBe ForkId(0x604f6ee1L, Some(999983)) |
| 93 | + create(999983) shouldBe ForkId(0xf42f5539L, Some(2520000)) |
| 94 | + create(2519999) shouldBe ForkId(0xf42f5539L, Some(2520000)) |
| 95 | + create(2520000) shouldBe ForkId(0x66b5c286L, None) |
| 96 | + // TODO: Add Magneto |
| 97 | + // create(2520000) shouldBe ForkId(0x66b5c286L, Some(3985893)) |
| 98 | + // create(3985893) shouldBe ForkId(0x66b5c286L, Some(3985893)) |
| 99 | + // create(3985894) shouldBe ForkId(0x92b323e0L, None) |
| 100 | + } |
| 101 | + |
| 102 | + // 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): |
| 103 | + "be correctly encoded via rlp" in { |
| 104 | + roundTrip(ForkId(0, None), "c6840000000080") |
| 105 | + roundTrip(ForkId(0xdeadbeefL, Some(0xBADDCAFEL)), "ca84deadbeef84baddcafe") |
| 106 | + |
| 107 | + val maxUInt64 = (BigInt(0x7FFFFFFFFFFFFFFFL) << 1) + 1 |
| 108 | + maxUInt64.toByteArray shouldBe Array(0, -1, -1, -1, -1, -1, -1, -1, -1) |
| 109 | + val maxUInt32 = BigInt(0xFFFFFFFFL) |
| 110 | + maxUInt32.toByteArray shouldBe Array(0, -1, -1, -1, -1) |
| 111 | + |
| 112 | + roundTrip(ForkId(maxUInt32, Some(maxUInt64)), "ce84ffffffff88ffffffffffffffff") |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private def roundTrip(forkId: ForkId, hex: String) = { |
| 117 | + encode(forkId.toRLPEncodable) shouldBe Hex.decode(hex) |
| 118 | + decode[ForkId](Hex.decode(hex)) shouldBe forkId |
| 119 | + } |
| 120 | +} |
0 commit comments