Skip to content

Commit

Permalink
Merge branch 'main' into debug_setHead
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellehrner committed Nov 10, 2022
2 parents 982f13c + b181f99 commit ce70f50
Show file tree
Hide file tree
Showing 31 changed files with 577 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil;
import org.hyperledger.besu.ethereum.eth.EthProtocol;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.EthProtocolVersion;
import org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthMessages;
Expand Down Expand Up @@ -82,7 +82,7 @@ private MockPeerConnection setupPeer(
final MockPeerConnection peer = setupPeerWithoutStatusExchange(ethManager, onSend);
final StatusMessage statusMessage =
StatusMessage.create(
EthProtocol.EthVersion.V63,
EthProtocolVersion.V63,
BigInteger.ONE,
blockchain.getChainHead().getTotalDifficulty(),
blockchain.getChainHeadHash(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration;
import org.hyperledger.besu.nat.NatService;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
Expand All @@ -63,28 +62,29 @@

import io.vertx.core.Vertx;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.io.TempDir;

public class JsonRpcHttpServiceTlsMisconfigurationTest {
@ClassRule public static final TemporaryFolder folder = new TemporaryFolder();
class JsonRpcHttpServiceTlsMisconfigurationTest {

protected static final Vertx vertx = Vertx.vertx();

private static final String CLIENT_VERSION = "TestClientVersion/0.1.0";
private static final BigInteger CHAIN_ID = BigInteger.valueOf(123);
private static final NatService natService = new NatService(Optional.empty());
private final SelfSignedP12Certificate besuCertificate = SelfSignedP12Certificate.create();
@TempDir private Path tempDir;
private Path knownClientsFile;
private Map<String, JsonRpcMethod> rpcMethods;
private JsonRpcHttpService service;

@Before
public void beforeEach() throws IOException {
knownClientsFile = folder.newFile().toPath();
@BeforeEach
public void beforeEach() {
knownClientsFile = tempDir.resolve("knownClients");
writeToKnownClientsFile(
besuCertificate.getCommonName(),
besuCertificate.getCertificateHexFingerprint(),
Expand Down Expand Up @@ -124,18 +124,18 @@ public void beforeEach() throws IOException {
mock(MetricsConfiguration.class),
natService,
Collections.emptyMap(),
folder.getRoot().toPath(),
tempDir.getRoot(),
mock(EthPeers.class),
vertx));
}

@After
@AfterEach
public void shutdownServer() {
Optional.ofNullable(service).ifPresent(s -> service.stop().join());
}

@Test
public void exceptionRaisedWhenNonExistentKeystoreFileIsSpecified() throws IOException {
void exceptionRaisedWhenNonExistentKeystoreFileIsSpecified() {
Assertions.setMaxStackTraceElementsDisplayed(60);
service =
createJsonRpcHttpService(
Expand All @@ -150,7 +150,7 @@ public void exceptionRaisedWhenNonExistentKeystoreFileIsSpecified() throws IOExc
}

@Test
public void exceptionRaisedWhenIncorrectKeystorePasswordIsSpecified() throws IOException {
void exceptionRaisedWhenIncorrectKeystorePasswordIsSpecified() {
service =
createJsonRpcHttpService(
rpcMethods, createJsonRpcConfig(invalidPasswordTlsConfiguration()));
Expand All @@ -165,7 +165,7 @@ public void exceptionRaisedWhenIncorrectKeystorePasswordIsSpecified() throws IOE
}

@Test
public void exceptionRaisedWhenIncorrectKeystorePasswordFileIsSpecified() throws IOException {
void exceptionRaisedWhenIncorrectKeystorePasswordFileIsSpecified() {
service =
createJsonRpcHttpService(
rpcMethods, createJsonRpcConfig(invalidPasswordFileTlsConfiguration()));
Expand All @@ -180,7 +180,8 @@ public void exceptionRaisedWhenIncorrectKeystorePasswordFileIsSpecified() throws
}

@Test
public void exceptionRaisedWhenInvalidKeystoreFileIsSpecified() throws IOException {
@DisabledOnJre({JRE.JAVA_17, JRE.JAVA_18}) // error message changed
void exceptionRaisedWhenInvalidKeystoreFileIsSpecified() throws IOException {
service =
createJsonRpcHttpService(
rpcMethods, createJsonRpcConfig(invalidKeystoreFileTlsConfiguration()));
Expand All @@ -195,7 +196,23 @@ public void exceptionRaisedWhenInvalidKeystoreFileIsSpecified() throws IOExcepti
}

@Test
public void exceptionRaisedWhenInvalidKnownClientsFileIsSpecified() throws IOException {
@DisabledOnJre({JRE.JAVA_11, JRE.JAVA_12, JRE.JAVA_13, JRE.JAVA_14, JRE.JAVA_15, JRE.JAVA_16})
void exceptionRaisedWhenInvalidKeystoreFileIsSpecified_NewJava() throws IOException {
service =
createJsonRpcHttpService(
rpcMethods, createJsonRpcConfig(invalidKeystoreFileTlsConfiguration()));
assertThatExceptionOfType(CompletionException.class)
.isThrownBy(
() -> {
service.start().join();
Assertions.fail("service.start should have failed");
})
.withCauseInstanceOf(JsonRpcServiceException.class)
.withMessageContaining("Tag number over 30 is not supported");
}

@Test
void exceptionRaisedWhenInvalidKnownClientsFileIsSpecified() throws IOException {
service =
createJsonRpcHttpService(
rpcMethods, createJsonRpcConfig(invalidKnownClientsTlsConfiguration()));
Expand All @@ -210,9 +227,10 @@ public void exceptionRaisedWhenInvalidKnownClientsFileIsSpecified() throws IOExc
}

private TlsConfiguration invalidKeystoreFileTlsConfiguration() throws IOException {
final File tempFile = folder.newFile();
final Path tempFile = tempDir.resolve("invalidKeystoreFileTlsConfig");
Files.createFile(tempFile);
return aTlsConfiguration()
.withKeyStorePath(tempFile.toPath())
.withKeyStorePath(tempFile)
.withKeyStorePasswordSupplier(() -> "invalid_password")
.withClientAuthConfiguration(
aTlsClientAuthConfiguration().withKnownClientsFile(knownClientsFile).build())
Expand Down Expand Up @@ -248,7 +266,7 @@ private TlsConfiguration invalidPasswordFileTlsConfiguration() {
}

private TlsConfiguration invalidKnownClientsTlsConfiguration() throws IOException {
final Path tempKnownClientsFile = folder.newFile().toPath();
final Path tempKnownClientsFile = tempDir.resolve("invalidKnownClientsTlsConfiguration");
Files.write(tempKnownClientsFile, List.of("cn invalid_sha256"));

return TlsConfiguration.Builder.aTlsConfiguration()
Expand All @@ -260,11 +278,12 @@ private TlsConfiguration invalidKnownClientsTlsConfiguration() throws IOExceptio
}

private JsonRpcHttpService createJsonRpcHttpService(
final Map<String, JsonRpcMethod> rpcMethods, final JsonRpcConfiguration jsonRpcConfig)
throws IOException {
final Map<String, JsonRpcMethod> rpcMethods, final JsonRpcConfiguration jsonRpcConfig) {
final Path testDir = tempDir.resolve("createJsonRpcHttpSercice");
testDir.toFile().mkdirs();
return new JsonRpcHttpService(
vertx,
folder.newFolder().toPath(),
tempDir,
jsonRpcConfig,
new NoOpMetricsSystem(),
natService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
public class ClassicProtocolSpecs {
private static final Wei MAX_BLOCK_REWARD = Wei.fromEth(5);

private ClassicProtocolSpecs() {
// utility class
}

public static ProtocolSpecBuilder classicRecoveryInitDefinition(
final OptionalInt contractSizeLimit,
final OptionalInt configStackSizeLimit,
Expand Down Expand Up @@ -80,7 +84,7 @@ public static ProtocolSpecBuilder tangerineWhistleDefinition(

public static ProtocolSpecBuilder dieHardDefinition(
final Optional<BigInteger> chainId,
final OptionalInt configContractSizeLimit,
final OptionalInt ignoredConfigContractSizeLimit,
final OptionalInt configStackSizeLimit,
final boolean quorumCompatibilityMode,
final EvmConfiguration evmConfiguration) {
Expand Down Expand Up @@ -197,6 +201,7 @@ public static ProtocolSpecBuilder atlantisDefinition(
contractCreationProcessor,
messageCallProcessor,
true,
false,
stackSizeLimit,
FeeMarket.legacy(),
CoinbaseFeePriceCalculator.frontier()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
import org.hyperledger.besu.config.MergeConfigOptions;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.feemarket.BaseFeeMarket;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.AncestryValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.AttachedComposedFromDetachedRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.BaseFeeMarketBlockHeaderGasPriceValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.CalculatedDifficultyValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.ConstantFieldValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.ConstantOmmersHashRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.ExtraDataMaxLengthValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.GasLimitRangeAndDeltaValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.GasUsageValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.IncrementalTimestampRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.NoDifficultyRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.NoNonceRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.ProofOfWorkValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.TimestampBoundedByFutureParameter;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.TimestampMoreRecentThanParent;
Expand All @@ -44,6 +49,10 @@ public final class MainnetBlockHeaderValidator {
public static final Bytes CLASSIC_FORK_BLOCK_HEADER =
Bytes.fromHexString("0x94365e3a8c0b35089c1d1195081fe7489b528a84b22199c916180db8b28ade7f");

private MainnetBlockHeaderValidator() {
// utility class
}

public static BlockHeaderValidator.Builder create() {
return createPgaFeeMarketValidator(PoWHasher.ETHASH_LIGHT);
}
Expand Down Expand Up @@ -85,11 +94,6 @@ static BlockHeaderValidator.Builder createLegacyFeeMarketOmmerValidator() {
new EpochCalculator.DefaultEpochCalculator(), PoWHasher.ETHASH_LIGHT);
}

static BlockHeaderValidator.Builder createLegacyFeeMarketOmmerValidator(final PoWHasher hasher) {
return createLegacyFeeMarketOmmerValidator(
new EpochCalculator.DefaultEpochCalculator(), hasher);
}

static BlockHeaderValidator.Builder createLegacyFeeMarketOmmerValidator(
final EpochCalculator epochCalculator, final PoWHasher hasher) {
return new BlockHeaderValidator.Builder()
Expand Down Expand Up @@ -177,4 +181,23 @@ static BlockHeaderValidator.Builder createBaseFeeMarketOmmerValidator(
Optional.of(baseFeeMarket)))
.addRule((new BaseFeeMarketBlockHeaderGasPriceValidationRule(baseFeeMarket)));
}

public static BlockHeaderValidator.Builder mergeBlockHeaderValidator(final FeeMarket feeMarket) {

var baseFeeMarket = (BaseFeeMarket) feeMarket;

return new BlockHeaderValidator.Builder()
.addRule(new AncestryValidationRule())
.addRule(new GasUsageValidationRule())
.addRule(
new GasLimitRangeAndDeltaValidationRule(
MIN_GAS_LIMIT, Long.MAX_VALUE, Optional.of(baseFeeMarket)))
.addRule(new TimestampBoundedByFutureParameter(TIMESTAMP_TOLERANCE_S))
.addRule(new ExtraDataMaxLengthValidationRule(BlockHeader.MAX_EXTRA_DATA_BYTES))
.addRule((new BaseFeeMarketBlockHeaderGasPriceValidationRule(baseFeeMarket)))
.addRule(new ConstantOmmersHashRule())
.addRule(new NoNonceRule())
.addRule(new NoDifficultyRule())
.addRule(new IncrementalTimestampRule());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ private MainnetDifficultyCalculators() {}
(time, parent, protocolContext) ->
calculateThawedDifficulty(time, parent, GRAY_GLACIER_FAKE_BLOCK_OFFSET);

// Proof-of-Stake difficulty must not be altered
static final DifficultyCalculator PROOF_OF_STAKE_DIFFICULTY =
(time, parent, protocolContext) -> parent.getDifficulty().getAsBigInteger();

private static BigInteger calculateThawedDifficulty(
final long time, final BlockHeader parent, final long fakeBlockOffset) {
final BigInteger parentDifficulty = difficulty(parent.getDifficulty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public abstract class MainnetProtocolSpecs {

public static final int SPURIOUS_DRAGON_CONTRACT_SIZE_LIMIT = 24576;

public static final String LONDON_FORK_NAME = "London";

private static final Address RIPEMD160_PRECOMPILE =
Address.fromHexString("0x0000000000000000000000000000000000000003");

Expand Down Expand Up @@ -134,6 +132,7 @@ public static ProtocolSpecBuilder frontierDefinition(
contractCreationProcessor,
messageCallProcessor,
false,
false,
stackSizeLimit,
FeeMarket.legacy(),
CoinbaseFeePriceCalculator.frontier()))
Expand Down Expand Up @@ -324,6 +323,7 @@ public static ProtocolSpecBuilder spuriousDragonDefinition(
contractCreationProcessor,
messageCallProcessor,
true,
false,
stackSizeLimit,
FeeMarket.legacy(),
CoinbaseFeePriceCalculator.frontier()))
Expand Down Expand Up @@ -540,6 +540,7 @@ static ProtocolSpecBuilder londonDefinition(
contractCreationProcessor,
messageCallProcessor,
true,
false,
stackSizeLimit,
londonFeeMarket,
CoinbaseFeePriceCalculator.eip1559()))
Expand All @@ -564,7 +565,7 @@ static ProtocolSpecBuilder londonDefinition(
feeMarket ->
MainnetBlockHeaderValidator.createBaseFeeMarketOmmerValidator(londonFeeMarket))
.blockBodyValidatorBuilder(BaseFeeBlockBodyValidator::new)
.name(LONDON_FORK_NAME);
.name("London");
}

static ProtocolSpecBuilder arrowGlacierDefinition(
Expand Down Expand Up @@ -627,6 +628,9 @@ static ProtocolSpecBuilder parisDefinition(
.evmBuilder(
(gasCalculator, jdCacheConfig) ->
MainnetEVMs.paris(gasCalculator, chainId.orElse(BigInteger.ZERO), evmConfiguration))
.difficultyCalculator(MainnetDifficultyCalculators.PROOF_OF_STAKE_DIFFICULTY)
.blockHeaderValidatorBuilder(MainnetBlockHeaderValidator::mergeBlockHeaderValidator)
.blockReward(Wei.ZERO)
.name("ParisFork");
}

Expand All @@ -638,6 +642,12 @@ static ProtocolSpecBuilder shandongDefinition(
final GenesisConfigOptions genesisConfigOptions,
final boolean quorumCompatibilityMode,
final EvmConfiguration evmConfiguration) {
final int stackSizeLimit = configStackSizeLimit.orElse(MessageFrame.DEFAULT_MAX_STACK_SIZE);
final long londonForkBlockNumber = genesisConfigOptions.getLondonBlockNumber().orElse(0L);
final BaseFeeMarket londonFeeMarket =
genesisConfigOptions.isZeroBaseFee()
? FeeMarket.zeroBaseFee(londonForkBlockNumber)
: FeeMarket.london(londonForkBlockNumber, genesisConfigOptions.getBaseFeePerGas());

return parisDefinition(
chainId,
Expand All @@ -651,6 +661,21 @@ static ProtocolSpecBuilder shandongDefinition(
(gasCalculator, jdCacheConfig) ->
MainnetEVMs.shandong(
gasCalculator, chainId.orElse(BigInteger.ZERO), evmConfiguration))
.transactionProcessorBuilder(
(gasCalculator,
transactionValidator,
contractCreationProcessor,
messageCallProcessor) ->
new MainnetTransactionProcessor(
gasCalculator,
transactionValidator,
contractCreationProcessor,
messageCallProcessor,
true,
true,
stackSizeLimit,
londonFeeMarket,
CoinbaseFeePriceCalculator.eip1559()))
.name("Shandong");
}

Expand Down
Loading

0 comments on commit ce70f50

Please sign in to comment.