From 0b7bf52698874259d8b170b6c25ee30b9126075e Mon Sep 17 00:00:00 2001 From: Kim Rader Date: Mon, 23 Dec 2024 15:12:02 -0800 Subject: [PATCH 1/3] Add test for TokenCancelAirdrop to fail fast with invalid parameters Signed-off-by: Kim Rader --- .../handlers/TokenCancelAirdropHandler.java | 8 ++++---- .../suites/hip904/TokenCancelAirdropTest.java | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java b/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java index c0e7ae1675a4..74aacb92ba20 100644 --- a/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java +++ b/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java @@ -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; @@ -78,7 +78,7 @@ public void preHandle(@NonNull final PreHandleContext context) throws PreCheckEx 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); } } @@ -93,8 +93,8 @@ public void pureChecks(@NonNull final TransactionBody txn) throws PreCheckExcept 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(); diff --git a/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/suites/hip904/TokenCancelAirdropTest.java b/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/suites/hip904/TokenCancelAirdropTest.java index a0cdb2336ac2..1d48c78231c7 100644 --- a/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/suites/hip904/TokenCancelAirdropTest.java +++ b/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/suites/hip904/TokenCancelAirdropTest.java @@ -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; @@ -78,6 +80,21 @@ static void beforeAll(@NonNull final TestLifecycle lifecycle) { lifecycle.doAdhoc(setUpTokensAndAllReceivers()); } + @HapiTest + @DisplayName("fails gracefully with null parameters") + final Stream 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 cancelNotCreatedNFTPendingAirdrop() { From 8571408c5fa4458d2c52e0d7f833a9c01d05e0c0 Mon Sep 17 00:00:00 2001 From: Kim Rader Date: Thu, 2 Jan 2025 08:19:45 -0800 Subject: [PATCH 2/3] Fix tests Signed-off-by: Kim Rader --- .../handlers/TokenCancelAirdropHandlerPureChecksTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/TokenCancelAirdropHandlerPureChecksTest.java b/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/TokenCancelAirdropHandlerPureChecksTest.java index 5c214f9ee091..15faf13bed80 100644 --- a/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/TokenCancelAirdropHandlerPureChecksTest.java +++ b/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/TokenCancelAirdropHandlerPureChecksTest.java @@ -18,6 +18,8 @@ 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_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; @@ -44,7 +46,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); @@ -110,7 +112,7 @@ void handleCancelAirdropWithMissingReceiver() { Assertions.assertThatThrownBy(() -> subject.pureChecks(txn)) .isInstanceOf(PreCheckException.class) - .has(responseCode(INVALID_ACCOUNT_ID)); + .has(responseCode(INVALID_PENDING_AIRDROP_ID)); } @Test @@ -124,7 +126,7 @@ void handleCancelAirdropWithMissingSender() { Assertions.assertThatThrownBy(() -> subject.pureChecks(txn)) .isInstanceOf(PreCheckException.class) - .has(responseCode(INVALID_ACCOUNT_ID)); + .has(responseCode(INVALID_PENDING_AIRDROP_ID)); } @Test From a2df7f710a8c47934217cf7d58ed52724339aaa6 Mon Sep 17 00:00:00 2001 From: Kim Rader Date: Thu, 2 Jan 2025 11:06:32 -0800 Subject: [PATCH 3/3] Spotless Signed-off-by: Kim Rader --- .../token/impl/handlers/TokenCancelAirdropHandler.java | 2 +- .../handlers/TokenCancelAirdropHandlerPureChecksTest.java | 4 +--- .../services/bdd/suites/hip904/TokenCancelAirdropTest.java | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java b/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java index 74aacb92ba20..17593f449f9a 100644 --- a/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java +++ b/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java @@ -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. diff --git a/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/TokenCancelAirdropHandlerPureChecksTest.java b/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/TokenCancelAirdropHandlerPureChecksTest.java index 15faf13bed80..fffd7e660925 100644 --- a/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/TokenCancelAirdropHandlerPureChecksTest.java +++ b/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/TokenCancelAirdropHandlerPureChecksTest.java @@ -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. @@ -17,9 +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_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; diff --git a/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/suites/hip904/TokenCancelAirdropTest.java b/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/suites/hip904/TokenCancelAirdropTest.java index 1d48c78231c7..2f6e41e3e576 100644 --- a/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/suites/hip904/TokenCancelAirdropTest.java +++ b/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/suites/hip904/TokenCancelAirdropTest.java @@ -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.