Skip to content

Commit 08005db

Browse files
fix: tokenAssociate throws IndexOutOfBoundsException (#12453)
Signed-off-by: Zhivko Kelchev <[email protected]> Co-authored-by: Valentin Tronkov <[email protected]>
1 parent e585287 commit 08005db

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/BaseTokenHandler.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,22 @@ protected void createAndLinkTokenRels(
173173
// Link the new token relations to the account
174174
linkTokenRels(account, newTokenRels, tokenRelStore);
175175

176-
// Now replace the account's old head token number with the new head token number. This is
177-
// how we link the new tokenRels to the account
178-
final var firstOfNewTokenRels = newTokenRels.get(0);
179-
final var updatedAcct = account.copyBuilder()
180-
// replace the head token number with the first token number of the new tokenRels
181-
.headTokenId(firstOfNewTokenRels.tokenId())
182-
// and also update the account's total number of token associations
183-
.numberAssociations(account.numberAssociations() + newTokenRels.size())
184-
.build();
185-
186-
// Save the results
187-
accountStore.put(updatedAcct);
188-
newTokenRels.forEach(tokenRelStore::put);
176+
// FUTURE - We may need to return a proper error status when tokens are empty
177+
if (!newTokenRels.isEmpty()) {
178+
// Now replace the account's old head token number with the new head token number. This is
179+
// how we link the new tokenRels to the account
180+
final var firstOfNewTokenRels = newTokenRels.get(0);
181+
final var updatedAcct = account.copyBuilder()
182+
// replace the head token number with the first token number of the new tokenRels
183+
.headTokenId(firstOfNewTokenRels.tokenId())
184+
// and also update the account's total number of token associations
185+
.numberAssociations(account.numberAssociations() + newTokenRels.size())
186+
.build();
187+
188+
// Save the results
189+
accountStore.put(updatedAcct);
190+
newTokenRels.forEach(tokenRelStore::put);
191+
}
189192
}
190193

191194
/**

hedera-node/test-clients/src/main/java/com/hedera/services/bdd/suites/contract/precompile/AssociatePrecompileSuite.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_REVERT_EXECUTED;
5454
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_ACCOUNT_ID;
5555
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_TOKEN_ID;
56-
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.REVERTED_SUCCESS;
5756
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.SUCCESS;
5857
import static com.hederahashgraph.api.proto.java.TokenType.FUNGIBLE_COMMON;
5958
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -398,7 +397,9 @@ final HapiSpec associateTokensNegativeScenarios() {
398397
NEGATIVE_ASSOCIATIONS_CONTRACT,
399398
"associateTokensWithEmptyTokensArray",
400399
accountAddress.get())
401-
.hasKnownStatus(CONTRACT_REVERT_EXECUTED)
400+
// match mono behaviour, this is a successful call, but it should not associate any
401+
// tokens
402+
.hasKnownStatus(SUCCESS)
402403
.gas(GAS_TO_OFFER)
403404
.signingWith(ACCOUNT)
404405
.via(nonExistingTokenArray)
@@ -447,9 +448,7 @@ final HapiSpec associateTokensNegativeScenarios() {
447448
CONTRACT_REVERT_EXECUTED,
448449
recordWith().status(INVALID_ACCOUNT_ID)),
449450
childRecordsCheck(
450-
nonExistingTokenArray,
451-
CONTRACT_REVERT_EXECUTED,
452-
recordWith().status(REVERTED_SUCCESS)),
451+
nonExistingTokenArray, SUCCESS, recordWith().status(SUCCESS)),
453452
childRecordsCheck(
454453
someNonExistingTokenArray, SUCCESS, recordWith().status(SUCCESS)),
455454
childRecordsCheck(

0 commit comments

Comments
 (0)