Skip to content

Commit b7b7d55

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

File tree

1 file changed

+19
-15
lines changed
  • hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers

1 file changed

+19
-15
lines changed

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

+19-15
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,26 @@ protected void createAndLinkTokenRels(
170170
@NonNull final WritableTokenRelationStore tokenRelStore) {
171171
// create list of token relations to be added
172172
final var newTokenRels = createTokenRelsToAccount(account, tokens);
173-
// Link the new token relations to the account
174-
linkTokenRels(account, newTokenRels, tokenRelStore);
175-
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();
185173

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

191195
/**

0 commit comments

Comments
 (0)