Skip to content

Commit

Permalink
Changes made for adding ETH field to ENR (#1893)
Browse files Browse the repository at this point in the history
Signed-off-by: David Mechler <david.mechler@consensys.net>
  • Loading branch information
David Mechler authored Feb 9, 2021
1 parent 327ba1e commit 4456dc1
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public void startNode(final BesuNode node) {
.besuPluginContext(new BesuPluginContextImpl())
.autoLogBloomCaching(false)
.storageProvider(storageProvider)
.forkIdSupplier(() -> besuController.getProtocolManager().getForkIdAsBytesList())
.build();

runner.start();
Expand Down
12 changes: 10 additions & 2 deletions besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -170,6 +171,7 @@ public class RunnerBuilder {
private boolean autoLogBloomCaching = true;
private boolean randomPeerPriority;
private StorageProvider storageProvider;
private Supplier<List<Bytes>> forkIdSupplier;

public RunnerBuilder vertx(final Vertx vertx) {
this.vertx = vertx;
Expand Down Expand Up @@ -341,6 +343,11 @@ public RunnerBuilder storageProvider(final StorageProvider storageProvider) {
return this;
}

public RunnerBuilder forkIdSupplier(final Supplier<List<Bytes>> forkIdSupplier) {
this.forkIdSupplier = forkIdSupplier;
return this;
}

public Runner build() {

Preconditions.checkNotNull(besuController);
Expand Down Expand Up @@ -414,9 +421,9 @@ public Runner build() {
LOG.info("Detecting NAT service.");
final boolean fallbackEnabled = natMethod == NatMethod.AUTO || natMethodFallbackEnabled;
final NatService natService = new NatService(buildNatManager(natMethod), fallbackEnabled);
final NetworkBuilder inactiveNetwork = (caps) -> new NoopP2PNetwork();
final NetworkBuilder inactiveNetwork = caps -> new NoopP2PNetwork();
final NetworkBuilder activeNetwork =
(caps) ->
caps ->
DefaultP2PNetwork.builder()
.vertx(vertx)
.nodeKey(nodeKey)
Expand All @@ -427,6 +434,7 @@ public Runner build() {
.natService(natService)
.randomPeerPriority(randomPeerPriority)
.storageProvider(storageProvider)
.forkIdSupplier(forkIdSupplier)
.build();

final NetworkRunner networkRunner =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2257,6 +2257,7 @@ private void synchronize(
.ethstatsUrl(unstableEthstatsOptions.getEthstatsUrl())
.ethstatsContact(unstableEthstatsOptions.getEthstatsContact())
.storageProvider(keyValueStorageProvider(keyValueStorageName))
.forkIdSupplier(() -> besuController.getProtocolManager().getForkIdAsBytesList())
.build();

addShutdownHook(runner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void enodeUrlShouldHaveAdvertisedHostWhenDiscoveryDisabled() {
.vertx(vertx)
.dataDir(dataDir.getRoot().toPath())
.storageProvider(mock(KeyValueStorageProvider.class))
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY))
.build();
runner.start();

Expand Down
6 changes: 5 additions & 1 deletion besu/src/test/java/org/hyperledger/besu/RunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
import org.awaitility.Awaitility;
import org.junit.After;
Expand Down Expand Up @@ -203,7 +204,8 @@ private void syncFromGenesis(final SyncMode mode, final GenesisConfigFile genesi
.maxPeers(3)
.metricsSystem(noOpMetricsSystem)
.staticNodes(emptySet())
.storageProvider(new InMemoryStorageProvider());
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY));

Runner runnerBehind = null;
final Runner runnerAhead =
Expand All @@ -217,6 +219,7 @@ private void syncFromGenesis(final SyncMode mode, final GenesisConfigFile genesi
.dataDir(dbAhead)
.pidPath(pidPath)
.besuPluginContext(new BesuPluginContextImpl())
.forkIdSupplier(() -> controllerAhead.getProtocolManager().getForkIdAsBytesList())
.build();
try {

Expand Down Expand Up @@ -269,6 +272,7 @@ private void syncFromGenesis(final SyncMode mode, final GenesisConfigFile genesi
.metricsConfiguration(behindMetricsConfiguration)
.dataDir(temp.newFolder().toPath())
.metricsSystem(noOpMetricsSystem)
.forkIdSupplier(() -> controllerBehind.getProtocolManager().getForkIdAsBytesList())
.build();

runnerBehind.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public void initMocks() throws Exception {
when(mockRunnerBuilder.ethstatsUrl(anyString())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.ethstatsContact(anyString())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.storageProvider(any())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.forkIdSupplier(any())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.build()).thenReturn(mockRunner);

final Bytes32 keyPairPrvKey =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -67,6 +68,7 @@
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.tuweni.bytes.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -263,6 +265,7 @@ private P2PNetwork createP2pNetwork() {
.config(config)
.metricsSystem(new NoOpMetricsSystem())
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY))
.build();

p2pNetwork.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.google.common.collect.ImmutableList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes;

public class EthProtocolManager implements ProtocolManager, MinedBlockObserver {
private static final Logger LOG = LogManager.getLogger();
Expand Down Expand Up @@ -358,4 +359,8 @@ public void blockMined(final Block block) {
"Unable to get total difficulty from blockchain for mined block."));
blockBroadcaster.propagate(block, totalDifficulty);
}

public List<Bytes> getForkIdAsBytesList() {
return forkIdManager.computeForkId().getForkIdAsBytesList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public Bytes getHash() {
return hash;
}

public List<Bytes> getForkIdAsBytesList() {
List<Bytes> bytesList = new ArrayList<>();
bytesList.add(hash);
bytesList.add(next);

return bytesList;
}

void createForkIdRLP() {
final BytesValueRLPOutput out = new BytesValueRLPOutput();
writeTo(out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public TestNode(
.metricsSystem(new NoOpMetricsSystem())
.supportedCapabilities(capabilities)
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY))
.build())
.metricsSystem(new NoOpMetricsSystem())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@

import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -97,14 +99,16 @@ public abstract class PeerDiscoveryAgent {
protected final Subscribers<PeerBondedObserver> peerBondedObservers = Subscribers.create();

private final StorageProvider storageProvider;
private final Supplier<List<Bytes>> forkIdSupplier;

protected PeerDiscoveryAgent(
final NodeKey nodeKey,
final DiscoveryConfiguration config,
final PeerPermissions peerPermissions,
final NatService natService,
final MetricsSystem metricsSystem,
final StorageProvider storageProvider) {
final StorageProvider storageProvider,
final Supplier<List<Bytes>> forkIdSupplier) {
this.metricsSystem = metricsSystem;
checkArgument(nodeKey != null, "nodeKey cannot be null");
checkArgument(config != null, "provided configuration cannot be null");
Expand All @@ -122,6 +126,7 @@ protected PeerDiscoveryAgent(
id = nodeKey.getPublicKey().getEncodedBytes();

this.storageProvider = storageProvider;
this.forkIdSupplier = forkIdSupplier;
}

protected abstract TimerUtil createTimer();
Expand Down Expand Up @@ -202,7 +207,8 @@ private Optional<NodeRecord> addLocalNodeRecord(
new EnrField(EnrField.PKEY_SECP256K1, Functions.compressPublicKey(nodeId)),
new EnrField(EnrField.IP_V4, addressBytes),
new EnrField(EnrField.TCP, tcpPort),
new EnrField(EnrField.UDP, udpPort));
new EnrField(EnrField.UDP, udpPort),
new EnrField("eth", Collections.singletonList(forkIdSupplier.get())));
nodeRecord.setSignature(
nodeKey
.sign(Hash.keccak256(nodeRecord.serializeNoSignature()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.IntSupplier;
Expand All @@ -49,6 +50,7 @@
import io.vertx.core.datagram.DatagramSocketOptions;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes;

public class VertxPeerDiscoveryAgent extends PeerDiscoveryAgent {
private static final Logger LOG = LogManager.getLogger();
Expand All @@ -64,8 +66,16 @@ public VertxPeerDiscoveryAgent(
final PeerPermissions peerPermissions,
final NatService natService,
final MetricsSystem metricsSystem,
final StorageProvider storageProvider) {
super(nodeKey, config, peerPermissions, natService, metricsSystem, storageProvider);
final StorageProvider storageProvider,
final Supplier<List<Bytes>> forkIdSupplier) {
super(
nodeKey,
config,
peerPermissions,
natService,
metricsSystem,
storageProvider,
forkIdSupplier);
checkArgument(vertx != null, "vertx instance cannot be null");
this.vertx = vertx;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -447,6 +448,7 @@ public static class Builder {

private MetricsSystem metricsSystem;
private StorageProvider storageProvider;
private Supplier<List<Bytes>> forkIdSupplier;

public P2PNetwork build() {
validate();
Expand Down Expand Up @@ -487,6 +489,7 @@ private void validate() {
checkState(metricsSystem != null, "MetricsSystem must be set.");
checkState(storageProvider != null, "StorageProvider must be set.");
checkState(peerDiscoveryAgent != null || vertx != null, "Vertx must be set.");
checkState(forkIdSupplier != null, "ForkIdSupplier must be set.");
}

private PeerDiscoveryAgent createDiscoveryAgent() {
Expand All @@ -498,7 +501,8 @@ private PeerDiscoveryAgent createDiscoveryAgent() {
peerPermissions,
natService,
metricsSystem,
storageProvider);
storageProvider,
forkIdSupplier);
}

private RlpxAgent createRlpxAgent(
Expand Down Expand Up @@ -589,5 +593,11 @@ public Builder storageProvider(final StorageProvider storageProvider) {
this.storageProvider = storageProvider;
return this;
}

public Builder forkIdSupplier(final Supplier<List<Bytes>> forkIdSupplier) {
checkNotNull(forkIdSupplier);
this.forkIdSupplier = forkIdSupplier;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.ethereum.beacon.discovery.schema.NodeRecord;
import org.junit.Test;
Expand Down Expand Up @@ -92,11 +93,14 @@ public void testNodeRecordCreated() {
assertThat(nodeRecord.getIdentityScheme()).isNotNull();
assertThat(nodeRecord.getSignature()).isNotNull();
assertThat(nodeRecord.getSeq()).isNotNull();
assertThat(nodeRecord.get("eth")).isNotNull();
assertThat(nodeRecord.get("eth"))
.isEqualTo(Collections.singletonList(Collections.singletonList(Bytes.EMPTY)));
assertThat(nodeRecord.asEnr())
.isEqualTo(
"enr:-Im4QIEcZbEzW8DSEX-0BPB36s1UwTT54D_I-mvrSHqsZpVzGg7wlXyHb6vRq3GTGNBNQyoUkKkJGryrTo"
+ "DTersRuNYBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA"
+ "8yWM0xOIN0Y3ACg3VkcIJ2Xw");
"enr:-JC4QOfroMOa1sB6ajxcBKdWn3s9S4Ojl33pbRm72S5FnCwyZfskmjkJvZznQaWNTrOHrnKxw1R9xMm9rl"
+ "EGOcsOyscBg2V0aMLBgIJpZIJ2NIJpcIR_AAABiXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEV"
+ "v0AHacwUAPMljNMTiDdGNwAoN1ZHCCdl8");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,13 @@ public MockPeerDiscoveryAgent build() {
config.setActive(active);

MockPeerDiscoveryAgent mockPeerDiscoveryAgent =
new MockPeerDiscoveryAgent(nodeKey, config, peerPermissions, agents, natService);
new MockPeerDiscoveryAgent(
nodeKey,
config,
peerPermissions,
agents,
natService,
() -> Collections.singletonList(Bytes.EMPTY));
mockPeerDiscoveryAgent.getAdvertisedPeer().ifPresent(peer -> peer.setNodeRecord(nodeRecord));

return mockPeerDiscoveryAgent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -49,14 +50,16 @@ public MockPeerDiscoveryAgent(
final DiscoveryConfiguration config,
final PeerPermissions peerPermissions,
final Map<Bytes, MockPeerDiscoveryAgent> agentNetwork,
final NatService natService) {
final NatService natService,
final Supplier<List<Bytes>> forkIdSupplier) {
super(
nodeKey,
config,
peerPermissions,
natService,
new NoOpMetricsSystem(),
new InMemoryStorageProvider());
new InMemoryStorageProvider(),
forkIdSupplier);
this.agentNetwork = agentNetwork;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@
import org.hyperledger.besu.nat.upnp.UpnpNatManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.tuweni.bytes.Bytes;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -347,6 +349,7 @@ private DefaultP2PNetwork.Builder builder() {
.maintainedPeers(maintainedPeers)
.metricsSystem(new NoOpMetricsSystem())
.supportedCapabilities(Capability.create("eth", 63))
.storageProvider(new InMemoryStorageProvider());
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY));
}
}
Loading

0 comments on commit 4456dc1

Please sign in to comment.