Skip to content

Commit

Permalink
Merge pull request #1548 from rsksmart/gas-exactimation-v2
Browse files Browse the repository at this point in the history
Gas Exactimation v2
  • Loading branch information
Vovchyk committed Nov 24, 2021
2 parents 5fb7adc + 796fd04 commit 58ce04c
Show file tree
Hide file tree
Showing 33 changed files with 2,016 additions and 131 deletions.
3 changes: 2 additions & 1 deletion rskj-core/src/main/java/co/rsk/RskContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ public synchronized EthModule getEthModule() {
getRepositoryLocator(),
getEthModuleWallet(),
getEthModuleTransaction(),
getBridgeSupportFactory()
getBridgeSupportFactory(),
getRskSystemProperties().getGasEstimationCap()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ public ReversibleTransactionExecutor(
this.transactionExecutorFactory = transactionExecutorFactory;
}

public TransactionExecutor estimateGas(Block executionBlock, RskAddress coinbase, byte[] gasPrice, byte[] gasLimit,
byte[] toAddress, byte[] value, byte[] data, RskAddress fromAddress) {
return reversibleExecution(
repositoryLocator.snapshotAt(executionBlock.getHeader()),
executionBlock,
coinbase,
gasPrice,
gasLimit,
toAddress,
value,
data,
fromAddress
);
}

public ProgramResult executeTransaction(
Block executionBlock,
RskAddress coinbase,
Expand All @@ -64,6 +79,8 @@ public ProgramResult executeTransaction(
);
}



@Deprecated
public ProgramResult executeTransaction_workaround(
RepositorySnapshot snapshot,
Expand All @@ -75,6 +92,12 @@ public ProgramResult executeTransaction_workaround(
byte[] value,
byte[] data,
RskAddress fromAddress) {
return reversibleExecution(snapshot, executionBlock, coinbase, gasPrice, gasLimit, toAddress, value, data, fromAddress).getResult();
}

private TransactionExecutor reversibleExecution(RepositorySnapshot snapshot, Block executionBlock, RskAddress coinbase,
byte[] gasPrice, byte[] gasLimit, byte[] toAddress, byte[] value,
byte[] data, RskAddress fromAddress) {
Repository track = snapshot.startTracking();

byte[] nonce = track.getNonce(fromAddress).toByteArray();
Expand All @@ -94,7 +117,7 @@ public ProgramResult executeTransaction_workaround(

executor.executeTransaction();

return executor.getResult();
return executor;
}

private static class UnsignedTransaction extends Transaction {
Expand Down
41 changes: 34 additions & 7 deletions rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import co.rsk.peg.BridgeSupportFactory;
import co.rsk.rpc.ExecutionBlockRetriever;
import co.rsk.trie.TrieStoreImpl;
import com.google.common.annotations.VisibleForTesting;
import org.ethereum.core.*;
import org.ethereum.datasource.HashMapDB;
import org.ethereum.db.MutableRepository;
Expand Down Expand Up @@ -71,7 +72,7 @@ public class EthModule
private final BridgeConstants bridgeConstants;
private final BridgeSupportFactory bridgeSupportFactory;
private final byte chainId;

private final long gasEstimationCap;

public EthModule(
BridgeConstants bridgeConstants,
Expand All @@ -83,7 +84,8 @@ public EthModule(
RepositoryLocator repositoryLocator,
EthModuleWallet ethModuleWallet,
EthModuleTransaction ethModuleTransaction,
BridgeSupportFactory bridgeSupportFactory) {
BridgeSupportFactory bridgeSupportFactory,
long gasEstimationCap) {
this.chainId = chainId;
this.blockchain = blockchain;
this.transactionPool = transactionPool;
Expand All @@ -94,6 +96,7 @@ public EthModule(
this.ethModuleTransaction = ethModuleTransaction;
this.bridgeConstants = bridgeConstants;
this.bridgeSupportFactory = bridgeSupportFactory;
this.gasEstimationCap = gasEstimationCap;
}

@Override
Expand Down Expand Up @@ -144,15 +147,38 @@ public String call(CallArguments args, String bnOrId) {
}

public String estimateGas(CallArguments args) {
String s = null;
String estimation = null;
Block bestBlock = blockchain.getBestBlock();
try {
ProgramResult res = callConstant(args, blockchain.getBestBlock());
return s = TypeConverter.toQuantityJsonHex(res.getGasUsed());
CallArgumentsToByteArray hexArgs = new CallArgumentsToByteArray(args);

TransactionExecutor executor = reversibleTransactionExecutor.estimateGas(
bestBlock,
bestBlock.getCoinbase(),
hexArgs.getGasPrice(),
hexArgs.gasLimitForGasEstimation(gasEstimationCap),
hexArgs.getToAddress(),
hexArgs.getValue(),
hexArgs.getData(),
hexArgs.getFromAddress()
);

estimation = internalEstimateGas(executor.getResult());

return estimation;
} finally {
LOGGER.debug("eth_estimateGas(): {}", s);
LOGGER.debug("eth_estimateGas(): {}", estimation);
}
}

protected String internalEstimateGas(ProgramResult reversibleExecutionResult) {
long estimatedGas = reversibleExecutionResult.getMovedRemainingGasToChild() ?
reversibleExecutionResult.getGasUsed() + reversibleExecutionResult.getDeductedRefund() :
reversibleExecutionResult.getMaxGasUsed();

return TypeConverter.toQuantityJsonHex(estimatedGas);
}

@Override
public String sendTransaction(CallArguments args) {
return ethModuleTransaction.sendTransaction(args);
Expand Down Expand Up @@ -224,7 +250,8 @@ private AccountInformationProvider getAccountInformationProvider(String id) {
}
}

private ProgramResult callConstant(CallArguments args, Block executionBlock) {
@VisibleForTesting
public ProgramResult callConstant(CallArguments args, Block executionBlock) {
CallArgumentsToByteArray hexArgs = new CallArgumentsToByteArray(args);
return reversibleTransactionExecutor.executeTransaction(
executionBlock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public abstract class SystemProperties {
private static final String PROPERTY_RPC_WEBSOCKET_SERVER_WRITE_TIMEOUT_SECONDS = "rpc.providers.web.ws.server_write_timeout_seconds";
private static final String PROPERTY_RPC_WEBSOCKET_SERVER_MAX_FRAME_SIZE = "rpc.providers.web.ws.max_frame_size";
private static final String PROPERTY_RPC_WEBSOCKET_SERVER_MAX_AGGREGATED_FRAME_SIZE = "rpc.providers.web.ws.max_aggregated_frame_size";
private static final String PROPERTY_RPC_GAS_ESTIMATION_CAP = "rpc.gasEstimationCap";

public static final String PROPERTY_PUBLIC_IP = "public.ip";
public static final String PROPERTY_BIND_ADDRESS = "bind_address";
Expand Down Expand Up @@ -695,4 +696,8 @@ private Optional<List<BtcECKey>> getGenesisFederationPublicKeys() {
.map(key -> BtcECKey.fromPublicOnly(Hex.decode(key))).collect(Collectors.toList())
);
}

public long getGasEstimationCap() {
return configFromFiles.getLong(PROPERTY_RPC_GAS_ESTIMATION_CAP);
}
}
Loading

0 comments on commit 58ce04c

Please sign in to comment.