Skip to content

Commit 604899c

Browse files
pingesCopilot
andauthored
Throw RLPException that is handled correctly instead of IllegalArgumentException (#10025)
* throw RLPException that is handled correctly instead of IllegalArgumentException Signed-off-by: stefan.pingel@consensys.net <stefan.pingel@consensys.net> Signed-off-by: Stefan Pingel <16143240+pinges@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9ed7c54 commit 604899c

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/encoding/TransactionAnnouncementDecoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ private static List<TransactionAnnouncement> decodeForEth68(final RLPInput input
5959
final var transactionType =
6060
TransactionType.fromEthSerializedType(b)
6161
.orElseThrow(
62-
() -> new IllegalArgumentException("Invalid transaction type %x".formatted(b)));
62+
() ->
63+
new RLPException(
64+
"Invalid transaction type 0x%02x".formatted(Byte.toUnsignedInt(b))));
6365
types.add(transactionType);
6466
}
6567

ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/NewPooledTransactionHashesMessageProcessorTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
4444
import org.hyperledger.besu.ethereum.eth.messages.NewPooledTransactionHashesMessage;
4545
import org.hyperledger.besu.ethereum.eth.transactions.NewPooledTransactionHashesMessageProcessor.FetcherCreatorTask;
46+
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason;
4647
import org.hyperledger.besu.ethereum.rlp.RLP;
4748
import org.hyperledger.besu.ethereum.rlp.RLPException;
4849
import org.hyperledger.besu.metrics.StubMetricsSystem;
@@ -366,8 +367,8 @@ void shouldThrowRLPExceptionWhenTypeIsInvalid() {
366367
() ->
367368
TransactionAnnouncementDecoder.getDecoder(EthProtocol.ETH68)
368369
.decode(RLP.input(invalidMessageBytes)))
369-
.isInstanceOf(IllegalArgumentException.class)
370-
.hasMessageContaining("Invalid transaction type 7");
370+
.isInstanceOf(RLPException.class)
371+
.hasMessageContaining("Invalid transaction type 0x07");
371372
}
372373

373374
@Test
@@ -406,4 +407,19 @@ void shouldThrowNullPointerIfArgumentsAreNull() {
406407
.isInstanceOf(NullPointerException.class)
407408
.hasMessage("Transaction cannot be null");
408409
}
410+
411+
@Test
412+
void shouldDisconnectPeerWhenInvalidTransactionType() {
413+
final Bytes invalidMessageBytes =
414+
Bytes.fromHexString(
415+
// ["0x07",["0x00000002"],["0x881699519a25b0e32db9b1ba9981f3fbec93fbc0726c3e096af89e5ada2b1351"]]
416+
"0xe907c58400000002e1a0881699519a25b0e32db9b1ba9981f3fbec93fbc0726c3e096af89e5ada2b1351");
417+
418+
final NewPooledTransactionHashesMessage message =
419+
new NewPooledTransactionHashesMessage(invalidMessageBytes, EthProtocol.ETH68);
420+
421+
messageHandler.processNewPooledTransactionHashesMessage(peer1, message, now(), ofMinutes(1));
422+
423+
verify(peer1).disconnect(DisconnectReason.BREACH_OF_PROTOCOL_MALFORMED_MESSAGE_RECEIVED);
424+
}
409425
}

0 commit comments

Comments
 (0)