Search in sources :

Example 1 with MutableProtocolSchedule

use of org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule in project besu by hyperledger.

the class ForkIdsTest method testForkId.

@Test
public void testForkId() {
    final GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(EthNetworkConfig.jsonConfig(chainName));
    final GenesisConfigOptions configOptions = genesisConfigFile.getConfigOptions();
    final ProtocolSchedule schedule = MainnetProtocolSchedule.fromConfig(configOptions, EvmConfiguration.DEFAULT);
    final GenesisState genesisState = GenesisState.fromConfig(genesisConfigFile, schedule);
    final Blockchain mockBlockchain = mock(Blockchain.class);
    when(mockBlockchain.getGenesisBlock()).thenReturn(genesisState.getBlock());
    final AtomicLong blockNumber = new AtomicLong();
    when(mockBlockchain.getChainHeadBlockNumber()).thenAnswer(o -> blockNumber.get());
    final ForkIdManager forkIdManager = new ForkIdManager(mockBlockchain, genesisConfigFile.getForks(), false);
    final var actualForkIds = Streams.concat(((MutableProtocolSchedule) schedule).streamMilestoneBlocks(), Stream.of(Long.MAX_VALUE)).map(block -> {
        blockNumber.set(block);
        return forkIdManager.getForkIdForChainHead();
    }).collect(Collectors.toList());
    assertThat(actualForkIds).containsExactlyElementsOf(expectedForkIds);
}
Also used : EthNetworkConfig(org.hyperledger.besu.cli.config.EthNetworkConfig) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) Bytes(org.apache.tuweni.bytes.Bytes) GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) NetworkName(org.hyperledger.besu.cli.config.NetworkName) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) ForkId(org.hyperledger.besu.ethereum.eth.manager.ForkId) Parameterized(org.junit.runners.Parameterized) Collection(java.util.Collection) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) MainnetProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ForkIdManager(org.hyperledger.besu.ethereum.eth.manager.ForkIdManager) Streams(com.google.common.collect.Streams) EvmConfiguration(org.hyperledger.besu.evm.internal.EvmConfiguration) Collectors(java.util.stream.Collectors) MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Stream(java.util.stream.Stream) Mockito.mock(org.mockito.Mockito.mock) GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) AtomicLong(java.util.concurrent.atomic.AtomicLong) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) MainnetProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule) MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) ForkIdManager(org.hyperledger.besu.ethereum.eth.manager.ForkIdManager) Test(org.junit.Test)

Example 2 with MutableProtocolSchedule

use of org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule in project besu by hyperledger.

the class BlockMinerTest method singleSpecSchedule.

private ProtocolSchedule singleSpecSchedule(final ProtocolSpec protocolSpec) {
    final MutableProtocolSchedule protocolSchedule = new MutableProtocolSchedule(Optional.of(BigInteger.valueOf(1234)));
    protocolSchedule.putMilestone(0, protocolSpec);
    return protocolSchedule;
}
Also used : MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule)

Example 3 with MutableProtocolSchedule

use of org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule in project besu by hyperledger.

the class CombinedProtocolScheduleFactory method create.

public ProtocolSchedule create(final NavigableSet<ForkSpec<ProtocolSchedule>> forkSpecs, final Optional<BigInteger> chainId) {
    final MutableProtocolSchedule combinedProtocolSchedule = new MutableProtocolSchedule(chainId);
    for (ForkSpec<ProtocolSchedule> spec : forkSpecs) {
        checkState(spec.getValue() instanceof MutableProtocolSchedule, "Consensus migration requires a MutableProtocolSchedule");
        final MutableProtocolSchedule protocolSchedule = (MutableProtocolSchedule) spec.getValue();
        final Optional<Long> endBlock = Optional.ofNullable(forkSpecs.higher(spec)).map(ForkSpec::getBlock);
        protocolSchedule.getScheduledProtocolSpecs().stream().filter(protocolSpecMatchesConsensusBlockRange(spec.getBlock(), endBlock)).forEach(s -> combinedProtocolSchedule.putMilestone(s.getBlock(), s.getSpec()));
        // our consensus mechanism's BesuControllerBuilder so any additional rules are applied
        if (spec.getBlock() > 0) {
            combinedProtocolSchedule.putMilestone(spec.getBlock(), protocolSchedule.getByBlockNumber(spec.getBlock()));
        }
    }
    return combinedProtocolSchedule;
}
Also used : MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)

Example 4 with MutableProtocolSchedule

use of org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule in project besu by hyperledger.

the class VMReferenceTest method runTest.

@Override
protected void runTest() {
    final MutableWorldState worldState = new DefaultMutableWorldState(spec.getInitialWorldState());
    final EnvironmentInformation execEnv = spec.getExec();
    final ProtocolSpec protocolSpec = MainnetProtocolSpecs.frontierDefinition(OptionalInt.empty(), OptionalInt.empty(), false, EvmConfiguration.DEFAULT).privacyParameters(PrivacyParameters.DEFAULT).privateTransactionValidatorBuilder(() -> new PrivateTransactionValidator(CHAIN_ID)).badBlocksManager(new BadBlockManager()).build(new MutableProtocolSchedule(CHAIN_ID));
    final ReferenceTestBlockchain blockchain = new ReferenceTestBlockchain(execEnv.getBlockHeader().getNumber());
    final MessageFrame frame = MessageFrame.builder().type(MessageFrame.Type.MESSAGE_CALL).messageFrameStack(new ArrayDeque<>()).worldUpdater(worldState.updater()).initialGas(spec.getExec().getGas()).contract(execEnv.getAccountAddress()).address(execEnv.getAccountAddress()).originator(execEnv.getOriginAddress()).gasPrice(execEnv.getGasPrice()).inputData(execEnv.getData()).sender(execEnv.getCallerAddress()).value(execEnv.getValue()).apparentValue(execEnv.getValue()).code(execEnv.getCode()).blockValues(execEnv.getBlockHeader()).depth(execEnv.getDepth()).completer(c -> {
    }).miningBeneficiary(execEnv.getBlockHeader().getCoinbase()).blockHashLookup(new BlockHashLookup(execEnv.getBlockHeader(), blockchain)).maxStackSize(MessageFrame.DEFAULT_MAX_STACK_SIZE).build();
    // This is normally set inside the containing message executing the code.
    frame.setState(MessageFrame.State.CODE_EXECUTING);
    protocolSpec.getEvm().runToHalt(frame, OperationTracer.NO_TRACING);
    if (spec.isExceptionHaltExpected()) {
        assertThat(frame.getState() == MessageFrame.State.EXCEPTIONAL_HALT).withFailMessage("VM should have exceptionally halted").isTrue();
    } else {
        // This is normally performed when the message processor executing the VM
        // executes to completion successfully.
        frame.getWorldUpdater().commit();
        assertThat(frame.getState() == MessageFrame.State.EXCEPTIONAL_HALT).withFailMessage("VM should not have exceptionally halted with " + frame.getExceptionalHaltReason()).isFalse();
        assertThat(frame.getOutputData()).withFailMessage("VM output differs").isEqualTo(spec.getOut());
        assertThat(worldState.rootHash()).withFailMessage("Final world state differs").isEqualTo(spec.getFinalWorldState().rootHash());
        final Gas actualGas = frame.getRemainingGas();
        final Gas expectedGas = spec.getFinalGas();
        final Gas difference = (expectedGas.compareTo(actualGas) > 0) ? expectedGas.minus(actualGas) : actualGas.minus(expectedGas);
        assertThat(actualGas).withFailMessage("Final gas does not match, with difference of %s", difference).isEqualTo(expectedGas);
    }
}
Also used : MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) Gas(org.hyperledger.besu.evm.Gas) EnvironmentInformation(org.hyperledger.besu.ethereum.referencetests.EnvironmentInformation) PrivateTransactionValidator(org.hyperledger.besu.ethereum.privacy.PrivateTransactionValidator) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) ReferenceTestBlockchain(org.hyperledger.besu.ethereum.referencetests.ReferenceTestBlockchain)

Aggregations

MutableProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule)4 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)2 Streams (com.google.common.collect.Streams)1 Collection (java.util.Collection)1 List (java.util.List)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 EthNetworkConfig (org.hyperledger.besu.cli.config.EthNetworkConfig)1 NetworkName (org.hyperledger.besu.cli.config.NetworkName)1 GenesisConfigFile (org.hyperledger.besu.config.GenesisConfigFile)1 GenesisConfigOptions (org.hyperledger.besu.config.GenesisConfigOptions)1 BadBlockManager (org.hyperledger.besu.ethereum.chain.BadBlockManager)1 Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)1 GenesisState (org.hyperledger.besu.ethereum.chain.GenesisState)1 MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)1 ForkId (org.hyperledger.besu.ethereum.eth.manager.ForkId)1 ForkIdManager (org.hyperledger.besu.ethereum.eth.manager.ForkIdManager)1