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/