Search in sources :

Example 6 with EVM

use of org.hyperledger.besu.evm.EVM in project besu by hyperledger.

the class CreateOperationTest method createFromMemoryMutationSafe.

@Test
public void createFromMemoryMutationSafe() {
    // Given:  Execute a CREATE operation with a contract that logs in the constructor
    final UInt256 memoryOffset = UInt256.fromHexString("0xFF");
    final UInt256 memoryLength = UInt256.valueOf(SIMPLE_CREATE.size());
    final ArrayDeque<MessageFrame> messageFrameStack = new ArrayDeque<>();
    final MessageFrame messageFrame = testMemoryFrame(memoryOffset, memoryLength, UInt256.ZERO, 1, messageFrameStack);
    when(account.getMutable()).thenReturn(mutableAccount);
    when(account.getNonce()).thenReturn(55L);
    when(mutableAccount.getBalance()).thenReturn(Wei.ZERO);
    when(worldUpdater.getAccount(any())).thenReturn(account);
    when(worldUpdater.get(any())).thenReturn(account);
    when(worldUpdater.getSenderAccount(any())).thenReturn(account);
    when(worldUpdater.getOrCreate(any())).thenReturn(newAccount);
    when(newAccount.getMutable()).thenReturn(newMutableAccount);
    when(newMutableAccount.getCode()).thenReturn(Bytes.EMPTY);
    when(worldUpdater.updater()).thenReturn(worldUpdater);
    final EVM evm = MainnetEVMs.london(EvmConfiguration.DEFAULT);
    operation.execute(messageFrame, evm);
    final MessageFrame createFrame = messageFrameStack.peek();
    final ContractCreationProcessor ccp = new ContractCreationProcessor(evm.getGasCalculator(), evm, false, List.of(), 0, List.of());
    ccp.process(createFrame, OperationTracer.NO_TRACING);
    final Log log = createFrame.getLogs().get(0);
    final String calculatedTopic = log.getTopics().get(0).toUnprefixedHexString();
    assertThat(calculatedTopic).isEqualTo(TOPIC);
    // WHEN the memory that the create operation was executed from is altered.
    messageFrame.writeMemory(memoryOffset.trimLeadingZeros().toInt(), SIMPLE_CREATE.size(), Bytes.random(SIMPLE_CREATE.size()));
    // THEN the logs still have the expected topic
    final String calculatedTopicAfter = log.getTopics().get(0).toUnprefixedHexString();
    assertThat(calculatedTopicAfter).isEqualTo(TOPIC);
}
Also used : ContractCreationProcessor(org.hyperledger.besu.evm.processor.ContractCreationProcessor) Log(org.hyperledger.besu.evm.log.Log) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) EVM(org.hyperledger.besu.evm.EVM) UInt256(org.apache.tuweni.units.bigints.UInt256) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 7 with EVM

use of org.hyperledger.besu.evm.EVM in project besu by hyperledger.

the class JumpOperationTest method init.

@Before
public void init() {
    blockchain = mock(Blockchain.class);
    address = Address.fromHexString("0x18675309");
    final WorldStateArchive worldStateArchive = createInMemoryWorldStateArchive();
    worldStateUpdater = worldStateArchive.getMutable().updater();
    worldStateUpdater.getOrCreate(address).getMutable().setBalance(Wei.of(1));
    worldStateUpdater.commit();
    final OperationRegistry registry = new OperationRegistry();
    registry.put(new JumpOperation(gasCalculator));
    registry.put(new JumpDestOperation(gasCalculator));
    evm = new EVM(registry, gasCalculator, EvmConfiguration.DEFAULT);
}
Also used : InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) JumpOperation(org.hyperledger.besu.evm.operation.JumpOperation) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) JumpDestOperation(org.hyperledger.besu.evm.operation.JumpDestOperation) OperationRegistry(org.hyperledger.besu.evm.operation.OperationRegistry) EVM(org.hyperledger.besu.evm.EVM) Before(org.junit.Before)

Example 8 with EVM

use of org.hyperledger.besu.evm.EVM in project besu by hyperledger.

the class PrevRanDaoOperationTest method pushesDifficultyWhenPresent.

@Test
public void pushesDifficultyWhenPresent() {
    PrevRanDaoOperation op = new PrevRanDaoOperation(new LondonGasCalculator());
    MessageFrame messageFrame = mock(MessageFrame.class);
    BlockValues blockHeader = mock(BlockValues.class);
    Bytes32 prevRandao = Bytes32.fromHexString("0xb0b0face");
    Bytes difficulty = Bytes.random(32);
    when(blockHeader.getDifficultyBytes()).thenReturn(difficulty);
    when(blockHeader.getMixHashOrPrevRandao()).thenReturn(prevRandao);
    when(messageFrame.getBlockValues()).thenReturn(blockHeader);
    EVM evm = mock(EVM.class);
    Operation.OperationResult r = op.executeFixedCostOperation(messageFrame, evm);
    assertThat(r.getHaltReason()).isNotPresent();
    verify(messageFrame).pushStackItem(difficulty);
}
Also used : PrevRanDaoOperation(org.hyperledger.besu.evm.operation.PrevRanDaoOperation) Bytes(org.apache.tuweni.bytes.Bytes) LondonGasCalculator(org.hyperledger.besu.evm.gascalculator.LondonGasCalculator) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) BlockValues(org.hyperledger.besu.evm.frame.BlockValues) Operation(org.hyperledger.besu.evm.operation.Operation) PrevRanDaoOperation(org.hyperledger.besu.evm.operation.PrevRanDaoOperation) Bytes32(org.apache.tuweni.bytes.Bytes32) EVM(org.hyperledger.besu.evm.EVM) Test(org.junit.Test)

Example 9 with EVM

use of org.hyperledger.besu.evm.EVM in project besu by hyperledger.

the class TestCodeExecutor method executeCode.

public MessageFrame executeCode(final String codeHexString, final long gasLimit, final Consumer<MutableAccount> accountSetup) {
    final ProtocolSpec protocolSpec = fixture.getProtocolSchedule().getByBlockNumber(0);
    final WorldUpdater worldUpdater = createInitialWorldState(accountSetup, fixture.getStateArchive());
    final Deque<MessageFrame> messageFrameStack = new ArrayDeque<>();
    final EVM evm = protocolSpec.getEvm();
    final MessageCallProcessor messageCallProcessor = new MessageCallProcessor(evm, new PrecompileContractRegistry());
    final Bytes codeBytes = Bytes.fromHexString(codeHexString);
    final Code code = evm.getCode(Hash.hash(codeBytes), codeBytes);
    final Transaction transaction = Transaction.builder().type(TransactionType.FRONTIER).value(Wei.ZERO).sender(SENDER_ADDRESS).signature(SignatureAlgorithmFactory.getInstance().createSignature(BigInteger.ONE, BigInteger.TEN, (byte) 1)).gasLimit(gasLimit).to(SENDER_ADDRESS).payload(Bytes.EMPTY).gasPrice(Wei.ZERO).nonce(0).build();
    final MessageFrame initialFrame = new MessageFrameTestFixture().messageFrameStack(messageFrameStack).blockchain(fixture.getBlockchain()).worldUpdater(worldUpdater).initialGas(gasLimit).address(SENDER_ADDRESS).originator(SENDER_ADDRESS).contract(SENDER_ADDRESS).gasPrice(transaction.getGasPrice().get()).inputData(transaction.getPayload()).sender(SENDER_ADDRESS).value(transaction.getValue()).code(code).blockHeader(blockHeader).depth(0).build();
    messageFrameStack.addFirst(initialFrame);
    while (!messageFrameStack.isEmpty()) {
        messageCallProcessor.process(messageFrameStack.peekFirst(), OperationTracer.NO_TRACING);
    }
    return initialFrame;
}
Also used : MessageCallProcessor(org.hyperledger.besu.evm.processor.MessageCallProcessor) Bytes(org.apache.tuweni.bytes.Bytes) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) EVM(org.hyperledger.besu.evm.EVM) PrecompileContractRegistry(org.hyperledger.besu.evm.precompile.PrecompileContractRegistry) Code(org.hyperledger.besu.evm.Code) ArrayDeque(java.util.ArrayDeque)

Example 10 with EVM

use of org.hyperledger.besu.evm.EVM in project besu by hyperledger.

the class EvmToyCommand method run.

@Override
public void run() {
    final WorldUpdater worldUpdater = new ToyWorld();
    worldUpdater.getOrCreate(sender).getMutable().setBalance(Wei.of(BigInteger.TWO.pow(20)));
    worldUpdater.getOrCreate(receiver).getMutable().setCode(codeBytes);
    int repeat = this.repeat;
    final EVM evm = MainnetEVMs.berlin(EvmConfiguration.DEFAULT);
    final Code code = evm.getCode(Hash.hash(codeBytes), codeBytes);
    final PrecompileContractRegistry precompileContractRegistry = new PrecompileContractRegistry();
    MainnetPrecompiledContracts.populateForIstanbul(precompileContractRegistry, evm.getGasCalculator());
    final Stopwatch stopwatch = Stopwatch.createUnstarted();
    long lastTime = 0;
    do {
        final boolean lastLoop = repeat == 0;
        final // You should have picked Mercy.
        OperationTracer tracer = lastLoop && showJsonResults ? new StandardJsonTracer(System.out, !noMemory) : OperationTracer.NO_TRACING;
        final Deque<MessageFrame> messageFrameStack = new ArrayDeque<>();
        messageFrameStack.add(MessageFrame.builder().type(MessageFrame.Type.MESSAGE_CALL).messageFrameStack(messageFrameStack).worldUpdater(worldUpdater.updater()).initialGas(gas).contract(Address.ZERO).address(receiver).originator(sender).sender(sender).gasPrice(gasPriceGWei).inputData(callData).value(ethValue).apparentValue(ethValue).code(code).blockValues(new ToyBlockValues()).depth(0).completer(c -> {
        }).miningBeneficiary(Address.ZERO).blockHashLookup(h -> null).build());
        final MessageCallProcessor mcp = new MessageCallProcessor(evm, precompileContractRegistry);
        final ContractCreationProcessor ccp = new ContractCreationProcessor(evm.getGasCalculator(), evm, false, List.of(), 0);
        stopwatch.start();
        while (!messageFrameStack.isEmpty()) {
            final MessageFrame messageFrame = messageFrameStack.peek();
            switch(messageFrame.getType()) {
                case CONTRACT_CREATION:
                    ccp.process(messageFrame, tracer);
                    break;
                case MESSAGE_CALL:
                    mcp.process(messageFrame, tracer);
                    break;
            }
            if (lastLoop) {
                if (messageFrame.getExceptionalHaltReason().isPresent()) {
                    out.println(messageFrame.getExceptionalHaltReason().get());
                }
                if (messageFrame.getRevertReason().isPresent()) {
                    out.println(new String(messageFrame.getRevertReason().get().toArray(), StandardCharsets.UTF_8));
                }
            }
            if (messageFrameStack.isEmpty()) {
                stopwatch.stop();
                if (lastTime == 0) {
                    lastTime = stopwatch.elapsed().toNanos();
                }
            }
            if (lastLoop && messageFrameStack.isEmpty()) {
                final long evmGas = gas - messageFrame.getRemainingGas();
                out.println();
                out.printf("{ \"gasUser\": \"0x%s\", \"timens\": %d, \"time\": %d }%n", Long.toHexString(evmGas), lastTime, lastTime / 1000);
            }
        }
        lastTime = stopwatch.elapsed().toNanos();
        stopwatch.reset();
    } while (repeat-- > 0);
}
Also used : MessageCallProcessor(org.hyperledger.besu.evm.processor.MessageCallProcessor) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) Stopwatch(com.google.common.base.Stopwatch) StandardJsonTracer(org.hyperledger.besu.evm.tracing.StandardJsonTracer) Code(org.hyperledger.besu.evm.Code) PrecompileContractRegistry(org.hyperledger.besu.evm.precompile.PrecompileContractRegistry) ArrayDeque(java.util.ArrayDeque) ContractCreationProcessor(org.hyperledger.besu.evm.processor.ContractCreationProcessor) EVM(org.hyperledger.besu.evm.EVM)

Aggregations

EVM (org.hyperledger.besu.evm.EVM)11 MessageFrame (org.hyperledger.besu.evm.frame.MessageFrame)9 ArrayDeque (java.util.ArrayDeque)7 Test (org.junit.Test)6 UInt256 (org.apache.tuweni.units.bigints.UInt256)4 PrecompileContractRegistry (org.hyperledger.besu.evm.precompile.PrecompileContractRegistry)4 Bytes (org.apache.tuweni.bytes.Bytes)3 Code (org.hyperledger.besu.evm.Code)3 MessageCallProcessor (org.hyperledger.besu.evm.processor.MessageCallProcessor)3 Stopwatch (com.google.common.base.Stopwatch)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 ProtocolSpec (org.hyperledger.besu.ethereum.mainnet.ProtocolSpec)2 ContractCreationProcessor (org.hyperledger.besu.evm.processor.ContractCreationProcessor)2 StandardJsonTracer (org.hyperledger.besu.evm.tracing.StandardJsonTracer)2 WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)2 JsonObject (io.vertx.core.json.JsonObject)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1