|
| 1 | +/* |
| 2 | + * Copyright Consensys Software Inc., 2022 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package tech.pegasys.teku.spec.config; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import tech.pegasys.teku.spec.Spec; |
| 20 | +import tech.pegasys.teku.spec.TestSpecFactory; |
| 21 | +import tech.pegasys.teku.spec.util.DataStructureUtil; |
| 22 | + |
| 23 | +public class SpecConfigElectraTest { |
| 24 | + private final Spec spec = TestSpecFactory.createMinimalDeneb(); |
| 25 | + |
| 26 | + @Test |
| 27 | + public void equals_mainnet() { |
| 28 | + final SpecConfigElectra configA = |
| 29 | + SpecConfigLoader.loadConfig("mainnet").toVersionElectra().orElseThrow(); |
| 30 | + final SpecConfigElectra configB = |
| 31 | + SpecConfigLoader.loadConfig("mainnet").toVersionElectra().orElseThrow(); |
| 32 | + |
| 33 | + assertThat(configA).isEqualTo(configB); |
| 34 | + assertThat(configA.hashCode()).isEqualTo(configB.hashCode()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void equals_sameRandomValues() { |
| 39 | + final SpecConfigDeneb specConfigDeneb = |
| 40 | + SpecConfigLoader.loadConfig("mainnet").toVersionDeneb().orElseThrow(); |
| 41 | + final SpecConfigElectra configA = createRandomElectraConfig(specConfigDeneb, 1); |
| 42 | + final SpecConfigElectra configB = createRandomElectraConfig(specConfigDeneb, 1); |
| 43 | + |
| 44 | + assertThat(configA).isEqualTo(configB); |
| 45 | + assertThat(configA.hashCode()).isEqualTo(configB.hashCode()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void equals_differentRandomValues() { |
| 50 | + final SpecConfigDeneb specConfigDeneb = |
| 51 | + SpecConfigLoader.loadConfig("mainnet").toVersionDeneb().orElseThrow(); |
| 52 | + final SpecConfigElectra configA = createRandomElectraConfig(specConfigDeneb, 1); |
| 53 | + final SpecConfigElectra configB = createRandomElectraConfig(specConfigDeneb, 2); |
| 54 | + |
| 55 | + assertThat(configA).isNotEqualTo(configB); |
| 56 | + assertThat(configA.hashCode()).isNotEqualTo(configB.hashCode()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void equals_denebConfigDiffer() { |
| 61 | + final SpecConfigDeneb denebA = |
| 62 | + SpecConfigLoader.loadConfig("mainnet").toVersionDeneb().orElseThrow(); |
| 63 | + final SpecConfigDeneb denebB = |
| 64 | + SpecConfigLoader.loadConfig( |
| 65 | + "mainnet", |
| 66 | + b -> b.denebBuilder(db -> db.maxBlobsPerBlock(denebA.getMaxBlobsPerBlock() + 4))) |
| 67 | + .toVersionDeneb() |
| 68 | + .orElseThrow(); |
| 69 | + |
| 70 | + final SpecConfigElectra configA = createRandomElectraConfig(denebA, 1); |
| 71 | + final SpecConfigElectra configB = createRandomElectraConfig(denebB, 1); |
| 72 | + |
| 73 | + assertThat(configA).isNotEqualTo(configB); |
| 74 | + assertThat(configA.hashCode()).isNotEqualTo(configB.hashCode()); |
| 75 | + } |
| 76 | + |
| 77 | + private SpecConfigElectra createRandomElectraConfig( |
| 78 | + final SpecConfigDeneb denebConfig, final int seed) { |
| 79 | + final DataStructureUtil dataStructureUtil = new DataStructureUtil(seed, spec); |
| 80 | + |
| 81 | + return new SpecConfigElectraImpl( |
| 82 | + denebConfig, |
| 83 | + dataStructureUtil.randomBytes4(), |
| 84 | + dataStructureUtil.randomUInt64(), |
| 85 | + dataStructureUtil.randomPositiveInt()) {}; |
| 86 | + } |
| 87 | +} |
0 commit comments