From 43c28b82ecba23f7c5dd1183d42ed5de58f15276 Mon Sep 17 00:00:00 2001 From: georgi-l95 Date: Tue, 20 Aug 2024 20:21:59 +0300 Subject: [PATCH 01/10] feat: create module Signed-off-by: georgi-l95 --- .../com.hedera.block.simulator.gradle.kts | 26 +++++++++++++ settings.gradle.kts | 1 + simulator/build.gradle.kts | 37 +++++++++++++++++++ .../block/simulator/BlockStreamSimulator.java | 30 +++++++++++++++ .../simulator/config/ConfigProvider.java | 9 +++++ .../simulator/config/ConfigProviderImpl.java | 13 +++++++ .../config/SimulatorConfigExtension.java | 29 +++++++++++++++ .../config/data/BlockStreamConfig.java | 12 ++++++ .../simulator/config/data/GrpcConfig.java | 13 +++++++ .../config/types/GenerationMode.java | 6 +++ simulator/src/main/java/module-info.java | 14 +++++++ 11 files changed, 190 insertions(+) create mode 100644 buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts create mode 100644 simulator/build.gradle.kts create mode 100644 simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java create mode 100644 simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java create mode 100644 simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java create mode 100644 simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java create mode 100644 simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java create mode 100644 simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java create mode 100644 simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java create mode 100644 simulator/src/main/java/module-info.java diff --git a/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts b/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts new file mode 100644 index 000000000..5bb202c3b --- /dev/null +++ b/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +plugins { + id("application") + id("com.hedera.block.conventions") + id("me.champeau.jmh") +} + +val maven = publishing.publications.create("maven") { from(components["java"]) } + +signing.sign(maven) + diff --git a/settings.gradle.kts b/settings.gradle.kts index 4770f3462..5cdb9b67e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -21,6 +21,7 @@ plugins { // Include the subprojects include(":stream") include(":server") +include(":simulator") includeBuild(".") // https://github.com/gradle/gradle/issues/21490#issuecomment-1458887481 diff --git a/simulator/build.gradle.kts b/simulator/build.gradle.kts new file mode 100644 index 000000000..82724b927 --- /dev/null +++ b/simulator/build.gradle.kts @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2022-2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +plugins { + id("application") + id("com.hedera.block.simulator") +} + +application { + mainModule = "com.hedera.block.simulator" + mainClass = "com.hedera.block.simulator.BlockStreamSimulator" +} + +mainModuleInfo { + annotationProcessor("com.google.auto.service.processor") + runtimeOnly("com.swirlds.config.impl") +} + +//testModuleInfo { +// requires("org.junit.jupiter.api") +// requires("org.mockito") +// requires("org.mockito.junit.jupiter") +// requiresStatic("com.github.spotbugs.annotations") +//} \ No newline at end of file diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java new file mode 100644 index 000000000..75881a744 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator; + +public class BlockStreamSimulator { + private static final System.Logger LOGGER = System.getLogger(BlockStreamSimulator.class.getName()); + + public BlockStreamSimulator() { + + } + + public static void main(String[] args) { + LOGGER.log(System.Logger.Level.INFO, "Starting Block Stream Simulator"); + + } +} \ No newline at end of file diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java new file mode 100644 index 000000000..fe35b2607 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java @@ -0,0 +1,9 @@ +package com.hedera.block.simulator.config; + +import com.swirlds.config.api.Configuration; +import edu.umd.cs.findbugs.annotations.NonNull; + +public interface ConfigProvider { + @NonNull + Configuration getConfiguration(); +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java new file mode 100644 index 000000000..b2af6ba57 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java @@ -0,0 +1,13 @@ +package com.hedera.block.simulator.config; + +import com.swirlds.config.api.Configuration; + +public class ConfigProviderImpl { +// private static final System.Logger LOGGER = System.getLogger(ConfigProviderImpl.class.getName()); +// private final Configuration configuration; +// +// public ConfigProviderImpl() { +// final var builder = +// } + +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java b/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java new file mode 100644 index 000000000..496b1e5fb --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java @@ -0,0 +1,29 @@ +package com.hedera.block.simulator.config; + +import com.google.auto.service.AutoService; +import com.hedera.block.simulator.config.data.BlockStreamConfig; +import com.hedera.block.simulator.config.data.GrpcConfig; +import com.swirlds.config.api.ConfigurationExtension; +import edu.umd.cs.findbugs.annotations.NonNull; +import java.util.Set; + +/** + * Sets up configuration for services. + */ +@AutoService(ConfigurationExtension.class) +public class SimulatorConfigExtension implements ConfigurationExtension { + + /** Explicitly defined constructor. */ + public SimulatorConfigExtension() { + super(); + } + + @NonNull + @Override + public Set> getConfigDataTypes() { + return Set.of( + BlockStreamConfig.class, + GrpcConfig.class + ); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java new file mode 100644 index 000000000..3c01543be --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java @@ -0,0 +1,12 @@ +package com.hedera.block.simulator.config.data; + +import com.hedera.block.simulator.config.types.GenerationMode; +import com.swirlds.config.api.ConfigData; +import com.swirlds.config.api.ConfigProperty; + +//TODO add block dir option to generate from directory +@ConfigData("blockStream") +public record BlockStreamConfig(@ConfigProperty(defaultValue = "SELF") GenerationMode generationMode) { + +} + diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java new file mode 100644 index 000000000..100d4d690 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java @@ -0,0 +1,13 @@ +package com.hedera.block.simulator.config.data; + +import com.swirlds.config.api.ConfigData; +import com.swirlds.config.api.ConfigProperty; +import com.swirlds.config.api.validation.annotation.Max; +import com.swirlds.config.api.validation.annotation.Min; + +@ConfigData("grpc") +public record GrpcConfig( + @ConfigProperty(defaultValue = "localhost") @Min(0) @Max(65535) String serverAddress, + @ConfigProperty(defaultValue = "8080") @Min(0) @Max(65535) int port +) { +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java new file mode 100644 index 000000000..705ad94e6 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java @@ -0,0 +1,6 @@ +package com.hedera.block.simulator.config.types; + +public enum GenerationMode { + FOLDER, + SELF +} diff --git a/simulator/src/main/java/module-info.java b/simulator/src/main/java/module-info.java new file mode 100644 index 000000000..af5294edb --- /dev/null +++ b/simulator/src/main/java/module-info.java @@ -0,0 +1,14 @@ +import com.hedera.block.simulator.config.SimulatorConfigExtension; + +module com.hedera.block.simulator { + exports com.hedera.block.simulator.config.data to + com.swirlds.config.impl; + + requires static com.github.spotbugs.annotations; + requires static com.google.auto.service; + requires static com.swirlds.config.api; + requires static com.swirlds.config.extensions; + + provides com.swirlds.config.api.ConfigurationExtension with + SimulatorConfigExtension; +} \ No newline at end of file From 711ad21535468521ea4960d76609d474eee1e81c Mon Sep 17 00:00:00 2001 From: georgi-l95 Date: Tue, 20 Aug 2024 23:43:19 +0300 Subject: [PATCH 02/10] feat: add configuration Signed-off-by: georgi-l95 --- .../com.hedera.block.simulator.gradle.kts | 1 - simulator/build.gradle.kts | 9 +-- .../block/simulator/BlockStreamSimulator.java | 16 ++++-- .../simulator/config/ConfigProvider.java | 16 ++++++ .../simulator/config/ConfigProviderImpl.java | 57 ++++++++++++++++--- .../config/SimulatorConfigExtension.java | 25 +++++--- .../config/data/BlockStreamConfig.java | 24 ++++++-- .../simulator/config/data/GrpcConfig.java | 20 ++++++- .../config/types/GenerationMode.java | 16 ++++++ simulator/src/main/java/module-info.java | 9 +-- 10 files changed, 153 insertions(+), 40 deletions(-) diff --git a/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts b/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts index 5bb202c3b..f5361b330 100644 --- a/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts +++ b/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts @@ -23,4 +23,3 @@ plugins { val maven = publishing.publications.create("maven") { from(components["java"]) } signing.sign(maven) - diff --git a/simulator/build.gradle.kts b/simulator/build.gradle.kts index 82724b927..97732c985 100644 --- a/simulator/build.gradle.kts +++ b/simulator/build.gradle.kts @@ -19,6 +19,8 @@ plugins { id("com.hedera.block.simulator") } +description = "Hedera Block Stream Simulator" + application { mainModule = "com.hedera.block.simulator" mainClass = "com.hedera.block.simulator.BlockStreamSimulator" @@ -28,10 +30,3 @@ mainModuleInfo { annotationProcessor("com.google.auto.service.processor") runtimeOnly("com.swirlds.config.impl") } - -//testModuleInfo { -// requires("org.junit.jupiter.api") -// requires("org.mockito") -// requires("org.mockito.junit.jupiter") -// requiresStatic("com.github.spotbugs.annotations") -//} \ No newline at end of file diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java index 75881a744..3c0e09538 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -16,15 +16,19 @@ package com.hedera.block.simulator; -public class BlockStreamSimulator { - private static final System.Logger LOGGER = System.getLogger(BlockStreamSimulator.class.getName()); +import com.hedera.block.simulator.config.ConfigProvider; +import com.hedera.block.simulator.config.ConfigProviderImpl; +import com.hedera.block.simulator.config.data.GrpcConfig; - public BlockStreamSimulator() { +public class BlockStreamSimulator { + private static final System.Logger LOGGER = + System.getLogger(BlockStreamSimulator.class.getName()); - } + public BlockStreamSimulator() {} public static void main(String[] args) { LOGGER.log(System.Logger.Level.INFO, "Starting Block Stream Simulator"); - + ConfigProvider configProvider = new ConfigProviderImpl(); + LOGGER.log(System.Logger.Level.INFO, configProvider.getConfiguration().getConfigData(GrpcConfig.class).port()); } -} \ No newline at end of file +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java index fe35b2607..b7f471993 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.hedera.block.simulator.config; import com.swirlds.config.api.Configuration; diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java index b2af6ba57..c9ae511b0 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java @@ -1,13 +1,56 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.hedera.block.simulator.config; import com.swirlds.config.api.Configuration; +import com.swirlds.config.api.ConfigurationBuilder; +import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource; +import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource; +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ConfigProviderImpl implements ConfigProvider { + private static final System.Logger LOGGER = + System.getLogger(ConfigProviderImpl.class.getName()); + private final Configuration configuration; + + public ConfigProviderImpl() { + final var builder = createConfigurationBuilder(); + // addFileSources(builder, useGenesisSource); + // if (overrideValues != null) { + // overrideValues.forEach(builder::withValue); + // } + configuration = builder.build(); + } + + @NonNull + @Override + public Configuration getConfiguration() { + return configuration; + } -public class ConfigProviderImpl { -// private static final System.Logger LOGGER = System.getLogger(ConfigProviderImpl.class.getName()); -// private final Configuration configuration; -// -// public ConfigProviderImpl() { -// final var builder = -// } + private ConfigurationBuilder createConfigurationBuilder() { + final ConfigurationBuilder builder = ConfigurationBuilder.create(); + builder + .withSource(SystemEnvironmentConfigSource.getInstance()) + .withSource(SystemPropertiesConfigSource.getInstance()) + // .withSource(new + // ClasspathFileConfigSource(Path.of(APPLICATION_PROPERTIES))) + .autoDiscoverExtensions(); + return builder; + } } diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java b/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java index 496b1e5fb..61f526ca8 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.hedera.block.simulator.config; import com.google.auto.service.AutoService; @@ -7,9 +23,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import java.util.Set; -/** - * Sets up configuration for services. - */ +/** Sets up configuration for services. */ @AutoService(ConfigurationExtension.class) public class SimulatorConfigExtension implements ConfigurationExtension { @@ -21,9 +35,6 @@ public SimulatorConfigExtension() { @NonNull @Override public Set> getConfigDataTypes() { - return Set.of( - BlockStreamConfig.class, - GrpcConfig.class - ); + return Set.of(BlockStreamConfig.class, GrpcConfig.class); } } diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java index 3c01543be..89b4f049d 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java @@ -1,12 +1,26 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.hedera.block.simulator.config.data; import com.hedera.block.simulator.config.types.GenerationMode; import com.swirlds.config.api.ConfigData; import com.swirlds.config.api.ConfigProperty; -//TODO add block dir option to generate from directory +// TODO add block dir option to generate from directory @ConfigData("blockStream") -public record BlockStreamConfig(@ConfigProperty(defaultValue = "SELF") GenerationMode generationMode) { - -} - +public record BlockStreamConfig( + @ConfigProperty(defaultValue = "SELF") GenerationMode generationMode) {} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java index 100d4d690..cf8d4b3ef 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.hedera.block.simulator.config.data; import com.swirlds.config.api.ConfigData; @@ -8,6 +24,4 @@ @ConfigData("grpc") public record GrpcConfig( @ConfigProperty(defaultValue = "localhost") @Min(0) @Max(65535) String serverAddress, - @ConfigProperty(defaultValue = "8080") @Min(0) @Max(65535) int port -) { -} + @ConfigProperty(defaultValue = "8080") @Min(0) @Max(65535) int port) {} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java index 705ad94e6..9666b5274 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.hedera.block.simulator.config.types; public enum GenerationMode { diff --git a/simulator/src/main/java/module-info.java b/simulator/src/main/java/module-info.java index af5294edb..9a06e6d70 100644 --- a/simulator/src/main/java/module-info.java +++ b/simulator/src/main/java/module-info.java @@ -2,13 +2,14 @@ module com.hedera.block.simulator { exports com.hedera.block.simulator.config.data to - com.swirlds.config.impl; + com.swirlds.config.impl, com.swirlds.config.extensions; requires static com.github.spotbugs.annotations; requires static com.google.auto.service; - requires static com.swirlds.config.api; - requires static com.swirlds.config.extensions; + requires com.swirlds.config.api; + requires com.swirlds.config.extensions; provides com.swirlds.config.api.ConfigurationExtension with SimulatorConfigExtension; -} \ No newline at end of file + +} From 01d7a5f5a82bd7f72e1c3d2aa4b22a308959d994 Mon Sep 17 00:00:00 2001 From: georgi-l95 Date: Mon, 26 Aug 2024 11:37:11 +0300 Subject: [PATCH 03/10] feat: add block stream manager Signed-off-by: georgi-l95 --- .../block/simulator/BlockStreamSimulator.java | 15 +++++++++++--- .../generator/BlockStreamManager.java | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java index 3c0e09538..2e193767f 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -19,16 +19,25 @@ import com.hedera.block.simulator.config.ConfigProvider; import com.hedera.block.simulator.config.ConfigProviderImpl; import com.hedera.block.simulator.config.data.GrpcConfig; +import java.lang.System.Logger; public class BlockStreamSimulator { - private static final System.Logger LOGGER = + private static final Logger LOGGER = System.getLogger(BlockStreamSimulator.class.getName()); public BlockStreamSimulator() {} public static void main(String[] args) { - LOGGER.log(System.Logger.Level.INFO, "Starting Block Stream Simulator"); + BlockStreamSimulator blockStreamSimulator = new BlockStreamSimulator(); + blockStreamSimulator.start(); + } + + public void start() { ConfigProvider configProvider = new ConfigProviderImpl(); - LOGGER.log(System.Logger.Level.INFO, configProvider.getConfiguration().getConfigData(GrpcConfig.class).port()); + LOGGER.log(Logger.Level.INFO, "Starting Block Stream Simulator"); + } + + public void stop() { + } } diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java new file mode 100644 index 000000000..a30e46f95 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator.generator; + +public interface BlockStreamManager { +} From 6f29e725a47422d49ebf65382aba5d686f83db2a Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Fri, 30 Aug 2024 15:53:18 -0600 Subject: [PATCH 04/10] Add Dagger, Configuration and PBJ Types Signed-off-by: Alfredo Gutierrez --- simulator/build.gradle.kts | 1 + .../block/simulator/BlockStreamSimulator.java | 38 +++++++++++++----- ...lockStreamSimulatorInjectionComponent.java | 40 +++++++++++++++++++ .../config/ConfigInjectionModule.java | 40 +++++++++++++++++++ .../simulator/config/ConfigProviderImpl.java | 20 ++++++---- .../generator/BlockStreamManager.java | 4 ++ .../generator/GeneratorInjectionModule.java | 29 ++++++++++++++ .../generator/MockBlockStreamManagerImpl.java | 31 ++++++++++++++ simulator/src/main/java/module-info.java | 8 +++- simulator/src/main/resources/app.properties | 1 + 10 files changed, 193 insertions(+), 19 deletions(-) create mode 100644 simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java create mode 100644 simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java create mode 100644 simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java create mode 100644 simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java create mode 100644 simulator/src/main/resources/app.properties diff --git a/simulator/build.gradle.kts b/simulator/build.gradle.kts index 97732c985..5cbbcab7f 100644 --- a/simulator/build.gradle.kts +++ b/simulator/build.gradle.kts @@ -27,6 +27,7 @@ application { } mainModuleInfo { + annotationProcessor("dagger.compiler") annotationProcessor("com.google.auto.service.processor") runtimeOnly("com.swirlds.config.impl") } diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java index 2e193767f..997967354 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -18,26 +18,46 @@ import com.hedera.block.simulator.config.ConfigProvider; import com.hedera.block.simulator.config.ConfigProviderImpl; -import com.hedera.block.simulator.config.data.GrpcConfig; +import com.hedera.block.simulator.generator.BlockStreamManager; +import com.swirlds.config.api.Configuration; +import edu.umd.cs.findbugs.annotations.NonNull; import java.lang.System.Logger; +import javax.inject.Inject; public class BlockStreamSimulator { - private static final Logger LOGGER = - System.getLogger(BlockStreamSimulator.class.getName()); + private static final Logger LOGGER = System.getLogger(BlockStreamSimulator.class.getName()); - public BlockStreamSimulator() {} + Configuration configuration; + BlockStreamManager blockStreamManager; + + @Inject + public BlockStreamSimulator( + @NonNull Configuration configuration, @NonNull BlockStreamManager blockStreamManager) { + this.configuration = configuration; + this.blockStreamManager = blockStreamManager; + } public static void main(String[] args) { - BlockStreamSimulator blockStreamSimulator = new BlockStreamSimulator(); + + LOGGER.log(Logger.Level.INFO, "Starting Block Stream Simulator"); + + ConfigProvider configProvider = new ConfigProviderImpl(); + Configuration configuration = configProvider.getConfiguration(); + BlockStreamSimulatorInjectionComponent DIComponent = + DaggerBlockStreamSimulatorInjectionComponent.factory().create(configuration); + + BlockStreamSimulator blockStreamSimulator = DIComponent.getBlockStreamSimulator(); blockStreamSimulator.start(); } public void start() { - ConfigProvider configProvider = new ConfigProviderImpl(); - LOGGER.log(Logger.Level.INFO, "Starting Block Stream Simulator"); - } - public void stop() { + // use blockStreamManager to get block stream + + // use PublishStreamGrpcClient to stream it to the block-node. + LOGGER.log(Logger.Level.INFO, "Block Stream Simulator has started"); } + + public void stop() {} } diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java new file mode 100644 index 000000000..80fef1ffd --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator; + +import com.hedera.block.simulator.config.ConfigInjectionModule; +import com.hedera.block.simulator.generator.GeneratorInjectionModule; +import com.swirlds.config.api.Configuration; +import dagger.BindsInstance; +import dagger.Component; +import javax.inject.Singleton; + +@Singleton +@Component( + modules = { + ConfigInjectionModule.class, + GeneratorInjectionModule.class, + }) +public interface BlockStreamSimulatorInjectionComponent { + + BlockStreamSimulator getBlockStreamSimulator(); + + @Component.Factory + interface Factory { + BlockStreamSimulatorInjectionComponent create(@BindsInstance Configuration configuration); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java new file mode 100644 index 000000000..71091302a --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator.config; + +import com.hedera.block.simulator.config.data.BlockStreamConfig; +import com.hedera.block.simulator.config.data.GrpcConfig; +import com.swirlds.config.api.Configuration; +import dagger.Module; +import dagger.Provides; +import javax.inject.Singleton; + +@Module +public interface ConfigInjectionModule { + + @Singleton + @Provides + static BlockStreamConfig provideBlockStreamConfig(Configuration configuration) { + return configuration.getConfigData(BlockStreamConfig.class); + } + + @Singleton + @Provides + static GrpcConfig provideGrpcConfig(Configuration configuration) { + return configuration.getConfigData(GrpcConfig.class); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java index c9ae511b0..2577e58db 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java @@ -18,9 +18,12 @@ import com.swirlds.config.api.Configuration; import com.swirlds.config.api.ConfigurationBuilder; +import com.swirlds.config.extensions.sources.ClasspathFileConfigSource; import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource; import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource; import edu.umd.cs.findbugs.annotations.NonNull; +import java.io.IOException; +import java.nio.file.Path; public class ConfigProviderImpl implements ConfigProvider { private static final System.Logger LOGGER = @@ -43,14 +46,15 @@ public Configuration getConfiguration() { } private ConfigurationBuilder createConfigurationBuilder() { - final ConfigurationBuilder builder = ConfigurationBuilder.create(); + try { + return ConfigurationBuilder.create() + .withSource(SystemEnvironmentConfigSource.getInstance()) + .withSource(SystemPropertiesConfigSource.getInstance()) + .withSource(new ClasspathFileConfigSource(Path.of("app.properties"))) + .autoDiscoverExtensions(); - builder - .withSource(SystemEnvironmentConfigSource.getInstance()) - .withSource(SystemPropertiesConfigSource.getInstance()) - // .withSource(new - // ClasspathFileConfigSource(Path.of(APPLICATION_PROPERTIES))) - .autoDiscoverExtensions(); - return builder; + } catch (IOException e) { + throw new RuntimeException(e); + } } } diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java index a30e46f95..7818e58ff 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java @@ -16,5 +16,9 @@ package com.hedera.block.simulator.generator; +import com.hedera.hapi.block.stream.Block; + public interface BlockStreamManager { + + Block getNextBlock(); } diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java b/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java new file mode 100644 index 000000000..4764e9fdc --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator.generator; + +import dagger.Binds; +import dagger.Module; +import javax.inject.Singleton; + +@Module +public interface GeneratorInjectionModule { + + @Singleton + @Binds + BlockStreamManager bindBlockStreamManager(MockBlockStreamManagerImpl blockStreamManager); +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java b/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java new file mode 100644 index 000000000..57ccd8afc --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator.generator; + +import com.hedera.hapi.block.stream.Block; +import javax.inject.Inject; + +public class MockBlockStreamManagerImpl implements BlockStreamManager { + + @Inject + public MockBlockStreamManagerImpl() {} + + @Override + public Block getNextBlock() { + return Block.newBuilder().build(); + } +} diff --git a/simulator/src/main/java/module-info.java b/simulator/src/main/java/module-info.java index 9a06e6d70..ece9c9b46 100644 --- a/simulator/src/main/java/module-info.java +++ b/simulator/src/main/java/module-info.java @@ -2,14 +2,18 @@ module com.hedera.block.simulator { exports com.hedera.block.simulator.config.data to - com.swirlds.config.impl, com.swirlds.config.extensions; + com.swirlds.config.impl, + com.swirlds.config.extensions; requires static com.github.spotbugs.annotations; requires static com.google.auto.service; + requires com.hedera.block.stream; + // requires com.hedera.pbj.runtime; // leaving it here since it will be needed soon. requires com.swirlds.config.api; requires com.swirlds.config.extensions; + requires dagger; + requires javax.inject; provides com.swirlds.config.api.ConfigurationExtension with SimulatorConfigExtension; - } diff --git a/simulator/src/main/resources/app.properties b/simulator/src/main/resources/app.properties new file mode 100644 index 000000000..2ce7c6a76 --- /dev/null +++ b/simulator/src/main/resources/app.properties @@ -0,0 +1 @@ +blockStream.folderRootPath=/Users/user/Projects/hedera-block-node/simulator/src/main/resources/block-0.0.3/ From 9a964356bef4bee52f036a1a28970fdbbf88e247 Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Fri, 30 Aug 2024 16:08:58 -0600 Subject: [PATCH 05/10] Adding JUnit to project and first test Signed-off-by: Alfredo Gutierrez --- simulator/build.gradle.kts | 7 +++ .../block/simulator/BlockStreamSimulator.java | 7 ++- .../simulator/BlockStreamSimulatorTest.java | 60 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java diff --git a/simulator/build.gradle.kts b/simulator/build.gradle.kts index 5cbbcab7f..54d8ab765 100644 --- a/simulator/build.gradle.kts +++ b/simulator/build.gradle.kts @@ -31,3 +31,10 @@ mainModuleInfo { annotationProcessor("com.google.auto.service.processor") runtimeOnly("com.swirlds.config.impl") } + +testModuleInfo { + requires("org.junit.jupiter.api") + requires("org.mockito") + requires("org.mockito.junit.jupiter") + requiresStatic("com.github.spotbugs.annotations") +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java index 997967354..05f433eab 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -29,6 +29,7 @@ public class BlockStreamSimulator { Configuration configuration; BlockStreamManager blockStreamManager; + boolean isRunning = false; @Inject public BlockStreamSimulator( @@ -55,9 +56,13 @@ public void start() { // use blockStreamManager to get block stream // use PublishStreamGrpcClient to stream it to the block-node. - + isRunning = true; LOGGER.log(Logger.Level.INFO, "Block Stream Simulator has started"); } + public boolean isRunning() { + return isRunning; + } + public void stop() {} } diff --git a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java new file mode 100644 index 000000000..71bd1b17d --- /dev/null +++ b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator; + +import static org.junit.jupiter.api.Assertions.*; + +import com.hedera.block.simulator.generator.BlockStreamManager; +import com.swirlds.config.api.Configuration; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class BlockStreamSimulatorTest { + + @Mock private Configuration configuration; + + @Mock private BlockStreamManager blockStreamManager; + + @InjectMocks private BlockStreamSimulator blockStreamSimulator; + + @BeforeEach + void setUp() { + blockStreamSimulator = new BlockStreamSimulator(configuration, blockStreamManager); + } + + @AfterEach + void tearDown() { + blockStreamSimulator.stop(); + } + + @Test + void start_logsStartedMessage() { + blockStreamSimulator.start(); + assertTrue(blockStreamSimulator.isRunning()); + } + + @Test + void stop_doesNotThrowException() { + assertDoesNotThrow(() -> blockStreamSimulator.stop()); + } +} From 5384ffe3603fc2519bfa6bb2ab8923cf927ac234 Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Fri, 30 Aug 2024 16:19:44 -0600 Subject: [PATCH 06/10] Adding missing javadocs and little cleanup Signed-off-by: Alfredo Gutierrez --- .../block/simulator/BlockStreamSimulator.java | 24 ++++++++++++++++++- ...lockStreamSimulatorInjectionComponent.java | 13 ++++++++++ .../config/ConfigInjectionModule.java | 13 ++++++++++ .../simulator/config/ConfigProvider.java | 8 +++++++ .../simulator/config/ConfigProviderImpl.java | 12 ++++++---- .../config/data/BlockStreamConfig.java | 6 ++++- .../simulator/config/data/GrpcConfig.java | 6 +++++ .../config/types/GenerationMode.java | 3 +++ .../generator/BlockStreamManager.java | 6 +++++ .../generator/GeneratorInjectionModule.java | 7 ++++++ .../generator/MockBlockStreamManagerImpl.java | 7 ++++++ simulator/src/main/java/module-info.java | 5 ++-- 12 files changed, 101 insertions(+), 9 deletions(-) diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java index 05f433eab..d7dd10d81 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -24,6 +24,7 @@ import java.lang.System.Logger; import javax.inject.Inject; +/** The BlockStreamSimulator class defines the simulator for the block stream. */ public class BlockStreamSimulator { private static final Logger LOGGER = System.getLogger(BlockStreamSimulator.class.getName()); @@ -31,6 +32,12 @@ public class BlockStreamSimulator { BlockStreamManager blockStreamManager; boolean isRunning = false; + /** + * Creates a new BlockStreamSimulator instance. + * + * @param configuration the configuration to be used by the block stream simulator + * @param blockStreamManager the block stream manager to be used by the block stream simulator + */ @Inject public BlockStreamSimulator( @NonNull Configuration configuration, @NonNull BlockStreamManager blockStreamManager) { @@ -38,6 +45,11 @@ public BlockStreamSimulator( this.blockStreamManager = blockStreamManager; } + /** + * The main entry point for the block stream simulator. + * + * @param args the arguments to be passed to the block stream simulator + */ public static void main(String[] args) { LOGGER.log(Logger.Level.INFO, "Starting Block Stream Simulator"); @@ -51,6 +63,7 @@ public static void main(String[] args) { blockStreamSimulator.start(); } + /** Starts the block stream simulator. */ public void start() { // use blockStreamManager to get block stream @@ -60,9 +73,18 @@ public void start() { LOGGER.log(Logger.Level.INFO, "Block Stream Simulator has started"); } + /** + * Returns whether the block stream simulator is running. + * + * @return true if the block stream simulator is running, false otherwise + */ public boolean isRunning() { return isRunning; } - public void stop() {} + /** Stops the block stream simulator. */ + public void stop() { + isRunning = false; + LOGGER.log(Logger.Level.INFO, "Block Stream Simulator has stopped"); + } } diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java index 80fef1ffd..ef8e4fe8f 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java @@ -23,6 +23,7 @@ import dagger.Component; import javax.inject.Singleton; +/** The component used to inject the block stream simulator into the application. */ @Singleton @Component( modules = { @@ -31,10 +32,22 @@ }) public interface BlockStreamSimulatorInjectionComponent { + /** + * Gets the block stream simulator. + * + * @return the block stream simulator + */ BlockStreamSimulator getBlockStreamSimulator(); + /** The factory used to create the block stream simulator injection component. */ @Component.Factory interface Factory { + /** + * Creates the block stream simulator injection component. + * + * @param configuration the configuration to be used by the block stream simulator + * @return the block stream simulator injection component + */ BlockStreamSimulatorInjectionComponent create(@BindsInstance Configuration configuration); } } diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java index 71091302a..ff2d4efb3 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java @@ -23,15 +23,28 @@ import dagger.Provides; import javax.inject.Singleton; +/** The module used to inject the configuration data into the application. */ @Module public interface ConfigInjectionModule { + /** + * Provides the block stream configuration. + * + * @param configuration the configuration to be used by the block stream + * @return the block stream configuration + */ @Singleton @Provides static BlockStreamConfig provideBlockStreamConfig(Configuration configuration) { return configuration.getConfigData(BlockStreamConfig.class); } + /** + * Provides the gRPC configuration. + * + * @param configuration the configuration to be used by the gRPC + * @return the gRPC configuration + */ @Singleton @Provides static GrpcConfig provideGrpcConfig(Configuration configuration) { diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java index b7f471993..5a22e44c7 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java @@ -19,7 +19,15 @@ import com.swirlds.config.api.Configuration; import edu.umd.cs.findbugs.annotations.NonNull; +/** The provider used to get the configuration for the simulator. */ public interface ConfigProvider { + + /** + * Gets the configuration for the simulator, specifies the mode of generation, the mode of + * simulator and every configurable detail. + * + * @return the configuration + */ @NonNull Configuration getConfiguration(); } diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java index 2577e58db..ddd89ce7a 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java @@ -25,20 +25,23 @@ import java.io.IOException; import java.nio.file.Path; +/** The ConfigProviderImpl class provides the implementation for the configuration provider. */ public class ConfigProviderImpl implements ConfigProvider { private static final System.Logger LOGGER = System.getLogger(ConfigProviderImpl.class.getName()); private final Configuration configuration; + /** Creates a new ConfigProviderImpl instance. */ public ConfigProviderImpl() { final var builder = createConfigurationBuilder(); - // addFileSources(builder, useGenesisSource); - // if (overrideValues != null) { - // overrideValues.forEach(builder::withValue); - // } configuration = builder.build(); } + /** + * Returns the configuration. + * + * @return the configuration + */ @NonNull @Override public Configuration getConfiguration() { @@ -54,6 +57,7 @@ private ConfigurationBuilder createConfigurationBuilder() { .autoDiscoverExtensions(); } catch (IOException e) { + LOGGER.log(System.Logger.Level.ERROR, "Failed to create configuration builder", e); throw new RuntimeException(e); } } diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java index 89b4f049d..302c5a00f 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java @@ -20,7 +20,11 @@ import com.swirlds.config.api.ConfigData; import com.swirlds.config.api.ConfigProperty; -// TODO add block dir option to generate from directory +/** + * The BlockStreamConfig class defines the configuration data for the block stream. + * + * @param generationMode the mode of generation for the block stream + */ @ConfigData("blockStream") public record BlockStreamConfig( @ConfigProperty(defaultValue = "SELF") GenerationMode generationMode) {} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java index cf8d4b3ef..abfbd4a61 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java @@ -21,6 +21,12 @@ import com.swirlds.config.api.validation.annotation.Max; import com.swirlds.config.api.validation.annotation.Min; +/** + * The GrpcConfig class defines the configuration data for the gRPC client. + * + * @param serverAddress the address of the gRPC server + * @param port the port of the gRPC server + */ @ConfigData("grpc") public record GrpcConfig( @ConfigProperty(defaultValue = "localhost") @Min(0) @Max(65535) String serverAddress, diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java index 9666b5274..5b637dcb1 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java @@ -16,7 +16,10 @@ package com.hedera.block.simulator.config.types; +/** The GenerationMode enum defines the modes of generation for the block stream. */ public enum GenerationMode { + /** Reads Blocks from a Folder. */ FOLDER, + /** Generates Blocks from rules */ SELF } diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java index 7818e58ff..74e744a4c 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java @@ -18,7 +18,13 @@ import com.hedera.hapi.block.stream.Block; +/** this interface defines the contract for managing the block stream. */ public interface BlockStreamManager { + /** + * Gets the next block in the block stream. + * + * @return the next block in the block stream + */ Block getNextBlock(); } diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java b/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java index 4764e9fdc..95266dfc8 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java @@ -20,9 +20,16 @@ import dagger.Module; import javax.inject.Singleton; +/** The module used to inject the block stream manager. */ @Module public interface GeneratorInjectionModule { + /** + * Provides the block stream manager. + * + * @param blockStreamManager the block stream manager to be used + * @return the block stream manager + */ @Singleton @Binds BlockStreamManager bindBlockStreamManager(MockBlockStreamManagerImpl blockStreamManager); diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java b/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java index 57ccd8afc..7f4ae0d3a 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java @@ -19,11 +19,18 @@ import com.hedera.hapi.block.stream.Block; import javax.inject.Inject; +/** A mock implementation of the BlockStreamManager interface. */ public class MockBlockStreamManagerImpl implements BlockStreamManager { + /** Constructs a new MockBlockStreamManagerImpl. */ @Inject public MockBlockStreamManagerImpl() {} + /** + * Gets the next block in the block stream. + * + * @return the next block in the block stream + */ @Override public Block getNextBlock() { return Block.newBuilder().build(); diff --git a/simulator/src/main/java/module-info.java b/simulator/src/main/java/module-info.java index ece9c9b46..9051122a0 100644 --- a/simulator/src/main/java/module-info.java +++ b/simulator/src/main/java/module-info.java @@ -1,9 +1,8 @@ import com.hedera.block.simulator.config.SimulatorConfigExtension; +/** Runtime module of the simulator. */ module com.hedera.block.simulator { - exports com.hedera.block.simulator.config.data to - com.swirlds.config.impl, - com.swirlds.config.extensions; + exports com.hedera.block.simulator.config.data; requires static com.github.spotbugs.annotations; requires static com.google.auto.service; From ef7f0a9d623a2b899e69d348fd308e94621bff3a Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Tue, 3 Sep 2024 17:19:35 -0600 Subject: [PATCH 07/10] Removed ConfigProvider, since we will be injecting the config we don't need a provider. And refactored code around that, also divided the main entry point from the ActualApp so we can only ignore the entry point in the unit test coverage. Added Missing unit tests Signed-off-by: Alfredo Gutierrez --- codecov.yml | 1 + .../block/simulator/BlockStreamSimulator.java | 72 ++++++------------- .../simulator/BlockStreamSimulatorApp.java | 71 ++++++++++++++++++ ...lockStreamSimulatorInjectionComponent.java | 2 +- .../simulator/config/ConfigProviderImpl.java | 64 ----------------- .../simulator/BlockStreamSimulatorTest.java | 6 +- .../config/ConfigInjectionModuleTest.java | 66 +++++++++++++++++ .../config/types/GenerationModeTest.java} | 25 ++++--- .../generator/BlockStreamManagerTest.java | 30 ++++++++ 9 files changed, 205 insertions(+), 132 deletions(-) create mode 100644 simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java delete mode 100644 simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java create mode 100644 simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java rename simulator/src/{main/java/com/hedera/block/simulator/config/ConfigProvider.java => test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java} (55%) create mode 100644 simulator/src/test/java/com/hedera/block/simulator/generator/BlockStreamManagerTest.java diff --git a/codecov.yml b/codecov.yml index e21c93660..620cc39a3 100644 --- a/codecov.yml +++ b/codecov.yml @@ -21,3 +21,4 @@ coverage: ignore: - "server/src/main/java/com/hedera/block/server/Server.java" - "server/src/main/java/com/hedera/block/server/Translator.java" + - "simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java" diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java index d7dd10d81..5344f689b 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -16,75 +16,45 @@ package com.hedera.block.simulator; -import com.hedera.block.simulator.config.ConfigProvider; -import com.hedera.block.simulator.config.ConfigProviderImpl; -import com.hedera.block.simulator.generator.BlockStreamManager; import com.swirlds.config.api.Configuration; -import edu.umd.cs.findbugs.annotations.NonNull; +import com.swirlds.config.api.ConfigurationBuilder; +import com.swirlds.config.extensions.sources.ClasspathFileConfigSource; +import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource; +import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource; +import java.io.IOException; import java.lang.System.Logger; -import javax.inject.Inject; +import java.nio.file.Path; /** The BlockStreamSimulator class defines the simulator for the block stream. */ public class BlockStreamSimulator { private static final Logger LOGGER = System.getLogger(BlockStreamSimulator.class.getName()); - Configuration configuration; - BlockStreamManager blockStreamManager; - boolean isRunning = false; - - /** - * Creates a new BlockStreamSimulator instance. - * - * @param configuration the configuration to be used by the block stream simulator - * @param blockStreamManager the block stream manager to be used by the block stream simulator - */ - @Inject - public BlockStreamSimulator( - @NonNull Configuration configuration, @NonNull BlockStreamManager blockStreamManager) { - this.configuration = configuration; - this.blockStreamManager = blockStreamManager; - } + /** This constructor should not be instantiated. */ + private BlockStreamSimulator() {} /** * The main entry point for the block stream simulator. * * @param args the arguments to be passed to the block stream simulator + * @throws IOException if an I/O error occurs */ - public static void main(String[] args) { + public static void main(String[] args) throws IOException { LOGGER.log(Logger.Level.INFO, "Starting Block Stream Simulator"); - ConfigProvider configProvider = new ConfigProviderImpl(); - Configuration configuration = configProvider.getConfiguration(); - BlockStreamSimulatorInjectionComponent DIComponent = - DaggerBlockStreamSimulatorInjectionComponent.factory().create(configuration); - - BlockStreamSimulator blockStreamSimulator = DIComponent.getBlockStreamSimulator(); - blockStreamSimulator.start(); - } - - /** Starts the block stream simulator. */ - public void start() { - - // use blockStreamManager to get block stream + ConfigurationBuilder configurationBuilder = + ConfigurationBuilder.create() + .withSource(SystemEnvironmentConfigSource.getInstance()) + .withSource(SystemPropertiesConfigSource.getInstance()) + .withSource(new ClasspathFileConfigSource(Path.of("app.properties"))) + .autoDiscoverExtensions(); - // use PublishStreamGrpcClient to stream it to the block-node. - isRunning = true; - LOGGER.log(Logger.Level.INFO, "Block Stream Simulator has started"); - } + Configuration configuration = configurationBuilder.build(); - /** - * Returns whether the block stream simulator is running. - * - * @return true if the block stream simulator is running, false otherwise - */ - public boolean isRunning() { - return isRunning; - } + BlockStreamSimulatorInjectionComponent DIComponent = + DaggerBlockStreamSimulatorInjectionComponent.factory().create(configuration); - /** Stops the block stream simulator. */ - public void stop() { - isRunning = false; - LOGGER.log(Logger.Level.INFO, "Block Stream Simulator has stopped"); + BlockStreamSimulatorApp blockStreamSimulatorApp = DIComponent.getBlockStreamSimulatorApp(); + blockStreamSimulatorApp.start(); } } diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java new file mode 100644 index 000000000..7c1f6cad2 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator; + +import com.hedera.block.simulator.generator.BlockStreamManager; +import com.swirlds.config.api.Configuration; +import edu.umd.cs.findbugs.annotations.NonNull; +import javax.inject.Inject; + +/** BlockStream Simulator App */ +public class BlockStreamSimulatorApp { + + private static final System.Logger LOGGER = + System.getLogger(BlockStreamSimulatorApp.class.getName()); + + Configuration configuration; + BlockStreamManager blockStreamManager; + boolean isRunning = false; + + /** + * Creates a new BlockStreamSimulatorApp instance. + * + * @param configuration the configuration to be used by the block stream simulator + * @param blockStreamManager the block stream manager to be used by the block stream simulator + */ + @Inject + public BlockStreamSimulatorApp( + @NonNull Configuration configuration, @NonNull BlockStreamManager blockStreamManager) { + this.configuration = configuration; + this.blockStreamManager = blockStreamManager; + } + + /** Starts the block stream simulator. */ + public void start() { + + // use blockStreamManager to get block stream + + // use PublishStreamGrpcClient to stream it to the block-node. + isRunning = true; + LOGGER.log(System.Logger.Level.INFO, "Block Stream Simulator has started"); + } + + /** + * Returns whether the block stream simulator is running. + * + * @return true if the block stream simulator is running, false otherwise + */ + public boolean isRunning() { + return isRunning; + } + + /** Stops the block stream simulator. */ + public void stop() { + isRunning = false; + LOGGER.log(System.Logger.Level.INFO, "Block Stream Simulator has stopped"); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java index ef8e4fe8f..cf5589e1a 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java @@ -37,7 +37,7 @@ public interface BlockStreamSimulatorInjectionComponent { * * @return the block stream simulator */ - BlockStreamSimulator getBlockStreamSimulator(); + BlockStreamSimulatorApp getBlockStreamSimulatorApp(); /** The factory used to create the block stream simulator injection component. */ @Component.Factory diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java deleted file mode 100644 index ddd89ce7a..000000000 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2024 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hedera.block.simulator.config; - -import com.swirlds.config.api.Configuration; -import com.swirlds.config.api.ConfigurationBuilder; -import com.swirlds.config.extensions.sources.ClasspathFileConfigSource; -import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource; -import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource; -import edu.umd.cs.findbugs.annotations.NonNull; -import java.io.IOException; -import java.nio.file.Path; - -/** The ConfigProviderImpl class provides the implementation for the configuration provider. */ -public class ConfigProviderImpl implements ConfigProvider { - private static final System.Logger LOGGER = - System.getLogger(ConfigProviderImpl.class.getName()); - private final Configuration configuration; - - /** Creates a new ConfigProviderImpl instance. */ - public ConfigProviderImpl() { - final var builder = createConfigurationBuilder(); - configuration = builder.build(); - } - - /** - * Returns the configuration. - * - * @return the configuration - */ - @NonNull - @Override - public Configuration getConfiguration() { - return configuration; - } - - private ConfigurationBuilder createConfigurationBuilder() { - try { - return ConfigurationBuilder.create() - .withSource(SystemEnvironmentConfigSource.getInstance()) - .withSource(SystemPropertiesConfigSource.getInstance()) - .withSource(new ClasspathFileConfigSource(Path.of("app.properties"))) - .autoDiscoverExtensions(); - - } catch (IOException e) { - LOGGER.log(System.Logger.Level.ERROR, "Failed to create configuration builder", e); - throw new RuntimeException(e); - } - } -} diff --git a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java index 71bd1b17d..2572d6b02 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java @@ -29,17 +29,17 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) -class BlockStreamSimulatorTest { +class BlockStreamSimulatorAppTest { @Mock private Configuration configuration; @Mock private BlockStreamManager blockStreamManager; - @InjectMocks private BlockStreamSimulator blockStreamSimulator; + @InjectMocks private BlockStreamSimulatorApp blockStreamSimulator; @BeforeEach void setUp() { - blockStreamSimulator = new BlockStreamSimulator(configuration, blockStreamManager); + blockStreamSimulator = new BlockStreamSimulatorApp(configuration, blockStreamManager); } @AfterEach diff --git a/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java b/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java new file mode 100644 index 000000000..84846eae2 --- /dev/null +++ b/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator.config; + +import com.hedera.block.simulator.config.data.BlockStreamConfig; +import com.hedera.block.simulator.config.data.GrpcConfig; +import com.hedera.block.simulator.config.types.GenerationMode; +import com.swirlds.config.api.Configuration; +import com.swirlds.config.api.ConfigurationBuilder; +import com.swirlds.config.extensions.sources.ClasspathFileConfigSource; +import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource; +import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource; +import java.io.IOException; +import java.nio.file.Path; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ConfigInjectionModuleTest { + + static Configuration configuration; + + @BeforeAll + static void setUpAll() throws IOException { + configuration = + ConfigurationBuilder.create() + .withSource(SystemEnvironmentConfigSource.getInstance()) + .withSource(SystemPropertiesConfigSource.getInstance()) + .withSource(new ClasspathFileConfigSource(Path.of("app.properties"))) + .autoDiscoverExtensions() + .build(); + } + + @Test + void provideBlockStreamConfig() { + + BlockStreamConfig blockStreamConfig = + ConfigInjectionModule.provideBlockStreamConfig(configuration); + + Assertions.assertNotNull(blockStreamConfig); + Assertions.assertEquals(GenerationMode.SELF, blockStreamConfig.generationMode()); + } + + @Test + void provideGrpcConfig() { + GrpcConfig grpcConfig = ConfigInjectionModule.provideGrpcConfig(configuration); + + Assertions.assertNotNull(grpcConfig); + Assertions.assertEquals("localhost", grpcConfig.serverAddress()); + Assertions.assertEquals(8080, grpcConfig.port()); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java b/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java similarity index 55% rename from simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java rename to simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java index 5a22e44c7..b78a41def 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java +++ b/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java @@ -14,20 +14,19 @@ * limitations under the License. */ -package com.hedera.block.simulator.config; +package com.hedera.block.simulator.config.types; -import com.swirlds.config.api.Configuration; -import edu.umd.cs.findbugs.annotations.NonNull; +import static org.junit.jupiter.api.Assertions.*; -/** The provider used to get the configuration for the simulator. */ -public interface ConfigProvider { +import org.junit.jupiter.api.Test; - /** - * Gets the configuration for the simulator, specifies the mode of generation, the mode of - * simulator and every configurable detail. - * - * @return the configuration - */ - @NonNull - Configuration getConfiguration(); +class GenerationModeTest { + + @Test + void testGenerationMode() { + GenerationMode mode = GenerationMode.FOLDER; + assertEquals(GenerationMode.FOLDER, mode); + mode = GenerationMode.SELF; + assertEquals(GenerationMode.SELF, mode); + } } diff --git a/simulator/src/test/java/com/hedera/block/simulator/generator/BlockStreamManagerTest.java b/simulator/src/test/java/com/hedera/block/simulator/generator/BlockStreamManagerTest.java new file mode 100644 index 000000000..0e4802887 --- /dev/null +++ b/simulator/src/test/java/com/hedera/block/simulator/generator/BlockStreamManagerTest.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator.generator; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class BlockStreamManagerTest { + + @Test + void getNextBlock() { + BlockStreamManager blockStreamManager = new MockBlockStreamManagerImpl(); + assertNotNull(blockStreamManager.getNextBlock()); + } +} From 3210816b47746949224627d57584f8bf78c9d233 Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Wed, 4 Sep 2024 10:11:47 -0600 Subject: [PATCH 08/10] PR Review comments Signed-off-by: Alfredo Gutierrez --- .../com/hedera/block/simulator/BlockStreamSimulator.java | 4 +++- .../hedera/block/simulator/config/data/GrpcConfig.java | 2 +- .../block/simulator/config/types/GenerationMode.java | 4 ++-- .../block/simulator/config/ConfigInjectionModuleTest.java | 2 +- .../block/simulator/config/types/GenerationModeTest.java | 8 ++++---- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java index 5344f689b..c376d2778 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -16,6 +16,8 @@ package com.hedera.block.simulator; +import static java.lang.System.Logger.Level.INFO; + import com.swirlds.config.api.Configuration; import com.swirlds.config.api.ConfigurationBuilder; import com.swirlds.config.extensions.sources.ClasspathFileConfigSource; @@ -40,7 +42,7 @@ private BlockStreamSimulator() {} */ public static void main(String[] args) throws IOException { - LOGGER.log(Logger.Level.INFO, "Starting Block Stream Simulator"); + LOGGER.log(INFO, "Starting Block Stream Simulator"); ConfigurationBuilder configurationBuilder = ConfigurationBuilder.create() diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java index abfbd4a61..dea9e07d2 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java @@ -29,5 +29,5 @@ */ @ConfigData("grpc") public record GrpcConfig( - @ConfigProperty(defaultValue = "localhost") @Min(0) @Max(65535) String serverAddress, + @ConfigProperty(defaultValue = "localhost") String serverAddress, @ConfigProperty(defaultValue = "8080") @Min(0) @Max(65535) int port) {} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java index 5b637dcb1..cf61bdaab 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java @@ -19,7 +19,7 @@ /** The GenerationMode enum defines the modes of generation for the block stream. */ public enum GenerationMode { /** Reads Blocks from a Folder. */ - FOLDER, + DIR, /** Generates Blocks from rules */ - SELF + ADHOC } diff --git a/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java b/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java index 84846eae2..36126a4b1 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java @@ -52,7 +52,7 @@ void provideBlockStreamConfig() { ConfigInjectionModule.provideBlockStreamConfig(configuration); Assertions.assertNotNull(blockStreamConfig); - Assertions.assertEquals(GenerationMode.SELF, blockStreamConfig.generationMode()); + Assertions.assertEquals(GenerationMode.ADHOC, blockStreamConfig.generationMode()); } @Test diff --git a/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java b/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java index b78a41def..1a4448ffd 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java @@ -24,9 +24,9 @@ class GenerationModeTest { @Test void testGenerationMode() { - GenerationMode mode = GenerationMode.FOLDER; - assertEquals(GenerationMode.FOLDER, mode); - mode = GenerationMode.SELF; - assertEquals(GenerationMode.SELF, mode); + GenerationMode mode = GenerationMode.DIR; + assertEquals(GenerationMode.DIR, mode); + mode = GenerationMode.ADHOC; + assertEquals(GenerationMode.ADHOC, mode); } } From 563a2a011214a2eb89c25413196eb9ffc8dc2fbf Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Wed, 4 Sep 2024 10:13:47 -0600 Subject: [PATCH 09/10] changing default to DIR Signed-off-by: Alfredo Gutierrez --- .../hedera/block/simulator/config/data/BlockStreamConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java index 302c5a00f..d0107b988 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java @@ -27,4 +27,4 @@ */ @ConfigData("blockStream") public record BlockStreamConfig( - @ConfigProperty(defaultValue = "SELF") GenerationMode generationMode) {} + @ConfigProperty(defaultValue = "DIR") GenerationMode generationMode) {} From 0d9f422eed617622e7691172f9facaa661e4a683 Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Wed, 4 Sep 2024 10:25:57 -0600 Subject: [PATCH 10/10] changing default to DIR Signed-off-by: Alfredo Gutierrez --- .../block/simulator/config/ConfigInjectionModuleTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java b/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java index 36126a4b1..4b8f2dbae 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java @@ -52,7 +52,7 @@ void provideBlockStreamConfig() { ConfigInjectionModule.provideBlockStreamConfig(configuration); Assertions.assertNotNull(blockStreamConfig); - Assertions.assertEquals(GenerationMode.ADHOC, blockStreamConfig.generationMode()); + Assertions.assertEquals(GenerationMode.DIR, blockStreamConfig.generationMode()); } @Test