Skip to content

Commit

Permalink
chore: Fix HashMap in token service CryptoTransfer to `LinkedHash…
Browse files Browse the repository at this point in the history
…Map` (#10361)

Signed-off-by: Neeharika-Sompalli <neeharika.sompalli@swirldslabs.com>
  • Loading branch information
Neeharika-Sompalli authored Dec 7, 2023
1 parent 3d59673 commit 8510491
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -68,8 +68,8 @@ public void doIn(@NonNull final TransferContext transferContext) {
final var accountStore = handleContext.writableStore(WritableAccountStore.class);

// two maps for aggregating the changes to the token balances and allowances.
final Map<EntityIDPair, Long> aggregatedFungibleTokenChanges = new HashMap<>();
final Map<EntityIDPair, Long> allowanceTransfers = new HashMap<>();
final Map<EntityIDPair, Long> aggregatedFungibleTokenChanges = new LinkedHashMap<>();
final Map<EntityIDPair, Long> allowanceTransfers = new LinkedHashMap<>();

// Look at all fungible token transfers and put into aggregatedFungibleTokenChanges map.
// Also, put any transfers happening with allowances in allowanceTransfers map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class AdjustHbarChangesStep extends BaseTokenHandler implements TransferStep {
Expand All @@ -53,9 +53,9 @@ public void doIn(@NonNull final TransferContext transferContext) {

final var accountStore = transferContext.getHandleContext().writableStore(WritableAccountStore.class);
// Aggregate all the hbar balances from the changes. It also includes allowance transfer amounts
final Map<AccountID, Long> netHbarTransfers = new HashMap<>();
final Map<AccountID, Long> netHbarTransfers = new LinkedHashMap<>();
// Allowance transfers is only for negative amounts, it is used to reduce allowance for the spender
final Map<AccountID, Long> allowanceTransfers = new HashMap<>();
final Map<AccountID, Long> allowanceTransfers = new LinkedHashMap<>();
for (final var aa : op.transfersOrElse(TransferList.DEFAULT).accountAmountsOrElse(Collections.emptyList())) {
netHbarTransfers.merge(aa.accountID(), aa.amount(), Long::sum);
if (aa.isApproval() && aa.amount() < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import com.hedera.hapi.node.base.Duration;
import com.hedera.hapi.node.base.Key;
import com.hedera.hapi.node.base.ResponseCodeEnum;
import com.hedera.hapi.node.base.TokenID;
import com.hedera.hapi.node.state.primitives.ProtoBytes;
import com.hedera.hapi.node.token.CryptoCreateTransactionBody;
import com.hedera.hapi.node.token.CryptoUpdateTransactionBody;
import com.hedera.hapi.node.transaction.TransactionBody;
Expand All @@ -42,17 +40,10 @@
import com.hedera.node.config.data.AccountsConfig;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class AutoAccountCreator {
private WritableAccountStore accountStore;
private HandleContext handleContext;
// checks tokenAliasMap if the change consists an alias that is already used in previous
// iteration of the token transfer list. This map is used to count number of
// maxAutoAssociations needed on auto created account
protected final Map<ProtoBytes, Set<TokenID>> tokenAliasMap = new HashMap<>();
private static final CryptoUpdateTransactionBody.Builder UPDATE_TXN_BODY_BUILDER =
CryptoUpdateTransactionBody.newBuilder()
.key(Key.newBuilder().ecdsaSecp256k1(Bytes.EMPTY).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import com.hedera.hapi.node.token.CryptoTransferTransactionBody;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -46,7 +46,7 @@ public class EnsureAliasesStep implements TransferStep {
// Temporary token transfer resolutions map containing the token transfers to alias, is needed to check if
// an alias is repeated. It is allowed to be repeated in multiple token transfer lists, but not in a single
// token transfer list
private final Map<Bytes, AccountID> tokenTransferResolutions = new HashMap<>();
private final Map<Bytes, AccountID> tokenTransferResolutions = new LinkedHashMap<>();

public EnsureAliasesStep(@NonNull final CryptoTransferTransactionBody op) {
this.op = requireNonNull(op);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.hedera.node.config.data.TokensConfig;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -46,7 +46,7 @@ public class TransferContextImpl implements TransferContext {
private final HandleContext context;
private int numAutoCreations;
private int numLazyCreations;
private final Map<Bytes, AccountID> resolutions = new HashMap<>();
private final Map<Bytes, AccountID> resolutions = new LinkedHashMap<>();
private final AutoCreationConfig autoCreationConfig;
private final LazyCreationConfig lazyCreationConfig;
private final TokensConfig tokensConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.hedera.hapi.node.transaction.FixedFee;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -146,7 +145,7 @@ private static void addHtsAdjustment(
* @return The list of credits
*/
public static Map<AccountID, Long> getFungibleTokenCredits(final Map<AccountID, Long> tokenIdChanges) {
final var credits = new HashMap<AccountID, Long>();
final var credits = new LinkedHashMap<AccountID, Long>();
for (final var entry : tokenIdChanges.entrySet()) {
final var account = entry.getKey();
final var amount = entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -56,9 +55,9 @@ public AssessmentResult(

inputHbarAdjustments = buildHbarTransferMap(inputHbarTransfers);

htsAdjustments = new HashMap<>();
hbarAdjustments = new HashMap<>();
royaltiesPaid = new HashSet<>();
htsAdjustments = new LinkedHashMap<>();
hbarAdjustments = new LinkedHashMap<>();
royaltiesPaid = new LinkedHashSet<>();
assessedCustomFees = new ArrayList<>();
}

Expand Down Expand Up @@ -104,7 +103,7 @@ public Map<AccountID, Long> getInputHbarAdjustments() {

private Map<TokenID, Map<AccountID, Long>> buildFungibleTokenTransferMap(
final List<TokenTransferList> tokenTransfers) {
final var fungibleTransfersMap = new HashMap<TokenID, Map<AccountID, Long>>();
final var fungibleTransfersMap = new LinkedHashMap<TokenID, Map<AccountID, Long>>();
for (final var xfer : tokenTransfers) {
final var tokenId = xfer.token();
final var fungibleTokenTransfers = xfer.transfersOrElse(emptyList());
Expand All @@ -121,7 +120,7 @@ private Map<TokenID, Map<AccountID, Long>> buildFungibleTokenTransferMap(
}

private Map<AccountID, Long> buildHbarTransferMap(@NonNull final List<AccountAmount> hbarTransfers) {
final var adjustments = new HashMap<AccountID, Long>();
final var adjustments = new LinkedHashMap<AccountID, Long>();
for (final var aa : hbarTransfers) {
adjustments.put(aa.accountID(), aa.amount());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.hedera.hapi.node.transaction.FractionalFee;
import com.hedera.node.app.spi.workflows.HandleException;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
Expand Down Expand Up @@ -180,7 +180,7 @@ private Map<AccountID, Long> filteredByExemptions(
@NonNull final Map<AccountID, Long> creditsForToken,
@NonNull final CustomFeeMeta feeMeta,
@NonNull final CustomFee fee) {
final var filteredCredits = new HashMap<AccountID, Long>();
final var filteredCredits = new LinkedHashMap<AccountID, Long>();
for (final var entry : creditsForToken.entrySet()) {
final var account = entry.getKey();
final var amount = entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ private HapiSpec getsInsufficientPayerBalanceIfSendingAccountCanPayEverythingBut
.gas(gasToOffer)
.payingWith(civilian)
.balance(maxSendable.get())
// because this fails depending on the previous operation reaching
// consensus before the current operation or after, since we have added
// deferStatusResolution
.hasPrecheckFrom(OK, INSUFFICIENT_PAYER_BALANCE)
.hasKnownStatus(INSUFFICIENT_PAYER_BALANCE)));
}

Expand Down

0 comments on commit 8510491

Please sign in to comment.