diff --git a/src/main/kotlin/io/emeraldpay/dshackle/startup/configure/UpstreamCreator.kt b/src/main/kotlin/io/emeraldpay/dshackle/startup/configure/UpstreamCreator.kt index 9ce08e3c8..cd6f28ae6 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/startup/configure/UpstreamCreator.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/startup/configure/UpstreamCreator.kt @@ -23,7 +23,9 @@ abstract class UpstreamCreator( companion object { fun getHash(nodeId: Int?, obj: Any, hashes: MutableMap): Short = - nodeId?.toShort() ?: (obj.hashCode() % 65535).let { + nodeId?.toShort()?.also { + hashes[it] = true + } ?: (obj.hashCode() % 65535).let { if (it == 0) 1 else it }.let { nonZeroHash -> listOf>( diff --git a/src/test/kotlin/io/emeraldpay/dshackle/startup/configure/UpstreamCreatorTest.kt b/src/test/kotlin/io/emeraldpay/dshackle/startup/configure/UpstreamCreatorTest.kt new file mode 100644 index 000000000..a467525b9 --- /dev/null +++ b/src/test/kotlin/io/emeraldpay/dshackle/startup/configure/UpstreamCreatorTest.kt @@ -0,0 +1,38 @@ +package io.emeraldpay.dshackle.startup.configure + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource + +class UpstreamCreatorTest { + + @ParameterizedTest + @MethodSource("data") + fun `test getHash`( + inputHash: Int?, + obj: Any, + answer: Short + ) { + val hash = UpstreamCreator.getHash(inputHash, obj, hashes) + + assertThat(hash).isEqualTo(answer) + } + + companion object { + private val hashes: MutableMap = HashMap() + + @JvmStatic + fun data(): List = + listOf( + Arguments.of(49, 1, 49.toShort()), + Arguments.of(4000, 1, 4000.toShort()), + Arguments.of(24000, 1, 24000.toShort()), + Arguments.of(null, 49, (-49).toShort()), + Arguments.of(null, 49, (32718).toShort()), + Arguments.of(null, 49, (-32719).toShort()), + Arguments.of(null, 49, (-32768).toShort()), + Arguments.of(null, 49, (-32767).toShort()) + ) + } +}