Skip to content

Commit

Permalink
[PAN-3202] add basic forkId (eip-2124) creation class and related test
Browse files Browse the repository at this point in the history
Signed-off-by: SteveM <SteveM@myEtherWallet.com>
  • Loading branch information
SteveMieskoski committed Nov 4, 2019
1 parent 38462bb commit c3da60e
Show file tree
Hide file tree
Showing 4 changed files with 650 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ protected EthProtocolManager createEthProtocolManager(
syncConfig.getComputationParallelism(),
clock,
metricsSystem,
ethereumWireProtocolConfiguration);
ethereumWireProtocolConfiguration,
gatherForks());
}

private List<PeerValidator> createPeerValidators(final ProtocolSchedule<C> protocolSchedule) {
Expand All @@ -372,4 +373,16 @@ private List<PeerValidator> createPeerValidators(final ProtocolSchedule<C> proto

protected abstract PluginServiceFactory createAdditionalPluginServices(
final Blockchain blockchain);

private List<Long> gatherForks() {
// todo: may need to check what protocol version is being used. (i.e. if less than x return null to disable this check)
List<Long> listb = new ArrayList<>();
Map<String, Object> values = genesisConfig.getConfigOptions(genesisConfigOverrides).asMap();
values.forEach((x, y) -> {
if (y instanceof Long && !x.equals("chainId")) {
listb.add((Long) y);
}
});
return listb;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver {
private final AtomicBoolean stopped = new AtomicBoolean(false);

private final Hash genesisHash;
private final ForkId forkId;
private final BigInteger networkId;
private final EthPeers ethPeers;
private final EthMessages ethMessages;
Expand Down Expand Up @@ -91,6 +92,8 @@ public EthProtocolManager(
this.shutdown = new CountDownLatch(1);
genesisHash = blockchain.getBlockHashByNumber(0L).get();

forkId = ForkId.buildCollection(genesisHash);

ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem);
ethMessages = new EthMessages();
ethContext = new EthContext(ethPeers, ethMessages, scheduler);
Expand Down Expand Up @@ -153,6 +156,46 @@ public EthProtocolManager(
metricsSystem);
}

public EthProtocolManager(
final Blockchain blockchain,
final WorldStateArchive worldStateArchive,
final BigInteger networkId,
final List<PeerValidator> peerValidators,
final boolean fastSyncEnabled,
final int syncWorkers,
final int txWorkers,
final int computationWorkers,
final Clock clock,
final MetricsSystem metricsSystem,
final EthProtocolConfiguration ethereumWireProtocolConfiguration,
final List<Long> forks) {
this.networkId = networkId;
this.peerValidators = peerValidators;
this.scheduler = new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem);
this.blockchain = blockchain;
this.fastSyncEnabled = fastSyncEnabled;

this.shutdown = new CountDownLatch(1);
genesisHash = blockchain.getBlockHashByNumber(0L).get();

// todo: check about modification of the above and related constructor
forkId = ForkId.buildCollection(genesisHash, forks, blockchain);

ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem);
ethMessages = new EthMessages();
ethContext = new EthContext(ethPeers, ethMessages, scheduler);

this.blockBroadcaster = new BlockBroadcaster(ethContext);

// Run validators
for (final PeerValidator peerValidator : this.peerValidators) {
PeerValidatorRunner.runValidator(ethContext, peerValidator);
}

// Set up request handlers
new EthServer(blockchain, worldStateArchive, ethMessages, ethereumWireProtocolConfiguration);
}

public EthContext ethContext() {
return ethContext;
}
Expand Down Expand Up @@ -284,7 +327,7 @@ 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 (!status.genesisHash().equals(genesisHash)) {
} else if (forkId.peerCheck(status.genesisHash())) {
LOG.debug(
"Disconnecting from peer with matching network id ({}), but non-matching genesis hash: {}",
networkId,
Expand Down
Loading

0 comments on commit c3da60e

Please sign in to comment.