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);
}
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);
}
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);
}
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;
}
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);
}
Aggregations