Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PAN-3202][BESU-37] Implement EIP-2124 #156

Merged
merged 59 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
5db0202
edits to test names (#153)
macfarla Nov 4, 2019
3ac0ef5
Moving JsonRpcMethods classes into the same package, prior to refacto…
CjHare Nov 4, 2019
a416b4e
Add --identity flag for client identification in node browsers (#150)
shemnon Nov 4, 2019
a26c73e
[PAN-3202] add basic forkId (eip-2124) creation class and related test
SteveMieskoski Nov 4, 2019
7e38ff5
[PAN-2798] Enable pruning by default for fast sync (#135)
RatanRSur Nov 4, 2019
5349e18
commenting out circles publish on the release branch so we're ready f…
joshuafernandes Nov 5, 2019
46329a2
fix style to properly conform
SteveMieskoski Nov 5, 2019
32ca662
Redesign of how JsonRpcMethods are created (#159)
CjHare Nov 5, 2019
b89494f
remove print statement from forkId
SteveMieskoski Nov 5, 2019
138d030
check protocol version and bypass at present until eth protocol with …
SteveMieskoski Nov 5, 2019
b222e1b
Merge branch 'master' into eip-2124
SteveMieskoski Nov 5, 2019
935e1ec
commit suggestion to param description
SteveMieskoski Nov 7, 2019
288f26f
Merge remote-tracking branch 'origin/eip-2124' into eip-2124
SteveMieskoski Nov 11, 2019
65e66d3
Merge branch 'master' into eip-2124
SteveMieskoski Nov 16, 2019
1882dcb
change forkId to forkIdManager, idEntry to ForkId, modify statusMessa…
SteveMieskoski Nov 17, 2019
27c42ff
include forkId in statusMessage class, move fork collection to genesi…
SteveMieskoski Nov 29, 2019
4d07927
Merge branch 'master' into eip-2124
SteveMieskoski Nov 29, 2019
fb9cde1
clean up unused imports and remove some comments related to forkId
SteveMieskoski Nov 29, 2019
cbef3e9
eip-2124 RLP encoding checks pass, store next block number as bytesValue
SteveMieskoski Nov 30, 2019
9c946a8
eip-2124 RLP encoding checks pass, store next block number as bytesVa…
SteveMieskoski Nov 30, 2019
35f012f
remove redundant methods in forkId and modify fork collection to use …
SteveMieskoski Nov 30, 2019
2de97a8
Merge pull request #1 from SteveMieskoski/eip-2364
SteveMieskoski Nov 30, 2019
2597ac9
needed to lint prior commit
SteveMieskoski Nov 30, 2019
0da2285
Merge branch 'eip-2364' into eip-2124
SteveMieskoski Nov 30, 2019
5b5cac6
correct forkId test
SteveMieskoski Nov 30, 2019
14e56ba
reduce duplication, and check getForks order
SteveMieskoski Dec 3, 2019
36cbe98
replace ArrayDeque usage with ArrayList
SteveMieskoski Dec 4, 2019
ea69e62
lint via spotlessApply
SteveMieskoski Dec 4, 2019
6ef9548
enforce exhaustiveness of fork sequence in test
RatanRSur Dec 3, 2019
b3601da
remove commented code
RatanRSur Dec 3, 2019
a1fb990
asserts
RatanRSur Dec 3, 2019
c226b3a
immutable list
RatanRSur Dec 3, 2019
1dd5486
functional style
RatanRSur Dec 3, 2019
2280d86
Math.max
RatanRSur Dec 4, 2019
1bdeb0b
add final
RatanRSur Dec 4, 2019
1130695
rename
RatanRSur Dec 4, 2019
4b7a090
cleanup fork generation
RatanRSur Dec 8, 2019
5839ecd
visible for testing
RatanRSur Dec 9, 2019
b68a84d
spotless
RatanRSur Dec 9, 2019
28d507e
add ordering to get forks test
RatanRSur Dec 9, 2019
4d4b187
remove unused methods
RatanRSur Dec 10, 2019
23e75dc
Merge pull request #2 from RatanRSur/eip-2124
SteveMieskoski Dec 12, 2019
7883f10
Merge branch 'master' into eip-2124
SteveMieskoski Dec 12, 2019
5486679
Merge branch 'master' into eip-2124
SteveMieskoski Dec 18, 2019
23d12fd
Merge branch 'master' into eip-2124
SteveMieskoski Dec 30, 2019
f72f0e4
update to use tuweni bytes
SteveMieskoski Dec 31, 2019
fcd87db
ran spotlessJava
SteveMieskoski Dec 31, 2019
4463ca4
add missing item to StatusMessage.java to fix failing test
SteveMieskoski Dec 31, 2019
f85d693
Merge branch 'master' into eip-2124
RatanRSur Jan 8, 2020
992a156
fix ci failure from forkId
SteveMieskoski Jan 8, 2020
4268b8e
re-fix ci failure from forkId
SteveMieskoski Jan 8, 2020
c93769c
fix getLatestForkId out of bounds error
SteveMieskoski Jan 8, 2020
93ce270
Merge remote-tracking branch 'upstream/master' into eip-2124
SteveMieskoski Jan 8, 2020
203ee57
Merge branch 'master' into eip-2124
SteveMieskoski Jan 8, 2020
1547427
change method used to get byte array
SteveMieskoski Jan 9, 2020
758e950
Merge remote-tracking branch 'origin/eip-2124' into eip-2124
SteveMieskoski Jan 9, 2020
b7e4c83
Merge remote-tracking branch 'upstream/master' into eip-2124
SteveMieskoski Jan 9, 2020
bfa9efe
Merge branch 'master' into eip-2124
SteveMieskoski Jan 9, 2020
fa0b5d4
Merge branch 'master' into eip-2124
RatanRSur Jan 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ protected EthProtocolManager createEthProtocolManager(
syncConfig.getComputationParallelism(),
clock,
metricsSystem,
ethereumWireProtocolConfiguration);
ethereumWireProtocolConfiguration,
genesisConfig.getForks());
}

private List<PeerValidator> createPeerValidators(final ProtocolSchedule<C> protocolSchedule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package org.hyperledger.besu.config;

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

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
Expand Down Expand Up @@ -131,6 +133,20 @@ public long getTimestamp() {
return parseLong("timestamp", JsonUtil.getValueAsString(configRoot, "timestamp", "0x0"));
}

public List<Long> getForks() {
return JsonUtil.getObjectNode(configRoot, "config").stream()
.flatMap(
node ->
Streams.stream(node.fieldNames())
.map(String::toLowerCase)
.filter(name -> !name.equals("chainid"))
.filter(name -> node.get(name).canConvertToLong())
.filter(name -> name.contains("block"))
.map(name -> node.get(name).asLong()))
.sorted()
.collect(toUnmodifiableList());
}

private String getRequiredString(final String key) {
if (!configRoot.has(key)) {
throw new IllegalArgumentException(
Expand Down
2 changes: 1 addition & 1 deletion config/src/main/resources/mainnet.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"config": {
"chainId": 1,
"homesteadBlock": 1150000,
"daoForkBlock": 1920000,
"homesteadBlock": 1150000,
"daoForkSupport": true,
"eip150Block": 2463000,
"eip150Hash": "0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,29 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hyperledger.besu.config.GenesisConfigFile.fromConfig;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.io.Resources;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.Test;

public class GenesisConfigFileTest {

private static final BigInteger MAINNET_CHAIN_ID = BigInteger.ONE;
private static final BigInteger DEVELOPMENT_CHAIN_ID = BigInteger.valueOf(2018);
private static final GenesisConfigFile EMPTY_CONFIG = GenesisConfigFile.fromConfig("{}");
private static final GenesisConfigFile EMPTY_CONFIG = fromConfig("{}");

@Test
public void shouldLoadMainnetConfigFile() {
Expand Down Expand Up @@ -143,7 +149,7 @@ public void shouldDefaultTimestampToZero() {
@Test
public void shouldGetAllocations() {
final GenesisConfigFile config =
GenesisConfigFile.fromConfig(
fromConfig(
"{"
+ " \"alloc\": {"
+ " \"fe3b557e8fb62b89f4916b721be55ceb828dbd73\": {"
Expand Down Expand Up @@ -193,14 +199,14 @@ public void shouldGetAllocations() {

@Test
public void shouldGetEmptyAllocationsWhenAllocNotPresent() {
final GenesisConfigFile config = GenesisConfigFile.fromConfig("{}");
final GenesisConfigFile config = fromConfig("{}");
assertThat(config.streamAllocations()).isEmpty();
}

@Test
public void shouldGetLargeChainId() {
final GenesisConfigFile config =
GenesisConfigFile.fromConfig(
fromConfig(
"{\"config\": { \"chainId\": 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095 }}");
assertThat(config.getConfigOptions().getChainId())
.contains(
Expand All @@ -212,7 +218,7 @@ public void shouldGetLargeChainId() {
public void mustNotAcceptComments() {
assertThatThrownBy(
() ->
GenesisConfigFile.fromConfig(
fromConfig(
"{\"config\": { \"chainId\": 2017 }\n/* C comment }*/\n//C++ comment }\n}"))
.hasCauseInstanceOf(JsonParseException.class)
.hasMessageContaining("Unexpected character ('/'");
Expand All @@ -228,6 +234,7 @@ public void testOverridePresent() {
override.put("chainId", bigBlockString);
override.put("contractSizeLimit", bigBlockString);

assertThat(config.getForks()).isNotEmpty();
assertThat(config.getConfigOptions(override).getIstanbulBlockNumber()).hasValue(bigBlock);
assertThat(config.getConfigOptions(override).getChainId())
.hasValue(BigInteger.valueOf(bigBlock));
Expand All @@ -242,6 +249,7 @@ public void testOverrideNull() {
override.put("chainId", null);
override.put("contractSizeLimit", null);

assertThat(config.getForks()).isNotEmpty();
assertThat(config.getConfigOptions(override).getIstanbulBlockNumber()).isNotPresent();
assertThat(config.getConfigOptions(override).getChainId()).isNotPresent();
assertThat(config.getConfigOptions(override).getContractSizeLimit()).isNotPresent();
Expand Down Expand Up @@ -290,8 +298,29 @@ public void testNoOverride() {
assertThat(config.getConfigOptions().getEvmStackSize()).isNotPresent();
}

@Test
public void shouldLoadForksInSortedOrder() throws IOException {
final ObjectNode configNode =
new ObjectMapper()
.createObjectNode()
.set(
"config",
JsonUtil.objectNodeFromString(
Resources.toString(
Resources.getResource(
// If you inspect this config, you should see that fork block 2 is
// declared before 1
"valid_config_with_custom_forks.json"),
StandardCharsets.UTF_8)));

final GenesisConfigFile config = fromConfig(configNode);

assertThat(config.getForks()).containsExactly(1L, 2L, 3L, 3L, 1035301L);
assertThat(config.getConfigOptions().getChainId()).hasValue(BigInteger.valueOf(4));
}

private GenesisConfigFile configWithProperty(final String key, final String value) {
return GenesisConfigFile.fromConfig("{\"" + key + "\":\"" + value + "\"}");
return fromConfig("{\"" + key + "\":\"" + value + "\"}");
}

private void assertInvalidConfiguration(final ThrowingCallable getter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"chainId": 4,
"homesteadBlock": 1,
"eip150Block": 2,
"homesteadBlock": 1,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 3,
"eip158Block": 3,
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 Expand Up @@ -120,5 +121,6 @@ public static EthProtocol get() {
public static class EthVersion {
public static final int V62 = 62;
public static final int V63 = 63;
public static final int V64 = 64;
}
}
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 ForkIdManager forkIdManager;
private final BigInteger networkId;
private final EthPeers ethPeers;
private final EthMessages ethMessages;
Expand All @@ -81,7 +82,8 @@ public EthProtocolManager(
final EthScheduler scheduler,
final EthProtocolConfiguration ethereumWireProtocolConfiguration,
final Clock clock,
final MetricsSystem metricsSystem) {
final MetricsSystem metricsSystem,
final ForkIdManager forkIdManager) {
this.networkId = networkId;
this.peerValidators = peerValidators;
this.scheduler = scheduler;
Expand All @@ -91,6 +93,8 @@ public EthProtocolManager(
this.shutdown = new CountDownLatch(1);
genesisHash = blockchain.getBlockHashByNumber(0L).get();

this.forkIdManager = ForkIdManager.buildCollection(genesisHash);

ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem);
ethMessages = new EthMessages();
ethContext = new EthContext(ethPeers, ethMessages, scheduler);
Expand Down Expand Up @@ -126,7 +130,8 @@ public EthProtocolManager(
new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem),
EthProtocolConfiguration.defaultConfig(),
clock,
metricsSystem);
metricsSystem,
ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get()));
}

public EthProtocolManager(
Expand All @@ -150,7 +155,35 @@ public EthProtocolManager(
new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem),
ethereumWireProtocolConfiguration,
clock,
metricsSystem);
metricsSystem,
ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get()));
}

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(
blockchain,
worldStateArchive,
networkId,
peerValidators,
fastSyncEnabled,
new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem),
ethereumWireProtocolConfiguration,
clock,
metricsSystem,
ForkIdManager.buildCollection(
blockchain.getBlockHashByNumber(0L).get(), forks, blockchain));
}

public EthContext ethContext() {
Expand Down Expand Up @@ -241,13 +274,16 @@ 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.
final StatusMessage status =
StatusMessage.create(
cap.getVersion(),
networkId,
blockchain.getChainHead().getTotalDifficulty(),
blockchain.getChainHeadHash(),
genesisHash);
genesisHash,
forkIdManager.getLatestForkId());
try {
LOG.debug("Sending status message to {}.", peer);
peer.send(status);
Expand Down Expand Up @@ -284,7 +320,13 @@ 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 (!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,
Expand Down
Loading