Skip to content

Commit

Permalink
add granite precompile contract
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod committed Sep 19, 2024
1 parent e5bc92a commit cf77337
Show file tree
Hide file tree
Showing 22 changed files with 624 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public BesuControllerBuilder fromGenesisFile(
// https://github.com/hyperledger/besu/issues/2897
return new TransitionBesuControllerBuilder(builder, new MergeBesuControllerBuilder())
.genesisConfigFile(genesisConfigFile)
.genesisConfigOptions(genesisConfigFile.getConfigOptions(overrides));
.genesisConfigOptions(genesisConfigFile.withOverrides(overrides).getConfigOptions());
}

} else return builder.genesisConfigFile(genesisConfigFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,21 @@ default boolean isConsensusMigration() {
*/
boolean isFjord(long headTime);

/**
* Gets granite time.
*
* @return the granite time
*/
OptionalLong getGraniteTime();

/**
* Returns whether a fork scheduled at granite timestamp is active at the given head timestamp.
*
* @param headTime the current head time
* @return the boolean
*/
boolean isGranite(long headTime);

/**
* Gets interop time.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,23 @@ public boolean isFjord(final long headTime) {
return false;
}

@Override
public OptionalLong getGraniteTime() {
return getOptionalLong("granitetime");
}

@Override
public boolean isGranite(final long headTime) {
if (!isOptimism()) {
return false;
}
var graniteTime = getGraniteTime();
if (graniteTime.isPresent()) {
return graniteTime.getAsLong() <= headTime;
}
return false;
}

@Override
public OptionalLong getInteropTime() {
return getOptionalLong("interoptime");
Expand Down Expand Up @@ -740,19 +757,17 @@ public List<Long> getForkBlockNumbers() {
public List<Long> getForkBlockTimestamps() {
Stream<OptionalLong> forkBlockTimestamps;
if (this.isOptimism()) {
forkBlockTimestamps = Stream.of(
getFjordTime(),
getCanyonTime(),
getEcotoneTime());
forkBlockTimestamps = Stream.of(getFjordTime(), getCanyonTime(), getEcotoneTime());
} else {
forkBlockTimestamps = Stream.of(
getShanghaiTime(),
getCancunTime(),
getCancunEOFTime(),
getPragueTime(),
getPragueEOFTime(),
getFutureEipsTime(),
getExperimentalEipsTime());
forkBlockTimestamps =
Stream.of(
getShanghaiTime(),
getCancunTime(),
getCancunEOFTime(),
getPragueTime(),
getPragueEOFTime(),
getFutureEipsTime(),
getExperimentalEipsTime());
}
// when adding forks add an entry to ${REPO_ROOT}/config/src/test/resources/all_forks.json

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class StubGenesisConfigOptions implements GenesisConfigOptions, Cloneable
private OptionalLong canyonTime = OptionalLong.empty();
private OptionalLong ecotoneTime = OptionalLong.empty();
private OptionalLong fjordTime = OptionalLong.empty();
private OptionalLong graniteTime = OptionalLong.empty();
private OptionalLong interopTime = OptionalLong.empty();
private OptionalLong terminalBlockNumber = OptionalLong.empty();
private Optional<Hash> terminalBlockHash = Optional.empty();
Expand Down Expand Up @@ -332,6 +333,16 @@ public boolean isFjord(final long headTime) {
return false;
}

@Override
public OptionalLong getGraniteTime() {
return graniteTime;
}

@Override
public boolean isGranite(final long headTime) {
return false;
}

@Override
public OptionalLong getInteropTime() {
return interopTime;
Expand Down Expand Up @@ -830,6 +841,17 @@ public StubGenesisConfigOptions fjordTime(final long timestamp) {
return this;
}

/**
* Granite time.
*
* @param timestamp the timestamp
* @return the stub genesis config options
*/
public StubGenesisConfigOptions graniteTime(final long timestamp) {
graniteTime = OptionalLong.of(timestamp);
return this;
}

/**
* Interop time.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,42 @@ public String description() {
return description;
}
}

/** List of all Optimism hard forks. */
enum OptimismHardforkId implements HardforkId {
/** Bedrock fork. */
BEDROCK(true, "Bedrock"),
/** Regolith fork. */
REGOLITH(true, "Regolith"),
/** Bedrock fork. */
CANYON(true, "Canyon"),
/** DELTA fork. */
DELTA(true, "Delta"),
/** Ecotone fork. */
ECOTONE(true, "Ecotone"),
/** Fjord fork. */
FJORD(true, "Fjord"),
/** Granite fork. */
GRANITE(true, "Granite"),
/** Interop fork. */
INTEROP(false, "Interop");

final boolean finalized;
final String description;

OptimismHardforkId(final boolean finalized, final String description) {
this.finalized = finalized;
this.description = description;
}

@Override
public boolean finalized() {
return finalized;
}

@Override
public String description() {
return description;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static TransactionType of(final int serializedTypeValue) {
TransactionType.EIP1559,
TransactionType.BLOB,
TransactionType.OPTIMISM_DEPOSIT,
TransactionType.SET_CODE
TransactionType.SET_CODE
})
.filter(transactionType -> transactionType.typeValue == serializedTypeValue)
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod.EngineStatus.SYNCING;
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod.EngineStatus.VALID;

import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator;
import org.hyperledger.besu.consensus.merge.blockcreation.PayloadIdentifier;
import org.hyperledger.besu.ethereum.ProtocolContext;
Expand All @@ -42,6 +41,7 @@

import graphql.VisibleForTesting;
import io.vertx.core.Vertx;
import org.apache.tuweni.bytes.Bytes;

public class EnginePreparePayloadDebug extends ExecutionEngineJsonRpcMethod {
private final MergeMiningCoordinator mergeCoordinator;
Expand Down Expand Up @@ -108,9 +108,9 @@ Optional<PayloadIdentifier> generatePayload(final EnginePreparePayloadParameter
param.getWithdrawals().stream().map(WithdrawalParameter::toWithdrawal).collect(toList());

final List<Transaction> transactions =
param.getTransactions().stream()
.map(s -> Transaction.readFrom(Bytes.fromHexString(s)))
.collect(toList());
param.getTransactions().stream()
.map(s -> Transaction.readFrom(Bytes.fromHexString(s)))
.collect(toList());
return param
.getParentHash()
.map(header -> protocolContext.getBlockchain().getBlockHeader(header))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,14 @@ private TransactionSelectionResult handleTransactionSelected(
transaction.getType(), processingResult, worldState, cumulativeGasUsed);
} else {
GenesisConfigOptions options = genesisConfigOptions.orElseThrow();

Optional<Long> depositNonce =
options.isRegolith(blockSelectionContext.processableBlockHeader().getTimestamp())
options.isRegolith(blockSelectionContext.pendingBlockHeader().getTimestamp())
? Optional.of(nonce)
: Optional.empty();

Optional<Long> canyonDepositReceiptVer =
options.isCanyon(blockSelectionContext.processableBlockHeader().getTimestamp())
options.isCanyon(blockSelectionContext.pendingBlockHeader().getTimestamp())
? Optional.of(1L)
: Optional.empty();
receipt =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface Decoder {
TransactionType.BLOB,
BlobTransactionDecoder::decode,
TransactionType.OPTIMISM_DEPOSIT,
OptimismDepositTransactionDecoder::decode),
OptimismDepositTransactionDecoder::decode,
TransactionType.SET_CODE,
SetCodeTransactionDecoder::decode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ TransactionReceipt create(
@FunctionalInterface
public interface OpTransactionReceiptFactory {
TransactionReceipt create(
TransactionType transactionType,
TransactionProcessingResult result,
WorldState worldState,
long gasUsed);
TransactionType transactionType,
TransactionProcessingResult result,
WorldState worldState,
long gasUsed);
}

private static final Logger LOG = LoggerFactory.getLogger(AbstractBlockProcessor.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForFjord;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForFrontier;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForFutureEIPs;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForGranite;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForIstanbul;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForPrague;

Expand Down Expand Up @@ -68,6 +69,13 @@ static PrecompileContractRegistry fjord(
return registry;
}

static PrecompileContractRegistry granite(
final PrecompiledContractConfiguration precompiledContractConfiguration) {
final PrecompileContractRegistry registry = new PrecompileContractRegistry();
populateForGranite(registry, precompiledContractConfiguration.getGasCalculator());
return registry;
}

static PrecompileContractRegistry prague(
final PrecompiledContractConfiguration precompiledContractConfiguration) {
final PrecompileContractRegistry registry = new PrecompileContractRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,48 @@ public ProtocolSpecBuilder cancunEOFDefinition(final GenesisConfigOptions genesi
metricsSystem);
}

public ProtocolSpecBuilder regolithDefinition(final GenesisConfigOptions genesisConfigOptions) {
return MainnetProtocolSpecs.regolithDefinition(
chainId,
isRevertReasonEnabled,
genesisConfigOptions,
evmConfiguration,
miningParameters,
isParallelTxProcessingEnabled,
metricsSystem);
}

public ProtocolSpecBuilder canyonDefinition(final GenesisConfigOptions genesisConfigOptions) {
return MainnetProtocolSpecs.canyonDefinition(
chainId,
isRevertReasonEnabled,
genesisConfigOptions,
evmConfiguration,
miningParameters,
isParallelTxProcessingEnabled,
metricsSystem);
}

public ProtocolSpecBuilder fjordDefinition(final GenesisConfigOptions genesisConfigOptions) {
return MainnetProtocolSpecs.fjordDefinition(
chainId,
contractSizeLimit,
evmStackSize,
isRevertReasonEnabled,
genesisConfigOptions,
evmConfiguration,
miningParameters);
miningParameters,
isParallelTxProcessingEnabled,
metricsSystem);
}

public ProtocolSpecBuilder graniteDefinition(final GenesisConfigOptions genesisConfigOptions) {
return MainnetProtocolSpecs.graniteDefinition(
chainId,
isRevertReasonEnabled,
genesisConfigOptions,
evmConfiguration,
miningParameters,
isParallelTxProcessingEnabled,
metricsSystem);
}

public ProtocolSpecBuilder pragueDefinition(final GenesisConfigOptions genesisConfigOptions) {
Expand Down
Loading

0 comments on commit cf77337

Please sign in to comment.