Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow default ContractIDs #13438

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -726,14 +726,15 @@ public static long fromTinycentsToTinybars(final ExchangeRate exchangeRate, fina
* Given a {@link ContractID} return the corresponding Besu {@link Address}
* Importantly, this method does NOT check for the existence of the contract in the ledger
*
* @param contractId
* @param contractId the contract id
* @return the equivalent Besu address
*/
public static @NonNull Address contractIDToBesuAddress(final ContractID contractId) {
if (contractId.hasEvmAddress()) {
return pbjToBesuAddress(contractId.evmAddress());
return pbjToBesuAddress(contractId.evmAddressOrThrow());
} else {
return asLongZeroAddress(contractId.contractNumOrThrow());
// OrElse(0) is needed, as an UNSET contract OneOf has null number
return asLongZeroAddress(contractId.contractNumOrElse(0L));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.asLongZeroAddress;
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.asNumberedAccountId;
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.asNumberedContractId;
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.contractIDToBesuAddress;
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.numberOfLongZero;
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.pbjLogFrom;
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.pbjLogsFrom;
Expand Down Expand Up @@ -81,6 +82,11 @@ void outOfRangeBiValuesAreZero() {
0L, asExactLongValueOrZero(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE)));
}

@Test
void besuAddressIsZeroForDefaultContractId() {
assertEquals(Address.ZERO, contractIDToBesuAddress(ContractID.DEFAULT));
}

@Test
void inRangeBiValuesAreExact() {
assertEquals(Long.MAX_VALUE, asExactLongValueOrZero(BigInteger.valueOf(Long.MAX_VALUE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.hedera.services.bdd.spec.transactions.TxnUtils;
import com.hederahashgraph.api.proto.java.AccountID;
import com.hederahashgraph.api.proto.java.ContractCallTransactionBody;
import com.hederahashgraph.api.proto.java.ContractID;
import com.hederahashgraph.api.proto.java.HederaFunctionality;
import com.hederahashgraph.api.proto.java.Key;
import com.hederahashgraph.api.proto.java.ResponseCodeEnum;
Expand All @@ -54,6 +55,8 @@
import java.util.function.Supplier;

public class HapiContractCall extends HapiBaseCall<HapiContractCall> {
public static final String DEFAULT_ID_SENTINEL = "<DEFAULT_ID>";

protected List<String> otherSigs = Collections.emptyList();
private Optional<String> details = Optional.empty();
private Optional<Function<HapiSpec, Object[]>> paramsFn = Optional.empty();
Expand Down Expand Up @@ -292,7 +295,9 @@ protected Consumer<TransactionBody.Builder> opBodyDef(HapiSpec spec) throws Thro
.<ContractCallTransactionBody, ContractCallTransactionBody.Builder>body(
ContractCallTransactionBody.class, builder -> {
if (contract != null) {
if (!tryAsHexedAddressIfLenMatches) {
if (DEFAULT_ID_SENTINEL.equals(contract)) {
builder.setContractID(ContractID.getDefaultInstance());
} else if (!tryAsHexedAddressIfLenMatches) {
builder.setContractID(spec.registry().getContractId(contract));
} else {
builder.setContractID(TxnUtils.asContractId(contract, spec));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static com.hedera.services.bdd.spec.transactions.TxnVerbs.tokenAssociate;
import static com.hedera.services.bdd.spec.transactions.TxnVerbs.tokenCreate;
import static com.hedera.services.bdd.spec.transactions.TxnVerbs.uploadInitCode;
import static com.hedera.services.bdd.spec.transactions.contract.HapiContractCall.DEFAULT_ID_SENTINEL;
import static com.hedera.services.bdd.spec.transactions.contract.HapiParserUtil.asHeadlongAddress;
import static com.hedera.services.bdd.spec.transactions.crypto.HapiCryptoTransfer.tinyBarsFromAccountToAlias;
import static com.hedera.services.bdd.spec.transactions.token.TokenMovement.moving;
Expand Down Expand Up @@ -140,7 +141,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;

@HapiTestSuite(fuzzyMatch = true)
@HapiTestSuite(fuzzyMatch = false)
@Tag(SMART_CONTRACT)
public class ContractCallSuite extends HapiSuite {

Expand Down Expand Up @@ -1577,7 +1578,11 @@ final HapiSpec idVariantsTreatedAsExpected() {
return defaultHapiSpec("idVariantsTreatedAsExpected")
.given(uploadInitCode(PAY_RECEIVABLE_CONTRACT), contractCreate(PAY_RECEIVABLE_CONTRACT))
.when()
.then(submitModified(withSuccessivelyVariedBodyIds(), () -> contractCall(PAY_RECEIVABLE_CONTRACT)));
.then(
submitModified(withSuccessivelyVariedBodyIds(), () -> contractCall(PAY_RECEIVABLE_CONTRACT)),
// It's also ok to use a default PBJ ContractID (i.e. an id with
// UNSET contract oneof) to make a no-op call to address 0x00...00
contractCall(DEFAULT_ID_SENTINEL));
}

@HapiTest
Expand Down
Loading