-
Notifications
You must be signed in to change notification settings - Fork 981
Fix issues with RLPExceptions #5140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
a47afef
15d4298
47ba092
5ce791b
6406e1d
29c5f90
308fa94
c3b0d86
88cc399
2b9ce4b
46c1dbe
bf503bf
fd25eb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import org.hyperledger.besu.ethereum.rlp.RLPInput; | ||
| import org.hyperledger.besu.plugin.data.TransactionType; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
|
|
@@ -69,13 +70,14 @@ private static List<TransactionAnnouncement> decodeForEth66(final RLPInput input | |
| */ | ||
| private static List<TransactionAnnouncement> decodeForEth68(final RLPInput input) { | ||
| input.enterList(); | ||
| final List<TransactionType> types = | ||
| input.readList( | ||
| rlp -> { | ||
| final int type = rlp.readByte() & 0xff; | ||
| return type == 0 ? TransactionType.FRONTIER : TransactionType.of(type); | ||
| }); | ||
| final List<Long> sizes = input.readList(RLPInput::readUnsignedInt); | ||
|
|
||
| final List<TransactionType> types = new ArrayList<>(); | ||
| final byte[] bytes = input.readBytes().toArray(); | ||
| for (final byte b : bytes) { | ||
| types.add(b == 0 ? TransactionType.FRONTIER : TransactionType.of(b)); | ||
| } | ||
|
Comment on lines
+74
to
+78
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix decoding |
||
|
|
||
| final List<Long> sizes = input.readList(in -> (long) in.readBytes().toInt()); | ||
| final List<Hash> hashes = input.readList(rlp -> Hash.wrap(rlp.readBytes32())); | ||
| input.leaveList(); | ||
| if (!(types.size() == hashes.size() && hashes.size() == sizes.size())) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,30 +76,42 @@ private static Bytes encodeForEth66(final List<Transaction> transactions) { | |
| */ | ||
| private static Bytes encodeForEth68(final List<Transaction> transactions) { | ||
| final List<Integer> sizes = new ArrayList<>(transactions.size()); | ||
| final List<TransactionType> types = new ArrayList<>(transactions.size()); | ||
| final byte[] types = new byte[transactions.size()]; | ||
| final List<Hash> hashes = new ArrayList<>(transactions.size()); | ||
| transactions.forEach( | ||
| transaction -> { | ||
| types.add(transaction.getType()); | ||
| sizes.add(transaction.getSize()); | ||
| hashes.add(transaction.getHash()); | ||
| }); | ||
|
|
||
| for (int i = 0; i < transactions.size(); i++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do prefer the forEach ... But the for loop should work as well :-) |
||
| final TransactionType type = transactions.get(i).getType(); | ||
| types[i] = type == TransactionType.FRONTIER ? 0x00 : type.getSerializedType(); | ||
| sizes.add(transactions.get(i).getSize()); | ||
| hashes.add(transactions.get(i).getHash()); | ||
| } | ||
|
|
||
| return encodeForEth68(types, sizes, hashes); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| public static Bytes encodeForEth68( | ||
| final List<TransactionType> types, final List<Integer> sizes, final List<Hash> hashes) { | ||
|
|
||
| final byte[] byteTypes = new byte[types.size()]; | ||
| for (int i = 0; i < types.size(); i++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. forEach would work here as well :-) |
||
| final TransactionType type = types.get(i); | ||
| byteTypes[i] = type == TransactionType.FRONTIER ? 0x00 : type.getSerializedType(); | ||
| } | ||
| return encodeForEth68(byteTypes, sizes, hashes); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| public static Bytes encodeForEth68( | ||
| final byte[] types, final List<Integer> sizes, final List<Hash> hashes) { | ||
| final BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
| // Check if lists have the same size | ||
| if (!(types.size() == hashes.size() && hashes.size() == sizes.size())) { | ||
| if (!(types.length == hashes.size() && hashes.size() == sizes.size())) { | ||
| throw new IllegalArgumentException( | ||
| "Hashes, sizes and types must have the same number of elements"); | ||
| } | ||
| out.startList(); | ||
| out.writeList( | ||
| types, (h, w) -> w.writeByte(h == TransactionType.FRONTIER ? 0x00 : h.getSerializedType())); | ||
| out.writeBytes(Bytes.wrap((types))); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix encoding |
||
| out.writeList(sizes, (h, w) -> w.writeInt(h)); | ||
| out.writeList(hashes, (h, w) -> w.writeBytes(h)); | ||
| out.endList(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,6 @@ public List<BlockBody> bodies(final ProtocolSchedule protocolSchedule) { | |
| final BlockHeaderFunctions blockHeaderFunctions = | ||
| ScheduleBasedBlockHeaderFunctions.create(protocolSchedule); | ||
| return new BytesValueRLPInput(data, false) | ||
| .readList(rlp -> BlockBody.readFrom(rlp, blockHeaderFunctions)); | ||
| .readList(rlp -> BlockBody.readFrom(rlp, blockHeaderFunctions, true)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allow empty block bodies here |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,16 +14,22 @@ | |
| */ | ||
| package org.hyperledger.besu.ethereum.eth.messages; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.hyperledger.besu.config.GenesisConfigFile; | ||
| import org.hyperledger.besu.ethereum.core.BlockBody; | ||
| import org.hyperledger.besu.ethereum.core.BlockHeader; | ||
| import org.hyperledger.besu.ethereum.core.BlockHeaderFunctions; | ||
| import org.hyperledger.besu.ethereum.core.Transaction; | ||
| import org.hyperledger.besu.ethereum.difficulty.fixed.FixedDifficultyProtocolSchedule; | ||
| import org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions; | ||
| import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; | ||
| import org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions; | ||
| import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData; | ||
| import org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage; | ||
| import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput; | ||
| import org.hyperledger.besu.ethereum.rlp.RLP; | ||
| import org.hyperledger.besu.ethereum.rlp.RLPException; | ||
| import org.hyperledger.besu.ethereum.rlp.RLPInput; | ||
| import org.hyperledger.besu.evm.internal.EvmConfiguration; | ||
|
|
||
|
|
@@ -36,11 +42,21 @@ | |
| import com.google.common.io.Resources; | ||
| import org.apache.tuweni.bytes.Bytes; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| /** Tests for {@link BlockBodiesMessage}. */ | ||
| public final class BlockBodiesMessageTest { | ||
|
|
||
| private ProtocolSchedule protocolSchedule; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| protocolSchedule = | ||
| FixedDifficultyProtocolSchedule.create( | ||
| GenesisConfigFile.development().getConfigOptions(), false, EvmConfiguration.DEFAULT); | ||
| } | ||
|
|
||
| @Test | ||
| public void blockBodiesRoundTrip() throws IOException { | ||
| final List<BlockBody> bodies = new ArrayList<>(); | ||
|
|
@@ -65,16 +81,37 @@ public void blockBodiesRoundTrip() throws IOException { | |
| final MessageData initialMessage = BlockBodiesMessage.create(bodies); | ||
| final MessageData raw = new RawMessage(EthPV62.BLOCK_BODIES, initialMessage.getData()); | ||
| final BlockBodiesMessage message = BlockBodiesMessage.readFrom(raw); | ||
| final Iterator<BlockBody> readBodies = | ||
| message | ||
| .bodies( | ||
| FixedDifficultyProtocolSchedule.create( | ||
| GenesisConfigFile.development().getConfigOptions(), | ||
| false, | ||
| EvmConfiguration.DEFAULT)) | ||
| .iterator(); | ||
| final Iterator<BlockBody> readBodies = message.bodies(protocolSchedule).iterator(); | ||
| for (int i = 0; i < 50; ++i) { | ||
| Assertions.assertThat(readBodies.next()).isEqualTo(bodies.get(i)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldEncodeEmptyBlocksInBlockBodiesMessage() { | ||
| final Bytes bytes = Bytes.fromHexString("0xc2c0c0"); | ||
| final MessageData raw = new RawMessage(EthPV62.BLOCK_BODIES, bytes); | ||
| final BlockBodiesMessage message = BlockBodiesMessage.readFrom(raw); | ||
| final List<BlockBody> bodies = message.bodies(protocolSchedule); | ||
| bodies.forEach(blockBody -> Assertions.assertThat(blockBody.isEmpty()).isTrue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldNotThrowRLPExceptionIfAllowedEmptyBody() { | ||
| final Bytes bytes = Bytes.fromHexString("0xc0"); | ||
| final BlockBody empty = BlockBody.readFrom(RLP.input(bytes), null, true); | ||
| Assertions.assertThat(empty.isEmpty()).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldThrowRLPExceptionIfNotAllowedEmptyBody() { | ||
| final Bytes bytes = Bytes.fromHexString("0xc0"); | ||
| final BlockHeaderFunctions blockHeaderFunctions = | ||
| ScheduleBasedBlockHeaderFunctions.create(protocolSchedule); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the blockHeaderFunctions used?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not created for the other test shouldNotThrowRLPExceptionIfAllowedEmptyBody |
||
| assertThrows( | ||
| RLPException.class, | ||
| () -> { | ||
| BlockBody.readFrom(RLP.input(bytes), blockHeaderFunctions, false); | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle empty block body