Search in sources :

Example 71 with WorldUpdater

use of org.hyperledger.besu.evm.worldstate.WorldUpdater 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 72 with WorldUpdater

use of org.hyperledger.besu.evm.worldstate.WorldUpdater in project hedera-services by hashgraph.

the class CallLocalEvmTxProcessorTest method assertTransactionSenderAndValue.

@Test
void assertTransactionSenderAndValue() {
    // setup:
    doReturn(Optional.of(receiver.getId().asEvmAddress())).when(transaction).getTo();
    given(worldState.updater()).willReturn(mock(HederaWorldState.Updater.class));
    given(codeCache.getIfPresent(any())).willReturn(new Code());
    given(transaction.getSender()).willReturn(sender.getId().asEvmAddress());
    given(transaction.getValue()).willReturn(Wei.of(1L));
    final MessageFrame.Builder commonInitialFrame = MessageFrame.builder().messageFrameStack(mock(Deque.class)).maxStackSize(MAX_STACK_SIZE).worldUpdater(mock(WorldUpdater.class)).initialGas(mock(Gas.class)).originator(sender.getId().asEvmAddress()).gasPrice(mock(Wei.class)).sender(sender.getId().asEvmAddress()).value(Wei.of(transaction.getValue().getAsBigInteger())).apparentValue(Wei.of(transaction.getValue().getAsBigInteger())).blockValues(mock(BlockValues.class)).depth(0).completer(__ -> {
    }).miningBeneficiary(mock(Address.class)).blockHashLookup(h -> null);
    // when:
    MessageFrame buildMessageFrame = callLocalEvmTxProcessor.buildInitialFrame(commonInitialFrame, worldState.updater(), (Address) transaction.getTo().get(), Bytes.EMPTY);
    // expect:
    assertEquals(transaction.getSender(), buildMessageFrame.getSenderAddress());
    assertEquals(transaction.getValue(), buildMessageFrame.getApparentValue());
}
Also used : WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) Gas(org.hyperledger.besu.evm.Gas) BlockValues(org.hyperledger.besu.evm.frame.BlockValues) Wei(org.hyperledger.besu.datatypes.Wei) Code(org.hyperledger.besu.evm.Code) Test(org.junit.jupiter.api.Test)

Aggregations

WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)72 MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)37 MutableAccount (org.hyperledger.besu.evm.account.MutableAccount)33 Test (org.junit.Test)33 TransactionProcessingResult (org.hyperledger.besu.ethereum.processing.TransactionProcessingResult)16 Wei (org.hyperledger.besu.datatypes.Wei)14 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)14 Address (org.hyperledger.besu.datatypes.Address)13 ArrayList (java.util.ArrayList)12 Hash (org.hyperledger.besu.datatypes.Hash)12 Transaction (org.hyperledger.besu.ethereum.core.Transaction)12 MessageFrame (org.hyperledger.besu.evm.frame.MessageFrame)11 Bytes (org.apache.tuweni.bytes.Bytes)9 Bytes32 (org.apache.tuweni.bytes.Bytes32)9 BlockHashLookup (org.hyperledger.besu.ethereum.vm.BlockHashLookup)9 UInt256 (org.apache.tuweni.units.bigints.UInt256)8 ProcessableBlockHeader (org.hyperledger.besu.ethereum.core.ProcessableBlockHeader)8 Code (org.hyperledger.besu.evm.Code)8 Account (org.hyperledger.besu.evm.account.Account)8 Map (java.util.Map)6