Skip to content

Commit

Permalink
Run estimate gas lazily in web3 tests (#10093)
Browse files Browse the repository at this point in the history
Run estimate gas lazily in web3 tests. Reduces the number of unnecessary estimate calls by 234 and the overall test time by 1 minute (from 4 to 3 min locally).

Signed-off-by: Steven Sheehy <[email protected]>
  • Loading branch information
steven-sheehy authored Jan 13, 2025
1 parent d6f0aec commit 736be02
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -85,7 +86,7 @@ public class TestWeb3jService implements Web3jService {
private Address sender = Address.fromHexString("");
private boolean isEstimateGas = false;
private String transactionResult;
private String estimatedGas;
private Supplier<String> estimatedGas;
private long value = 0L;
private boolean persistContract = true;
private byte[] contractRuntime;
Expand All @@ -101,6 +102,10 @@ public TestWeb3jService(ContractExecutionService contractExecutionService, Domai
this.web3j = Web3j.build(this);
}

public String getEstimatedGas() {
return estimatedGas != null ? estimatedGas.get() : null;
}

public void setSender(String sender) {
this.sender = Address.fromHexString(sender);
}
Expand All @@ -110,6 +115,7 @@ public void setEstimateGas(final boolean isEstimateGas) {
}

public void reset() {
this.estimatedGas = null;
this.isEstimateGas = false;
this.contractRuntime = null;
this.persistContract = true;
Expand Down Expand Up @@ -210,7 +216,8 @@ private EthSendTransaction sendEthCall(RawTransaction rawTransaction, String tra
res.setId(request.getId());
res.setJsonrpc(request.getJsonrpc());

transactionResult = estimatedGas = mirrorNodeResult;
transactionResult = mirrorNodeResult;
estimatedGas = () -> mirrorNodeResult;
return res;
}

Expand All @@ -227,7 +234,7 @@ private EthCall ethCall(List<Transaction> reqParams, Request request) {
// Then get the estimated gas
final var serviceParametersForEstimate =
serviceParametersForExecutionSingle(transaction, ETH_ESTIMATE_GAS, blockType);
estimatedGas = contractExecutionService.processCall(serviceParametersForEstimate);
estimatedGas = () -> contractExecutionService.processCall(serviceParametersForEstimate);
}

final var ethCall = new EthCall();
Expand Down

0 comments on commit 736be02

Please sign in to comment.