Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
matkt committed May 7, 2023
1 parent f4101ff commit a432ee1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Bytes32> 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<Bytes32> 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<Bytes32> topics) {
if (!transactionType.equals(TransactionType.FRONTIER)) {
rlpOutput.writeIntScalar(transactionType.getSerializedType());
}
Expand All @@ -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());
}
Expand All @@ -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<>());
}

/**
Expand All @@ -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<Bytes32> topics) {
RLPInput input = rlpInput;
TransactionType transactionType = TransactionType.FRONTIER;
if (!rlpInput.nextIsList()) {
Expand All @@ -267,7 +276,8 @@ public static TransactionReceipt readFrom(
if (!isCompressed) {
bloomFilter = LogsBloomFilter.readFrom(input);
}
final List<Log> logs = input.readList(in -> Log.readFrom(in, isCompressed));

final List<Log> logs = input.readList(in -> Log.readFrom(in, isCompressed, topics));

if (bloomFilter == null) {
bloomFilter = LogsBloomFilter.builder().insertLogs(logs).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,7 +131,15 @@ public Updater updater() {
}

private List<TransactionReceipt> rlpDecodeTransactionReceipts(final Bytes bytes) {
return RLP.input(bytes).readList(input -> TransactionReceipt.readFrom(input, true, true));
return RLP.input(bytes).readList(input ->{
input.enterList();

ArrayList<Bytes32> 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) {
Expand Down Expand Up @@ -248,11 +262,19 @@ private void remove(final Bytes prefix, final Bytes key) {

private Bytes rlpEncode(final List<TransactionReceipt> receipts) {
return RLP.encode(
o ->
o.writeList(
receipts,
(transactionReceipt, rlpOutput) ->
transactionReceipt.writeToWithRevertReason(rlpOutput, true)));
o -> {
o.startList();
final Set<Bytes32> topics = new HashSet<>();
receipts.forEach(receipt -> receipt.getLogs().forEach(log -> topics.addAll(log.getTopics())));
ArrayList<Bytes32> logTopics = new ArrayList<>(topics);
o.writeList(logTopics, (bytes32, rlpOutput) -> rlpOutput.writeBytes(bytes32));
o.writeList(
receipts,
(transactionReceipt, rlpOutput) ->
transactionReceipt.writeToWithRevertReason(rlpOutput, true, logTopics));
o.endList();
});

}
}
}
53 changes: 25 additions & 28 deletions evm/src/main/java/org/hyperledger/besu/evm/log/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,52 +55,44 @@ public Log(final Address logger, final Bytes data, final List<LogTopic> 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<Bytes32> 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.
*
* @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<Bytes32> logTopics) {
in.enterList();
final Address logger = Address.wrap(in.readBytes());
final List<LogTopic> 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<LogTopic> 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);
Expand Down

0 comments on commit a432ee1

Please sign in to comment.