Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Replace KVState.size() with ENTITY_COUNTS usage #17436

Draft
wants to merge 4 commits into
base: 17316-D-add-entity-counts
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ public Node get(final long nodeId) {
return nodesState.get(EntityNumber.newBuilder().number(nodeId).build());
}

/**
* Returns the number of topics in the state.
* @return the number of topics in the state
*/
public long sizeOfState() {
return nodesState.size();
}

protected <T extends ReadableKVState<EntityNumber, Node>> T nodesState() {
return (T) nodesState;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -81,15 +81,6 @@ public Node getForModify(final long nodeId) {
.getForModify(EntityNumber.newBuilder().number(nodeId).build());
}

/**
* Returns the number of nodes in the state.
* @return the number of nodes in the state
*/
@Override
public long sizeOfState() {
return nodesState().size();
}

/**
* Returns the set of nodes modified in existing state.
* @return the set of nodes modified in existing state
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,7 @@
import com.hedera.node.app.service.token.ReadableAccountStore;
import com.hedera.node.app.spi.fees.FeeContext;
import com.hedera.node.app.spi.fees.Fees;
import com.hedera.node.app.spi.ids.ReadableEntityIdStore;
import com.hedera.node.app.spi.workflows.HandleContext;
import com.hedera.node.app.spi.workflows.PreCheckException;
import com.hedera.node.app.spi.workflows.PreHandleContext;
Expand Down Expand Up @@ -100,9 +101,10 @@ public void handle(@NonNull final HandleContext handleContext) {
final var storeFactory = handleContext.storeFactory();
final var nodeStore = storeFactory.writableStore(WritableNodeStore.class);
final var accountStore = storeFactory.readableStore(ReadableAccountStore.class);
final var entityIdStore = storeFactory.readableStore(ReadableEntityIdStore.class);
final var accountId = op.accountIdOrElse(AccountID.DEFAULT);

validateFalse(nodeStore.sizeOfState() >= nodeConfig.maxNumber(), MAX_NODES_CREATED);
validateFalse(entityIdStore.numNodes() >= nodeConfig.maxNumber(), MAX_NODES_CREATED);
validateTrue(accountStore.contains(accountId), INVALID_NODE_ACCOUNT_ID);
addressBookValidator.validateDescription(op.description(), nodeConfig);
addressBookValidator.validateGossipEndpoint(op.gossipEndpoint(), nodeConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ void nullArgsFail() {
assertThrows(NullPointerException.class, () -> new ReadableNodeStoreImpl(null));
}

@Test
void getSizeOfState() {
final var store = new ReadableNodeStoreImpl(readableStates);
assertEquals(readableStates.get(NODES_KEY).size(), store.sizeOfState());
}

@Test
void keysWorks() {
final var stateBuilder = emptyReadableNodeStateBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
* Copyright (C) 2023-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -232,7 +232,7 @@ void failsWhenMaxNodesExceeds() {
given(handleContext.storeFactory()).willReturn(storeFactory);
given(storeFactory.writableStore(WritableNodeStore.class)).willReturn(writableStore);

assertEquals(1, writableStore.sizeOfState());
// assertEquals(1, writableStore.sizeOfState());
final var msg = assertThrows(HandleException.class, () -> subject.handle(handleContext));
assertEquals(ResponseCodeEnum.MAX_NODES_CREATED, msg.getStatus());
assertEquals(0, writableStore.modifiedNodes().size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,7 +61,7 @@ private AddressBookHelper() {
public static long getNextNodeID(@NonNull final ReadableNodeStore nodeStore) {
requireNonNull(nodeStore);
final long maxNodeId = StreamSupport.stream(
Spliterators.spliterator(nodeStore.keys(), nodeStore.sizeOfState(), DISTINCT), false)
Spliterators.spliteratorUnknownSize(nodeStore.keys(), DISTINCT), false)
.mapToLong(EntityNumber::number)
.max()
.orElse(-1L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ public interface ReadableNodeStore {
@Nullable
Node get(long nodeId);

/**
* Returns the number of nodes in the state.
* @return the number of nodes in the state
*/
long sizeOfState();

/**
* Warms the system by preloading a node into memory
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
* Copyright (C) 2023-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,10 +28,10 @@

import com.hedera.hapi.node.base.HederaFunctionality;
import com.hedera.hapi.node.transaction.TransactionBody;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.ids.ReadableEntityIdStoreImpl;
import com.hedera.node.app.service.contract.impl.state.ContractStateStore;
import com.hedera.node.app.service.file.ReadableFileStore;
import com.hedera.node.app.service.token.*;
import com.hedera.node.app.spi.ids.ReadableEntityIdStore;
import com.hedera.node.app.store.ReadableStoreFactory;
import com.hedera.node.app.throttle.annotations.CryptoTransferThrottleMultiplier;
import com.hedera.node.app.workflows.TransactionInfo;
Expand Down Expand Up @@ -128,11 +128,9 @@ private int roundedAccountPercentUtil(@NonNull final ReadableStoreFactory storeF
final var maxNumOfAccounts =
configuration.getConfigData(AccountsConfig.class).maxNumber();

final var accountsStore = storeFactory.getStore(ReadableAccountStore.class);
final var numAccountsAndContracts = accountsStore.sizeOfAccountState();

final var contractsStore = storeFactory.getStore(ContractStateStore.class);
final var numContracts = contractsStore.getNumBytecodes();
final var entityIdStore = storeFactory.getStore(ReadableEntityIdStore.class);
final var numAccountsAndContracts = entityIdStore.numAccounts();
final var numContracts = entityIdStore.numContractBytecodes();
final var numAccounts = numAccountsAndContracts - numContracts;

return maxNumOfAccounts == 0 ? 100 : (int) ((100 * numAccounts) / maxNumOfAccounts);
Expand All @@ -153,8 +151,8 @@ private int roundedFilePercentUtil(@NonNull final ReadableStoreFactory storeFact
final var configuration = configProvider.getConfiguration();
final var maxNumOfFiles = configuration.getConfigData(FilesConfig.class).maxNumber();

final var fileStore = storeFactory.getStore(ReadableFileStore.class);
final var numOfFiles = fileStore.sizeOfState();
final var entityIdStore = storeFactory.getStore(ReadableEntityIdStore.class);
final var numOfFiles = entityIdStore.numFiles();

return maxNumOfFiles == 0 ? 100 : (int) ((100 * numOfFiles) / maxNumOfFiles);
}
Expand All @@ -163,8 +161,8 @@ private int roundedNftPercentUtil(@NonNull final ReadableStoreFactory storeFacto
final var configuration = configProvider.getConfiguration();
final var maxNumOfNfts = configuration.getConfigData(TokensConfig.class).nftsMaxAllowedMints();

final var nftStore = storeFactory.getStore(ReadableNftStore.class);
final var numOfNfts = nftStore.sizeOfState();
final var entityIdStore = storeFactory.getStore(ReadableEntityIdStore.class);
final var numOfNfts = entityIdStore.numNfts();

return maxNumOfNfts == 0 ? 100 : (int) ((100 * numOfNfts) / maxNumOfNfts);
}
Expand All @@ -174,8 +172,8 @@ private int roundedTokenPercentUtil(@NonNull final ReadableStoreFactory storeFac
final var maxNumOfTokens =
configuration.getConfigData(TokensConfig.class).maxNumber();

final var tokenStore = storeFactory.getStore(ReadableTokenStore.class);
final var numOfTokens = tokenStore.sizeOfState();
final var entityIdStore = storeFactory.getStore(ReadableEntityIdStore.class);
final var numOfTokens = entityIdStore.numTokens();

return maxNumOfTokens == 0 ? 100 : (int) ((100 * numOfTokens) / maxNumOfTokens);
}
Expand All @@ -185,8 +183,8 @@ private int roundedTokenRelPercentUtil(@NonNull final ReadableStoreFactory store
final var maxNumOfTokenRels =
configuration.getConfigData(TokensConfig.class).maxAggregateRels();

final var tokenRelStore = storeFactory.getStore(ReadableTokenRelationStore.class);
final var numOfTokensRels = tokenRelStore.sizeOfState();
final var entityIdStore = storeFactory.getStore(ReadableEntityIdStore.class);
final var numOfTokensRels = entityIdStore.numTokenRelations();

return maxNumOfTokenRels == 0 ? 100 : (int) ((100 * numOfTokensRels) / maxNumOfTokenRels);
}
Expand All @@ -196,8 +194,8 @@ private int roundedTopicPercentUtil(@NonNull final ReadableStoreFactory storeFac
final var maxNumberOfTopics =
configuration.getConfigData(TopicsConfig.class).maxNumber();

final var topicStore = storeFactory.getStore(ReadableTopicStore.class);
final var numOfTopics = topicStore.sizeOfState();
final var entityIdStore = storeFactory.getStore(ReadableEntityIdStoreImpl.class);
final var numOfTopics = entityIdStore.numTopics();

return maxNumberOfTopics == 0 ? 100 : (int) ((100 * numOfTopics) / maxNumberOfTopics);
}
Expand All @@ -207,8 +205,8 @@ private int roundedAirdropPercentUtil(@NonNull final ReadableStoreFactory storeF
final var maxNumAirdrops =
configuration.getConfigData(TokensConfig.class).maxAllowedPendingAirdrops();

final var airdropStore = storeFactory.getStore(ReadableAirdropStore.class);
final var numPendingAirdrops = airdropStore.sizeOfState();
final var entityIdStore = storeFactory.getStore(ReadableEntityIdStore.class);
final var numPendingAirdrops = entityIdStore.numAirdrops();

return maxNumAirdrops == 0 ? 100 : (int) ((100 * numPendingAirdrops) / maxNumAirdrops);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
* Copyright (C) 2023-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
* Copyright (C) 2023-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static com.hedera.hapi.node.base.ResponseCodeEnum.BUSY;
import static com.hedera.hapi.node.base.ResponseCodeEnum.FAIL_INVALID;
import static com.hedera.hapi.util.HapiUtils.asTimestamp;
import static com.hedera.node.app.ids.schemas.V0590EntityIdSchema.ENTITY_COUNTS_KEY;
import static com.hedera.node.app.records.schemas.V0490BlockRecordSchema.BLOCK_INFO_STATE_KEY;
import static com.hedera.node.app.service.file.impl.schemas.V0490FileSchema.BLOBS_KEY;
import static com.hedera.node.app.spi.workflows.HandleContext.TransactionCategory.SCHEDULED;
import static com.hedera.node.app.spi.workflows.HandleContext.TransactionCategory.USER;
import static com.hedera.node.app.spi.workflows.record.StreamBuilder.ReversingBehavior.REVERSIBLE;
Expand Down Expand Up @@ -51,6 +51,7 @@
import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.node.base.Transaction;
import com.hedera.hapi.node.state.blockrecords.BlockInfo;
import com.hedera.hapi.node.state.entity.EntityCounts;
import com.hedera.hapi.node.transaction.ExchangeRateSet;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.hedera.hapi.util.HapiUtils;
Expand All @@ -64,14 +65,14 @@
import com.hedera.node.app.hints.impl.WritableHintsStoreImpl;
import com.hedera.node.app.history.HistoryService;
import com.hedera.node.app.history.impl.WritableHistoryStoreImpl;
import com.hedera.node.app.ids.EntityIdService;
import com.hedera.node.app.records.BlockRecordManager;
import com.hedera.node.app.records.BlockRecordService;
import com.hedera.node.app.roster.ActiveRosters;
import com.hedera.node.app.roster.RosterService;
import com.hedera.node.app.service.addressbook.AddressBookService;
import com.hedera.node.app.service.addressbook.impl.WritableNodeStore;
import com.hedera.node.app.service.addressbook.impl.helpers.AddressBookHelper;
import com.hedera.node.app.service.file.FileService;
import com.hedera.node.app.service.schedule.ExecutableTxn;
import com.hedera.node.app.service.schedule.ScheduleService;
import com.hedera.node.app.service.schedule.impl.WritableScheduleStoreImpl;
Expand Down Expand Up @@ -929,9 +930,10 @@ private static void logPreDispatch(@NonNull final UserTxn userTxn) {
* @return the type of the boundary transaction
*/
private TransactionType typeOfBoundary(@NonNull final State state) {
final var files = state.getReadableStates(FileService.NAME).get(BLOBS_KEY);
final var entityCounts =
state.getReadableStates(EntityIdService.NAME).<EntityCounts>getSingleton(ENTITY_COUNTS_KEY);
// The files map is empty only at genesis
if (files.size() == 0) {
if (entityCounts.get().numFiles() == 0) {
return GENESIS_TRANSACTION;
}
final var blockInfo = state.getReadableStates(BlockRecordService.NAME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ public void handleTxBody(
final var nodeStore =
new ReadableNodeStoreImpl(state.getReadableStates(AddressBookService.NAME));
final var candidateRoster = new Roster(StreamSupport.stream(
Spliterators.spliterator(nodeStore.keys(), nodeStore.sizeOfState(), DISTINCT),
false)
Spliterators.spliteratorUnknownSize(nodeStore.keys(), DISTINCT), false)
.mapToLong(EntityNumber::number)
.sorted()
.mapToObj(nodeStore::get)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
* Copyright (C) 2023-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -784,9 +784,9 @@ private static Condition<ReadableStates> content(Map<String, String> expected) {
private static Predicate<ReadableStates> contentCheck(Map<String, String> expected) {
return readableStates -> {
final var actual = readableStates.get(FRUIT_STATE_KEY);
if (expected.size() != actual.size()) {
return false;
}
// if (expected.size() != actual.size()) {
// return false;
// }
for (final var entry : expected.entrySet()) {
if (!Objects.equals(entry.getValue(), actual.get(entry.getKey()))) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
* Copyright (C) 2022-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -82,15 +82,6 @@ public Topic getForModify(@NonNull final TopicID topicID) {
requireNonNull(topicID);
return topicState().getForModify(topicID);
}

/**
* Returns the number of topics in the state.
* @return the number of topics in the state
*/
public long sizeOfState() {
return topicState().size();
}

/**
* Returns the set of topics modified in existing state.
* @return the set of topics modified in existing state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.hedera.node.app.service.consensus.impl.records.ConsensusCreateTopicStreamBuilder;
import com.hedera.node.app.spi.fees.FeeContext;
import com.hedera.node.app.spi.fees.Fees;
import com.hedera.node.app.spi.ids.ReadableEntityIdStore;
import com.hedera.node.app.spi.validation.EntityType;
import com.hedera.node.app.spi.validation.ExpiryMeta;
import com.hedera.node.app.spi.workflows.HandleContext;
Expand Down Expand Up @@ -120,9 +121,9 @@ public void handle(@NonNull final HandleContext handleContext) {
handleContext.attributeValidator().validateKey(op.submitKey());
builder.submitKey(op.submitKey());
}

final var entityIdStore = handleContext.storeFactory().readableStore(ReadableEntityIdStore.class);
/* Validate if the current topic can be created */
if (topicStore.sizeOfState() >= topicConfig.maxNumber()) {
if (entityIdStore.numTopics() >= topicConfig.maxNumber()) {
throw new HandleException(MAX_ENTITIES_IN_PRICE_REGIME_HAVE_BEEN_CREATED);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
* Copyright (C) 2023-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -106,10 +106,4 @@ void constructorCreatesTopicState() {
void nullArgsFail() {
assertThrows(NullPointerException.class, () -> new ReadableTopicStoreImpl(null));
}

@Test
void getSizeOfState() {
final var store = new ReadableTopicStoreImpl(readableStates);
assertEquals(readableStates.get(TOPICS_KEY).size(), store.sizeOfState());
}
}
Loading
Loading