This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/main by this push:
     new b1c1219b Handle when headers don't match bodies
     new d32fe8b1 Merge pull request #433 from atoulme/handle_mismatch
b1c1219b is described below

commit b1c1219b2931959306d49a4e9276c27c6eae026a
Author: Antoine Toulme <[email protected]>
AuthorDate: Tue Aug 9 00:13:08 2022 -0700

    Handle when headers don't match bodies
---
 devp2p-eth/build.gradle                            |  1 +
 .../org/apache/tuweni/devp2p/eth/EthClient66.kt    |  2 +-
 .../org/apache/tuweni/devp2p/eth/EthController.kt  |  4 ++
 .../apache/tuweni/devp2p/eth/EthControllerTest.kt  | 47 ++++++++++++++++++++++
 4 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/devp2p-eth/build.gradle b/devp2p-eth/build.gradle
index fa141da0..14da9ad7 100644
--- a/devp2p-eth/build.gradle
+++ b/devp2p-eth/build.gradle
@@ -41,6 +41,7 @@ dependencies {
   testImplementation 'org.junit.jupiter:junit-jupiter-api'
   testImplementation 'org.junit.jupiter:junit-jupiter-params'
   testImplementation 'org.mockito:mockito-junit-jupiter'
+  testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin'
 
   testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
   testRuntimeOnly 'ch.qos.logback:logback-classic'
diff --git 
a/devp2p-eth/src/main/kotlin/org/apache/tuweni/devp2p/eth/EthClient66.kt 
b/devp2p-eth/src/main/kotlin/org/apache/tuweni/devp2p/eth/EthClient66.kt
index 130376f2..9bc614ef 100644
--- a/devp2p-eth/src/main/kotlin/org/apache/tuweni/devp2p/eth/EthClient66.kt
+++ b/devp2p-eth/src/main/kotlin/org/apache/tuweni/devp2p/eth/EthClient66.kt
@@ -36,7 +36,7 @@ import org.slf4j.LoggerFactory
 /**
  * Client of the ETH subprotocol, allowing to request block and node data
  */
-class EthClient66(
+open class EthClient66(
   private val service: RLPxService,
   private val pendingTransactionsPool: TransactionPool,
   private val connectionSelectionStrategy: ConnectionSelectionStrategy,
diff --git 
a/devp2p-eth/src/main/kotlin/org/apache/tuweni/devp2p/eth/EthController.kt 
b/devp2p-eth/src/main/kotlin/org/apache/tuweni/devp2p/eth/EthController.kt
index 97eb021d..545740e5 100644
--- a/devp2p-eth/src/main/kotlin/org/apache/tuweni/devp2p/eth/EthController.kt
+++ b/devp2p-eth/src/main/kotlin/org/apache/tuweni/devp2p/eth/EthController.kt
@@ -129,6 +129,10 @@ class EthController(
       return
     }
     val hashes = request.data as List<*>
+    if (hashes.size != bodies.size) {
+      logger.warn("Block bodies size ${bodies.size} does not match the block 
header hashes size ${hashes.size}")
+      return
+    }
     for (i in 0 until hashes.size) {
       repository.storeBlockBody(hashes[i] as Hash, bodies[i])
     }
diff --git 
a/devp2p-eth/src/test/kotlin/org/apache/tuweni/devp2p/eth/EthControllerTest.kt 
b/devp2p-eth/src/test/kotlin/org/apache/tuweni/devp2p/eth/EthControllerTest.kt
new file mode 100644
index 00000000..0be3bca4
--- /dev/null
+++ 
b/devp2p-eth/src/test/kotlin/org/apache/tuweni/devp2p/eth/EthControllerTest.kt
@@ -0,0 +1,47 @@
+/*
+ * 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.eth
+
+import kotlinx.coroutines.runBlocking
+import org.apache.tuweni.bytes.Bytes
+import org.apache.tuweni.eth.BlockBody
+import org.apache.tuweni.eth.repository.BlockchainRepository
+import org.apache.tuweni.eth.repository.MemoryTransactionPool
+import org.apache.tuweni.genesis.Genesis
+import org.apache.tuweni.rlpx.wire.WireConnection
+import org.junit.jupiter.api.Test
+import com.nhaarman.mockitokotlin2.mock
+import org.apache.tuweni.junit.BouncyCastleExtension
+import org.junit.jupiter.api.extension.ExtendWith
+
+@ExtendWith(BouncyCastleExtension::class)
+class EthControllerTest {
+
+  @Test
+  fun testMismatchHeaders() = runBlocking {
+
+    val requestsManager = EthClient66(mock {}, mock {}, mock {})
+    val controller = EthController(
+      BlockchainRepository.inMemory(Genesis.dev()),
+      MemoryTransactionPool(),
+      requestsManager,
+    ) { _, _ -> }
+    val conn = mock<WireConnection> {}
+
+    controller.addNewBlockBodies(conn, Bytes.fromHexString("0x0010"), 
listOf(BlockBody(emptyList(), emptyList())))
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to