This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
[PAN-2811] Be more lenient with discovery message deserialization #1580
Merged
mbaxter
merged 4 commits into
PegaSysEng:master
from
mbaxter:PAN-2811/fix-discovery-msg-deserialization
Jun 19, 2019
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
909a45e
Be more lenient with discovery message deserialization
mbaxter 2472d8c
Merge branch 'master' into PAN-2811/fix-discovery-msg-deserialization
mbaxter 97d9043
Code review - clean up RLPInput API
mbaxter 3fdf56c
Merge branch 'master' into PAN-2811/fix-discovery-msg-deserialization
mbaxter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...va/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/FindNeighborsPacketDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed 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 tech.pegasys.pantheon.ethereum.p2p.discovery.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer; | ||
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput; | ||
import tech.pegasys.pantheon.ethereum.rlp.RLP; | ||
import tech.pegasys.pantheon.util.bytes.BytesValue; | ||
|
||
import org.junit.Test; | ||
|
||
public class FindNeighborsPacketDataTest { | ||
@Test | ||
public void serializeDeserialize() { | ||
final long time = System.currentTimeMillis(); | ||
final BytesValue target = Peer.randomId(); | ||
|
||
final FindNeighborsPacketData packet = FindNeighborsPacketData.create(target); | ||
final BytesValue serialized = RLP.encode(packet::writeTo); | ||
final FindNeighborsPacketData deserialized = | ||
FindNeighborsPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getTarget()).isEqualTo(target); | ||
assertThat(deserialized.getExpiration()).isGreaterThan(time); | ||
} | ||
|
||
@Test | ||
public void readFrom() { | ||
final long time = System.currentTimeMillis(); | ||
final BytesValue target = Peer.randomId(); | ||
|
||
BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeBytesValue(target); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
final BytesValue encoded = out.encoded(); | ||
|
||
final FindNeighborsPacketData deserialized = | ||
FindNeighborsPacketData.readFrom(RLP.input(encoded)); | ||
assertThat(deserialized.getTarget()).isEqualTo(target); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
|
||
@Test | ||
public void readFrom_withExtraFields() { | ||
final long time = System.currentTimeMillis(); | ||
final BytesValue target = Peer.randomId(); | ||
|
||
BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeBytesValue(target); | ||
out.writeLongScalar(time); | ||
// Add extra list elements | ||
out.writeLong(123L); | ||
out.endList(); | ||
final BytesValue encoded = out.encoded(); | ||
|
||
final FindNeighborsPacketData deserialized = | ||
FindNeighborsPacketData.readFrom(RLP.input(encoded)); | ||
assertThat(deserialized.getTarget()).isEqualTo(target); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...t/java/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/NeighborsPacketDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed 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 tech.pegasys.pantheon.ethereum.p2p.discovery.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static tech.pegasys.pantheon.ethereum.p2p.peers.PeerTestHelper.enode; | ||
|
||
import tech.pegasys.pantheon.ethereum.p2p.discovery.DiscoveryPeer; | ||
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput; | ||
import tech.pegasys.pantheon.ethereum.rlp.RLP; | ||
import tech.pegasys.pantheon.util.bytes.BytesValue; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
public class NeighborsPacketDataTest { | ||
|
||
@Test | ||
public void serializeDeserialize() { | ||
final long time = System.currentTimeMillis(); | ||
final List<DiscoveryPeer> peers = | ||
Arrays.asList(DiscoveryPeer.fromEnode(enode()), DiscoveryPeer.fromEnode(enode())); | ||
|
||
final NeighborsPacketData packet = NeighborsPacketData.create(peers); | ||
final BytesValue serialized = RLP.encode(packet::writeTo); | ||
final NeighborsPacketData deserialized = NeighborsPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getNodes()).isEqualTo(peers); | ||
assertThat(deserialized.getExpiration()).isGreaterThan(time); | ||
} | ||
|
||
@Test | ||
public void readFrom() { | ||
final long time = System.currentTimeMillis(); | ||
final List<DiscoveryPeer> peers = | ||
Arrays.asList(DiscoveryPeer.fromEnode(enode()), DiscoveryPeer.fromEnode(enode())); | ||
|
||
BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeList(peers, DiscoveryPeer::writeTo); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
BytesValue encoded = out.encoded(); | ||
|
||
final NeighborsPacketData deserialized = NeighborsPacketData.readFrom(RLP.input(encoded)); | ||
assertThat(deserialized.getNodes()).isEqualTo(peers); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
|
||
@Test | ||
public void readFrom_extraFields() { | ||
final long time = System.currentTimeMillis(); | ||
final List<DiscoveryPeer> peers = | ||
Arrays.asList(DiscoveryPeer.fromEnode(enode()), DiscoveryPeer.fromEnode(enode())); | ||
|
||
BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeList(peers, DiscoveryPeer::writeTo); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
BytesValue encoded = out.encoded(); | ||
|
||
final NeighborsPacketData deserialized = NeighborsPacketData.readFrom(RLP.input(encoded)); | ||
assertThat(deserialized.getNodes()).isEqualTo(peers); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...c/test/java/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/PingPacketDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed 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 tech.pegasys.pantheon.ethereum.p2p.discovery.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import tech.pegasys.pantheon.ethereum.p2p.discovery.Endpoint; | ||
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput; | ||
import tech.pegasys.pantheon.ethereum.rlp.RLP; | ||
import tech.pegasys.pantheon.util.bytes.BytesValue; | ||
|
||
import java.util.OptionalInt; | ||
|
||
import org.junit.Test; | ||
|
||
public class PingPacketDataTest { | ||
|
||
@Test | ||
public void serializeDeserialize() { | ||
final long currentTime = System.currentTimeMillis(); | ||
|
||
final Endpoint from = new Endpoint("127.0.0.1", 30303, OptionalInt.of(30303)); | ||
final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty()); | ||
final PingPacketData packet = PingPacketData.create(from, to); | ||
final BytesValue serialized = RLP.encode(packet::writeTo); | ||
final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getFrom()).isEqualTo(from); | ||
assertThat(deserialized.getTo()).isEqualTo(to); | ||
assertThat(deserialized.getExpiration()).isGreaterThan(currentTime); | ||
} | ||
|
||
@Test | ||
public void readFrom() { | ||
final int version = 4; | ||
final Endpoint from = new Endpoint("127.0.0.1", 30303, OptionalInt.of(30303)); | ||
final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty()); | ||
final long time = System.currentTimeMillis(); | ||
|
||
final BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeIntScalar(version); | ||
from.encodeStandalone(out); | ||
to.encodeStandalone(out); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
|
||
final BytesValue serialized = out.encoded(); | ||
final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getFrom()).isEqualTo(from); | ||
assertThat(deserialized.getTo()).isEqualTo(to); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
|
||
@Test | ||
public void readFrom_withExtraFields() { | ||
final int version = 4; | ||
final Endpoint from = new Endpoint("127.0.0.1", 30303, OptionalInt.of(30303)); | ||
final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty()); | ||
final long time = System.currentTimeMillis(); | ||
|
||
final BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeIntScalar(version); | ||
from.encodeStandalone(out); | ||
to.encodeStandalone(out); | ||
out.writeLongScalar(time); | ||
// Add extra field | ||
out.writeLongScalar(11); | ||
out.endList(); | ||
|
||
final BytesValue serialized = out.encoded(); | ||
final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getFrom()).isEqualTo(from); | ||
assertThat(deserialized.getTo()).isEqualTo(to); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
|
||
@Test | ||
public void readFrom_unknownVersion() { | ||
final int version = 99; | ||
final Endpoint from = new Endpoint("127.0.0.1", 30303, OptionalInt.of(30303)); | ||
final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty()); | ||
final long time = System.currentTimeMillis(); | ||
|
||
final BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeIntScalar(version); | ||
from.encodeStandalone(out); | ||
to.encodeStandalone(out); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
|
||
final BytesValue serialized = out.encoded(); | ||
final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getFrom()).isEqualTo(from); | ||
assertThat(deserialized.getTo()).isEqualTo(to); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
(optional) make leaveList parameter an enum. Much easier to understand what
in.leaveList(RLPInput.IgnoreRest)
does as opposed toin.leaveList(true)
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.
I'd say either @RatanRSur's suggestion or at least leave a comment that (true) means to ignore the rest of the list, for all the
leaveList(true)
calls.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.
Third option would be to have two methods - the usual
leaveList
and the newleaveListSkippingRemainingElements
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.
Changed this to
leaveListLenient()