Skip to content

Commit

Permalink
Merge pull request #1 from SteveMieskoski/eip-2364
Browse files Browse the repository at this point in the history
Extend to include EIP-2364 (eth/64 using both forkId and genesis hash to validate peers)
  • Loading branch information
SteveMieskoski authored Nov 30, 2019
2 parents fb9cde1 + 35f012f commit 2de97a8
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.hyperledger.besu.config.JsonUtil.normalizeKeys;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Streams;
import com.google.common.io.Resources;
Expand Down Expand Up @@ -137,17 +133,17 @@ public long getTimestamp() {
return parseLong("timestamp", JsonUtil.getValueAsString(configRoot, "timestamp", "0x0"));
}

// TODO look into how to handle invalid or exceptional (i.e. null or "") fork values
public List<Long> getForks() {
return JsonUtil.getObjectNode(configRoot, "config").stream()
.flatMap(node ->
Streams.stream(node.fieldNames())
.filter(name -> !name.toLowerCase().equals("chainid"))
.filter(name -> node.get(name).canConvertToLong())
.filter(name -> name.contains("block"))
.map(name -> node.get(name).asLong())
)
.collect(toList());
.flatMap(
node ->
Streams.stream(node.fieldNames())
.filter(name -> !name.toLowerCase().equals("chainid"))
.filter(name -> node.get(name).canConvertToLong())
.filter(name -> name.contains("block"))
.map(name -> node.get(name).asLong()))
.distinct()
.collect(toList());
}

private String getRequiredString(final String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class EthProtocol implements SubProtocol {
public static final String NAME = "eth";
public static final Capability ETH62 = Capability.create(NAME, EthVersion.V62);
public static final Capability ETH63 = Capability.create(NAME, EthVersion.V63);
public static final Capability ETH64 = Capability.create(NAME, EthVersion.V64);
private static final EthProtocol INSTANCE = new EthProtocol();

private static final List<Integer> eth62Messages =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public EthProtocolManager(
genesisHash = blockchain.getBlockHashByNumber(0L).get();

forkIdManager =
ForkIdManager.buildCollection(genesisHash, forks, blockchain, getSupportedCapabilities());
ForkIdManager.buildCollection(genesisHash, forks, blockchain);

ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem);
ethMessages = new EthMessages();
Expand Down Expand Up @@ -286,36 +286,20 @@ public void handleNewConnection(final PeerConnection connection) {
final Capability cap = connection.capability(getSupportedProtocol());
// TODO: look to consolidate code below if possible
// making status non-final and implementing it above would be one way.
if (cap.getVersion() > EthProtocol.EthVersion.V63) {
final StatusMessage status =
StatusMessage.create(
cap.getVersion(),
networkId,
blockchain.getChainHead().getTotalDifficulty(),
blockchain.getChainHeadHash(),
forkIdManager.getLatestForkId());
try {
LOG.debug("Sending status message to {}.", peer);
peer.send(status);
peer.registerStatusSent();
} catch (final PeerNotConnected peerNotConnected) {
// Nothing to do.
}
} else {
final StatusMessage status =
StatusMessage.create(
cap.getVersion(),
networkId,
blockchain.getChainHead().getTotalDifficulty(),
blockchain.getChainHeadHash(),
genesisHash);
try {
LOG.debug("Sending status message to {}.", peer);
peer.send(status);
peer.registerStatusSent();
} catch (final PeerNotConnected peerNotConnected) {
// Nothing to do.
}
final StatusMessage status =
StatusMessage.create(
cap.getVersion(),
networkId,
blockchain.getChainHead().getTotalDifficulty(),
blockchain.getChainHeadHash(),
genesisHash,
forkIdManager.getLatestForkId());
try {
LOG.debug("Sending status message to {}.", peer);
peer.send(status);
peer.registerStatusSent();
} catch (final PeerNotConnected peerNotConnected) {
// Nothing to do.
}
}

Expand Down Expand Up @@ -346,18 +330,17 @@ private void handleStatusMessage(final EthPeer peer, final MessageData data) {
if (!status.networkId().equals(networkId)) {
LOG.debug("Disconnecting from peer with mismatched network id: {}", status.networkId());
peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED);
} else if (forkIdManager.peerCheck(status)) {
if (status.genesisHash() != null) {
LOG.debug(
"Disconnecting from peer with matching network id ({}), but non-matching genesis hash: {}",
networkId,
status.genesisHash());
} else {
LOG.debug(
"Disconnecting from peer with matching network id ({}), but non-matching fork id: {}",
networkId,
status.forkId());
}
} else if (!forkIdManager.peerCheck(status.forkId()) && status.protocolVersion() > 63) {
LOG.debug(
"Disconnecting from peer with matching network id ({}), but non-matching fork id: {}",
networkId,
status.forkId());
peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED);
} else if (forkIdManager.peerCheck(status.genesisHash())) {
LOG.debug(
"Disconnecting from peer with matching network id ({}), but non-matching genesis hash: {}",
networkId,
status.genesisHash());
peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED);
} else {
LOG.debug("Received status message from {}: {}", peer, status);
Expand Down
Loading

0 comments on commit 2de97a8

Please sign in to comment.