Skip to content

Commit c412a26

Browse files
authored
fix: block field and related logic for MirrorNodeContractQuery (#2614)
Signed-off-by: Mustafa Uzun <mustafa.uzun@limechain.tech>
1 parent 019d848 commit c412a26

4 files changed

Lines changed: 33 additions & 34 deletions

File tree

examples/src/main/java/com/hedera/hashgraph/sdk/examples/MirrorNodeContractQueriesExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public static void main(String[] args) throws Exception {
117117
.setContractId(contractId)
118118
.setSender(client.getOperatorAccountId())
119119
.setGasLimit(30_000)
120-
.setBlockNumber(10000)
120+
.setBlockNumber(10000L)
121121
.setGasPrice(1234)
122122
.setFunction("getMessage")
123123
.execute(client);

sdk/src/main/java/com/hedera/hashgraph/sdk/MirrorNodeContractQuery.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public abstract class MirrorNodeContractQuery<T extends MirrorNodeContractQuery<
3232
private long gasLimit;
3333
// The gas price
3434
private long gasPrice;
35-
// The block number for the simulation
36-
private long blockNumber;
35+
// The block for the simulation
36+
// Long so that if not set it defaults to empty which is latest
37+
private Long block;
3738

3839
@SuppressWarnings("unchecked")
3940
protected T self() {
@@ -201,20 +202,20 @@ public T setGasPrice(long gasPrice) {
201202
return self();
202203
}
203204

204-
public long getBlockNumber() {
205-
return this.blockNumber;
205+
public Long getBlockNumber() {
206+
return this.block;
206207
}
207208

208209
/**
209210
* Sets the block number for the simulation of the contract call.
210211
* <p>
211212
* The block number determines the context of the contract call simulation within the blockchain.
212213
*
213-
* @param blockNumber the block number at which to simulate the contract call
214+
* @param block the block number at which to simulate the contract call
214215
* @return {@code this}
215216
*/
216-
public T setBlockNumber(long blockNumber) {
217-
this.blockNumber = blockNumber;
217+
public T setBlockNumber(Long block) {
218+
this.block = block;
218219
return self();
219220
}
220221

@@ -240,8 +241,7 @@ protected long estimate(Client client) throws ExecutionException, InterruptedExc
240241
*/
241242
protected String call(Client client) throws ExecutionException, InterruptedException {
242243
fillEvmAddresses();
243-
var blockNum = this.blockNumber == 0 ? "latest" : String.valueOf(this.blockNumber);
244-
return getContractCallResultFromMirrorNodeAsync(client, blockNum).get();
244+
return getContractCallResultFromMirrorNodeAsync(client).get();
245245
}
246246

247247
private void fillEvmAddresses() {
@@ -255,17 +255,15 @@ private void fillEvmAddresses() {
255255
}
256256
}
257257

258-
private CompletableFuture<String> getContractCallResultFromMirrorNodeAsync(Client client, String blockNumber) {
259-
return executeMirrorNodeRequest(client, blockNumber, false)
260-
.thenApply(MirrorNodeContractQuery::parseContractCallResult);
258+
private CompletableFuture<String> getContractCallResultFromMirrorNodeAsync(Client client) {
259+
return executeMirrorNodeRequest(client, false).thenApply(MirrorNodeContractQuery::parseContractCallResult);
261260
}
262261

263262
private CompletableFuture<Long> getEstimateGasFromMirrorNodeAsync(Client client) {
264-
return executeMirrorNodeRequest(client, "latest", true)
265-
.thenApply(MirrorNodeContractQuery::parseHexEstimateToLong);
263+
return executeMirrorNodeRequest(client, true).thenApply(MirrorNodeContractQuery::parseHexEstimateToLong);
266264
}
267265

268-
private CompletableFuture<String> executeMirrorNodeRequest(Client client, String blockNumber, boolean estimate) {
266+
private CompletableFuture<String> executeMirrorNodeRequest(Client client, boolean estimate) {
269267
String apiEndpoint = "/contracts/call";
270268
String jsonPayload = createJsonPayload(
271269
this.callData,
@@ -274,7 +272,7 @@ private CompletableFuture<String> executeMirrorNodeRequest(Client client, String
274272
this.gasLimit,
275273
this.gasPrice,
276274
this.value,
277-
blockNumber,
275+
this.block,
278276
estimate);
279277

280278
String baseUrl = client.getMirrorRestBaseUrl();
@@ -302,17 +300,19 @@ static String createJsonPayload(
302300
long gas,
303301
long gasPrice,
304302
long value,
305-
String blockNumber,
303+
Long block,
306304
boolean estimate) {
307305
String hexData = Hex.toHexString(data);
308306

309307
JsonObject jsonObject = new JsonObject();
310308
jsonObject.addProperty("data", hexData);
311309
jsonObject.addProperty("to", contractAddress);
312310
jsonObject.addProperty("estimate", estimate);
313-
jsonObject.addProperty("blockNumber", blockNumber);
314311

315312
// Conditionally add fields if they are set to non-default values
313+
if (block != null) {
314+
jsonObject.addProperty("block", block);
315+
}
316316
if (senderAddress != null && !senderAddress.isEmpty()) {
317317
jsonObject.addProperty("from", senderAddress);
318318
}
@@ -348,7 +348,7 @@ public String toString() {
348348
+ Arrays.toString(callData) + ", value="
349349
+ value + ", gasLimit="
350350
+ gasLimit + ", gasPrice="
351-
+ gasPrice + ", blockNumber="
352-
+ blockNumber + '}';
351+
+ gasPrice + ", block="
352+
+ block + '}';
353353
}
354354
}

sdk/src/test/java/com/hedera/hashgraph/sdk/MirrorNodeContractQueryTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void testCreateJsonPayloadAllFieldsSet() {
146146
long gas = 50000;
147147
long gasPrice = 2000;
148148
long value = 1000;
149-
String blockNumber = "latest";
149+
Long blockNumber = 0L;
150150
boolean estimate = true;
151151

152152
String jsonPayload = MirrorNodeContractQuery.createJsonPayload(
@@ -156,7 +156,7 @@ void testCreateJsonPayloadAllFieldsSet() {
156156
expectedJson.addProperty("data", "7465737444617461");
157157
expectedJson.addProperty("to", contractAddress);
158158
expectedJson.addProperty("estimate", estimate);
159-
expectedJson.addProperty("blockNumber", blockNumber);
159+
expectedJson.addProperty("block", blockNumber);
160160
expectedJson.addProperty("from", senderAddress);
161161
expectedJson.addProperty("gas", gas);
162162
expectedJson.addProperty("gasPrice", gasPrice);
@@ -173,17 +173,16 @@ void testCreateJsonPayloadOnlyRequiredFieldsSet() {
173173
long gas = 0;
174174
long gasPrice = 0;
175175
long value = 0;
176-
String blockNumber = "latest";
176+
Long block = null;
177177
boolean estimate = true;
178178

179179
String jsonPayload = MirrorNodeContractQuery.createJsonPayload(
180-
data, senderAddress, contractAddress, gas, gasPrice, value, blockNumber, estimate);
180+
data, senderAddress, contractAddress, gas, gasPrice, value, block, estimate);
181181

182182
JsonObject expectedJson = new JsonObject();
183183
expectedJson.addProperty("data", "7465737444617461");
184184
expectedJson.addProperty("to", contractAddress);
185185
expectedJson.addProperty("estimate", estimate);
186-
expectedJson.addProperty("blockNumber", blockNumber);
187186

188187
assertEquals(expectedJson.toString(), jsonPayload);
189188
}
@@ -196,17 +195,17 @@ void testCreateJsonPayloadSomeOptionalFieldsSet() {
196195
long gas = 50000;
197196
long gasPrice = 0;
198197
long value = 1000;
199-
String blockNumber = "latest";
198+
Long block = 0L;
200199
boolean estimate = false;
201200

202201
String jsonPayload = MirrorNodeContractQuery.createJsonPayload(
203-
data, senderAddress, contractAddress, gas, gasPrice, value, blockNumber, estimate);
202+
data, senderAddress, contractAddress, gas, gasPrice, value, block, estimate);
204203

205204
JsonObject expectedJson = new JsonObject();
206205
expectedJson.addProperty("data", "7465737444617461");
207206
expectedJson.addProperty("to", contractAddress);
208207
expectedJson.addProperty("estimate", estimate);
209-
expectedJson.addProperty("blockNumber", blockNumber);
208+
expectedJson.addProperty("block", block);
210209
expectedJson.addProperty("from", senderAddress);
211210
expectedJson.addProperty("gas", gas);
212211
expectedJson.addProperty("value", value);
@@ -222,17 +221,17 @@ void testCreateJsonPayloadAllOptionalFieldsDefault() {
222221
long gas = 0;
223222
long gasPrice = 0;
224223
long value = 0;
225-
String blockNumber = "latest";
224+
Long block = 0L;
226225
boolean estimate = false;
227226

228227
String jsonPayload = MirrorNodeContractQuery.createJsonPayload(
229-
data, senderAddress, contractAddress, gas, gasPrice, value, blockNumber, estimate);
228+
data, senderAddress, contractAddress, gas, gasPrice, value, block, estimate);
230229

231230
JsonObject expectedJson = new JsonObject();
232231
expectedJson.addProperty("data", "7465737444617461");
233232
expectedJson.addProperty("to", contractAddress);
234233
expectedJson.addProperty("estimate", estimate);
235-
expectedJson.addProperty("blockNumber", blockNumber);
234+
expectedJson.addProperty("block", block);
236235

237236
assertEquals(expectedJson.toString(), jsonPayload);
238237
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
com.hedera.hashgraph.sdk.MirrorNodeContractQueryTest.shouldSerialize=[
2-
"MirrorNodeContractEstimateGasQuery{contractId=null, contractEvmAddress='0x1234567890abcdef1234567890abcdef12345678', sender=null, senderEvmAddress='0xabcdefabcdefabcdefabcdefabcdefabcdef', callData=[116, 101, 115, 116, 68, 97, 116, 97], value=1000, gasLimit=500000, gasPrice=20, blockNumber=123456}MirrorNodeContractCallQuery{contractId=null, contractEvmAddress='0x1234567890abcdef1234567890abcdef12345678', sender=null, senderEvmAddress='0xabcdefabcdefabcdefabcdefabcdefabcdef', callData=[116, 101, 115, 116, 68, 97, 116, 97], value=1000, gasLimit=500000, gasPrice=20, blockNumber=123456}"
3-
]
2+
"MirrorNodeContractEstimateGasQuery{contractId=null, contractEvmAddress='0x1234567890abcdef1234567890abcdef12345678', sender=null, senderEvmAddress='0xabcdefabcdefabcdefabcdefabcdefabcdef', callData=[116, 101, 115, 116, 68, 97, 116, 97], value=1000, gasLimit=500000, gasPrice=20, block=123456}MirrorNodeContractCallQuery{contractId=null, contractEvmAddress='0x1234567890abcdef1234567890abcdef12345678', sender=null, senderEvmAddress='0xabcdefabcdefabcdefabcdefabcdefabcdef', callData=[116, 101, 115, 116, 68, 97, 116, 97], value=1000, gasLimit=500000, gasPrice=20, block=123456}"
3+
]

0 commit comments

Comments
 (0)