Search in sources :

Example 21 with Log

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());
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Address(org.hyperledger.besu.datatypes.Address) Log(org.hyperledger.besu.evm.log.Log) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)

Example 22 with Log

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());
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Address(org.hyperledger.besu.datatypes.Address) Log(org.hyperledger.besu.evm.log.Log) ImmutableList(com.google.common.collect.ImmutableList) LogTopic(org.hyperledger.besu.evm.log.LogTopic)

Example 23 with Log

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);
}
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 24 with Log

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);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Transaction(org.hyperledger.besu.ethereum.core.Transaction) Log(org.hyperledger.besu.evm.log.Log)

Example 25 with Log

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);
}
Also used : BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) Transaction(org.hyperledger.besu.ethereum.core.Transaction) Log(org.hyperledger.besu.evm.log.Log) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) ArrayList(java.util.ArrayList) Block(org.hyperledger.besu.ethereum.core.Block) BlockWithReceipts(org.hyperledger.besu.ethereum.core.BlockWithReceipts)

Aggregations

Log (org.hyperledger.besu.evm.log.Log)53 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)30 Address (org.hyperledger.besu.datatypes.Address)24 LogTopic (org.hyperledger.besu.evm.log.LogTopic)22 LogsQuery (org.hyperledger.besu.ethereum.api.query.LogsQuery)18 Bytes (org.apache.tuweni.bytes.Bytes)17 List (java.util.List)13 Collections.emptyList (java.util.Collections.emptyList)12 Collections.singletonList (java.util.Collections.singletonList)12 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)7 BlockWithReceipts (org.hyperledger.besu.ethereum.core.BlockWithReceipts)6 PrecompiledContract (org.hyperledger.besu.evm.precompile.PrecompiledContract)6 Enclave (org.hyperledger.besu.enclave.Enclave)5 ReceiveResponse (org.hyperledger.besu.enclave.types.ReceiveResponse)5 LogResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.LogResult)5 Block (org.hyperledger.besu.ethereum.core.Block)5 Transaction (org.hyperledger.besu.ethereum.core.Transaction)5 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)5 VersionedPrivateTransaction (org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction)4