Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: Add Dagger for Dependency Injection Framework #118

Merged
merged 12 commits into from
Aug 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ jvmDependencyConflicts.patch {
module("io.grpc:grpc-stub") { annotationLibraries.forEach { removeDependency(it) } }
module("io.grpc:grpc-testing") { annotationLibraries.forEach { removeDependency(it) } }
module("io.grpc:grpc-util") { annotationLibraries.forEach { removeDependency(it) } }
module("com.google.dagger:dagger-compiler") {
annotationLibraries.forEach { removeDependency(it) }
}
module("com.google.dagger:dagger-producers") {
annotationLibraries.forEach { removeDependency(it) }
}
module("com.google.dagger:dagger-spi") { annotationLibraries.forEach { removeDependency(it) } }
module("com.google.guava:guava") {
(annotationLibraries -
"com.google.code.findbugs:jsr305" -
Expand All @@ -63,6 +70,9 @@ jvmDependencyConflicts.patch {
removeDependency("io.prometheus:simpleclient_tracer_otel")
removeDependency("io.prometheus:simpleclient_tracer_otel_agent")
}
module("org.jetbrains.kotlin:kotlin-stdlib") {
removeDependency("org.jetbrains.kotlin:kotlin-stdlib-common")
}
module("junit:junit") { removeDependency("org.hamcrest:hamcrest-core") }
module("org.hyperledger.besu:secp256k1") { addApiDependency("net.java.dev.jna:jna") }
}
Expand Down Expand Up @@ -110,7 +120,7 @@ extraJavaModuleInfo {
}
module("com.google.guava:failureaccess", "com.google.common.util.concurrent.internal")
module("com.google.api.grpc:proto-google-common-protos", "com.google.api.grpc.common")

module("com.google.dagger:dagger", "dagger")
module("io.perfmark:perfmark-api", "io.perfmark")
module("javax.inject:javax.inject", "javax.inject")

Expand All @@ -121,6 +131,7 @@ extraJavaModuleInfo {

module("org.checkerframework:checker-qual", "org.checkerframework.checker.qual")
module("net.i2p.crypto:eddsa", "net.i2p.crypto.eddsa")
module("org.jetbrains:annotations", "org.jetbrains.annotations")
module("org.antlr:antlr4-runtime", "org.antlr.antlr4.runtime")

// needed for metrics and logging, but also several platform classes
Expand All @@ -143,9 +154,24 @@ extraJavaModuleInfo {
module("com.google.auto.service:auto-service-annotations", "com.google.auto.service")
module("com.google.auto.service:auto-service", "com.google.auto.service.processor")
module("com.google.auto:auto-common", "com.google.auto.common")
module("com.google.dagger:dagger-compiler", "dagger.compiler")
module("com.google.dagger:dagger-producers", "dagger.producers")
module("com.google.dagger:dagger-spi", "dagger.spi")
module(
"com.google.devtools.ksp:symbol-processing-api",
"com.google.devtools.ksp.symbolprocessingapi"
)
module("com.google.errorprone:javac-shaded", "com.google.errorprone.javac.shaded")
module("com.google.googlejavaformat:google-java-format", "com.google.googlejavaformat")
module("net.ltgt.gradle.incap:incap", "net.ltgt.gradle.incap")
jsync-swirlds marked this conversation as resolved.
Show resolved Hide resolved
module("org.jetbrains.kotlinx:kotlinx-metadata-jvm", "kotlinx.metadata.jvm")

// Test clients only
module("com.google.protobuf:protobuf-java-util", "com.google.protobuf.util")
module("com.squareup:javapoet", "com.squareup.javapoet") {
exportAllPackages()
requires("java.compiler")
}
module("junit:junit", "junit")
module("org.hamcrest:hamcrest", "org.hamcrest")
module("org.json:json", "org.json")
Expand Down
7 changes: 7 additions & 0 deletions gradle/modules.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ org.apache.commons.compress=org.apache.commons:commons-compress

java.annotation=javax.annotation:javax.annotation-api
org.apache.logging.log4j.slf4j2.impl=org.apache.logging.log4j:log4j-slf4j2-impl

grpc.protobuf=io.grpc:grpc-protobuf
dagger=com.google.dagger:dagger
dagger.compiler=com.google.dagger:dagger-compiler
com.squareup.javapoet=com.squareup:javapoet
javax.inject=javax.inject:javax.inject
org.checkerframework.checker.qual=org.checkerframework:checker-qual
1 change: 1 addition & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ application {
}

mainModuleInfo {
annotationProcessor("dagger.compiler")
annotationProcessor("com.google.auto.service.processor")
runtimeOnly("com.swirlds.config.impl")
runtimeOnly("org.apache.logging.log4j.slf4j2.impl")
Expand Down
132 changes: 132 additions & 0 deletions server/src/main/java/com/hedera/block/server/BlockNodeApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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.server;

import static java.lang.System.Logger;
import static java.lang.System.Logger.Level.INFO;

import com.hedera.block.server.config.BlockNodeContext;
import com.hedera.block.server.data.ObjectEvent;
import com.hedera.block.server.health.HealthService;
import com.hedera.block.server.mediator.LiveStreamMediatorBuilder;
import com.hedera.block.server.mediator.StreamMediator;
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig;
import com.hedera.block.server.persistence.storage.read.BlockAsDirReaderBuilder;
import com.hedera.block.server.persistence.storage.read.BlockReader;
import com.hedera.block.server.persistence.storage.write.BlockAsDirWriterBuilder;
import com.hedera.block.server.persistence.storage.write.BlockWriter;
import com.hedera.hapi.block.SubscribeStreamResponse;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import edu.umd.cs.findbugs.annotations.NonNull;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.grpc.GrpcRouting;
import io.helidon.webserver.http.HttpRouting;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;

jsync-swirlds marked this conversation as resolved.
Show resolved Hide resolved
/**
* The main class for the Block Node application. This class is responsible for starting the server
* and initializing the context.
*/
@Singleton
public class BlockNodeApp {

private static final Logger LOGGER = System.getLogger(BlockNodeApp.class.getName());

Check warning on line 50 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L50

Added line #L50 was not covered by tests

private final ServiceStatus serviceStatus;
private final HealthService healthService;
private final BlockNodeContext blockNodeContext;

/**
* Has all needed dependencies to start the server and initialize the context.
*
* @param serviceStatus the status of the service
* @param healthService the health service
* @param blockNodeContext the context of the block node
*/
@Inject
public BlockNodeApp(
@NonNull ServiceStatus serviceStatus,
@NonNull HealthService healthService,
@NonNull BlockNodeContext blockNodeContext) {
this.serviceStatus = serviceStatus;
this.healthService = healthService;
this.blockNodeContext = blockNodeContext;
}

Check warning on line 71 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L67-L71

Added lines #L67 - L71 were not covered by tests

/**
* Starts the server and binds to the specified port.
*
* @throws IOException if the server cannot be started
*/
public void start() throws IOException {

jsync-swirlds marked this conversation as resolved.
Show resolved Hide resolved
final BlockWriter<BlockItem> blockWriter =
BlockAsDirWriterBuilder.newBuilder(blockNodeContext).build();
final StreamMediator<BlockItem, ObjectEvent<SubscribeStreamResponse>> streamMediator =
LiveStreamMediatorBuilder.newBuilder(blockWriter, blockNodeContext, serviceStatus)
.build();

Check warning on line 84 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L80-L84

Added lines #L80 - L84 were not covered by tests

final BlockReader<Block> blockReader =
BlockAsDirReaderBuilder.newBuilder(

Check warning on line 87 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L86-L87

Added lines #L86 - L87 were not covered by tests
blockNodeContext
.configuration()
.getConfigData(PersistenceStorageConfig.class))
.build();

Check warning on line 91 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L89-L91

Added lines #L89 - L91 were not covered by tests

final BlockStreamService blockStreamService =
buildBlockStreamService(

Check warning on line 94 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L93-L94

Added lines #L93 - L94 were not covered by tests
streamMediator, blockReader, serviceStatus, blockNodeContext);

final GrpcRouting.Builder grpcRouting = GrpcRouting.builder().service(blockStreamService);

Check warning on line 97 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L97

Added line #L97 was not covered by tests

final HttpRouting.Builder httpRouting =
HttpRouting.builder().register(healthService.getHealthRootPath(), healthService);

Check warning on line 100 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L100

Added line #L100 was not covered by tests

// Build the web server
// TODO: make port server a configurable value.
final WebServer webServer =
WebServer.builder()
.port(8080)
.addRouting(grpcRouting)
.addRouting(httpRouting)
.build();

Check warning on line 109 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L105-L109

Added lines #L105 - L109 were not covered by tests

// Update the serviceStatus with the web server
serviceStatus.setWebServer(webServer);

Check warning on line 112 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L112

Added line #L112 was not covered by tests

// Start the web server
webServer.start();

Check warning on line 115 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L115

Added line #L115 was not covered by tests

// Log the server status
LOGGER.log(INFO, String.format("Block Node Server started at port: %d", webServer.port()));
}

Check warning on line 119 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L118-L119

Added lines #L118 - L119 were not covered by tests

@NonNull
private static BlockStreamService buildBlockStreamService(
@NonNull
final StreamMediator<BlockItem, ObjectEvent<SubscribeStreamResponse>>
streamMediator,
@NonNull final BlockReader<Block> blockReader,
@NonNull final ServiceStatus serviceStatus,
@NonNull final BlockNodeContext blockNodeContext) {

return new BlockStreamService(streamMediator, blockReader, serviceStatus, blockNodeContext);

Check warning on line 130 in server/src/main/java/com/hedera/block/server/BlockNodeApp.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeApp.java#L130

Added line #L130 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.server;

import com.hedera.block.server.health.HealthInjectionModule;
import dagger.Component;
import javax.inject.Singleton;

/** The infrastructure used to manage the instances and inject them using Dagger */
@Singleton
@Component(
modules = {
BlockNodeAppInjectionModule.class,
HealthInjectionModule.class,
})
jsync-swirlds marked this conversation as resolved.
Show resolved Hide resolved
public interface BlockNodeAppInjectionComponent {
/**
* Get the block node app server.
*
* @return the block node app server
*/
BlockNodeApp getBlockNodeApp();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.server;

import com.hedera.block.server.config.BlockNodeContext;
import com.hedera.block.server.config.BlockNodeContextFactory;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import java.io.IOException;
import javax.inject.Singleton;

/**
* A Dagger Module for interfaces that are at the BlockNodeApp Level, should be temporary and
* everything should be inside its own modules.
*/
@Module
public interface BlockNodeAppInjectionModule {

/**
* Binds the service status to the service status implementation.
*
* @param serviceStatus needs a service status implementation
* @return the service status implementation
*/
@Singleton
@Binds
ServiceStatus bindServiceStatus(ServiceStatusImpl serviceStatus);

/**
* Provides a block node context singleton using the factory.
*
* @return a block node context singleton
*/
@Provides
@Singleton
static BlockNodeContext provideBlockNodeContext() {
try {
return BlockNodeContextFactory.create();
} catch (IOException e) {
throw new RuntimeException(e);

Check warning on line 55 in server/src/main/java/com/hedera/block/server/BlockNodeAppInjectionModule.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/BlockNodeAppInjectionModule.java#L54-L55

Added lines #L54 - L55 were not covered by tests
}
}
}
Loading
Loading