Skip to content

Commit

Permalink
feat: Stop returning token associations and token balances data in qu…
Browse files Browse the repository at this point in the history
…eries as per HIP-367 (#10149)
  • Loading branch information
Neeharika-Sompalli authored Dec 1, 2023
1 parent 282c61e commit a7dea9f
Show file tree
Hide file tree
Showing 31 changed files with 261 additions and 608 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,7 @@ private Optional<FileGetInfoResponse.FileInfo> getFileInfo(final FileID id) {
}

public Optional<CryptoGetInfoResponse.AccountInfo> infoForAccount(
final AccountID id,
final AliasManager aliasManager,
final int maxTokensForAccountInfo,
final RewardCalculator rewardCalculator) {
final AccountID id, final AliasManager aliasManager, final RewardCalculator rewardCalculator) {
final var accountNum = id.getAlias().isEmpty() ? fromAccountId(id) : aliasManager.lookupIdBy(id.getAlias());
final var account = accounts().get(accountNum);
if (account == null) {
Expand All @@ -421,10 +418,6 @@ public Optional<CryptoGetInfoResponse.AccountInfo> infoForAccount(
if (!isOfEvmAddressSize(account.getAlias())) {
info.setAlias(account.getAlias());
}
final var tokenRels = tokenRels(this, account, maxTokensForAccountInfo);
if (!tokenRels.isEmpty()) {
info.addAllTokenRelationships(tokenRels);
}
info.setStakingInfo(stakingInfo(account, rewardCalculator));

return Optional.of(info.build());
Expand Down Expand Up @@ -546,10 +539,7 @@ public long numNftsOwnedBy(AccountID target) {
}

public Optional<ContractGetInfoResponse.ContractInfo> infoForContract(
final ContractID id,
final AliasManager aliasManager,
final int maxTokensForAccountInfo,
final RewardCalculator rewardCalculator) {
final ContractID id, final AliasManager aliasManager, final RewardCalculator rewardCalculator) {
final var contractId = EntityIdUtils.unaliased(id, aliasManager);
final var contract = contracts().get(contractId);
if (contract == null) {
Expand Down Expand Up @@ -578,11 +568,6 @@ public Optional<ContractGetInfoResponse.ContractInfo> infoForContract(
} else {
info.setContractAccountID(asHexedEvmAddress(mirrorId));
}
final var tokenRels = tokenRels(this, contract, maxTokensForAccountInfo);
if (!tokenRels.isEmpty()) {
info.addAllTokenRelationships(tokenRels);
}

info.setStakingInfo(stakingInfo(contract, rewardCalculator));

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public boolean applicableTo(final Query query) {
@Override
public FeeData usageGiven(final Query query, final StateView view, @Nullable final Map<String, Object> queryCtx) {
final var op = query.getContractGetInfo();
final var tentativeInfo = view.infoForContract(
op.getContractID(), aliasManager, dynamicProperties.maxTokensRelsPerInfoQuery(), rewardCalculator);
final var tentativeInfo = view.infoForContract(op.getContractID(), aliasManager, rewardCalculator);
if (tentativeInfo.isPresent()) {
final var info = tentativeInfo.get();
putIfNotNull(queryCtx, CONTRACT_INFO_CTX_KEY, info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.hedera.node.app.hapi.fees.usage.crypto.CryptoOpsUsage;
import com.hedera.node.app.hapi.fees.usage.crypto.ExtantCryptoContext;
import com.hedera.node.app.service.mono.context.primitives.StateView;
import com.hedera.node.app.service.mono.context.properties.GlobalDynamicProperties;
import com.hedera.node.app.service.mono.fees.calculation.QueryResourceUsageEstimator;
import com.hedera.node.app.service.mono.ledger.accounts.AliasManager;
import com.hedera.node.app.service.mono.ledger.accounts.staking.RewardCalculator;
Expand All @@ -37,18 +36,15 @@
public final class GetAccountInfoResourceUsage implements QueryResourceUsageEstimator {
private final CryptoOpsUsage cryptoOpsUsage;
private final AliasManager aliasManager;
private final GlobalDynamicProperties dynamicProperties;
private final RewardCalculator rewardCalculator;

@Inject
public GetAccountInfoResourceUsage(
final CryptoOpsUsage cryptoOpsUsage,
final AliasManager aliasManager,
final GlobalDynamicProperties dynamicProperties,
final RewardCalculator rewardCalculator) {
this.cryptoOpsUsage = cryptoOpsUsage;
this.aliasManager = aliasManager;
this.dynamicProperties = dynamicProperties;
this.rewardCalculator = rewardCalculator;
}

Expand All @@ -62,8 +58,7 @@ public FeeData usageGiven(final Query query, final StateView view, final Map<Str
final var op = query.getCryptoGetInfo();

final var account = op.getAccountID();
final var info = view.infoForAccount(
account, aliasManager, dynamicProperties.maxTokensRelsPerInfoQuery(), rewardCalculator);
final var info = view.infoForAccount(account, aliasManager, rewardCalculator);
/* Given the test in {@code GetAccountInfoAnswer.checkValidity}, this can only be empty
* under the extraordinary circumstance that the desired account expired during the query
* answer flow (which will now fail downstream with an appropriate status code); so
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static com.hederahashgraph.api.proto.java.ResponseType.COST_ANSWER;

import com.hedera.node.app.service.mono.context.primitives.StateView;
import com.hedera.node.app.service.mono.context.properties.GlobalDynamicProperties;
import com.hedera.node.app.service.mono.ledger.accounts.AliasManager;
import com.hedera.node.app.service.mono.ledger.accounts.staking.RewardCalculator;
import com.hedera.node.app.service.mono.queries.AnswerService;
Expand All @@ -44,24 +43,23 @@
import javax.inject.Inject;
import javax.inject.Singleton;

/**
* Implements the {@link HederaFunctionality#ContractGetInfo} query handler.
* The token relationships field is deprecated and is no more returned by this query.
*/
@Singleton
public class GetContractInfoAnswer implements AnswerService {
public static final String CONTRACT_INFO_CTX_KEY = GetContractInfoAnswer.class.getSimpleName() + "_contractInfo";

private final AliasManager aliasManager;
private final OptionValidator validator;
private final GlobalDynamicProperties dynamicProperties;
private final RewardCalculator rewardCalculator;

@Inject
public GetContractInfoAnswer(
final AliasManager aliasManager,
final OptionValidator validator,
final GlobalDynamicProperties dynamicProperties,
final RewardCalculator rewardCalculator) {
final AliasManager aliasManager, final OptionValidator validator, final RewardCalculator rewardCalculator) {
this.aliasManager = aliasManager;
this.validator = validator;
this.dynamicProperties = dynamicProperties;
this.rewardCalculator = rewardCalculator;
}

Expand Down Expand Up @@ -155,8 +153,7 @@ private void setAnswerOnly(
response.setContractInfo((ContractGetInfoResponse.ContractInfo) ctx.get(CONTRACT_INFO_CTX_KEY));
}
} else {
final var info = view.infoForContract(
op.getContractID(), aliasManager, dynamicProperties.maxTokensRelsPerInfoQuery(), rewardCalculator);
final var info = view.infoForContract(op.getContractID(), aliasManager, rewardCalculator);
if (info.isEmpty()) {
response.setHeader(answerOnlyHeader(INVALID_CONTRACT_ID));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package com.hedera.node.app.service.mono.queries.crypto;

import static com.hedera.node.app.service.mono.context.primitives.StateView.doBoundedIteration;
import static com.hedera.node.app.service.mono.utils.EntityIdUtils.asAccount;
import static com.hedera.node.app.service.mono.utils.EntityIdUtils.isAlias;
import static com.hederahashgraph.api.proto.java.HederaFunctionality.CryptoGetAccountBalance;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_ACCOUNT_ID;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.OK;

import com.hedera.node.app.service.mono.context.primitives.StateView;
import com.hedera.node.app.service.mono.context.properties.GlobalDynamicProperties;
import com.hedera.node.app.service.mono.ledger.accounts.AliasManager;
import com.hedera.node.app.service.mono.queries.AnswerService;
import com.hedera.node.app.service.mono.state.migration.AccountStorageAdapter;
Expand All @@ -39,27 +37,25 @@
import com.hederahashgraph.api.proto.java.Query;
import com.hederahashgraph.api.proto.java.Response;
import com.hederahashgraph.api.proto.java.ResponseCodeEnum;
import com.hederahashgraph.api.proto.java.TokenBalance;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Objects;
import java.util.Optional;
import javax.inject.Inject;
import javax.inject.Singleton;

/**
* Implements the {@link HederaFunctionality#CryptoGetAccountBalance} query handler.
* The token balances field is deprecated and is no more returned by this query.
*/
@Singleton
public class GetAccountBalanceAnswer implements AnswerService {
private final AliasManager aliasManager;
private final OptionValidator optionValidator;
private final GlobalDynamicProperties dynamicProperties;

@Inject
public GetAccountBalanceAnswer(
final AliasManager aliasManager,
final OptionValidator optionValidator,
final GlobalDynamicProperties dynamicProperties) {
public GetAccountBalanceAnswer(final AliasManager aliasManager, final OptionValidator optionValidator) {
this.aliasManager = aliasManager;
this.optionValidator = optionValidator;
this.dynamicProperties = dynamicProperties;
}

@Override
Expand Down Expand Up @@ -94,18 +90,6 @@ public Response responseGiven(
final var key = EntityNum.fromAccountId(id);
final var account = accounts.get(key);
opAnswer.setBalance(account.getBalance());
final var maxRels = dynamicProperties.maxTokensRelsPerInfoQuery();
final var firstRel = account.getLatestAssociation();
doBoundedIteration(
view.tokenAssociations(),
view.tokens(),
firstRel,
maxRels,
(token, rel) -> opAnswer.addTokenBalances(TokenBalance.newBuilder()
.setTokenId(token.grpcId())
.setDecimals(token.decimals())
.setBalance(rel.getBalance())
.build()));
}

return Response.newBuilder().setCryptogetAccountBalance(opAnswer).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static com.hederahashgraph.api.proto.java.ResponseType.COST_ANSWER;

import com.hedera.node.app.service.mono.context.primitives.StateView;
import com.hedera.node.app.service.mono.context.properties.GlobalDynamicProperties;
import com.hedera.node.app.service.mono.ledger.accounts.AliasManager;
import com.hedera.node.app.service.mono.ledger.accounts.staking.RewardCalculator;
import com.hedera.node.app.service.mono.queries.AnswerService;
Expand All @@ -44,22 +43,23 @@
import javax.inject.Inject;
import javax.inject.Singleton;

/**
* Implements the {@link HederaFunctionality#CryptoGetInfo} query handler.
* This does not return token relationships anymore, since the field is deprecated.
*/
@Singleton
public class GetAccountInfoAnswer implements AnswerService {
private final OptionValidator optionValidator;
private final AliasManager aliasManager;
private final GlobalDynamicProperties dynamicProperties;
private final RewardCalculator rewardCalculator;

@Inject
public GetAccountInfoAnswer(
final OptionValidator optionValidator,
final AliasManager aliasManager,
final GlobalDynamicProperties dynamicProperties,
final RewardCalculator rewardCalculator) {
this.optionValidator = optionValidator;
this.aliasManager = aliasManager;
this.dynamicProperties = dynamicProperties;
this.rewardCalculator = rewardCalculator;
}

Expand All @@ -85,9 +85,8 @@ public Response responseGiven(
response.setHeader(costAnswerHeader(OK, cost));
} else {
final AccountID id = op.getAccountID();
final var optionalInfo = Objects.requireNonNull(view)
.infoForAccount(
id, aliasManager, dynamicProperties.maxTokensRelsPerInfoQuery(), rewardCalculator);
final var optionalInfo =
Objects.requireNonNull(view).infoForAccount(id, aliasManager, rewardCalculator);
if (optionalInfo.isPresent()) {
response.setHeader(answerOnlyHeader(OK));
response.setAccountInfo(optionalInfo.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.hedera.node.app.service.mono.queries.meta;

import static com.hederahashgraph.api.proto.java.HederaFunctionality.GetAccountDetails;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.ACCOUNT_DELETED;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.FAIL_INVALID;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.OK;
import static com.hederahashgraph.api.proto.java.ResponseType.COST_ANSWER;
Expand Down Expand Up @@ -62,7 +63,8 @@ public ResponseCodeEnum checkValidity(final Query query, final StateView view) {
final AccountID id = query.getAccountDetails().getAccountId();
final var entityNum =
id.getAlias().isEmpty() ? EntityNum.fromAccountId(id) : aliasManager.lookupIdBy(id.getAlias());
return optionValidator.queryableAccountOrContractStatus(entityNum, view.accounts());
final var validity = optionValidator.queryableAccountOrContractStatus(entityNum, view.accounts());
return (validity == ACCOUNT_DELETED) ? OK : validity;
}

@Override
Expand Down
Loading

0 comments on commit a7dea9f

Please sign in to comment.