Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Dec 11, 2022
1 parent 1fa7d45 commit f27f80d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package org.apache.tuweni.devp2p

import com.google.common.cache.Cache
import com.google.common.cache.CacheBuilder
import com.google.common.util.concurrent.UncheckedExecutionException
import org.apache.tuweni.crypto.SECP256K1
import org.apache.tuweni.kademlia.KademliaRoutingTable
import java.lang.IllegalArgumentException

/**
* A routing table for ÐΞVp2p peers.
Expand Down Expand Up @@ -98,8 +100,11 @@ internal class DevP2PPeerRoutingTable(selfId: SECP256K1.PublicKey) : PeerRouting
private fun hashForId(id: SECP256K1.PublicKey): ByteArray? {
try {
return idHashCache.get(id) { EthereumNodeRecord.nodeId(id).toArrayUnsafe() }
} catch (e: IllegalArgumentException) {
return null
} catch (e: UncheckedExecutionException) {
if (e.cause is IllegalArgumentException) {
return null
}
throw e
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tuweni.devp2p.v5

import org.apache.tuweni.bytes.Bytes
import org.apache.tuweni.crypto.SECP256K1
import org.apache.tuweni.devp2p.DevP2PPeerRoutingTable
import org.apache.tuweni.junit.BouncyCastleExtension
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(BouncyCastleExtension::class)
class PeerRoutingTableTest {

@Test
fun invalidPublicKey() {
val routingTable = DevP2PPeerRoutingTable(SECP256K1.KeyPair.random().publicKey())
val invalidPublicKey = SECP256K1.PublicKey.fromBytes(Bytes.repeat(0, 64))
val peers = routingTable.nearest(invalidPublicKey, 3)
assertEquals(0, peers.size)
}
}

0 comments on commit f27f80d

Please sign in to comment.