use of org.hyperledger.besu.evm.log.Log in project besu by hyperledger.
the class ChainForTestCreator method prepareHeader.
public static BlockHeader prepareHeader(final long number, final Optional<String> message) {
final Address testAddress = Address.fromHexString(message.orElse(String.format("%02X", number)));
final Bytes testMessage = Bytes.fromHexString(String.format("%02X", number));
final Log testLog = new Log(testAddress, testMessage, List.of());
return new BlockHeader(Hash.EMPTY, Hash.EMPTY, Address.ZERO, Hash.EMPTY, Hash.EMPTY, Hash.EMPTY, LogsBloomFilter.builder().insertLog(testLog).build(), Difficulty.ZERO, number, 0, 0, 0, Bytes.EMPTY, null, Hash.EMPTY, 0, new MainnetBlockHeaderFunctions());
}
use of org.hyperledger.besu.evm.log.Log in project besu by hyperledger.
the class LogOperation method execute.
@Override
public OperationResult execute(final MessageFrame frame, final EVM evm) {
final long dataLocation = clampedToLong(frame.popStackItem());
final long numBytes = clampedToLong(frame.popStackItem());
final long cost = gasCalculator().logOperationGasCost(frame, dataLocation, numBytes, numTopics);
if (frame.isStatic()) {
return new OperationResult(OptionalLong.of(cost), Optional.of(ExceptionalHaltReason.ILLEGAL_STATE_CHANGE));
} else if (frame.getRemainingGas() < cost) {
return new OperationResult(OptionalLong.of(cost), Optional.of(ExceptionalHaltReason.INSUFFICIENT_GAS));
}
final Address address = frame.getRecipientAddress();
final Bytes data = frame.readMemory(dataLocation, numBytes);
final ImmutableList.Builder<LogTopic> builder = ImmutableList.builderWithExpectedSize(numTopics);
for (int i = 0; i < numTopics; i++) {
builder.add(LogTopic.create(leftPad(frame.popStackItem())));
}
frame.addLog(new Log(address, data, builder.build()));
return new OperationResult(OptionalLong.of(cost), Optional.empty());
}
use of org.hyperledger.besu.evm.log.Log 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.log.Log in project besu by hyperledger.
the class LogsSubscriptionServiceTest method assertLogResultMatches.
private void assertLogResultMatches(final LogResult result, final Block block, final List<TransactionReceipt> receipts, final int txIndex, final int logIndex, final boolean isRemoved) {
final Transaction expectedTransaction = block.getBody().getTransactions().get(txIndex);
final Log expectedLog = receipts.get(txIndex).getLogsList().get(logIndex);
assertThat(result.getLogIndex()).isEqualTo(Quantity.create(logIndex));
assertThat(result.getTransactionIndex()).isEqualTo(Quantity.create(txIndex));
assertThat(result.getBlockNumber()).isEqualTo(Quantity.create(block.getHeader().getNumber()));
assertThat(result.getBlockHash()).isEqualTo(block.getHash().toString());
assertThat(result.getTransactionHash()).isEqualTo(expectedTransaction.getHash().toString());
assertThat(result.getAddress()).isEqualTo(expectedLog.getLogger().toString());
assertThat(result.getData()).isEqualTo(expectedLog.getData().toString());
assertThat(result.getTopics()).isEqualTo(expectedLog.getTopics().stream().map(Bytes::toString).collect(Collectors.toList()));
assertThat(result.isRemoved()).isEqualTo(isRemoved);
}
use of org.hyperledger.besu.evm.log.Log in project besu by hyperledger.
the class LogsSubscriptionServiceTest method generateBlock.
private BlockWithReceipts generateBlock(final BlockHeader parentHeader, final int txCount, final Supplier<List<Log>> logsSupplier) {
final List<TransactionReceipt> receipts = new ArrayList<>();
final List<Log> logs = new ArrayList<>();
final BlockOptions blockOptions = BlockOptions.create();
for (int i = 0; i < txCount; i++) {
final Transaction tx = gen.transaction();
final TransactionReceipt receipt = gen.receipt(logsSupplier.get());
receipts.add(receipt);
receipt.getLogsList().forEach(logs::add);
blockOptions.addTransaction(tx);
}
blockOptions.setParentHash(parentHeader.getHash());
blockOptions.setBlockNumber(parentHeader.getNumber() + 1L);
final Block block = gen.block(blockOptions);
return new BlockWithReceipts(block, receipts);
}
Aggregations