Skip to content

Commit bf6d2b4

Browse files
authored
TransactionDecoder: handle empty bytes (#8997)
* handle empty bytes * check for empty before getting tx type Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com> --------- Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
1 parent 6aef9cd commit bf6d2b4

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/TransactionDecoder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ private static Transaction decodeTypedTransaction(
108108
*/
109109
public static Transaction decodeOpaqueBytes(
110110
final Bytes opaqueBytes, final EncodingContext context) {
111+
if (opaqueBytes.isEmpty()) {
112+
throw new IllegalArgumentException("Empty opaque bytes");
113+
}
111114
var transactionType = getTransactionType(opaqueBytes);
112115
if (transactionType.isPresent()) {
113116
return decodeTypedTransaction(opaqueBytes, transactionType.get(), context);
@@ -128,6 +131,9 @@ public static Transaction decodeOpaqueBytes(
128131
*/
129132
private static Optional<TransactionType> getTransactionType(final Bytes opaqueBytes) {
130133
try {
134+
if (opaqueBytes.isEmpty()) {
135+
return Optional.empty();
136+
}
131137
byte transactionTypeByte = opaqueBytes.get(0);
132138
return Optional.of(TransactionType.of(transactionTypeByte));
133139
} catch (IllegalArgumentException ex) {

ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionRLPDecoderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ void shouldDecodeWithHighNonce() {
115115
assertThat(transaction.getNonce()).isEqualTo(MAX_NONCE - 1);
116116
}
117117

118+
@Test
119+
void shouldThrowIllegalArgWithEmptyBytes() {
120+
assertThatThrownBy(
121+
() -> TransactionDecoder.decodeOpaqueBytes(Bytes.EMPTY, EncodingContext.BLOCK_BODY))
122+
.isInstanceOf(IllegalArgumentException.class);
123+
}
124+
118125
@Test
119126
void testForAccessListTransaction() {
120127
BlockDataGenerator gen = new BlockDataGenerator();

0 commit comments

Comments
 (0)