Skip to content

Commit

Permalink
Addressed PR comments (#9672)
Browse files Browse the repository at this point in the history
Signed-off-by: ilko-iliev-lime <ilko.iliev@limechain.tech>
  • Loading branch information
ilko-iliev-lime committed Nov 8, 2023
1 parent ac8aa65 commit 0b1a1a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import edu.umd.cs.findbugs.annotations.Nullable;

public class NftTokenInfoCall extends AbstractNonRevertibleTokenViewCall {
private static final long TREASURY_OWNER_NUM = 0L;
private final Configuration configuration;
private final boolean isStaticCall;
private final long serialNumber;
Expand All @@ -65,25 +64,29 @@ public NftTokenInfoCall(
@Override
protected @NonNull FullResult resultOfViewingToken(@NonNull final Token token) {
requireNonNull(token);
return fullResultsFor(SUCCESS, gasCalculator.viewGasRequirement(), token);
final var nft = enhancement
.nativeOperations()
.getNft(token.tokenIdOrElse(ZERO_TOKEN_ID).tokenNum(), serialNumber);
final var status = nft != null ? SUCCESS : ResponseCodeEnum.INVALID_TOKEN_NFT_SERIAL_NUMBER;
return fullResultsFor(status, gasCalculator.viewGasRequirement(), token, nft);
}

@Override
protected @NonNull FullResult viewCallResultWith(
@NonNull final ResponseCodeEnum status, final long gasRequirement) {
return fullResultsFor(status, gasRequirement, Token.DEFAULT);
return fullResultsFor(status, gasRequirement, Token.DEFAULT, null);
}

private @NonNull FullResult fullResultsFor(
@NonNull final ResponseCodeEnum status, final long gasRequirement, @NonNull final Token token) {
@NonNull final ResponseCodeEnum status,
final long gasRequirement,
@NonNull final Token token,
@Nullable final Nft nft) {
requireNonNull(status);
requireNonNull(token);

final var ledgerConfig = configuration.getConfigData(LedgerConfig.class);
final var ledgerId = Bytes.wrap(ledgerConfig.id().toByteArray()).toString();
final var nft = enhancement
.nativeOperations()
.getNft(token.tokenIdOrElse(ZERO_TOKEN_ID).tokenNum(), serialNumber);
// @Future remove to revert #9074 after modularization is completed
if (isStaticCall && (status != SUCCESS || nft == null)) {
return revertResult(status, gasCalculator.viewGasRequirement());
Expand All @@ -105,9 +108,6 @@ public NftTokenInfoCall(

private Account getOwnerAccount(Nft nft, Token token) {
final var explicitId = nft.ownerIdOrElse(AccountID.DEFAULT);
if (explicitId.account().kind() == AccountID.AccountOneOfType.UNSET) {
return null;
}
final long ownerNum;
if (explicitId.hasAccountNum()) {
ownerNum = explicitId.accountNumOrThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ void returnsNftTokenInfoStatusForPresentToken() {
result.getOutput());
}

@Test
void returnsWhenTryingToFetchTokenWithInvalidSerialNumber() {
when(config.getConfigData(LedgerConfig.class)).thenReturn(ledgerConfig);
when(ledgerConfig.id()).thenReturn(com.hedera.pbj.runtime.io.buffer.Bytes.fromHex(LEDGER_ID));
when(nativeOperations.getNft(FUNGIBLE_EVERYTHING_TOKEN.tokenId().tokenNum(), 2L))
.thenReturn(null);
when(nativeOperations.getAccount(1234L)).thenReturn(A_NEW_ACCOUNT);

final var subject =
new NftTokenInfoCall(gasCalculator, mockEnhancement(), false, FUNGIBLE_EVERYTHING_TOKEN, 2L, config);
final var result = subject.execute().fullResult().result();
assertEquals(MessageFrame.State.COMPLETED_SUCCESS, result.getState());
}

@Test
void revertsWhenTryingToFetchMissingTokenStaticCall() {
when(config.getConfigData(LedgerConfig.class)).thenReturn(ledgerConfig);
Expand All @@ -107,7 +121,7 @@ void revertsWhenTryingToFetchMissingTokenStaticCall() {
}

@Test
void revertsWhenFetchingTokenWithNoOwnerAccount() {
void revertsWhenTryingToFetchTokenWithNoOwnerAccount() {
when(config.getConfigData(LedgerConfig.class)).thenReturn(ledgerConfig);
when(ledgerConfig.id()).thenReturn(com.hedera.pbj.runtime.io.buffer.Bytes.fromHex(LEDGER_ID));
when(nativeOperations.getNft(FUNGIBLE_EVERYTHING_TOKEN.tokenId().tokenNum(), 2L))
Expand Down

0 comments on commit 0b1a1a0

Please sign in to comment.