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: reload config from saved state #11341

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -39,6 +39,7 @@
import static com.hedera.node.app.state.merkle.MerkleSchemaRegistry.isSoOrdered;
import static com.hedera.node.app.throttle.ThrottleAccumulator.ThrottleType.BACKEND_THROTTLE;
import static com.hedera.node.app.throttle.ThrottleAccumulator.ThrottleType.FRONTEND_THROTTLE;
import static com.hedera.node.app.util.FileUtilities.observePropertiesAndPermissions;
import static com.hedera.node.app.util.HederaAsciiArt.HEDERA;
import static com.swirlds.platform.system.InitTrigger.EVENT_STREAM_RECOVERY;
import static com.swirlds.platform.system.InitTrigger.GENESIS;
Expand Down Expand Up @@ -1074,6 +1075,7 @@ private void initializeForTrigger(
// the various migration methods may depend on configuration to do their work
logger.info("Initializing Reconnect configuration");
this.configProvider = new ConfigProviderImpl(false);
observePropertiesAndPermissions(state, configProvider.getConfiguration(), configProvider::update);
logConfiguration();

logger.info("Initializing ThrottleManager");
Expand Down Expand Up @@ -1107,7 +1109,6 @@ private void initializeForTrigger(
initializeExchangeRateManager(state);
initializeFeeManager(state);
initializeThrottles(state);
// TODO We may need to update the config with the latest version in file 121
}

/*==================================================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
package com.hedera.node.app.util;

import static com.hedera.node.app.service.file.impl.FileServiceImpl.BLOBS_KEY;
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.base.FileID;
import com.hedera.hapi.node.state.file.File;
import com.hedera.node.app.service.file.FileService;
import com.hedera.node.app.state.HederaState;
import com.hedera.node.config.data.FilesConfig;
import com.hedera.node.config.data.HederaConfig;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.config.api.Configuration;
import edu.umd.cs.findbugs.annotations.NonNull;

public class FileUtilities {
Expand All @@ -38,4 +42,58 @@ public static Bytes getFileContent(@NonNull final HederaState state, @NonNull fi
final var file = filesMap.get(fileID);
return file != null ? file.contents() : Bytes.EMPTY;
}

/**
* Observes the properties and permissions of the network from a saved state.
*/
public interface SpecialFilesObserver {
/**
* Accepts the properties and permissions of the network from a saved state.
*
* @param properties the raw bytes of the properties file
* @param permissions the raw bytes of the permissions file
*/
void acceptPropertiesAndPermissions(Bytes properties, Bytes permissions);
}

/**
* Given a state, configuration, and observer, observes the properties and
* permissions of the network.
*
* @param state the state to observe
* @param config the configuration to use
* @param observer the observer to notify
*/
public static void observePropertiesAndPermissions(
@NonNull final HederaState state,
@NonNull final Configuration config,
@NonNull final SpecialFilesObserver observer) {
requireNonNull(state);
requireNonNull(config);
requireNonNull(observer);
final var filesConfig = config.getConfigData(FilesConfig.class);
observePropertiesAndPermissions(
state,
FileUtilities.createFileID(filesConfig.networkProperties(), config),
FileUtilities.createFileID(filesConfig.hapiPermissions(), config),
observer);
}

private static void observePropertiesAndPermissions(
@NonNull final HederaState state,
@NonNull final FileID propertiesId,
@NonNull final FileID permissionsId,
@NonNull final SpecialFilesObserver observer) {
observer.acceptPropertiesAndPermissions(
getFileContent(state, propertiesId), getFileContent(state, permissionsId));
}

private static FileID createFileID(final long fileNum, @NonNull final Configuration configuration) {
final var hederaConfig = configuration.getConfigData(HederaConfig.class);
return FileID.newBuilder()
.realmNum(hederaConfig.realm())
.shardNum(hederaConfig.shard())
.fileNum(fileNum)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.hedera.node.app.workflows.handle;

import static com.hedera.hapi.node.base.ResponseCodeEnum.SUCCESS;
import static com.hedera.node.app.util.FileUtilities.observePropertiesAndPermissions;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

Expand All @@ -33,7 +34,6 @@
import com.hedera.node.app.throttle.ThrottleManager;
import com.hedera.node.app.util.FileUtilities;
import com.hedera.node.config.data.FilesConfig;
import com.hedera.node.config.data.HederaConfig;
import com.hedera.node.config.data.LedgerConfig;
import com.hedera.pbj.runtime.ParseException;
import com.hedera.pbj.runtime.io.buffer.Bytes;
Expand Down Expand Up @@ -115,31 +115,23 @@ public ResponseCodeEnum handleTxBody(@NonNull final HederaState state, @NonNull

// If it is a special file, call the updater.
// We load the file only, if there is an updater for it.
final var config = configuration.getConfigData(FilesConfig.class);
final var filesConfig = configuration.getConfigData(FilesConfig.class);

if (fileNum == config.feeSchedules()) {
if (fileNum == filesConfig.feeSchedules()) {
return feeManager.update(FileUtilities.getFileContent(state, fileID));
} else if (fileNum == config.exchangeRates()) {
} else if (fileNum == filesConfig.exchangeRates()) {
exchangeRateManager.update(FileUtilities.getFileContent(state, fileID), payer);
} else if (fileNum == config.networkProperties()) {
final var networkProperties = FileUtilities.getFileContent(state, fileID);
final var permissions =
FileUtilities.getFileContent(state, createFileID(config.hapiPermissions(), configuration));
configProvider.update(networkProperties, permissions);
logContentsOf("Network properties", networkProperties);
} else if (fileNum == filesConfig.networkProperties()) {
updateConfig(configuration, ConfigType.NETWORK_PROPERTIES, state);
backendThrottle.applyGasConfig();
frontendThrottle.applyGasConfig();

// Updating the multiplier source to use the new gas throttle
// values that are coming from the network properties
congestionMultipliers.resetExpectations();
} else if (fileNum == config.hapiPermissions()) {
final var networkProperties =
FileUtilities.getFileContent(state, createFileID(config.networkProperties(), configuration));
final var permissions = FileUtilities.getFileContent(state, fileID);
configProvider.update(networkProperties, permissions);
logContentsOf("API permissions", permissions);
} else if (fileNum == config.throttleDefinitions()) {
} else if (fileNum == filesConfig.hapiPermissions()) {
updateConfig(configuration, ConfigType.API_PERMISSIONS, state);
} else if (fileNum == filesConfig.throttleDefinitions()) {
final var result = throttleManager.update(FileUtilities.getFileContent(state, fileID));
backendThrottle.rebuildFor(throttleManager.throttleDefinitions());
frontendThrottle.rebuildFor(throttleManager.throttleDefinitions());
Expand All @@ -151,6 +143,25 @@ public ResponseCodeEnum handleTxBody(@NonNull final HederaState state, @NonNull
return SUCCESS;
}

private enum ConfigType {
NETWORK_PROPERTIES,
API_PERMISSIONS,
}

private void updateConfig(
@NonNull final Configuration configuration,
@NonNull final ConfigType configType,
@NonNull final HederaState state) {
observePropertiesAndPermissions(state, configuration, (properties, permissions) -> {
configProvider.update(properties, permissions);
if (configType == ConfigType.NETWORK_PROPERTIES) {
logContentsOf("Network properties", properties);
} else {
logContentsOf("API permissions", permissions);
}
});
}

private void logContentsOf(@NonNull final String configFileName, @NonNull final Bytes contents) {
try {
final var configList = ServicesConfigurationList.PROTOBUF.parseStrict(contents.toReadableSequentialData());
Expand All @@ -165,13 +176,4 @@ private void logContentsOf(@NonNull final String configFileName, @NonNull final
// If this isn't parseable we won't have updated anything, also don't log
}
}

private FileID createFileID(final long fileNum, @NonNull final Configuration configuration) {
final var hederaConfig = configuration.getConfigData(HederaConfig.class);
return FileID.newBuilder()
.realmNum(hederaConfig.realm())
.shardNum(hederaConfig.shard())
.fileNum(fileNum)
.build();
}
}
Loading