Skip to content

Commit

Permalink
Allow use of large chain ids (PegaSysEng#1289)
Browse files Browse the repository at this point in the history
  • Loading branch information
jframe authored and rain-on committed Apr 24, 2019
1 parent 9c4d682 commit ac53e3d
Show file tree
Hide file tree
Showing 44 changed files with 334 additions and 126 deletions.
20 changes: 20 additions & 0 deletions config/src/main/java/tech/pegasys/pantheon/config/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package tech.pegasys.pantheon.config;

import java.math.BigInteger;
import java.util.Optional;
import java.util.OptionalLong;

import io.vertx.core.json.JsonObject;
Expand All @@ -22,4 +24,22 @@ public static OptionalLong getOptionalLong(final JsonObject jsonObject, final St
? OptionalLong.of(jsonObject.getLong(key))
: OptionalLong.empty();
}

public static Optional<BigInteger> getOptionalBigInteger(
final JsonObject jsonObject, final String key) {
return jsonObject.containsKey(key)
? Optional.ofNullable(getBigInteger(jsonObject, key))
: Optional.empty();
}

private static BigInteger getBigInteger(final JsonObject jsonObject, final String key) {
final Number value = (Number) jsonObject.getMap().get(key);
if (value == null) {
return null;
} else if (value instanceof BigInteger) {
return (BigInteger) value;
} else {
return BigInteger.valueOf(value.longValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/
package tech.pegasys.pantheon.config;

import java.math.BigInteger;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;

Expand Down Expand Up @@ -48,7 +50,7 @@ public interface GenesisConfigOptions {

OptionalLong getConstantinopleFixBlockNumber();

OptionalInt getChainId();
Optional<BigInteger> getChainId();

OptionalInt getContractSizeLimit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/
package tech.pegasys.pantheon.config;

import static tech.pegasys.pantheon.config.ConfigUtil.getOptionalBigInteger;

import java.math.BigInteger;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;

Expand Down Expand Up @@ -119,10 +123,8 @@ public OptionalLong getConstantinopleFixBlockNumber() {
}

@Override
public OptionalInt getChainId() {
return configRoot.containsKey("chainid")
? OptionalInt.of(configRoot.getInteger("chainid"))
: OptionalInt.empty();
public Optional<BigInteger> getChainId() {
return getOptionalBigInteger(configRoot, "chainid");
}

@Override
Expand All @@ -135,7 +137,7 @@ public OptionalInt getContractSizeLimit() {
@Override
public Map<String, Object> asMap() {
final ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.put("chainId", getChainId().getAsInt());
getChainId().ifPresent(chainId -> builder.put("chainId", chainId));
getHomesteadBlockNumber().ifPresent(l -> builder.put("homesteadBlock", l));
getDaoForkBlock()
.ifPresent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/
package tech.pegasys.pantheon.config;

import java.math.BigInteger;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;

Expand All @@ -27,7 +29,7 @@ public class StubGenesisConfigOptions implements GenesisConfigOptions {
private OptionalLong byzantiumBlockNumber = OptionalLong.empty();
private OptionalLong constantinopleBlockNumber = OptionalLong.empty();
private OptionalLong constantinopleFixBlockNumber = OptionalLong.empty();
private OptionalInt chainId = OptionalInt.empty();
private Optional<BigInteger> chainId = Optional.empty();
private OptionalInt contractSizeLimit = OptionalInt.empty();

@Override
Expand Down Expand Up @@ -111,14 +113,14 @@ public OptionalInt getContractSizeLimit() {
}

@Override
public OptionalInt getChainId() {
public Optional<BigInteger> getChainId() {
return chainId;
}

@Override
public Map<String, Object> asMap() {
final ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.put("chainId", getChainId().getAsInt());
getChainId().ifPresent(chainId -> builder.put("chainId", chainId));
getHomesteadBlockNumber().ifPresent(l -> builder.put("homesteadBlock", l));
getDaoForkBlock()
.ifPresent(
Expand Down Expand Up @@ -187,8 +189,8 @@ public StubGenesisConfigOptions constantinopleFixBlock(final long blockNumber) {
return this;
}

public StubGenesisConfigOptions chainId(final int chainId) {
this.chainId = OptionalInt.of(chainId);
public StubGenesisConfigOptions chainId(final BigInteger chainId) {
this.chainId = Optional.ofNullable(chainId);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;

import java.math.BigInteger;
import java.util.Map;
import java.util.stream.Collectors;

Expand All @@ -24,8 +25,8 @@

public class GenesisConfigFileTest {

private static final int MAINNET_CHAIN_ID = 1;
private static final int DEVELOPMENT_CHAIN_ID = 2018;
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("{}");

@Test
Expand Down Expand Up @@ -170,6 +171,17 @@ public void shouldGetEmptyAllocationsWhenAllocNotPresent() {
assertThat(config.getAllocations()).isEmpty();
}

@Test
public void shouldGetLargeChainId() {
final GenesisConfigFile config =
GenesisConfigFile.fromConfig(
"{\"config\": { \"chainId\": 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095 }}");
assertThat(config.getConfigOptions().getChainId())
.contains(
new BigInteger(
"31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095"));
}

private GenesisConfigFile configWithProperty(final String key, final String value) {
return GenesisConfigFile.fromConfig("{\"" + key + "\":\"" + value + "\"}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigInteger;
import java.util.Collections;
import java.util.Map;

Expand Down Expand Up @@ -128,8 +129,9 @@ public void shouldNotReturnEmptyOptionalWhenBlockNumberNotSpecified() {

@Test
public void shouldGetChainIdWhenSpecified() {
final GenesisConfigOptions config = fromConfigOptions(singletonMap("chainId", 32));
assertThat(config.getChainId()).hasValue(32);
final GenesisConfigOptions config =
fromConfigOptions(singletonMap("chainId", BigInteger.valueOf(32)));
assertThat(config.getChainId()).hasValue(BigInteger.valueOf(32));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolScheduleBuilder;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSpecBuilder;

import java.math.BigInteger;

/** Defines the protocol behaviours for a blockchain using Clique. */
public class CliqueProtocolSchedule {

private static final int DEFAULT_CHAIN_ID = 4;
private static final BigInteger DEFAULT_CHAIN_ID = BigInteger.valueOf(4);

public static ProtocolSchedule<CliqueContext> create(
final GenesisConfigOptions config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/** Defines the protocol behaviours for a blockchain using IBFT. */
public class IbftProtocolSchedule {

private static final int DEFAULT_CHAIN_ID = 1;
private static final BigInteger DEFAULT_CHAIN_ID = BigInteger.ONE;

public static ProtocolSchedule<IbftContext> create(
final GenesisConfigOptions config, final PrivacyParameters privacyParameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/** Defines the protocol behaviours for a blockchain using IBFT. */
public class IbftProtocolSchedule {

private static final int DEFAULT_CHAIN_ID = 1;
private static final BigInteger DEFAULT_CHAIN_ID = BigInteger.ONE;

public static ProtocolSchedule<IbftContext> create(
final GenesisConfigOptions config, final PrivacyParameters privacyParameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSpec;
import tech.pegasys.pantheon.util.Subscribers;

import java.math.BigInteger;
import java.util.Optional;

import com.google.common.collect.Lists;
import org.junit.Test;

Expand Down Expand Up @@ -124,7 +127,8 @@ private static Subscribers<MinedBlockObserver> subscribersContaining(
}

private ProtocolSchedule<Void> singleSpecSchedule(final ProtocolSpec<Void> protocolSpec) {
final MutableProtocolSchedule<Void> protocolSchedule = new MutableProtocolSchedule<>(1234);
final MutableProtocolSchedule<Void> protocolSchedule =
new MutableProtocolSchedule<>(Optional.of(BigInteger.valueOf(1234)));
protocolSchedule.putMilestone(0, protocolSpec);
return protocolSchedule;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.uint.UInt256;

import java.math.BigInteger;
import java.time.Instant;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -567,7 +568,7 @@ private Transaction createTransaction(final int transactionNumber) {
.to(Address.ID)
.value(Wei.of(transactionNumber))
.sender(Address.ID)
.chainId(1)
.chainId(BigInteger.ONE)
.signAndBuild(keyPair);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.io.IOException;
import java.math.BigInteger;
import java.util.function.Function;

import com.google.common.collect.Lists;
Expand All @@ -53,7 +54,7 @@ public class EthHashBlockCreatorTest {
.protocolSchedule(
new ProtocolScheduleBuilder<>(
GenesisConfigFile.DEFAULT.getConfigOptions(),
42,
BigInteger.valueOf(42),
Function.identity(),
PrivacyParameters.DEFAULT)
.createProtocolSchedule())
Expand Down
Loading

0 comments on commit ac53e3d

Please sign in to comment.