mirror of https://github.com/hyperledger/besu
Transaction simulation service (#6686)
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>pull/6725/head
parent
efd1bc7070
commit
4cc6b744cf
@ -0,0 +1,84 @@ |
|||||||
|
/* |
||||||
|
* 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.services; |
||||||
|
|
||||||
|
import org.hyperledger.besu.datatypes.Hash; |
||||||
|
import org.hyperledger.besu.datatypes.Transaction; |
||||||
|
import org.hyperledger.besu.ethereum.chain.Blockchain; |
||||||
|
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams; |
||||||
|
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams; |
||||||
|
import org.hyperledger.besu.ethereum.transaction.CallParameter; |
||||||
|
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator; |
||||||
|
import org.hyperledger.besu.evm.tracing.OperationTracer; |
||||||
|
import org.hyperledger.besu.plugin.Unstable; |
||||||
|
import org.hyperledger.besu.plugin.data.TransactionSimulationResult; |
||||||
|
import org.hyperledger.besu.plugin.services.TransactionSimulationService; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
/** TransactionSimulationServiceImpl */ |
||||||
|
@Unstable |
||||||
|
public class TransactionSimulationServiceImpl implements TransactionSimulationService { |
||||||
|
private static final TransactionValidationParams SIMULATOR_ALLOWING_EXCEEDING_BALANCE = |
||||||
|
ImmutableTransactionValidationParams.builder() |
||||||
|
.from(TransactionValidationParams.transactionSimulator()) |
||||||
|
.isAllowExceedingBalance(true) |
||||||
|
.build(); |
||||||
|
private Blockchain blockchain; |
||||||
|
private TransactionSimulator transactionSimulator; |
||||||
|
|
||||||
|
/** Create an instance to be configured */ |
||||||
|
public TransactionSimulationServiceImpl() {} |
||||||
|
|
||||||
|
/** |
||||||
|
* Configure the service |
||||||
|
* |
||||||
|
* @param blockchain the blockchain |
||||||
|
* @param transactionSimulator transaction simulator |
||||||
|
*/ |
||||||
|
public void init(final Blockchain blockchain, final TransactionSimulator transactionSimulator) { |
||||||
|
this.blockchain = blockchain; |
||||||
|
this.transactionSimulator = transactionSimulator; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Optional<TransactionSimulationResult> simulate( |
||||||
|
final Transaction transaction, |
||||||
|
final Hash blockHash, |
||||||
|
final OperationTracer operationTracer, |
||||||
|
final boolean isAllowExceedingBalance) { |
||||||
|
|
||||||
|
final CallParameter callParameter = CallParameter.fromTransaction(transaction); |
||||||
|
|
||||||
|
final var blockHeader = |
||||||
|
blockchain |
||||||
|
.getBlockHeader(blockHash) |
||||||
|
.or(() -> blockchain.getBlockHeaderSafe(blockHash)) |
||||||
|
.orElseThrow( |
||||||
|
() -> |
||||||
|
new IllegalStateException( |
||||||
|
"Block header not yet present for chain head hash: " + blockHash)); |
||||||
|
|
||||||
|
return transactionSimulator |
||||||
|
.process( |
||||||
|
callParameter, |
||||||
|
isAllowExceedingBalance |
||||||
|
? SIMULATOR_ALLOWING_EXCEEDING_BALANCE |
||||||
|
: TransactionValidationParams.transactionSimulator(), |
||||||
|
operationTracer, |
||||||
|
blockHeader) |
||||||
|
.map(res -> new TransactionSimulationResult(transaction, res.result())); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,54 @@ |
|||||||
|
/* |
||||||
|
* 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.Transaction; |
||||||
|
|
||||||
|
/** |
||||||
|
* TransactionSimulationResult |
||||||
|
* |
||||||
|
* @param transaction tx |
||||||
|
* @param result res |
||||||
|
*/ |
||||||
|
public record TransactionSimulationResult( |
||||||
|
Transaction transaction, TransactionProcessingResult result) { |
||||||
|
|
||||||
|
/** |
||||||
|
* Was the simulation successful? |
||||||
|
* |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
public boolean isSuccessful() { |
||||||
|
return result.isSuccessful(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Was the transaction invalid? |
||||||
|
* |
||||||
|
* @return invalid |
||||||
|
*/ |
||||||
|
public boolean isInvalid() { |
||||||
|
return result.isInvalid(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Estimated gas used by the transaction |
||||||
|
* |
||||||
|
* @return estimated gas used |
||||||
|
*/ |
||||||
|
public long getGasEstimate() { |
||||||
|
return transaction.getGasLimit() - result.getGasRemaining(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,42 @@ |
|||||||
|
/* |
||||||
|
* 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.services; |
||||||
|
|
||||||
|
import org.hyperledger.besu.datatypes.Hash; |
||||||
|
import org.hyperledger.besu.datatypes.Transaction; |
||||||
|
import org.hyperledger.besu.evm.tracing.OperationTracer; |
||||||
|
import org.hyperledger.besu.plugin.Unstable; |
||||||
|
import org.hyperledger.besu.plugin.data.TransactionSimulationResult; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
/** Transaction simulation service interface */ |
||||||
|
@Unstable |
||||||
|
public interface TransactionSimulationService extends BesuService { |
||||||
|
/** |
||||||
|
* Simulate transaction execution at the block identified by the hash |
||||||
|
* |
||||||
|
* @param transaction tx |
||||||
|
* @param blockHash the hash of the block |
||||||
|
* @param operationTracer the tracer |
||||||
|
* @param isAllowExceedingBalance should ignore the sender balance during the simulation? |
||||||
|
* @return the result of the simulation |
||||||
|
*/ |
||||||
|
Optional<TransactionSimulationResult> simulate( |
||||||
|
Transaction transaction, |
||||||
|
Hash blockHash, |
||||||
|
OperationTracer operationTracer, |
||||||
|
boolean isAllowExceedingBalance); |
||||||
|
} |
Loading…
Reference in new issue