From a432ee1bc27cf3bd82950306511f88db7fa572af Mon Sep 17 00:00:00 2001 From: Karim TAAM Date: Sun, 7 May 2023 13:16:26 +0200 Subject: [PATCH] tmp --- .../ethereum/core/TransactionReceipt.java | 36 ++++++++----- ...ueStoragePrefixedKeyBlockchainStorage.java | 34 +++++++++--- .../org/hyperledger/besu/evm/log/Log.java | 53 +++++++++---------- 3 files changed, 76 insertions(+), 47 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/TransactionReceipt.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/TransactionReceipt.java index d00a68fe0e6..c2c429fdc2f 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/TransactionReceipt.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/TransactionReceipt.java @@ -23,12 +23,16 @@ import org.hyperledger.besu.ethereum.rlp.RLPInput; import org.hyperledger.besu.ethereum.rlp.RLPOutput; import org.hyperledger.besu.evm.log.Log; +import org.hyperledger.besu.evm.log.LogTopic; import org.hyperledger.besu.evm.log.LogsBloomFilter; import org.hyperledger.besu.plugin.data.TransactionType; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import com.google.common.base.MoreObjects; @@ -172,33 +176,33 @@ private TransactionReceipt( * @param out The RLP output to write to */ public void writeTo(final RLPOutput out) { - writeTo(out, false, false); + writeTo(out, false, false, new ArrayList<>()); } public void writeToWithRevertReason(final RLPOutput out) { - writeToWithRevertReason(out, false); + writeToWithRevertReason(out, false, new ArrayList<>()); } - public void writeToWithRevertReason(final RLPOutput out, final boolean isCompressed) { - writeTo(out, true, isCompressed); + public void writeToWithRevertReason(final RLPOutput out, final boolean isCompressed, final ArrayList topics) { + writeTo(out, true, isCompressed, topics); } private void writeTo( - final RLPOutput rlpOutput, final boolean withRevertReason, final boolean isCompressed) { + final RLPOutput rlpOutput, final boolean withRevertReason, final boolean isCompressed, final ArrayList topics) { if (transactionType.equals(TransactionType.FRONTIER)) { - writeToForReceiptTrie(rlpOutput, withRevertReason, isCompressed); + writeToForReceiptTrie(rlpOutput, withRevertReason, isCompressed, topics); } else { rlpOutput.writeBytes( - RLP.encode(out -> writeToForReceiptTrie(out, withRevertReason, isCompressed))); + RLP.encode(out -> writeToForReceiptTrie(out, withRevertReason, isCompressed, topics))); } } public void writeToForReceiptTrie(final RLPOutput rlpOutput, final boolean withRevertReason) { - writeToForReceiptTrie(rlpOutput, withRevertReason, false); + writeToForReceiptTrie(rlpOutput, withRevertReason, false, new ArrayList<>()); } public void writeToForReceiptTrie( - final RLPOutput rlpOutput, final boolean withRevertReason, final boolean isCompressed) { + final RLPOutput rlpOutput, final boolean withRevertReason, final boolean isCompressed, final ArrayList topics) { if (!transactionType.equals(TransactionType.FRONTIER)) { rlpOutput.writeIntScalar(transactionType.getSerializedType()); } @@ -216,7 +220,12 @@ public void writeToForReceiptTrie( if (!isCompressed) { rlpOutput.writeBytes(bloomFilter); } - rlpOutput.writeList(logs, (log, out) -> log.writeTo(out, isCompressed)); + if(isCompressed) { + rlpOutput.writeList(logs, (log, out) -> log.writeTo(out, true, topics)); + } else { + rlpOutput.writeList(logs, (log, out) -> log.writeTo(out, false, new ArrayList<>())); + } + if (withRevertReason && revertReason.isPresent()) { rlpOutput.writeBytes(revertReason.get()); } @@ -235,7 +244,7 @@ public static TransactionReceipt readFrom(final RLPInput input) { public static TransactionReceipt readFrom( final RLPInput rlpInput, final boolean revertReasonAllowed) { - return readFrom(rlpInput, revertReasonAllowed, false); + return readFrom(rlpInput, revertReasonAllowed, false, new ArrayList<>()); } /** @@ -247,7 +256,7 @@ public static TransactionReceipt readFrom( * @return the transaction receipt */ public static TransactionReceipt readFrom( - final RLPInput rlpInput, final boolean revertReasonAllowed, final boolean isCompressed) { + final RLPInput rlpInput, final boolean revertReasonAllowed, final boolean isCompressed, final ArrayList topics) { RLPInput input = rlpInput; TransactionType transactionType = TransactionType.FRONTIER; if (!rlpInput.nextIsList()) { @@ -267,7 +276,8 @@ public static TransactionReceipt readFrom( if (!isCompressed) { bloomFilter = LogsBloomFilter.readFrom(input); } - final List logs = input.readList(in -> Log.readFrom(in, isCompressed)); + + final List logs = input.readList(in -> Log.readFrom(in, isCompressed, topics)); if (bloomFilter == null) { bloomFilter = LogsBloomFilter.builder().insertLogs(logs).build(); diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java index 404561bd65f..3d89f8b35f0 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java @@ -22,14 +22,20 @@ import org.hyperledger.besu.ethereum.core.BlockHeaderFunctions; import org.hyperledger.besu.ethereum.core.Difficulty; import org.hyperledger.besu.ethereum.core.TransactionReceipt; +import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.ethereum.rlp.RLP; +import org.hyperledger.besu.ethereum.rlp.RLPInput; +import org.hyperledger.besu.evm.log.LogTopic; import org.hyperledger.besu.plugin.services.storage.KeyValueStorage; import org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Optional; +import java.util.Set; import com.google.common.collect.Lists; import org.apache.tuweni.bytes.Bytes; @@ -125,7 +131,15 @@ public Updater updater() { } private List rlpDecodeTransactionReceipts(final Bytes bytes) { - return RLP.input(bytes).readList(input -> TransactionReceipt.readFrom(input, true, true)); + return RLP.input(bytes).readList(input ->{ + input.enterList(); + + ArrayList logTopics = new ArrayList<>(input.readList(RLPInput::readBytes32)); + + TransactionReceipt transactionReceipt = TransactionReceipt.readFrom(input, true, true, logTopics); + input.leaveList(); + return transactionReceipt; + }); } private Hash bytesToHash(final Bytes bytes) { @@ -248,11 +262,19 @@ private void remove(final Bytes prefix, final Bytes key) { private Bytes rlpEncode(final List receipts) { return RLP.encode( - o -> - o.writeList( - receipts, - (transactionReceipt, rlpOutput) -> - transactionReceipt.writeToWithRevertReason(rlpOutput, true))); + o -> { + o.startList(); + final Set topics = new HashSet<>(); + receipts.forEach(receipt -> receipt.getLogs().forEach(log -> topics.addAll(log.getTopics()))); + ArrayList logTopics = new ArrayList<>(topics); + o.writeList(logTopics, (bytes32, rlpOutput) -> rlpOutput.writeBytes(bytes32)); + o.writeList( + receipts, + (transactionReceipt, rlpOutput) -> + transactionReceipt.writeToWithRevertReason(rlpOutput, true, logTopics)); + o.endList(); + }); + } } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/log/Log.java b/evm/src/main/java/org/hyperledger/besu/evm/log/Log.java index 1d782d82842..74a372f1987 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/log/Log.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/log/Log.java @@ -14,12 +14,17 @@ */ package org.hyperledger.besu.evm.log; +import org.apache.tuweni.bytes.Bytes32; import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.ethereum.rlp.RLPInput; import org.hyperledger.besu.ethereum.rlp.RLPOutput; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; +import java.util.Set; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; @@ -50,26 +55,25 @@ public Log(final Address logger, final Bytes data, final List topics) } public void writeTo(final RLPOutput out) { - writeTo(out, false); + writeTo(out, false, new ArrayList<>()); } - public void writeTo(final RLPOutput out, final boolean isCompacted) { + public void writeTo(final RLPOutput out, final boolean isCompacted, final ArrayList logTopics) { out.startList(); out.writeBytes(logger); - out.writeList(topics, (topic, listOut) -> listOut.writeBytes(topic)); - if (isCompacted) { - final Bytes shortData = data.trimLeadingZeros(); - final int zeroLeadDataSize = data.size() - shortData.size(); - out.writeInt(zeroLeadDataSize); - out.writeBytes(shortData); - } else { - out.writeBytes(data); - } + out.writeList(topics, (topic, listOut) -> { + if(isCompacted){ + listOut.writeInt(logTopics.indexOf(topic)); + }else { + listOut.writeBytes(topic); + } + }); + out.writeBytes(data); out.endList(); } public static Log readFrom(final RLPInput in) { - return readFrom(in, false); + return readFrom(in, false, new ArrayList<>()); } /** * Reads the log entry from the provided RLP input. @@ -77,25 +81,18 @@ public static Log readFrom(final RLPInput in) { * @param in the input from which to decode the log entry. * @return the read log entry. */ - public static Log readFrom(final RLPInput in, final boolean isCompacted) { + public static Log readFrom(final RLPInput in, final boolean isCompacted, final ArrayList logTopics) { in.enterList(); final Address logger = Address.wrap(in.readBytes()); - final List topics = in.readList(listIn -> LogTopic.wrap(listIn.readBytes32())); - final Bytes data; - if (isCompacted) { - final int zeroLeadDataSize = in.readInt(); - if (in.nextIsNull()) { - data = MutableBytes.create(zeroLeadDataSize); - in.skipNext(); - } else { - final Bytes shortData = in.readBytes(); - MutableBytes unCompactedData = MutableBytes.create(zeroLeadDataSize + shortData.size()); - unCompactedData.set(zeroLeadDataSize, shortData); - data = unCompactedData; + final List topics = in.readList(listIn -> { + if(isCompacted){ + return LogTopic.wrap(logTopics.get(listIn.readInt())); + }else { + return LogTopic.wrap(listIn.readBytes32()); } - } else { - data = in.readBytes(); - } + }); + final Bytes data; + data = in.readBytes(); in.leaveList(); return new Log(logger, data, topics);