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

feat: TokenCancelAirdrop should fail fast with invalid parameters #17168

Merged
merged 5 commits into from
Jan 3, 2025
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
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,8 +17,8 @@
package com.hedera.node.app.service.token.impl.handlers;

import static com.hedera.hapi.node.base.ResponseCodeEnum.EMPTY_PENDING_AIRDROP_ID_LIST;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_ACCOUNT_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_NFT_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_PENDING_AIRDROP_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_TOKEN_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_TOKEN_NFT_SERIAL_NUMBER;
import static com.hedera.hapi.node.base.ResponseCodeEnum.NOT_SUPPORTED;
Expand Down Expand Up @@ -78,7 +78,7 @@
final var op = txn.tokenCancelAirdropOrThrow();
final var allPendingAirdrops = op.pendingAirdrops();
for (final var airdrop : allPendingAirdrops) {
context.requireAliasedKeyOrThrow(airdrop.senderIdOrThrow(), INVALID_ACCOUNT_ID);
context.requireAliasedKeyOrThrow(airdrop.senderIdOrThrow(), INVALID_PENDING_AIRDROP_ID);

Check warning on line 81 in hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java#L81

Added line #L81 was not covered by tests
}
}

Expand All @@ -93,8 +93,8 @@
if (!uniquePendingAirdrops.add(airdrop)) {
throw new PreCheckException(PENDING_AIRDROP_ID_REPEATED);
}
validateAccountID(airdrop.receiverId(), null);
validateAccountID(airdrop.senderId(), null);
validateAccountID(airdrop.receiverId(), INVALID_PENDING_AIRDROP_ID);
validateAccountID(airdrop.senderId(), INVALID_PENDING_AIRDROP_ID);

if (airdrop.hasFungibleTokenType()) {
final var tokenID = airdrop.fungibleTokenType();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
package com.hedera.node.app.service.token.impl.test.handlers;

import static com.hedera.hapi.node.base.ResponseCodeEnum.EMPTY_PENDING_AIRDROP_ID_LIST;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_ACCOUNT_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_PENDING_AIRDROP_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_TOKEN_NFT_SERIAL_NUMBER;
import static com.hedera.hapi.node.base.ResponseCodeEnum.PENDING_AIRDROP_ID_REPEATED;
import static com.hedera.node.app.service.token.impl.handlers.BaseCryptoHandler.asAccount;
Expand All @@ -44,7 +44,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TokenCancelAirdropHandlerPureChecksTest extends CryptoTokenHandlerTestBase {
class TokenCancelAirdropHandlerPureChecksTest extends CryptoTokenHandlerTestBase {

private static final AccountID ACCOUNT_SENDER = asAccount(4444);
private static final AccountID ACCOUNT_RECEIVER = asAccount(3333);
Expand Down Expand Up @@ -110,7 +110,7 @@ void handleCancelAirdropWithMissingReceiver() {

Assertions.assertThatThrownBy(() -> subject.pureChecks(txn))
.isInstanceOf(PreCheckException.class)
.has(responseCode(INVALID_ACCOUNT_ID));
.has(responseCode(INVALID_PENDING_AIRDROP_ID));
}

@Test
Expand All @@ -124,7 +124,7 @@ void handleCancelAirdropWithMissingSender() {

Assertions.assertThatThrownBy(() -> subject.pureChecks(txn))
.isInstanceOf(PreCheckException.class)
.has(responseCode(INVALID_ACCOUNT_ID));
.has(responseCode(INVALID_PENDING_AIRDROP_ID));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,8 @@
import static com.hedera.services.bdd.spec.transactions.token.TokenMovement.moving;
import static com.hedera.services.bdd.spec.transactions.token.TokenMovement.movingUnique;
import static com.hedera.services.bdd.spec.utilops.UtilVerbs.newKeyNamed;
import static com.hedera.services.bdd.spec.utilops.UtilVerbs.submitModified;
import static com.hedera.services.bdd.spec.utilops.mod.ModificationUtils.withSuccessivelyVariedBodyIds;
import static com.hedera.services.bdd.suites.HapiSuite.FIVE_HBARS;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.ACCOUNT_HAS_PENDING_AIRDROPS;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.EMPTY_PENDING_AIRDROP_ID_LIST;
Expand Down Expand Up @@ -78,6 +80,21 @@ static void beforeAll(@NonNull final TestLifecycle lifecycle) {
lifecycle.doAdhoc(setUpTokensAndAllReceivers());
}

@HapiTest
@DisplayName("fails gracefully with null parameters")
final Stream<DynamicTest> idVariantsTreatedAsExpected() {
final var account = "account";
return hapiTest(
cryptoCreate(account),
tokenAssociate(account, FUNGIBLE_TOKEN),
cryptoTransfer(moving(10, FUNGIBLE_TOKEN).between(OWNER, account)),
tokenAirdrop(moving(10, FUNGIBLE_TOKEN).between(account, RECEIVER_WITH_0_AUTO_ASSOCIATIONS))
.payingWith(account),
submitModified(withSuccessivelyVariedBodyIds(), () -> tokenCancelAirdrop(
pendingAirdrop(account, RECEIVER_WITH_0_AUTO_ASSOCIATIONS, FUNGIBLE_TOKEN))
.payingWith(account)));
}

@HapiTest
@DisplayName("not created NFT pending airdrop")
final Stream<DynamicTest> cancelNotCreatedNFTPendingAirdrop() {
Expand Down
Loading