|
| 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.Config._ |
| 6 | +import io.iohk.ethereum.utils.ForkBlockNumbers |
| 7 | +import monix.eval.Task |
| 8 | +import monix.execution.Scheduler.Implicits.global |
| 9 | +import org.bouncycastle.util.encoders.Hex |
| 10 | +import org.scalatest.matchers.should._ |
| 11 | +import org.scalatest.wordspec.AnyWordSpec |
| 12 | + |
| 13 | +import scala.concurrent.duration._ |
| 14 | + |
| 15 | +import ForkIdValidator._ |
| 16 | + |
| 17 | +class ForkIdValidatorSpec extends AnyWordSpec with Matchers { |
| 18 | + |
| 19 | + val config = blockchains |
| 20 | + |
| 21 | + val ethGenesisHash = ByteString(Hex.decode("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")) |
| 22 | + |
| 23 | + "ForkIdValidator" must { |
| 24 | + "correctly validate ETH peers" in { |
| 25 | + // latest fork at the time of writing those assertions (in the spec) was Petersburg |
| 26 | + val ethForksList: List[BigInt] = List(1150000, 1920000, 2463000, 2675000, 4370000, 7280000) |
| 27 | + |
| 28 | + def validatePeer(head: BigInt, remoteForkId: ForkId) = |
| 29 | + ForkIdValidator |
| 30 | + .validatePeer[Task](ethGenesisHash, ethForksList)(head, remoteForkId) |
| 31 | + .runSyncUnsafe(Duration(1, SECONDS)) |
| 32 | + |
| 33 | + // Local is mainnet Petersburg, remote announces the same. No future fork is announced. |
| 34 | + validatePeer(7987396, ForkId(0x668db0afL, None)) shouldBe Connect |
| 35 | + |
| 36 | + // Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork |
| 37 | + // at block 0xffffffff, but that is uncertain. |
| 38 | + validatePeer(7279999, ForkId(0xa00bc324L, Some(ForkIdValidator.maxUInt64))) shouldBe Connect |
| 39 | + |
| 40 | + // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces |
| 41 | + // also Byzantium, and it's also aware of Petersburg (e.g. updated node before the fork). We |
| 42 | + // don't know if Petersburg passed yet (will pass) or not. |
| 43 | + validatePeer(7279999, ForkId(0xa00bc324L, Some(7280000))) shouldBe Connect |
| 44 | + |
| 45 | + // Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork |
| 46 | + // at block 0xffffffff, but that is uncertain. |
| 47 | + validatePeer(7987396, ForkId(0x668db0afL, Some(ForkIdValidator.maxUInt64))) shouldBe Connect |
| 48 | + |
| 49 | + // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces |
| 50 | + // also Byzantium, but it's not yet aware of Petersburg (e.g. non updated node before the fork). |
| 51 | + // In this case we don't know if Petersburg passed yet or not. |
| 52 | + validatePeer(7279999, ForkId(0xa00bc324L, None)) shouldBe Connect |
| 53 | + |
| 54 | + validatePeer(7279999, ForkId(0xa00bc324L, Some(7280000))) shouldBe Connect |
| 55 | + |
| 56 | + // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces |
| 57 | + // also Byzantium, and it's also aware of some random fork (e.g. misconfigured Petersburg). As |
| 58 | + // neither forks passed at neither nodes, they may mismatch, but we still connect for now. |
| 59 | + validatePeer(7279999, ForkId(0xa00bc324L, Some(ForkIdValidator.maxUInt64))) shouldBe Connect |
| 60 | + |
| 61 | + // Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote |
| 62 | + // is simply out of sync, accept. |
| 63 | + validatePeer(7987396, ForkId(0xa00bc324L, Some(7280000))) shouldBe Connect |
| 64 | + |
| 65 | + // Local is mainnet Petersburg, remote announces Spurious + knowledge about Byzantium. Remote |
| 66 | + // is definitely out of sync. It may or may not need the Petersburg update, we don't know yet. |
| 67 | + validatePeer(7987396, ForkId(0x3edd5b10L, Some(4370000))) shouldBe Connect |
| 68 | + |
| 69 | + // Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept. |
| 70 | + validatePeer(7279999, ForkId(0x668db0afL, None)) shouldBe Connect |
| 71 | + |
| 72 | + // Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. Local |
| 73 | + // out of sync. Local also knows about a future fork, but that is uncertain yet. |
| 74 | + validatePeer(4369999, ForkId(0xa00bc324L, None)) shouldBe Connect |
| 75 | + |
| 76 | + // Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks. |
| 77 | + // Remote needs software update. |
| 78 | + validatePeer(7987396, ForkId(0xa00bc324L, None)) shouldBe ErrRemoteStale |
| 79 | + |
| 80 | + // Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg + |
| 81 | + // 0xffffffff. Local needs software update, reject. |
| 82 | + validatePeer(7987396, ForkId(0x5cddc0e1L, None)) shouldBe ErrLocalIncompatibleOrStale |
| 83 | + |
| 84 | + // Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg + |
| 85 | + // 0xffffffff. Local needs software update, reject. |
| 86 | + validatePeer(7279999, ForkId(0x5cddc0e1L, None)) shouldBe ErrLocalIncompatibleOrStale |
| 87 | + |
| 88 | + // Local is mainnet Petersburg, remote is Rinkeby Petersburg. |
| 89 | + validatePeer(7987396, ForkId(0xafec6b27L, None)) shouldBe ErrLocalIncompatibleOrStale |
| 90 | + |
| 91 | + // Local is mainnet Petersburg, far in the future. Remote announces Gopherium (non existing fork) |
| 92 | + // at some future block 88888888, for itself, but past block for local. Local is incompatible. |
| 93 | + // |
| 94 | + // This case detects non-upgraded nodes with majority hash power (typical Ropsten mess). |
| 95 | + validatePeer(88888888, ForkId(0x668db0afL, Some(88888888))) shouldBe ErrLocalIncompatibleOrStale |
| 96 | + |
| 97 | + // Local is mainnet Byzantium. Remote is also in Byzantium, but announces Gopherium (non existing |
| 98 | + // fork) at block 7279999, before Petersburg. Local is incompatible. |
| 99 | + validatePeer(7279999, ForkId(0xa00bc324L, Some(7279999L))) shouldBe ErrLocalIncompatibleOrStale |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments