Search in sources :

Example 1 with DefaultMutableWorldState

use of org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState in project besu by hyperledger.

the class GenesisState method calculateGenesisStateHash.

private static Hash calculateGenesisStateHash(final List<GenesisAccount> genesisAccounts) {
    final WorldStateKeyValueStorage stateStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final WorldStatePreimageKeyValueStorage preimageStorage = new WorldStatePreimageKeyValueStorage(new InMemoryKeyValueStorage());
    final MutableWorldState worldState = new DefaultMutableWorldState(stateStorage, preimageStorage);
    writeAccountsTo(worldState, genesisAccounts, null);
    return worldState.rootHash();
}
Also used : WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) WorldStatePreimageKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStatePreimageKeyValueStorage)

Example 2 with DefaultMutableWorldState

use of org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState in project besu by hyperledger.

the class BlockchainModule method getMutableWorldView.

@Provides
MutableWorldView getMutableWorldView(@Named("StateRoot") final Bytes32 stateRoot, final WorldStateStorage worldStateStorage, final WorldStatePreimageStorage worldStatePreimageStorage, final GenesisState genesisState, @Named("KeyValueStorageName") final String keyValueStorageName) {
    if ("memory".equals(keyValueStorageName)) {
        final MutableWorldState mutableWorldState = new DefaultMutableWorldState(worldStateStorage, worldStatePreimageStorage);
        genesisState.writeStateTo(mutableWorldState);
        return (MutableWorldView) mutableWorldState;
    } else {
        return (MutableWorldView) new DefaultMutableWorldState(stateRoot, worldStateStorage, worldStatePreimageStorage);
    }
}
Also used : MutableWorldView(org.hyperledger.besu.evm.worldstate.MutableWorldView) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) Provides(dagger.Provides)

Example 3 with DefaultMutableWorldState

use of org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState in project besu by hyperledger.

the class StateTestSubCommand method traceTestSpecs.

private void traceTestSpecs(final String test, final List<GeneralStateTestCaseEipSpec> specs) {
    Log4j2ConfiguratorUtil.setLevel("org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder", Level.OFF);
    final var referenceTestProtocolSchedules = ReferenceTestProtocolSchedules.create();
    Log4j2ConfiguratorUtil.setLevel("org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder", null);
    final // You should have picked Mercy.
    OperationTracer tracer = parentCommand.showJsonResults ? new StandardJsonTracer(output, !parentCommand.noMemory) : OperationTracer.NO_TRACING;
    for (final GeneralStateTestCaseEipSpec spec : specs) {
        final BlockHeader blockHeader = spec.getBlockHeader();
        final WorldState initialWorldState = spec.getInitialWorldState();
        final Transaction transaction = spec.getTransaction();
        final ObjectNode summaryLine = objectMapper.createObjectNode();
        if (transaction == null) {
            // Check the world state root hash.
            summaryLine.put("test", test);
            summaryLine.put("fork", spec.getFork());
            summaryLine.put("d", spec.getDataIndex());
            summaryLine.put("g", spec.getGasIndex());
            summaryLine.put("v", spec.getValueIndex());
            summaryLine.put("pass", spec.getExpectException() != null);
            summaryLine.put("validationError", "Transaction had out-of-bounds parameters");
        } else {
            final MutableWorldState worldState = new DefaultMutableWorldState(initialWorldState);
            // `TransactionProcessor`, so these tests are skipped.
            if (transaction.getGasLimit() > blockHeader.getGasLimit() - blockHeader.getGasUsed()) {
                return;
            }
            final String forkName = fork == null ? spec.getFork() : fork;
            final ProtocolSchedule protocolSchedule = referenceTestProtocolSchedules.getByName(forkName);
            if (protocolSchedule == null) {
                throw new UnsupportedForkException(forkName);
            }
            final MainnetTransactionProcessor processor = protocolSchedule.getByBlockNumber(0).getTransactionProcessor();
            final WorldUpdater worldStateUpdater = worldState.updater();
            final ReferenceTestBlockchain blockchain = new ReferenceTestBlockchain(blockHeader.getNumber());
            final Stopwatch timer = Stopwatch.createStarted();
            final TransactionProcessingResult result = processor.processTransaction(blockchain, worldStateUpdater, blockHeader, transaction, blockHeader.getCoinbase(), new BlockHashLookup(blockHeader, blockchain), false, TransactionValidationParams.processingBlock(), tracer);
            timer.stop();
            if (shouldClearEmptyAccounts(spec.getFork())) {
                final Account coinbase = worldStateUpdater.getOrCreate(spec.getBlockHeader().getCoinbase());
                if (coinbase != null && coinbase.isEmpty()) {
                    worldStateUpdater.deleteAccount(coinbase.getAddress());
                }
                final Account sender = worldStateUpdater.getAccount(transaction.getSender());
                if (sender != null && sender.isEmpty()) {
                    worldStateUpdater.deleteAccount(sender.getAddress());
                }
            }
            worldStateUpdater.commit();
            summaryLine.put("output", result.getOutput().toUnprefixedHexString());
            final UInt256 gasUsed = UInt256.valueOf(transaction.getGasLimit() - result.getGasRemaining());
            summaryLine.put("gasUsed", StandardJsonTracer.shortNumber(gasUsed));
            summaryLine.put("time", timer.elapsed(TimeUnit.NANOSECONDS));
            // Check the world state root hash.
            summaryLine.put("test", test);
            summaryLine.put("fork", spec.getFork());
            summaryLine.put("d", spec.getDataIndex());
            summaryLine.put("g", spec.getGasIndex());
            summaryLine.put("v", spec.getValueIndex());
            summaryLine.put("postHash", worldState.rootHash().toHexString());
            final List<Log> logs = result.getLogs();
            final Hash actualLogsHash = Hash.hash(RLP.encode(out -> out.writeList(logs, Log::writeTo)));
            summaryLine.put("postLogsHash", actualLogsHash.toHexString());
            summaryLine.put("pass", spec.getExpectException() == null && worldState.rootHash().equals(spec.getExpectedRootHash()) && actualLogsHash.equals(spec.getExpectedLogsHash()));
            if (result.isInvalid()) {
                summaryLine.put("validationError", result.getValidationResult().getErrorMessage());
            } else if (spec.getExpectException() != null) {
                summaryLine.put("validationError", "Exception '" + spec.getExpectException() + "' was expected but did not occur");
            }
        }
        output.println(summaryLine);
    }
}
Also used : WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) Account(org.hyperledger.besu.evm.account.Account) Parameters(picocli.CommandLine.Parameters) LoggerFactory(org.slf4j.LoggerFactory) Level(org.apache.logging.log4j.Level) OperationTracer(org.hyperledger.besu.evm.tracing.OperationTracer) UnsupportedForkException(org.hyperledger.besu.evmtool.exception.UnsupportedForkException) ParentCommand(picocli.CommandLine.ParentCommand) Map(java.util.Map) UInt256(org.apache.tuweni.units.bigints.UInt256) ReferenceTestBlockchain(org.hyperledger.besu.ethereum.referencetests.ReferenceTestBlockchain) GeneralStateTestCaseSpec(org.hyperledger.besu.ethereum.referencetests.GeneralStateTestCaseSpec) TransactionValidationParams(org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams) Log(org.hyperledger.besu.evm.log.Log) StandardCharsets(java.nio.charset.StandardCharsets) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult) List(java.util.List) Option(picocli.CommandLine.Option) GeneralStateTestCaseEipSpec(org.hyperledger.besu.ethereum.referencetests.GeneralStateTestCaseEipSpec) ReferenceTestProtocolSchedules(org.hyperledger.besu.ethereum.referencetests.ReferenceTestProtocolSchedules) Hash(org.hyperledger.besu.datatypes.Hash) Stopwatch(com.google.common.base.Stopwatch) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) JavaType(com.fasterxml.jackson.databind.JavaType) Command(picocli.CommandLine.Command) PrintStream(java.io.PrintStream) WorldState(org.hyperledger.besu.evm.worldstate.WorldState) Log4j2ConfiguratorUtil(org.hyperledger.besu.util.Log4j2ConfiguratorUtil) Logger(org.slf4j.Logger) COMMAND_NAME(org.hyperledger.besu.evmtool.StateTestSubCommand.COMMAND_NAME) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Feature(com.fasterxml.jackson.core.JsonParser.Feature) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) MainnetTransactionProcessor(org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor) RLP(org.hyperledger.besu.ethereum.rlp.RLP) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BlockHashLookup(org.hyperledger.besu.ethereum.vm.BlockHashLookup) InputStreamReader(java.io.InputStreamReader) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ReferenceTestProtocolSchedules.shouldClearEmptyAccounts(org.hyperledger.besu.ethereum.referencetests.ReferenceTestProtocolSchedules.shouldClearEmptyAccounts) BufferedReader(java.io.BufferedReader) Transaction(org.hyperledger.besu.ethereum.core.Transaction) StandardJsonTracer(org.hyperledger.besu.evm.tracing.StandardJsonTracer) InputStream(java.io.InputStream) Account(org.hyperledger.besu.evm.account.Account) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) Stopwatch(com.google.common.base.Stopwatch) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) WorldState(org.hyperledger.besu.evm.worldstate.WorldState) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) StandardJsonTracer(org.hyperledger.besu.evm.tracing.StandardJsonTracer) Hash(org.hyperledger.besu.datatypes.Hash) UnsupportedForkException(org.hyperledger.besu.evmtool.exception.UnsupportedForkException) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Log(org.hyperledger.besu.evm.log.Log) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult) MainnetTransactionProcessor(org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor) Transaction(org.hyperledger.besu.ethereum.core.Transaction) BlockHashLookup(org.hyperledger.besu.ethereum.vm.BlockHashLookup) GeneralStateTestCaseEipSpec(org.hyperledger.besu.ethereum.referencetests.GeneralStateTestCaseEipSpec) ReferenceTestBlockchain(org.hyperledger.besu.ethereum.referencetests.ReferenceTestBlockchain) UInt256(org.apache.tuweni.units.bigints.UInt256)

Example 4 with DefaultMutableWorldState

use of org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState in project besu by hyperledger.

the class GeneralStateReferenceTestTools method executeTest.

public static void executeTest(final GeneralStateTestCaseEipSpec spec) {
    final BlockHeader blockHeader = spec.getBlockHeader();
    final WorldState initialWorldState = spec.getInitialWorldState();
    final Transaction transaction = spec.getTransaction();
    // will handle the case where we receive the TXs in a serialized form.
    if (transaction == null) {
        assertThat(spec.getExpectException()).withFailMessage("Transaction was not assembled, but no exception was expected").isNotNull();
        return;
    }
    final MutableWorldState worldState = new DefaultMutableWorldState(initialWorldState);
    // `TransactionProcessor`, so these tests are skipped.
    if (transaction.getGasLimit() > blockHeader.getGasLimit() - blockHeader.getGasUsed()) {
        return;
    }
    final MainnetTransactionProcessor processor = transactionProcessor(spec.getFork());
    final WorldUpdater worldStateUpdater = worldState.updater();
    final ReferenceTestBlockchain blockchain = new ReferenceTestBlockchain(blockHeader.getNumber());
    final TransactionProcessingResult result = processor.processTransaction(blockchain, worldStateUpdater, blockHeader, transaction, blockHeader.getCoinbase(), new BlockHashLookup(blockHeader, blockchain), false, TransactionValidationParams.processingBlock());
    if (!result.isInvalid()) {
        final Account coinbase = worldStateUpdater.getOrCreate(spec.getBlockHeader().getCoinbase());
        if (coinbase != null && coinbase.isEmpty() && shouldClearEmptyAccounts(spec.getFork())) {
            worldStateUpdater.deleteAccount(coinbase.getAddress());
        }
        worldStateUpdater.commit();
    }
    // Check the world state root hash.
    final Hash expectedRootHash = spec.getExpectedRootHash();
    assertThat(worldState.rootHash()).withFailMessage("Unexpected world state root hash; expected state: %s, computed state: %s", spec.getExpectedRootHash(), worldState.rootHash()).isEqualTo(expectedRootHash);
    // Check the logs.
    final Hash expectedLogsHash = spec.getExpectedLogsHash();
    Optional.ofNullable(expectedLogsHash).ifPresent(expected -> {
        final List<Log> logs = result.getLogs();
        assertThat(Hash.hash(RLP.encode(out -> out.writeList(logs, Log::writeTo)))).withFailMessage("Unmatched logs hash. Generated logs: %s", logs).isEqualTo(expected);
    });
}
Also used : Account(org.hyperledger.besu.evm.account.Account) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) Log(org.hyperledger.besu.evm.log.Log) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) WorldState(org.hyperledger.besu.evm.worldstate.WorldState) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) Hash(org.hyperledger.besu.datatypes.Hash) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult) MainnetTransactionProcessor(org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor) Transaction(org.hyperledger.besu.ethereum.core.Transaction) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) ReferenceTestBlockchain(org.hyperledger.besu.ethereum.referencetests.ReferenceTestBlockchain)

Example 5 with DefaultMutableWorldState

use of org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState 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

MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)5 DefaultMutableWorldState (org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState)5 ReferenceTestBlockchain (org.hyperledger.besu.ethereum.referencetests.ReferenceTestBlockchain)3 Hash (org.hyperledger.besu.datatypes.Hash)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 Transaction (org.hyperledger.besu.ethereum.core.Transaction)2 MainnetTransactionProcessor (org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor)2 TransactionProcessingResult (org.hyperledger.besu.ethereum.processing.TransactionProcessingResult)2 Account (org.hyperledger.besu.evm.account.Account)2 Log (org.hyperledger.besu.evm.log.Log)2 WorldState (org.hyperledger.besu.evm.worldstate.WorldState)2 WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)2 Feature (com.fasterxml.jackson.core.JsonParser.Feature)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Stopwatch (com.google.common.base.Stopwatch)1 Provides (dagger.Provides)1 BufferedReader (java.io.BufferedReader)1