mirror of https://github.com/hyperledger/besu
TraceService: return results for transactions in block (#6087)
* TraceService: return results for transactions in block Signed-off-by: Daniel Lehrner <daniel.lehrner@consensys.net>pull/5865/merge
parent
accac1ccbc
commit
a60b31b3af
@ -0,0 +1,124 @@ |
||||
/* |
||||
* Copyright Hyperledger Besu Contributors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.plugin.data; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* Represents the result of tracing a block, containing information about the transaction traces. |
||||
*/ |
||||
public class BlockTraceResult { |
||||
final List<TransactionTraceResult> transactionTraceResults; |
||||
|
||||
/** |
||||
* Constructs a BlockTraceResult with the given list of transaction trace results. |
||||
* |
||||
* @param transactionTraceResults The list of transaction trace results to be associated with this |
||||
* block. |
||||
*/ |
||||
public BlockTraceResult(final List<TransactionTraceResult> transactionTraceResults) { |
||||
this.transactionTraceResults = transactionTraceResults; |
||||
} |
||||
|
||||
/** |
||||
* Creates an empty BlockTraceResult with no transaction trace results. |
||||
* |
||||
* @return An empty BlockTraceResult. |
||||
*/ |
||||
public static BlockTraceResult empty() { |
||||
return new BlockTraceResult(new ArrayList<>()); |
||||
} |
||||
|
||||
/** |
||||
* Get the list of transaction trace results for this block. |
||||
* |
||||
* @return The list of transaction trace results. |
||||
*/ |
||||
public List<TransactionTraceResult> transactionTraceResults() { |
||||
return transactionTraceResults; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(final Object o) { |
||||
if (this == o) { |
||||
return true; |
||||
} |
||||
if (o == null || getClass() != o.getClass()) { |
||||
return false; |
||||
} |
||||
final BlockTraceResult that = (BlockTraceResult) o; |
||||
return transactionTraceResults.equals(that.transactionTraceResults()); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return Objects.hash(transactionTraceResults); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
final StringBuilder builder = new StringBuilder(); |
||||
builder.append("BlockTraceResult{transactionTraceResults=["); |
||||
|
||||
final Iterator<TransactionTraceResult> iterator = transactionTraceResults.iterator(); |
||||
while (iterator.hasNext()) { |
||||
builder.append(iterator.next().toString()); |
||||
|
||||
if (iterator.hasNext()) { |
||||
builder.append(","); |
||||
} |
||||
} |
||||
builder.append("]}"); |
||||
return builder.toString(); |
||||
} |
||||
|
||||
/** |
||||
* Creates a new builder to construct a BlockTraceResult. |
||||
* |
||||
* @return A new BlockTraceResult.Builder instance. |
||||
*/ |
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
/** A builder class for constructing a BlockTraceResult. */ |
||||
public static class Builder { |
||||
List<TransactionTraceResult> transactionTraceResults = new ArrayList<>(); |
||||
|
||||
/** |
||||
* Adds a transaction trace result to the builder. |
||||
* |
||||
* @param transactionTraceResult The transaction trace result to add to the builder. |
||||
* @return This builder instance, for method chaining. |
||||
*/ |
||||
public Builder addTransactionTraceResult(final TransactionTraceResult transactionTraceResult) { |
||||
transactionTraceResults.add(transactionTraceResult); |
||||
|
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Constructs a BlockTraceResult using the transaction trace results added to the builder. |
||||
* |
||||
* @return A BlockTraceResult containing the added transaction trace results. |
||||
*/ |
||||
public BlockTraceResult build() { |
||||
return new BlockTraceResult(transactionTraceResults); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,128 @@ |
||||
/* |
||||
* Copyright Hyperledger Besu Contributors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.plugin.data; |
||||
|
||||
import org.hyperledger.besu.datatypes.Hash; |
||||
|
||||
import java.util.Objects; |
||||
import java.util.Optional; |
||||
|
||||
/** |
||||
* Represents the result of tracing a transaction, including its status and optional error message. |
||||
*/ |
||||
public class TransactionTraceResult { |
||||
/** Enumeration representing the status of the transaction trace. */ |
||||
public enum Status { |
||||
/** |
||||
* The transaction was traced successfully. This might include transactions that have been |
||||
* reverted. |
||||
*/ |
||||
SUCCESS, |
||||
/** There was an internal error while generating the trace. */ |
||||
ERROR |
||||
} |
||||
|
||||
private final Hash txHash; |
||||
private final Status status; |
||||
private final String errorMessage; |
||||
|
||||
private TransactionTraceResult( |
||||
final Hash txHash, final Status status, final String errorMessage) { |
||||
this.txHash = txHash; |
||||
this.status = status; |
||||
this.errorMessage = errorMessage; |
||||
} |
||||
|
||||
/** |
||||
* Creates a TransactionTraceResult with a successful status and the given transaction hash. |
||||
* |
||||
* @param txHash The hash of the traced transaction. |
||||
* @return A successful TransactionTraceResult. |
||||
*/ |
||||
public static TransactionTraceResult success(final Hash txHash) { |
||||
return new TransactionTraceResult(txHash, Status.SUCCESS, null); |
||||
} |
||||
|
||||
/** |
||||
* Creates a TransactionTraceResult with an error status, the given transaction hash, and an error |
||||
* message. |
||||
* |
||||
* @param txHash The hash of the traced transaction. |
||||
* @param errorMessage An error message describing the issue encountered during tracing. |
||||
* @return An error TransactionTraceResult. |
||||
*/ |
||||
public static TransactionTraceResult error(final Hash txHash, final String errorMessage) { |
||||
return new TransactionTraceResult(txHash, Status.ERROR, errorMessage); |
||||
} |
||||
|
||||
/** |
||||
* Get the hash of the traced transaction. |
||||
* |
||||
* @return The hash of the transaction. |
||||
*/ |
||||
public Hash getTxHash() { |
||||
return txHash; |
||||
} |
||||
|
||||
/** |
||||
* Get the status of the transaction trace. |
||||
* |
||||
* @return The status of the transaction trace. |
||||
*/ |
||||
public Status getStatus() { |
||||
return status; |
||||
} |
||||
|
||||
/** |
||||
* Get an optional error message associated with the transaction trace. |
||||
* |
||||
* @return An optional error message, which may be empty if no error occurred. |
||||
*/ |
||||
public Optional<String> errorMessage() { |
||||
return Optional.ofNullable(errorMessage); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(final Object o) { |
||||
if (this == o) { |
||||
return true; |
||||
} |
||||
if (o == null || getClass() != o.getClass()) { |
||||
return false; |
||||
} |
||||
final TransactionTraceResult that = (TransactionTraceResult) o; |
||||
return Objects.equals(txHash, that.txHash) |
||||
&& status == that.status |
||||
&& Objects.equals(errorMessage, that.errorMessage); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return Objects.hash(txHash, status, errorMessage); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "TransactionTraceResult{" |
||||
+ "txHash=" |
||||
+ txHash |
||||
+ ", status=" |
||||
+ status |
||||
+ ", errorMessage='" |
||||
+ errorMessage |
||||
+ '\'' |
||||
+ '}'; |
||||
} |
||||
} |
Loading…
Reference in new issue