Search in sources :

Example 1 with LogsBloomFilter

use of org.hyperledger.besu.evm.log.LogsBloomFilter in project besu by hyperledger.

the class LogsWrapper method readFrom.

/**
 * Creates a transaction receipt for the given RLP
 *
 * @param rlpInput the RLP-encoded transaction receipt
 * @param revertReasonAllowed whether the rlp input is allowed to have a revert reason
 * @return the transaction receipt
 */
public static TransactionReceipt readFrom(final RLPInput rlpInput, final boolean revertReasonAllowed) {
    RLPInput input = rlpInput;
    TransactionType transactionType = TransactionType.FRONTIER;
    if (!rlpInput.nextIsList()) {
        final Bytes typedTransactionReceiptBytes = input.readBytes();
        transactionType = TransactionType.of(typedTransactionReceiptBytes.get(0));
        input = new BytesValueRLPInput(typedTransactionReceiptBytes.slice(1), false);
    }
    input.enterList();
    // Get the first element to check later to determine the
    // correct transaction receipt encoding to use.
    final RLPInput firstElement = input.readAsRlp();
    final long cumulativeGas = input.readLongScalar();
    // The logs below will populate the bloom filter upon construction.
    // TODO consider validating that the logs and bloom filter match.
    final LogsBloomFilter bloomFilter = LogsBloomFilter.readFrom(input);
    final List<Log> logs = input.readList(Log::readFrom);
    final Optional<Bytes> revertReason;
    if (input.isEndOfCurrentList()) {
        revertReason = Optional.empty();
    } else {
        if (!revertReasonAllowed) {
            throw new RLPException("Unexpected value at end of TransactionReceipt");
        }
        revertReason = Optional.of(input.readBytes());
    }
    // byte for success (0x01) or failure (0x80).
    if (firstElement.raw().size() == 1) {
        final int status = firstElement.readIntScalar();
        input.leaveList();
        return new TransactionReceipt(transactionType, status, cumulativeGas, logs, bloomFilter, revertReason);
    } else {
        final Hash stateRoot = Hash.wrap(firstElement.readBytes32());
        input.leaveList();
        return new TransactionReceipt(transactionType, stateRoot, cumulativeGas, logs, bloomFilter, revertReason);
    }
}
Also used : BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) TransactionType(org.hyperledger.besu.plugin.data.TransactionType) LogsBloomFilter(org.hyperledger.besu.evm.log.LogsBloomFilter) Log(org.hyperledger.besu.evm.log.Log) Hash(org.hyperledger.besu.datatypes.Hash) Bytes(org.apache.tuweni.bytes.Bytes) RLPException(org.hyperledger.besu.ethereum.rlp.RLPException) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput)

Example 2 with LogsBloomFilter

use of org.hyperledger.besu.evm.log.LogsBloomFilter in project hedera-mirror-node by hashgraph.

the class LogsBloomAggregatorTest method topicsMustBeFoundInsideAggregatedBloom.

@Test
void topicsMustBeFoundInsideAggregatedBloom() {
    LogsBloomAggregator bloomAggregator = new LogsBloomAggregator();
    String bloom1 = "00000004000000001000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000100000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000004000000000000000000000000000000000000000000000040000000000000000000000000000000001000000000000000000000000000000000000000000000001000000000000100000080000000000000000000000000000000000000000000000000000000000000000000000000";
    bloomAggregator.aggregate(ByteString.fromHex(bloom1).toByteArray());
    String bloom2 = "00000004000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008004000000000000000000000000000000000000000000000040000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000100002080000000000000000000000000000000000000000000000400000000000000000000000000";
    bloomAggregator.aggregate(ByteString.fromHex(bloom2).toByteArray());
    String[] topics = { // topic0 from bloom1
    "2F8788117E7EFF1D82E926EC794901D17C78024A50270940304540A733656F0D", // topic1 from bloom1
    "9F2DF0FED2C77648DE5860A4CC508CD0818C85B8B8A1AB4CEEEF8D981C8956A6", // evm address from bloom1
    "00000000000000000000000000000000000D98C7", // topic0 from bloom2
    "2F8788117E7EFF1D82E926EC794901D17C78024A50270940304540A733656F0D", // topic1 from bloom2
    "65D7A28E3265B37A6474929F336521B332C1681B933F6CB9F3376673440D862A", // evm address from bloom2
    "00000000000000000000000000000000000D98C7" };
    for (var topic : topics) {
        LogsBloomFilter topicBloom = LogsBloomFilter.builder().insertBytes(Bytes.fromHexString(topic)).build();
        assertTrue(bloomAggregator.couldContain(topicBloom.toArray()), topic);
    }
    String[] stringsNotPresentInAnyBloom = { "FF2F8788117E7EFF1D82E926EC794901D17C78024A50270940304540A733656F0D", "AA9F2DF0FED2C77648DE5860A4CC508CD0818C85B8B8A1AB4CEEEF8D981C8956A6" };
    for (var str : stringsNotPresentInAnyBloom) {
        LogsBloomFilter bloom = LogsBloomFilter.builder().insertBytes(Bytes.fromHexString(str)).build();
        assertFalse(bloomAggregator.couldContain(bloom.toArray()), str);
    }
}
Also used : LogsBloomAggregator(com.hedera.mirror.common.aggregator.LogsBloomAggregator) LogsBloomFilter(org.hyperledger.besu.evm.log.LogsBloomFilter) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test)

Example 3 with LogsBloomFilter

use of org.hyperledger.besu.evm.log.LogsBloomFilter in project besu by hyperledger.

the class BlockHeader method readFrom.

public static BlockHeader readFrom(final RLPInput input, final BlockHeaderFunctions blockHeaderFunctions) {
    input.enterList();
    final Hash parentHash = Hash.wrap(input.readBytes32());
    final Hash ommersHash = Hash.wrap(input.readBytes32());
    final Address coinbase = Address.readFrom(input);
    final Hash stateRoot = Hash.wrap(input.readBytes32());
    final Hash transactionsRoot = Hash.wrap(input.readBytes32());
    final Hash receiptsRoot = Hash.wrap(input.readBytes32());
    final LogsBloomFilter logsBloom = LogsBloomFilter.readFrom(input);
    final Difficulty difficulty = Difficulty.of(input.readUInt256Scalar());
    final long number = input.readLongScalar();
    final long gasLimit = input.readLongScalar();
    final long gasUsed = input.readLongScalar();
    final long timestamp = input.readLongScalar();
    final Bytes extraData = input.readBytes();
    final Bytes32 mixHashOrPrevRandao = input.readBytes32();
    final long nonce = input.readLong();
    final Wei baseFee = !input.isEndOfCurrentList() ? Wei.of(input.readUInt256Scalar()) : null;
    input.leaveList();
    return new BlockHeader(parentHash, ommersHash, coinbase, stateRoot, transactionsRoot, receiptsRoot, logsBloom, difficulty, number, gasLimit, gasUsed, timestamp, extraData, baseFee, mixHashOrPrevRandao, nonce, blockHeaderFunctions);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Address(org.hyperledger.besu.datatypes.Address) LogsBloomFilter(org.hyperledger.besu.evm.log.LogsBloomFilter) Wei(org.hyperledger.besu.datatypes.Wei) Hash(org.hyperledger.besu.datatypes.Hash) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 4 with LogsBloomFilter

use of org.hyperledger.besu.evm.log.LogsBloomFilter in project besu by hyperledger.

the class JsonRpcResponseUtils method response.

/**
 * @param values hex encoded values.
 */
public JsonRpcResponse response(final Map<JsonRpcResponseKey, String> values, final List<TransactionResult> transactions) {
    final Hash mixHash = hash(values.get(MIX_HASH));
    final Hash parentHash = hash(values.get(PARENT_HASH));
    final Hash ommersHash = hash(values.get(OMMERS_HASH));
    final Address coinbase = address(values.get(COINBASE));
    final Hash stateRoot = hash(values.get(STATE_ROOT));
    final Hash transactionsRoot = hash(values.get(TRANSACTION_ROOT));
    final Hash receiptsRoot = hash(values.get(RECEIPTS_ROOT));
    final LogsBloomFilter logsBloom = logsBloom(values.get(LOGS_BLOOM));
    final Difficulty difficulty = Difficulty.of(unsignedInt256(values.get(DIFFICULTY)));
    final Bytes extraData = bytes(values.get(EXTRA_DATA));
    final BlockHeaderFunctions blockHeaderFunctions = new MainnetBlockHeaderFunctions();
    final long number = unsignedLong(values.get(NUMBER));
    final long gasLimit = unsignedLong(values.get(GAS_LIMIT));
    final long gasUsed = unsignedLong(values.get(GAS_USED));
    final long timestamp = unsignedLong(values.get(TIMESTAMP));
    final long nonce = unsignedLong(values.get(NONCE));
    final Wei baseFee = values.containsKey(BASEFEE) ? Wei.of(unsignedInt256(values.get(BASEFEE))) : null;
    final Difficulty totalDifficulty = Difficulty.of(unsignedInt256(values.get(TOTAL_DIFFICULTY)));
    final int size = unsignedInt(values.get(SIZE));
    final List<JsonNode> ommers = new ArrayList<>();
    final BlockHeader header = new BlockHeader(parentHash, ommersHash, coinbase, stateRoot, transactionsRoot, receiptsRoot, logsBloom, difficulty, number, gasLimit, gasUsed, timestamp, extraData, baseFee, mixHash, nonce, blockHeaderFunctions);
    return new JsonRpcSuccessResponse(null, new BlockResult(header, transactions, ommers, totalDifficulty, size));
}
Also used : Address(org.hyperledger.besu.datatypes.Address) LogsBloomFilter(org.hyperledger.besu.evm.log.LogsBloomFilter) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) BlockHeaderFunctions(org.hyperledger.besu.ethereum.core.BlockHeaderFunctions) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Hash(org.hyperledger.besu.datatypes.Hash) Bytes(org.apache.tuweni.bytes.Bytes) BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult) Wei(org.hyperledger.besu.datatypes.Wei) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)

Example 5 with LogsBloomFilter

use of org.hyperledger.besu.evm.log.LogsBloomFilter in project besu by hyperledger.

the class BlockchainQueries method matchingLogsCached.

private List<LogWithMetadata> matchingLogsCached(final long segmentStart, final long offset, final long endOffset, final LogsQuery query, final Path cacheFile, final Supplier<Boolean> isQueryAlive) throws Exception {
    final List<LogWithMetadata> results = new ArrayList<>();
    try (final RandomAccessFile raf = new RandomAccessFile(cacheFile.toFile(), "r")) {
        raf.seek(offset * 256);
        final byte[] bloomBuff = new byte[256];
        final Bytes bytesValue = Bytes.wrap(bloomBuff);
        for (long pos = offset; pos <= endOffset; pos++) {
            BackendQuery.stopIfExpired(isQueryAlive);
            try {
                raf.readFully(bloomBuff);
            } catch (final EOFException e) {
                results.addAll(matchingLogsUncached(segmentStart + pos, segmentStart + endOffset, query, isQueryAlive));
                break;
            }
            final LogsBloomFilter logsBloom = new LogsBloomFilter(bytesValue);
            if (query.couldMatch(logsBloom)) {
                results.addAll(matchingLogs(blockchain.getBlockHashByNumber(segmentStart + pos).orElseThrow(), query, isQueryAlive));
            }
        }
    } catch (final IOException e) {
        e.printStackTrace(System.out);
        LOG.error("Error reading cached log blooms", e);
    }
    return results;
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) RandomAccessFile(java.io.RandomAccessFile) LogsBloomFilter(org.hyperledger.besu.evm.log.LogsBloomFilter) LogWithMetadata(org.hyperledger.besu.ethereum.core.LogWithMetadata) ArrayList(java.util.ArrayList) EOFException(java.io.EOFException) IOException(java.io.IOException)

Aggregations

LogsBloomFilter (org.hyperledger.besu.evm.log.LogsBloomFilter)7 Bytes (org.apache.tuweni.bytes.Bytes)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Hash (org.hyperledger.besu.datatypes.Hash)3 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)3 ArrayList (java.util.ArrayList)2 UInt256 (org.apache.tuweni.units.bigints.UInt256)2 Address (org.hyperledger.besu.datatypes.Address)2 Wei (org.hyperledger.besu.datatypes.Wei)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ByteString (com.google.protobuf.ByteString)1 LogsBloomAggregator (com.hedera.mirror.common.aggregator.LogsBloomAggregator)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)1 BlockResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult)1 BlockHeaderFunctions (org.hyperledger.besu.ethereum.core.BlockHeaderFunctions)1 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)1