Skip to content

Commit d5a847c

Browse files
fix ForkSchedule equals method (#474)
* fix ForkSchedule equals/hasCode method
1 parent 68d6d22 commit d5a847c

File tree

2 files changed

+91
-31
lines changed

2 files changed

+91
-31
lines changed

core/src/main/kotlin/maru/consensus/ForkSchedule.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,17 @@ class ForksSchedule(
101101

102102
other as ForksSchedule
103103

104-
return forks == other.forks
104+
if (chainId != other.chainId) return false
105+
if (forks.toSet() != other.forks.toSet()) return false
106+
107+
return true
105108
}
106109

107-
override fun hashCode(): Int = forks.hashCode()
110+
override fun hashCode(): Int {
111+
var result = chainId.hashCode()
112+
result = 31 * result + forks.hashCode()
113+
return result
114+
}
108115

109116
override fun toString(): String = "ForksSchedule(chainId=$chainId, forks=$forks)"
110117
}

core/src/test/kotlin/maru/consensus/ForksScheduleTest.kt

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
*/
99
package maru.consensus
1010

11+
import kotlin.random.Random
12+
import maru.core.Validator
1113
import org.assertj.core.api.Assertions.assertThat
14+
import org.junit.jupiter.api.Nested
1215
import org.junit.jupiter.api.Test
1316
import org.junit.jupiter.api.assertThrows
1417

@@ -120,35 +123,6 @@ class ForksScheduleTest {
120123
assertThat(exception).hasMessage("blockTimeSeconds must be greater or equal to 1 second")
121124
}
122125

123-
@Test
124-
fun equality() {
125-
val fork1 = ForkSpec(timestampSeconds = 1000UL, blockTimeSeconds = 10u, configuration = consensusConfig)
126-
val fork2 = ForkSpec(timestampSeconds = 2000UL, blockTimeSeconds = 20u, configuration = consensusConfig)
127-
val forks1 = listOf(fork1, fork2)
128-
val forks2 = listOf(fork1, fork2)
129-
130-
val schedule1 = ForksSchedule(expectedChainId, forks1)
131-
val schedule2 = ForksSchedule(expectedChainId, forks2)
132-
133-
assertThat(schedule1).isEqualTo(schedule2)
134-
assertThat(schedule1.hashCode()).isEqualTo(schedule2.hashCode())
135-
}
136-
137-
@Test
138-
fun inequality() {
139-
val fork1 = ForkSpec(timestampSeconds = 1000UL, blockTimeSeconds = 10u, configuration = consensusConfig)
140-
val fork2 = ForkSpec(timestampSeconds = 2000UL, blockTimeSeconds = 20u, configuration = consensusConfig)
141-
val fork3 = ForkSpec(timestampSeconds = 3000UL, blockTimeSeconds = 30u, configuration = consensusConfig)
142-
val forks1 = listOf(fork1, fork2)
143-
val forks2 = listOf(fork1, fork3)
144-
145-
val schedule1 = ForksSchedule(expectedChainId, forks1)
146-
val schedule2 = ForksSchedule(expectedChainId, forks2)
147-
148-
assertThat(schedule1).isNotEqualTo(schedule2)
149-
assertThat(schedule1.hashCode()).isNotEqualTo(schedule2.hashCode())
150-
}
151-
152126
@Test
153127
fun `getForkByConfigType throws exception when config class not found`() {
154128
val qbftFork = ForkSpec(timestampSeconds = 1000UL, blockTimeSeconds = 10u, configuration = qbftConsensusConfig)
@@ -257,4 +231,83 @@ class ForksScheduleTest {
257231
// Test with timestamp exactly one more than second fork
258232
assertThat(schedule.getNextForkByTimestamp(2001UL)).isNull()
259233
}
234+
235+
@Nested
236+
inner class EqualsAndHashCode {
237+
val validators = setOf(Validator(Random.nextBytes(20)))
238+
val difficultyAwareQbftFork =
239+
ForkSpec(
240+
timestampSeconds = 1000UL,
241+
blockTimeSeconds = 10u,
242+
configuration =
243+
DifficultyAwareQbftConfig(
244+
postTtdConfig =
245+
QbftConsensusConfig(
246+
validatorSet = validators,
247+
fork = ChainFork(ClFork.QBFT_PHASE0, ElFork.Prague),
248+
),
249+
terminalTotalDifficulty = 1000UL,
250+
),
251+
)
252+
val qbftFork =
253+
ForkSpec(
254+
timestampSeconds = 2000UL,
255+
blockTimeSeconds = 10u,
256+
configuration =
257+
QbftConsensusConfig(
258+
validatorSet = validators,
259+
fork = ChainFork(ClFork.QBFT_PHASE0, ElFork.Prague),
260+
),
261+
)
262+
263+
@Test
264+
fun `equals should return false with different consensus config`() {
265+
val schedule1 = ForksSchedule(expectedChainId, listOf(qbftFork))
266+
val schedule1Instance2 = ForksSchedule(expectedChainId, listOf(qbftFork))
267+
val schedule2 = ForksSchedule(expectedChainId, listOf(difficultyAwareQbftFork))
268+
val schedule2Instance2 = ForksSchedule(expectedChainId, listOf(difficultyAwareQbftFork))
269+
270+
assertThat(schedule1).isNotEqualTo(schedule2)
271+
assertThat(schedule1).isEqualTo(schedule1Instance2)
272+
assertThat(schedule2).isEqualTo(schedule2Instance2)
273+
274+
assertThat(schedule1.hashCode()).isNotEqualTo(schedule2.hashCode())
275+
assertThat(schedule1.hashCode()).isEqualTo(schedule1Instance2.hashCode())
276+
assertThat(schedule2.hashCode()).isEqualTo(schedule2Instance2.hashCode())
277+
}
278+
279+
@Test
280+
fun `equals should return take multiple forks into correct order`() {
281+
val forkList =
282+
listOf(
283+
difficultyAwareQbftFork,
284+
qbftFork.copy(timestampSeconds = difficultyAwareQbftFork.timestampSeconds + 1000UL),
285+
)
286+
val forkList2 =
287+
listOf(
288+
difficultyAwareQbftFork,
289+
qbftFork.copy(
290+
timestampSeconds = difficultyAwareQbftFork.timestampSeconds + 1000UL,
291+
configuration =
292+
QbftConsensusConfig(
293+
validatorSet = validators,
294+
fork = ChainFork(ClFork.QBFT_PHASE1, ElFork.Prague),
295+
),
296+
),
297+
)
298+
299+
val schedule1 = ForksSchedule(expectedChainId, forkList)
300+
val schedule1Instance2 = ForksSchedule(expectedChainId, forkList.reversed())
301+
val schedule2 = ForksSchedule(expectedChainId, forkList2)
302+
val schedule2Instance2 = ForksSchedule(expectedChainId, forkList2.reversed())
303+
304+
assertThat(schedule1).isNotEqualTo(schedule2)
305+
assertThat(schedule1).isEqualTo(schedule1Instance2)
306+
assertThat(schedule2).isEqualTo(schedule2Instance2)
307+
308+
assertThat(schedule1.hashCode()).isNotEqualTo(schedule2.hashCode())
309+
assertThat(schedule1.hashCode()).isEqualTo(schedule1Instance2.hashCode())
310+
assertThat(schedule2.hashCode()).isEqualTo(schedule2Instance2.hashCode())
311+
}
312+
}
260313
}

0 commit comments

Comments
 (0)