Skip to content

Commit

Permalink
feat: Simulator Base Project (#162)
Browse files Browse the repository at this point in the history
Signed-off-by: georgi-l95 <[email protected]>
Signed-off-by: Alfredo Gutierrez <[email protected]>
Co-authored-by: georgi-l95 <[email protected]>
  • Loading branch information
AlfredoG87 and georgi-l95 authored Sep 5, 2024
1 parent e957565 commit e392f2b
Show file tree
Hide file tree
Showing 21 changed files with 745 additions and 0 deletions.
25 changes: 25 additions & 0 deletions buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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<MavenPublication>("maven") { from(components["java"]) }

signing.sign(maven)
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ plugins {
// Include the subprojects
include(":stream")
include(":server")
include(":simulator")

includeBuild(".") // https://github.com/gradle/gradle/issues/21490#issuecomment-1458887481

Expand Down
40 changes: 40 additions & 0 deletions simulator/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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")
}

description = "Hedera Block Stream Simulator"

application {
mainModule = "com.hedera.block.simulator"
mainClass = "com.hedera.block.simulator.BlockStreamSimulator"
}

mainModuleInfo {
annotationProcessor("dagger.compiler")
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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 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;
import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource;
import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource;
import java.io.IOException;
import java.lang.System.Logger;
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());

/** 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) throws IOException {

LOGGER.log(INFO, "Starting Block Stream Simulator");

ConfigurationBuilder configurationBuilder =
ConfigurationBuilder.create()
.withSource(SystemEnvironmentConfigSource.getInstance())
.withSource(SystemPropertiesConfigSource.getInstance())
.withSource(new ClasspathFileConfigSource(Path.of("app.properties")))
.autoDiscoverExtensions();

Configuration configuration = configurationBuilder.build();

BlockStreamSimulatorInjectionComponent DIComponent =
DaggerBlockStreamSimulatorInjectionComponent.factory().create(configuration);

BlockStreamSimulatorApp blockStreamSimulatorApp = DIComponent.getBlockStreamSimulatorApp();
blockStreamSimulatorApp.start();
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

/** The component used to inject the block stream simulator into the application. */
@Singleton
@Component(
modules = {
ConfigInjectionModule.class,
GeneratorInjectionModule.class,
})
public interface BlockStreamSimulatorInjectionComponent {

/**
* Gets the block stream simulator.
*
* @return the block stream simulator
*/
BlockStreamSimulatorApp getBlockStreamSimulatorApp();

/** 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

/** 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) {
return configuration.getConfigData(GrpcConfig.class);
}
}
Original file line number Diff line number Diff line change
@@ -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.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<Class<? extends Record>> getConfigDataTypes() {
return Set.of(BlockStreamConfig.class, GrpcConfig.class);
}
}
Original file line number Diff line number Diff line change
@@ -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.config.data;

import com.hedera.block.simulator.config.types.GenerationMode;
import com.swirlds.config.api.ConfigData;
import com.swirlds.config.api.ConfigProperty;

/**
* 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 = "DIR") GenerationMode generationMode) {}
Loading

0 comments on commit e392f2b

Please sign in to comment.