Skip to content

Commit

Permalink
fix: Cherry-Pick: Handle non-existent token IDs in token fee schedule…
Browse files Browse the repository at this point in the history
… updates (#15832)

Signed-off-by: Matt Hess <[email protected]>
  • Loading branch information
mhess-swl authored Oct 7, 2024
1 parent dcb93dc commit 4f6bcfc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.hedera.node.app.hapi.fees.usage.SingletonEstimatorUtils.ESTIMATOR_UTILS;
import static com.hedera.node.app.hapi.fees.usage.token.TokenOpsUsage.LONG_BASIC_ENTITY_ID_SIZE;
import static com.hedera.node.app.spi.workflows.HandleException.validateTrue;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.base.AccountID;
Expand Down Expand Up @@ -192,8 +193,9 @@ public Fees calculateFees(@NonNull final FeeContext feeContext) {
final var effConsTime =
body.transactionIDOrThrow().transactionValidStartOrThrow().seconds();
final var lifetime = Math.max(0, token == null ? 0 : token.expirationSecond() - effConsTime);
final List<CustomFee> customFees = token == null ? emptyList() : token.customFees();

final var existingFeeReprBytes = currentFeeScheduleSize(token.customFees(), tokenOpsUsage);
final var existingFeeReprBytes = currentFeeScheduleSize(customFees, tokenOpsUsage);
final var rbsDelta = ESTIMATOR_UTILS.changeInBsUsage(existingFeeReprBytes, lifetime, newReprBytes, lifetime);
return feeContext
.feeCalculatorFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -309,6 +310,48 @@ public void testCalculateFeesHappyPath() {
assertEquals(calculateFees, Fees.FREE);
}

@Test
@DisplayName("If the requested token ID does not exist, the fee calculation should not throw an exception")
void calculateFeesTokenDoesNotExist() {
TransactionInfo txnInfo = mock(TransactionInfo.class);
FeeManager feeManager = mock(FeeManager.class);
FeeCalculator feeCalculator = mock(FeeCalculator.class);
ReadableStoreFactory storeFactory = mock(ReadableStoreFactory.class);
TransactionBody transactionBody = mock(TransactionBody.class);
TokenFeeScheduleUpdateTransactionBody tokenFeeScheduleUpdateTransactionBody =
mock(TokenFeeScheduleUpdateTransactionBody.class);
TransactionID transactionID = mock(TransactionID.class);

when(feeManager.createFeeCalculator(any(), any(), any(), anyInt(), anyInt(), any(), any(), anyBoolean(), any()))
.thenReturn(feeCalculator);
when(txnInfo.txBody()).thenReturn(transactionBody);
when(transactionBody.tokenFeeScheduleUpdateOrThrow()).thenReturn(tokenFeeScheduleUpdateTransactionBody);
// Any token ID that doesn't exist:
when(tokenFeeScheduleUpdateTransactionBody.tokenIdOrThrow())
.thenReturn(TokenID.newBuilder().tokenNum(1500).build());
when(storeFactory.getStore(ReadableTokenStore.class)).thenReturn(readableTokenStore);
when(transactionBody.transactionIDOrThrow()).thenReturn(transactionID);
when(transactionID.transactionValidStartOrThrow()).thenReturn(consensusTimestamp);
when(txnInfo.signatureMap()).thenReturn(SignatureMap.DEFAULT);
when(feeCalculator.addBytesPerTransaction(anyLong())).thenReturn(feeCalculator);
when(feeCalculator.addRamByteSeconds(anyLong())).thenReturn(feeCalculator);
when(feeCalculator.calculate()).thenReturn(Fees.FREE);

final var feeContext = new FeeContextImpl(
consensusInstant,
txnInfo,
payerKey,
payerId,
feeManager,
storeFactory,
configuration,
null,
-1,
transactionDispatcher);

Assertions.assertThatNoException().isThrownBy(() -> subject.calculateFees(feeContext));
}

private void givenTxn() {
txn = TransactionBody.newBuilder()
.tokenFeeScheduleUpdate(TokenFeeScheduleUpdateTransactionBody.newBuilder()
Expand Down

0 comments on commit 4f6bcfc

Please sign in to comment.