mirror of https://github.com/hyperledger/besu
Collapse MainnetTransactionProcessor Class and Transaction Processor Interface (#1544)
Signed-off-by: Ratan Rai Sur <ratan.r.sur@gmail.com>pull/1553/head
parent
bd59966212
commit
4ff73342b6
@ -1,292 +0,0 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* 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.ethereum.mainnet; |
||||
|
||||
import org.hyperledger.besu.ethereum.chain.Blockchain; |
||||
import org.hyperledger.besu.ethereum.core.Address; |
||||
import org.hyperledger.besu.ethereum.core.Log; |
||||
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader; |
||||
import org.hyperledger.besu.ethereum.core.Transaction; |
||||
import org.hyperledger.besu.ethereum.core.WorldUpdater; |
||||
import org.hyperledger.besu.ethereum.privacy.storage.PrivateMetadataUpdater; |
||||
import org.hyperledger.besu.ethereum.vm.BlockHashLookup; |
||||
import org.hyperledger.besu.ethereum.vm.OperationTracer; |
||||
|
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
import org.apache.tuweni.bytes.Bytes; |
||||
|
||||
/** Processes transactions. */ |
||||
public interface TransactionProcessor { |
||||
|
||||
/** A transaction processing result. */ |
||||
interface Result { |
||||
|
||||
/** The status of the transaction after being processed. */ |
||||
enum Status { |
||||
|
||||
/** The transaction was invalid for processing. */ |
||||
INVALID, |
||||
|
||||
/** The transaction was successfully processed. */ |
||||
SUCCESSFUL, |
||||
|
||||
/** The transaction failed to be completely processed. */ |
||||
FAILED |
||||
} |
||||
|
||||
/** |
||||
* Return the logs produced by the transaction. |
||||
* |
||||
* <p>This is only valid when {@code TransactionProcessor#isSuccessful} returns {@code true}. |
||||
* |
||||
* @return the logs produced by the transaction |
||||
*/ |
||||
List<Log> getLogs(); |
||||
|
||||
/** |
||||
* Returns the status of the transaction after being processed. |
||||
* |
||||
* @return the status of the transaction after being processed |
||||
*/ |
||||
Status getStatus(); |
||||
|
||||
/** |
||||
* Returns the gas remaining after the transaction was processed. |
||||
* |
||||
* <p>This is only valid when {@code TransactionProcessor#isSuccessful} returns {@code true}. |
||||
* |
||||
* @return the gas remaining after the transaction was processed |
||||
*/ |
||||
long getGasRemaining(); |
||||
|
||||
Bytes getOutput(); |
||||
|
||||
/** |
||||
* Returns whether or not the transaction was invalid. |
||||
* |
||||
* @return {@code true} if the transaction was invalid; otherwise {@code false} |
||||
*/ |
||||
default boolean isInvalid() { |
||||
return getStatus() == Status.INVALID; |
||||
} |
||||
|
||||
/** |
||||
* Returns whether or not the transaction was successfully processed. |
||||
* |
||||
* @return {@code true} if the transaction was successfully processed; otherwise {@code false} |
||||
*/ |
||||
default boolean isSuccessful() { |
||||
return getStatus() == Status.SUCCESSFUL; |
||||
} |
||||
|
||||
/** |
||||
* Returns the transaction validation result. |
||||
* |
||||
* @return the validation result, with the reason for failure (if applicable.) |
||||
*/ |
||||
ValidationResult<TransactionValidator.TransactionInvalidReason> getValidationResult(); |
||||
|
||||
/** |
||||
* Returns the reason why a transaction was reverted (if applicable). |
||||
* |
||||
* @return the revert reason. |
||||
*/ |
||||
Optional<Bytes> getRevertReason(); |
||||
|
||||
/** |
||||
* Returns the estimate gas used by the transaction Difference between the gas limit and the |
||||
* remaining gas |
||||
* |
||||
* @return the estimate gas used |
||||
*/ |
||||
long getEstimateGasUsedByTransaction(); |
||||
} |
||||
|
||||
/** |
||||
* Applies a transaction to the current system state. |
||||
* |
||||
* @param blockchain The current blockchain |
||||
* @param worldState The current world state |
||||
* @param blockHeader The current block header |
||||
* @param transaction The transaction to process |
||||
* @param miningBeneficiary The address which is to receive the transaction fee |
||||
* @param blockHashLookup The {@link BlockHashLookup} to use for BLOCKHASH operations |
||||
* @param isPersistingPrivateState Whether the resulting private state will be persisted |
||||
* @param transactionValidationParams Validation parameters that will be used by the {@link |
||||
* TransactionValidator} |
||||
* @return the transaction result |
||||
* @see TransactionValidator |
||||
* @see TransactionValidationParams |
||||
*/ |
||||
default Result processTransaction( |
||||
final Blockchain blockchain, |
||||
final WorldUpdater worldState, |
||||
final ProcessableBlockHeader blockHeader, |
||||
final Transaction transaction, |
||||
final Address miningBeneficiary, |
||||
final BlockHashLookup blockHashLookup, |
||||
final Boolean isPersistingPrivateState, |
||||
final TransactionValidationParams transactionValidationParams) { |
||||
return processTransaction( |
||||
blockchain, |
||||
worldState, |
||||
blockHeader, |
||||
transaction, |
||||
miningBeneficiary, |
||||
OperationTracer.NO_TRACING, |
||||
blockHashLookup, |
||||
isPersistingPrivateState, |
||||
transactionValidationParams); |
||||
} |
||||
|
||||
/** |
||||
* Applies a transaction to the current system state. |
||||
* |
||||
* @param blockchain The current blockchain |
||||
* @param worldState The current world state |
||||
* @param blockHeader The current block header |
||||
* @param transaction The transaction to process |
||||
* @param miningBeneficiary The address which is to receive the transaction fee |
||||
* @param blockHashLookup The {@link BlockHashLookup} to use for BLOCKHASH operations |
||||
* @param isPersistingPrivateState Whether the resulting private state will be persisted |
||||
* @param transactionValidationParams Validation parameters that will be used by the {@link |
||||
* TransactionValidator} |
||||
* @param operationTracer operation tracer {@link OperationTracer} |
||||
* @return the transaction result |
||||
* @see TransactionValidator |
||||
* @see TransactionValidationParams |
||||
*/ |
||||
default Result processTransaction( |
||||
final Blockchain blockchain, |
||||
final WorldUpdater worldState, |
||||
final ProcessableBlockHeader blockHeader, |
||||
final Transaction transaction, |
||||
final Address miningBeneficiary, |
||||
final BlockHashLookup blockHashLookup, |
||||
final Boolean isPersistingPrivateState, |
||||
final TransactionValidationParams transactionValidationParams, |
||||
final OperationTracer operationTracer) { |
||||
return processTransaction( |
||||
blockchain, |
||||
worldState, |
||||
blockHeader, |
||||
transaction, |
||||
miningBeneficiary, |
||||
operationTracer, |
||||
blockHashLookup, |
||||
isPersistingPrivateState, |
||||
transactionValidationParams); |
||||
} |
||||
|
||||
/** |
||||
* Applies a transaction to the current system state. |
||||
* |
||||
* @param blockchain The current blockchain |
||||
* @param worldState The current world state |
||||
* @param blockHeader The current block header |
||||
* @param transaction The transaction to process |
||||
* @param operationTracer The tracer to record results of each EVM operation |
||||
* @param miningBeneficiary The address which is to receive the transaction fee |
||||
* @param blockHashLookup The {@link BlockHashLookup} to use for BLOCKHASH operations |
||||
* @param isPersistingPrivateState Whether the resulting private state will be persisted |
||||
* @return the transaction result |
||||
*/ |
||||
default Result processTransaction( |
||||
final Blockchain blockchain, |
||||
final WorldUpdater worldState, |
||||
final ProcessableBlockHeader blockHeader, |
||||
final Transaction transaction, |
||||
final Address miningBeneficiary, |
||||
final OperationTracer operationTracer, |
||||
final BlockHashLookup blockHashLookup, |
||||
final Boolean isPersistingPrivateState) { |
||||
return processTransaction( |
||||
blockchain, |
||||
worldState, |
||||
blockHeader, |
||||
transaction, |
||||
miningBeneficiary, |
||||
operationTracer, |
||||
blockHashLookup, |
||||
isPersistingPrivateState, |
||||
new TransactionValidationParams.Builder().build()); |
||||
} |
||||
|
||||
/** |
||||
* Applies a transaction to the current system state. |
||||
* |
||||
* @param blockchain The current blockchain |
||||
* @param worldState The current world state |
||||
* @param blockHeader The current block header |
||||
* @param transaction The transaction to process |
||||
* @param operationTracer The tracer to record results of each EVM operation |
||||
* @param miningBeneficiary The address which is to receive the transaction fee |
||||
* @param blockHashLookup The {@link BlockHashLookup} to use for BLOCKHASH operations |
||||
* @param isPersistingPrivateState Whether the resulting private state will be persisted |
||||
* @param transactionValidationParams The transaction validation parameters to use |
||||
* @return the transaction result |
||||
*/ |
||||
default Result processTransaction( |
||||
final Blockchain blockchain, |
||||
final WorldUpdater worldState, |
||||
final ProcessableBlockHeader blockHeader, |
||||
final Transaction transaction, |
||||
final Address miningBeneficiary, |
||||
final OperationTracer operationTracer, |
||||
final BlockHashLookup blockHashLookup, |
||||
final Boolean isPersistingPrivateState, |
||||
final TransactionValidationParams transactionValidationParams) { |
||||
return processTransaction( |
||||
blockchain, |
||||
worldState, |
||||
blockHeader, |
||||
transaction, |
||||
miningBeneficiary, |
||||
operationTracer, |
||||
blockHashLookup, |
||||
isPersistingPrivateState, |
||||
transactionValidationParams, |
||||
null); |
||||
} |
||||
|
||||
/** |
||||
* Applies a transaction to the current system state. |
||||
* |
||||
* @param blockchain The current blockchain |
||||
* @param worldState The current world state |
||||
* @param blockHeader The current block header |
||||
* @param transaction The transaction to process |
||||
* @param operationTracer The tracer to record results of each EVM operation |
||||
* @param miningBeneficiary The address which is to receive the transaction fee |
||||
* @param blockHashLookup The {@link BlockHashLookup} to use for BLOCKHASH operations |
||||
* @param isPersistingPrivateState Whether the resulting private state will be persisted |
||||
* @param transactionValidationParams The transaction validation parameters to use |
||||
* @param privateMetadataUpdater The updater to use for the private metadata |
||||
* @return the transaction result |
||||
*/ |
||||
Result processTransaction( |
||||
Blockchain blockchain, |
||||
WorldUpdater worldState, |
||||
ProcessableBlockHeader blockHeader, |
||||
Transaction transaction, |
||||
Address miningBeneficiary, |
||||
OperationTracer operationTracer, |
||||
BlockHashLookup blockHashLookup, |
||||
Boolean isPersistingPrivateState, |
||||
TransactionValidationParams transactionValidationParams, |
||||
PrivateMetadataUpdater privateMetadataUpdater); |
||||
} |
@ -0,0 +1,189 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* 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.ethereum.processing; |
||||
|
||||
import org.hyperledger.besu.ethereum.core.Log; |
||||
import org.hyperledger.besu.ethereum.mainnet.TransactionValidator; |
||||
import org.hyperledger.besu.ethereum.mainnet.ValidationResult; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
import org.apache.tuweni.bytes.Bytes; |
||||
|
||||
public class TransactionProcessingResult { |
||||
|
||||
/** The status of the transaction after being processed. */ |
||||
public enum Status { |
||||
|
||||
/** The transaction was invalid for processing. */ |
||||
INVALID, |
||||
|
||||
/** The transaction was successfully processed. */ |
||||
SUCCESSFUL, |
||||
|
||||
/** The transaction failed to be completely processed. */ |
||||
FAILED |
||||
} |
||||
|
||||
private final Status status; |
||||
|
||||
private final long estimateGasUsedByTransaction; |
||||
|
||||
private final long gasRemaining; |
||||
|
||||
private final List<Log> logs; |
||||
|
||||
private final Bytes output; |
||||
|
||||
private final ValidationResult<TransactionValidator.TransactionInvalidReason> validationResult; |
||||
private final Optional<Bytes> revertReason; |
||||
|
||||
public static TransactionProcessingResult invalid( |
||||
final ValidationResult<TransactionValidator.TransactionInvalidReason> validationResult) { |
||||
return new TransactionProcessingResult( |
||||
Status.INVALID, new ArrayList<>(), -1, -1, Bytes.EMPTY, validationResult, Optional.empty()); |
||||
} |
||||
|
||||
public static TransactionProcessingResult failed( |
||||
final long gasUsedByTransaction, |
||||
final long gasRemaining, |
||||
final ValidationResult<TransactionValidator.TransactionInvalidReason> validationResult, |
||||
final Optional<Bytes> revertReason) { |
||||
return new TransactionProcessingResult( |
||||
Status.FAILED, |
||||
new ArrayList<>(), |
||||
gasUsedByTransaction, |
||||
gasRemaining, |
||||
Bytes.EMPTY, |
||||
validationResult, |
||||
revertReason); |
||||
} |
||||
|
||||
public static TransactionProcessingResult successful( |
||||
final List<Log> logs, |
||||
final long gasUsedByTransaction, |
||||
final long gasRemaining, |
||||
final Bytes output, |
||||
final ValidationResult<TransactionValidator.TransactionInvalidReason> validationResult) { |
||||
return new TransactionProcessingResult( |
||||
Status.SUCCESSFUL, |
||||
logs, |
||||
gasUsedByTransaction, |
||||
gasRemaining, |
||||
output, |
||||
validationResult, |
||||
Optional.empty()); |
||||
} |
||||
|
||||
public TransactionProcessingResult( |
||||
final Status status, |
||||
final List<Log> logs, |
||||
final long estimateGasUsedByTransaction, |
||||
final long gasRemaining, |
||||
final Bytes output, |
||||
final ValidationResult<TransactionValidator.TransactionInvalidReason> validationResult, |
||||
final Optional<Bytes> revertReason) { |
||||
this.status = status; |
||||
this.logs = logs; |
||||
this.estimateGasUsedByTransaction = estimateGasUsedByTransaction; |
||||
this.gasRemaining = gasRemaining; |
||||
this.output = output; |
||||
this.validationResult = validationResult; |
||||
this.revertReason = revertReason; |
||||
} |
||||
|
||||
/** |
||||
* Return the logs produced by the transaction. |
||||
* |
||||
* <p>This is only valid when {@code TransactionProcessor#isSuccessful} returns {@code true}. |
||||
* |
||||
* @return the logs produced by the transaction |
||||
*/ |
||||
public List<Log> getLogs() { |
||||
return logs; |
||||
} |
||||
|
||||
/** |
||||
* Returns the gas remaining after the transaction was processed. |
||||
* |
||||
* <p>This is only valid when {@code TransactionProcessor#isSuccessful} returns {@code true}. |
||||
* |
||||
* @return the gas remaining after the transaction was processed |
||||
*/ |
||||
public long getGasRemaining() { |
||||
return gasRemaining; |
||||
} |
||||
|
||||
/** |
||||
* Returns the estimate gas used by the transaction Difference between the gas limit and the |
||||
* remaining gas |
||||
* |
||||
* @return the estimate gas used |
||||
*/ |
||||
public long getEstimateGasUsedByTransaction() { |
||||
return estimateGasUsedByTransaction; |
||||
} |
||||
|
||||
/** |
||||
* Returns the status of the transaction after being processed. |
||||
* |
||||
* @return the status of the transaction after being processed |
||||
*/ |
||||
public Status getStatus() { |
||||
return status; |
||||
} |
||||
|
||||
public Bytes getOutput() { |
||||
return output; |
||||
} |
||||
|
||||
/** |
||||
* Returns whether or not the transaction was invalid. |
||||
* |
||||
* @return {@code true} if the transaction was invalid; otherwise {@code false} |
||||
*/ |
||||
public boolean isInvalid() { |
||||
return getStatus() == Status.INVALID; |
||||
} |
||||
|
||||
/** |
||||
* Returns whether or not the transaction was successfully processed. |
||||
* |
||||
* @return {@code true} if the transaction was successfully processed; otherwise {@code false} |
||||
*/ |
||||
public boolean isSuccessful() { |
||||
return getStatus() == Status.SUCCESSFUL; |
||||
} |
||||
|
||||
/** |
||||
* Returns the transaction validation result. |
||||
* |
||||
* @return the validation result, with the reason for failure (if applicable.) |
||||
*/ |
||||
public ValidationResult<TransactionValidator.TransactionInvalidReason> getValidationResult() { |
||||
return validationResult; |
||||
} |
||||
|
||||
/** |
||||
* Returns the reason why a transaction was reverted (if applicable). |
||||
* |
||||
* @return the revert reason. |
||||
*/ |
||||
public Optional<Bytes> getRevertReason() { |
||||
return revertReason; |
||||
} |
||||
} |
Loading…
Reference in new issue