Changes made for adding ETH field to ENR (#1893)

Signed-off-by: David Mechler <david.mechler@consensys.net>
pull/1901/head
David Mechler 4 years ago committed by GitHub
parent 327ba1e977
commit 4456dc15de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java
  2. 12
      besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
  3. 1
      besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
  4. 1
      besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java
  5. 6
      besu/src/test/java/org/hyperledger/besu/RunnerTest.java
  6. 1
      besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java
  7. 3
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java
  8. 5
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java
  9. 8
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java
  10. 1
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/TestNode.java
  11. 10
      ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/discovery/PeerDiscoveryAgent.java
  12. 14
      ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/discovery/VertxPeerDiscoveryAgent.java
  13. 12
      ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java
  14. 10
      ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/discovery/PeerDiscoveryAgentTest.java
  15. 8
      ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/discovery/PeerDiscoveryTestHelper.java
  16. 7
      ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/discovery/internal/MockPeerDiscoveryAgent.java
  17. 5
      ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java
  18. 5
      ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/NetworkingServiceLifecycleTest.java
  19. 4
      ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/P2PNetworkTest.java
  20. 2
      gradle/versions.gradle

@ -205,6 +205,7 @@ public class ThreadBesuNodeRunner implements BesuNodeRunner {
.besuPluginContext(new BesuPluginContextImpl())
.autoLogBloomCaching(false)
.storageProvider(storageProvider)
.forkIdSupplier(() -> besuController.getProtocolManager().getForkIdAsBytesList())
.build();
runner.start();

@ -119,6 +119,7 @@ import java.util.List;
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;
@ -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;
@ -341,6 +343,11 @@ public class RunnerBuilder {
return this;
}
public RunnerBuilder forkIdSupplier(final Supplier<List<Bytes>> forkIdSupplier) {
this.forkIdSupplier = forkIdSupplier;
return this;
}
public Runner build() {
Preconditions.checkNotNull(besuController);
@ -414,9 +421,9 @@ public class RunnerBuilder {
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)
@ -427,6 +434,7 @@ public class RunnerBuilder {
.natService(natService)
.randomPeerPriority(randomPeerPriority)
.storageProvider(storageProvider)
.forkIdSupplier(forkIdSupplier)
.build();
final NetworkRunner networkRunner =

@ -2257,6 +2257,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
.ethstatsUrl(unstableEthstatsOptions.getEthstatsUrl())
.ethstatsContact(unstableEthstatsOptions.getEthstatsContact())
.storageProvider(keyValueStorageProvider(keyValueStorageName))
.forkIdSupplier(() -> besuController.getProtocolManager().getForkIdAsBytesList())
.build();
addShutdownHook(runner);

@ -124,6 +124,7 @@ public final class RunnerBuilderTest {
.vertx(vertx)
.dataDir(dataDir.getRoot().toPath())
.storageProvider(mock(KeyValueStorageProvider.class))
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY))
.build();
runner.start();

@ -82,6 +82,7 @@ import okhttp3.OkHttpClient;
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;
@ -203,7 +204,8 @@ public final class RunnerTest {
.maxPeers(3)
.metricsSystem(noOpMetricsSystem)
.staticNodes(emptySet())
.storageProvider(new InMemoryStorageProvider());
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY));
Runner runnerBehind = null;
final Runner runnerAhead =
@ -217,6 +219,7 @@ public final class RunnerTest {
.dataDir(dbAhead)
.pidPath(pidPath)
.besuPluginContext(new BesuPluginContextImpl())
.forkIdSupplier(() -> controllerAhead.getProtocolManager().getForkIdAsBytesList())
.build();
try {
@ -269,6 +272,7 @@ public final class RunnerTest {
.metricsConfiguration(behindMetricsConfiguration)
.dataDir(temp.newFolder().toPath())
.metricsSystem(noOpMetricsSystem)
.forkIdSupplier(() -> controllerBehind.getProtocolManager().getForkIdAsBytesList())
.build();
runnerBehind.start();

@ -238,6 +238,7 @@ public abstract class CommandTestAbstract {
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 =

@ -51,6 +51,7 @@ import org.hyperledger.besu.nat.NatService;
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;
@ -67,6 +68,7 @@ import okhttp3.OkHttpClient;
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;
@ -263,6 +265,7 @@ public class JsonRpcHttpServiceRpcApisTest {
.config(config)
.metricsSystem(new NoOpMetricsSystem())
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY))
.build();
p2pNetwork.start();

@ -49,6 +49,7 @@ import com.google.common.annotations.VisibleForTesting;
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();
@ -358,4 +359,8 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver {
"Unable to get total difficulty from blockchain for mined block."));
blockBroadcaster.propagate(block, totalDifficulty);
}
public List<Bytes> getForkIdAsBytesList() {
return forkIdManager.computeForkId().getForkIdAsBytesList();
}
}

@ -47,6 +47,14 @@ public class ForkId {
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);

@ -168,6 +168,7 @@ public class TestNode implements Closeable {
.metricsSystem(new NoOpMetricsSystem())
.supportedCapabilities(capabilities)
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY))
.build())
.metricsSystem(new NoOpMetricsSystem())
.build();

@ -43,10 +43,12 @@ import org.hyperledger.besu.util.Subscribers;
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;
@ -97,6 +99,7 @@ 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,
@ -104,7 +107,8 @@ public abstract class PeerDiscoveryAgent {
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");
@ -122,6 +126,7 @@ public abstract class PeerDiscoveryAgent {
id = nodeKey.getPublicKey().getEncodedBytes();
this.storageProvider = storageProvider;
this.forkIdSupplier = forkIdSupplier;
}
protected abstract TimerUtil createTimer();
@ -202,7 +207,8 @@ public abstract class PeerDiscoveryAgent {
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()))

@ -34,6 +34,7 @@ import java.io.IOException;
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;
@ -49,6 +50,7 @@ import io.vertx.core.datagram.DatagramSocket;
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();
@ -64,8 +66,16 @@ public class VertxPeerDiscoveryAgent extends PeerDiscoveryAgent {
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;

@ -64,6 +64,7 @@ import java.util.concurrent.ScheduledExecutorService;
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;
@ -447,6 +448,7 @@ public class DefaultP2PNetwork implements P2PNetwork {
private MetricsSystem metricsSystem;
private StorageProvider storageProvider;
private Supplier<List<Bytes>> forkIdSupplier;
public P2PNetwork build() {
validate();
@ -487,6 +489,7 @@ public class DefaultP2PNetwork implements P2PNetwork {
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() {
@ -498,7 +501,8 @@ public class DefaultP2PNetwork implements P2PNetwork {
peerPermissions,
natService,
metricsSystem,
storageProvider);
storageProvider,
forkIdSupplier);
}
private RlpxAgent createRlpxAgent(
@ -589,5 +593,11 @@ public class DefaultP2PNetwork implements P2PNetwork {
this.storageProvider = storageProvider;
return this;
}
public Builder forkIdSupplier(final Supplier<List<Bytes>> forkIdSupplier) {
checkNotNull(forkIdSupplier);
this.forkIdSupplier = forkIdSupplier;
return this;
}
}
}

@ -45,6 +45,7 @@ import java.util.List;
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;
@ -92,11 +93,14 @@ public class PeerDiscoveryAgentTest {
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

@ -283,7 +283,13 @@ public class PeerDiscoveryTestHelper {
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;

@ -31,6 +31,7 @@ import java.util.Deque;
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;
@ -49,14 +50,16 @@ public class MockPeerDiscoveryAgent extends PeerDiscoveryAgent {
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;
}

@ -52,12 +52,14 @@ import org.hyperledger.besu.nat.core.domain.NetworkProtocol;
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;
@ -347,6 +349,7 @@ public final class DefaultP2PNetworkTest {
.maintainedPeers(maintainedPeers)
.metricsSystem(new NoOpMetricsSystem())
.supportedCapabilities(Capability.create("eth", 63))
.storageProvider(new InMemoryStorageProvider());
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY));
}
}

@ -30,8 +30,10 @@ import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import io.vertx.core.Vertx;
import org.apache.tuweni.bytes.Bytes;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Test;
@ -160,6 +162,7 @@ public class NetworkingServiceLifecycleTest {
.config(config)
.metricsSystem(new NoOpMetricsSystem())
.supportedCapabilities(Arrays.asList(Capability.create("eth", 63)))
.storageProvider(new InMemoryStorageProvider());
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY));
}
}

@ -41,6 +41,7 @@ import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@ -335,6 +336,7 @@ public class P2PNetworkTest {
.nodeKey(NodeKeyUtils.generate())
.metricsSystem(new NoOpMetricsSystem())
.supportedCapabilities(Arrays.asList(Capability.create("eth", 63)))
.storageProvider(new InMemoryStorageProvider());
.storageProvider(new InMemoryStorageProvider())
.forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY));
}
}

@ -148,7 +148,7 @@ dependencyManagement {
dependency 'org.yaml:snakeyaml:1.26'
dependency 'tech.pegasys.discovery:discovery:0.4.1'
dependency 'tech.pegasys.discovery:discovery:0.4.2'
dependency 'tech.pegasys.ethsigner.internal:core:0.4.0'
dependency 'tech.pegasys.ethsigner.internal:file-based:0.4.0'

Loading…
Cancel
Save