Skip to content

Commit cb9febe

Browse files
chore: [HIP-1215] Finish Java integration with services (#21036)
Signed-off-by: Glib Kozyryatskyy <[email protected]>
1 parent 3064e36 commit cb9febe

File tree

16 files changed

+383
-156
lines changed

16 files changed

+383
-156
lines changed

hedera-node/hedera-smart-contract-service-impl/src/main/java/com/hedera/node/app/service/contract/impl/exec/systemcontracts/hss/DispatchForResponseCodeHssCall.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
package com.hedera.node.app.service.contract.impl.exec.systemcontracts.hss;
33

44
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_TRANSACTION_BODY;
5-
import static com.hedera.hapi.node.base.ResponseCodeEnum.SUCCESS;
65
import static com.hedera.node.app.service.contract.impl.exec.failure.CustomExceptionalHaltReason.ERROR_DECODING_PRECOMPILE_INPUT;
76
import static com.hedera.node.app.service.contract.impl.exec.systemcontracts.FullResult.haltResult;
87
import static com.hedera.node.app.service.contract.impl.exec.systemcontracts.common.Call.PricedResult.gasOnly;
9-
import static com.hedera.node.app.service.contract.impl.exec.systemcontracts.hts.ReturnTypes.RC_AND_ADDRESS_ENCODER;
10-
import static com.hedera.node.app.service.contract.impl.exec.systemcontracts.hts.ReturnTypes.encodedRc;
118
import static com.hedera.node.app.service.contract.impl.exec.utils.FrameUtils.contractsConfigOf;
129
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.headlongAddressOf;
1310

@@ -20,6 +17,7 @@
2017
import com.hedera.node.app.service.contract.impl.exec.gas.SystemContractGasCalculator;
2118
import com.hedera.node.app.service.contract.impl.exec.scope.VerificationStrategy;
2219
import com.hedera.node.app.service.contract.impl.exec.systemcontracts.common.AbstractCall;
20+
import com.hedera.node.app.service.contract.impl.exec.systemcontracts.hts.ReturnTypes;
2321
import com.hedera.node.app.service.contract.impl.hevm.HederaWorldUpdater;
2422
import com.hedera.node.app.service.contract.impl.records.ContractCallStreamBuilder;
2523
import com.hedera.node.app.spi.workflows.DispatchOptions;
@@ -69,7 +67,7 @@ public DispatchForResponseCodeHssCall(
6967
attempt.defaultVerificationStrategy(),
7068
dispatchGasCalculator,
7169
authorizingKeys,
72-
e -> encodedRc(e.status()));
70+
e -> ReturnTypes.encodedRc(e.status()));
7371
}
7472

7573
/**
@@ -126,13 +124,7 @@ public DispatchForResponseCodeHssCall(
126124
usePresetTxnId);
127125
final var gasRequirement =
128126
dispatchGasCalculator.gasRequirement(syntheticBody, gasCalculator, enhancement, senderId);
129-
var status = recordBuilder.status();
130-
if (status != SUCCESS) {
131-
recordBuilder.status(status);
132-
return completionWith(gasRequirement, recordBuilder, encodedRc(status));
133-
} else {
134-
return completionWith(gasRequirement, recordBuilder, resultEncoder.apply(recordBuilder));
135-
}
127+
return completionWith(gasRequirement, recordBuilder, resultEncoder.apply(recordBuilder));
136128
}
137129

138130
/**
@@ -143,7 +135,10 @@ public DispatchForResponseCodeHssCall(
143135
* @return the encoded status
144136
*/
145137
public static ByteBuffer scheduleCreateResultEncode(@NonNull final ContractCallStreamBuilder recordBuilder) {
146-
return RC_AND_ADDRESS_ENCODER.encode(
147-
Tuple.of((long) recordBuilder.status().protoOrdinal(), headlongAddressOf(recordBuilder.scheduleID())));
138+
return ReturnTypes.RC_AND_ADDRESS_ENCODER.encode(Tuple.of(
139+
(long) recordBuilder.status().protoOrdinal(),
140+
recordBuilder.scheduleID() != null
141+
? headlongAddressOf(recordBuilder.scheduleID())
142+
: ReturnTypes.ZERO_ADDRESS));
148143
}
149144
}

hedera-node/hedera-smart-contract-service-impl/src/main/java/com/hedera/node/app/service/contract/impl/exec/systemcontracts/hss/hasschedulecapacity/HasScheduleCapacityTranslator.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package com.hedera.node.app.service.contract.impl.exec.systemcontracts.hss.hasschedulecapacity;
33

4-
import static java.math.BigInteger.ZERO;
5-
64
import com.hedera.node.app.service.contract.impl.exec.metrics.ContractMetrics;
75
import com.hedera.node.app.service.contract.impl.exec.systemcontracts.common.AbstractCallTranslator;
86
import com.hedera.node.app.service.contract.impl.exec.systemcontracts.common.Call;
@@ -11,9 +9,9 @@
119
import com.hedera.node.app.service.contract.impl.exec.utils.SystemContractMethod;
1210
import com.hedera.node.app.service.contract.impl.exec.utils.SystemContractMethod.Category;
1311
import com.hedera.node.app.service.contract.impl.exec.utils.SystemContractMethodRegistry;
12+
import com.hedera.node.app.service.contract.impl.utils.ConversionUtils;
1413
import com.hedera.node.config.data.ContractsConfig;
1514
import edu.umd.cs.findbugs.annotations.NonNull;
16-
import java.math.BigInteger;
1715
import java.util.Optional;
1816
import javax.inject.Inject;
1917
import javax.inject.Singleton;
@@ -26,7 +24,6 @@
2624
public class HasScheduleCapacityTranslator extends AbstractCallTranslator<HssCallAttempt> {
2725
private static final int EXPIRY_INDEX = 0;
2826
private static final int GAS_LIMIT_INDEX = 1;
29-
private static final BigInteger MAX_BI = BigInteger.valueOf(Long.MAX_VALUE);
3027

3128
public static final SystemContractMethod HAS_SCHEDULE_CAPACITY = SystemContractMethod.declare(
3229
"hasScheduleCapacity(uint256,uint256)", ReturnTypes.BOOL)
@@ -54,8 +51,8 @@ public Optional<SystemContractMethod> identifyMethod(@NonNull final HssCallAttem
5451
@Override
5552
public Call callFrom(@NonNull final HssCallAttempt attempt) {
5653
final var call = HAS_SCHEDULE_CAPACITY.decodeCall(attempt.inputBytes());
57-
final long expiry = ZERO.max(MAX_BI.min(call.get(EXPIRY_INDEX))).longValueExact();
58-
final long gasLimit = ZERO.max(MAX_BI.min(call.get(GAS_LIMIT_INDEX))).longValueExact();
54+
final long expiry = ConversionUtils.asLongLimitedToZeroOrMax(call.get(EXPIRY_INDEX));
55+
final long gasLimit = ConversionUtils.asLongLimitedToZeroOrMax(call.get(GAS_LIMIT_INDEX));
5956
return new HasScheduleCapacityCall(attempt, expiry, gasLimit);
6057
}
6158
}

hedera-node/hedera-smart-contract-service-impl/src/main/java/com/hedera/node/app/service/contract/impl/exec/systemcontracts/hss/schedulecall/ScheduleCallDecoder.java

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.hedera.node.app.service.contract.impl.utils.ConversionUtils;
1919
import com.hedera.pbj.runtime.io.buffer.Bytes;
2020
import edu.umd.cs.findbugs.annotations.NonNull;
21-
import java.math.BigInteger;
2221
import java.util.Set;
2322
import javax.inject.Inject;
2423
import javax.inject.Singleton;
@@ -47,30 +46,31 @@ public TransactionBody decodeScheduleCall(@NonNull final HssCallAttempt attempt,
4746
// read parameters
4847
final Tuple call;
4948
final Address to;
50-
final AccountID sender;
49+
final AccountID payer;
5150
final boolean waitForExpiry;
5251
int paramIndex = 0;
5352
if (attempt.isSelector(ScheduleCallTranslator.SCHEDULE_CALL)) {
5453
call = ScheduleCallTranslator.SCHEDULE_CALL.decodeCall(attempt.inputBytes());
5554
to = call.get(paramIndex++);
56-
sender = attempt.addressIdConverter().convertSender(attempt.senderAddress());
55+
payer = attempt.senderId();
5756
waitForExpiry = true;
58-
} else if (attempt.isSelector(ScheduleCallTranslator.SCHEDULE_CALL_WITH_SENDER)) {
59-
call = ScheduleCallTranslator.SCHEDULE_CALL_WITH_SENDER.decodeCall(attempt.inputBytes());
57+
} else if (attempt.isSelector(ScheduleCallTranslator.SCHEDULE_CALL_WITH_PAYER)) {
58+
call = ScheduleCallTranslator.SCHEDULE_CALL_WITH_PAYER.decodeCall(attempt.inputBytes());
6059
to = call.get(paramIndex++);
61-
sender = attempt.addressIdConverter().convert(call.get(paramIndex++));
60+
payer = attempt.addressIdConverter().convert(call.get(paramIndex++));
6261
waitForExpiry = true;
63-
} else if (attempt.isSelector(ScheduleCallTranslator.EXECUTE_CALL_ON_SENDER_SIGNATURE)) {
64-
call = ScheduleCallTranslator.EXECUTE_CALL_ON_SENDER_SIGNATURE.decodeCall(attempt.inputBytes());
62+
} else if (attempt.isSelector(ScheduleCallTranslator.EXECUTE_CALL_ON_PAYER_SIGNATURE)) {
63+
call = ScheduleCallTranslator.EXECUTE_CALL_ON_PAYER_SIGNATURE.decodeCall(attempt.inputBytes());
6564
to = call.get(paramIndex++);
66-
sender = attempt.addressIdConverter().convert(call.get(paramIndex++));
65+
payer = attempt.addressIdConverter().convert(call.get(paramIndex++));
6766
waitForExpiry = false;
6867
} else {
6968
throw new IllegalStateException("Unexpected function selector");
7069
}
71-
final BigInteger expirySecond = call.get(paramIndex++);
72-
final BigInteger gasLimit = call.get(paramIndex++);
73-
final BigInteger value = call.get(paramIndex++);
70+
71+
final long expirySecond = ConversionUtils.asLongLimitedToZeroOrMax(call.get(paramIndex++));
72+
final long gasLimit = ConversionUtils.asLongLimitedToZeroOrMax(call.get(paramIndex++));
73+
final long value = ConversionUtils.asLongLimitedToZeroOrMax(call.get(paramIndex++));
7474
final byte[] callData = call.get(paramIndex);
7575

7676
// convert parameters
@@ -83,7 +83,7 @@ public TransactionBody decodeScheduleCall(@NonNull final HssCallAttempt attempt,
8383
scheduledTransactionBodyFor(contractId, gasLimit, value, callData),
8484
keys,
8585
expirySecond,
86-
sender,
86+
payer,
8787
waitForExpiry));
8888
}
8989

@@ -92,9 +92,9 @@ public TransactionBody decodeScheduleCall(@NonNull final HssCallAttempt attempt,
9292
* <br>
9393
* - {@code scheduleCall(address,uint256,uint256,uint64,bytes)}
9494
* <br>
95-
* - {@code scheduleCallWithSender(address,address,uint256,uint256,uint64,bytes)}
95+
* - {@code scheduleCallWithPayer(address,address,uint256,uint256,uint64,bytes)}
9696
* <br>
97-
* - {@code executeCallOnSenderSignature(address,address,uint256,uint256,uint64,bytes)}
97+
* - {@code executeCallOnPayerSignature(address,address,uint256,uint256,uint64,bytes)}
9898
*
9999
* @param attempt the HSS call attempt
100100
* @param scheduleCreateTrx the 'schedule create' transaction body
@@ -116,34 +116,33 @@ public TransactionBody transactionBodyFor(
116116
* <br>
117117
* - {@code scheduleCall(address,uint256,uint256,uint64,bytes)}
118118
* <br>
119-
* - {@code scheduleCallWithSender(address,address,uint256,uint256,uint64,bytes)}
119+
* - {@code scheduleCallWithPayer(address,address,uint256,uint256,uint64,bytes)}
120120
* <br>
121-
* - {@code executeCallOnSenderSignature(address,address,uint256,uint256,uint64,bytes)}
121+
* - {@code executeCallOnPayerSignature(address,address,uint256,uint256,uint64,bytes)}
122122
*
123123
* @param scheduleTrx scheduled transaction body for this schedule create
124124
* @param keys the key set for scheduled calls
125125
* @param expirySecond an expiration time of the future call
126-
* @param sender an account identifier of a `payer` for the scheduled transaction
126+
* @param payer an account identifier of a `payer` for the scheduled transaction
127127
* @param waitForExpiry a flag to delay execution until expiration
128128
* @return the 'schedule create' transaction body
129129
*/
130130
@VisibleForTesting
131131
public ScheduleCreateTransactionBody scheduleCreateTransactionBodyFor(
132132
@NonNull final SchedulableTransactionBody scheduleTrx,
133133
@NonNull final Set<Key> keys,
134-
@NonNull final BigInteger expirySecond,
135-
@NonNull final AccountID sender,
134+
final long expirySecond,
135+
@NonNull final AccountID payer,
136136
boolean waitForExpiry) {
137137
requireNonNull(scheduleTrx);
138138
requireNonNull(keys);
139-
requireNonNull(expirySecond);
140-
requireNonNull(sender);
139+
requireNonNull(payer);
141140
return ScheduleCreateTransactionBody.newBuilder()
142141
.scheduledTransactionBody(scheduleTrx)
143142
// we need to set adminKey for make schedule not immutable and to be able to delete schedule
144143
.adminKey(keys.stream().findFirst().orElse(null))
145-
.expirationTime(Timestamp.newBuilder().seconds(expirySecond.longValueExact()))
146-
.payerAccountID(sender)
144+
.expirationTime(Timestamp.newBuilder().seconds(expirySecond))
145+
.payerAccountID(payer)
147146
.waitForExpiry(waitForExpiry)
148147
.build();
149148
}
@@ -153,9 +152,9 @@ public ScheduleCreateTransactionBody scheduleCreateTransactionBodyFor(
153152
* <br>
154153
* - {@code scheduleCall(address,uint256,uint256,uint64,bytes)}
155154
* <br>
156-
* - {@code scheduleCallWithSender(address,address,uint256,uint256,uint64,bytes)}
155+
* - {@code scheduleCallWithPayer(address,address,uint256,uint256,uint64,bytes)}
157156
* <br>
158-
* - {@code executeCallOnSenderSignature(address,address,uint256,uint256,uint64,bytes)}
157+
* - {@code executeCallOnPayerSignature(address,address,uint256,uint256,uint64,bytes)}
159158
*
160159
* @param contractId contract id for the future call
161160
* @param gasLimit a maximum limit to the amount of gas to use for future call
@@ -167,19 +166,14 @@ public ScheduleCreateTransactionBody scheduleCreateTransactionBodyFor(
167166
*/
168167
@VisibleForTesting
169168
public SchedulableTransactionBody scheduledTransactionBodyFor(
170-
@NonNull final ContractID contractId,
171-
@NonNull final BigInteger gasLimit,
172-
@NonNull final BigInteger value,
173-
@NonNull byte[] callData) {
169+
@NonNull final ContractID contractId, final long gasLimit, final long value, @NonNull byte[] callData) {
174170
requireNonNull(contractId);
175-
requireNonNull(gasLimit);
176-
requireNonNull(value);
177171
requireNonNull(callData);
178172
return SchedulableTransactionBody.newBuilder()
179173
.contractCall(ContractCallTransactionBody.newBuilder()
180174
.contractID(contractId)
181-
.gas(gasLimit.longValueExact())
182-
.amount(value.longValueExact())
175+
.gas(gasLimit)
176+
.amount(value)
183177
.functionParameters(Bytes.wrap(callData)))
184178
.build();
185179
}

hedera-node/hedera-smart-contract-service-impl/src/main/java/com/hedera/node/app/service/contract/impl/exec/systemcontracts/hss/schedulecall/ScheduleCallTranslator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public class ScheduleCallTranslator extends AbstractCallTranslator<HssCallAttemp
3131
public static final SystemContractMethod SCHEDULE_CALL = SystemContractMethod.declare(
3232
"scheduleCall(address,uint256,uint256,uint64,bytes)", ReturnTypes.RESPONSE_CODE_ADDRESS)
3333
.withCategories(Category.SCHEDULE);
34-
public static final SystemContractMethod SCHEDULE_CALL_WITH_SENDER = SystemContractMethod.declare(
35-
"scheduleCallWithSender(address,address,uint256,uint256,uint64,bytes)",
34+
public static final SystemContractMethod SCHEDULE_CALL_WITH_PAYER = SystemContractMethod.declare(
35+
"scheduleCallWithPayer(address,address,uint256,uint256,uint64,bytes)",
3636
ReturnTypes.RESPONSE_CODE_ADDRESS)
3737
.withCategories(Category.SCHEDULE);
38-
public static final SystemContractMethod EXECUTE_CALL_ON_SENDER_SIGNATURE = SystemContractMethod.declare(
39-
"executeCallOnSenderSignature(address,address,uint256,uint256,uint64,bytes)",
38+
public static final SystemContractMethod EXECUTE_CALL_ON_PAYER_SIGNATURE = SystemContractMethod.declare(
39+
"executeCallOnPayerSignature(address,address,uint256,uint256,uint64,bytes)",
4040
ReturnTypes.RESPONSE_CODE_ADDRESS)
4141
.withCategories(Category.SCHEDULE);
4242

@@ -49,14 +49,14 @@ public ScheduleCallTranslator(
4949
@NonNull final ContractMetrics contractMetrics) {
5050
super(SystemContractMethod.SystemContract.HSS, systemContractMethodRegistry, contractMetrics);
5151
this.decoder = decoder;
52-
registerMethods(SCHEDULE_CALL, SCHEDULE_CALL_WITH_SENDER, EXECUTE_CALL_ON_SENDER_SIGNATURE);
52+
registerMethods(SCHEDULE_CALL, SCHEDULE_CALL_WITH_PAYER, EXECUTE_CALL_ON_PAYER_SIGNATURE);
5353
}
5454

5555
@Override
5656
@NonNull
5757
public Optional<SystemContractMethod> identifyMethod(@NonNull final HssCallAttempt attempt) {
5858
if (attempt.configuration().getConfigData(ContractsConfig.class).systemContractScheduleCallEnabled()) {
59-
return attempt.isMethod(SCHEDULE_CALL, SCHEDULE_CALL_WITH_SENDER, EXECUTE_CALL_ON_SENDER_SIGNATURE);
59+
return attempt.isMethod(SCHEDULE_CALL, SCHEDULE_CALL_WITH_PAYER, EXECUTE_CALL_ON_PAYER_SIGNATURE);
6060
} else {
6161
return Optional.empty();
6262
}

hedera-node/hedera-smart-contract-service-impl/src/main/java/com/hedera/node/app/service/contract/impl/utils/ConversionUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static com.hedera.node.app.service.contract.impl.exec.utils.FrameUtils.proxyUpdaterFor;
1010
import static com.hedera.node.app.service.contract.impl.utils.SynthTxnUtils.hasNonDegenerateAutoRenewAccountId;
1111
import static com.hedera.node.app.service.token.AliasUtils.extractEvmAddress;
12+
import static java.math.BigInteger.ZERO;
1213
import static java.util.Objects.requireNonNull;
1314
import static org.hiero.base.utility.CommonUtils.unhex;
1415

@@ -141,6 +142,24 @@ public static long asExactLongValueOrZero(@NonNull final BigInteger value) {
141142
return value.longValueExact();
142143
}
143144

145+
/**
146+
* Given a {@link BigInteger} representing 'uint' value.
147+
* Returns either:
148+
* <br>
149+
* - its long value
150+
* <br>
151+
* - ZERO if it is less than ZERO
152+
* <br>
153+
* - MAX_LONG_VALUE if it is more than MAX_LONG_VALUE
154+
*
155+
* @param value the {@link BigInteger}
156+
* @return long value
157+
*/
158+
public static long asLongLimitedToZeroOrMax(@NonNull final BigInteger value) {
159+
requireNonNull(value);
160+
return ZERO.max(MAX_LONG_VALUE.min(value)).longValueExact();
161+
}
162+
144163
/**
145164
* Given a {@link AccountID}, returns its address as a headlong address.
146165
*

0 commit comments

Comments
 (0)