From b11b628c057e020476a18083578921ce940b09f5 Mon Sep 17 00:00:00 2001 From: kfa-aguda <157432197+kfa-aguda@users.noreply.github.com> Date: Mon, 24 Jun 2024 08:43:12 -0700 Subject: [PATCH] feat: Reduce services-hedera's usage of PlatformContext (#13652) --- .../main/java/com/hedera/node/app/Hedera.java | 5 +++- .../node/app/HederaInjectionComponent.java | 4 ++++ .../com/hedera/node/app/ServicesMain.java | 13 ++++++++--- .../app/metrics/MetricsInjectionModule.java | 8 ------- .../app/components/IngestComponentTest.java | 23 +++---------------- 5 files changed, 21 insertions(+), 32 deletions(-) diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java index 2ed28be02202..d28f15a2c123 100644 --- a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java @@ -189,6 +189,8 @@ public final class Hedera implements SwirldMain { */ private PlatformStatus platformStatus = STARTING_UP; + private Metrics metrics; + /*================================================================================================================== * * Hedera Object Construction. @@ -319,7 +321,7 @@ public void onStateInitialized( throw new IllegalStateException("Platform should never change once set"); } this.platform = requireNonNull(platform); - final var metrics = platform.getContext().getMetrics(); + this.metrics = platform.getContext().getMetrics(); this.configProvider = new ConfigProviderImpl(trigger == GENESIS, metrics); logger.info( "Initializing Hedera state version {} in {} mode with trigger {} and previous version {}", @@ -678,6 +680,7 @@ private void initializeDagger( .currentPlatformStatus(new CurrentPlatformStatusImpl(platform)) .servicesRegistry(servicesRegistry) .instantSource(InstantSource.system()) + .metrics(metrics) .build(); daggerApp.workingStateAccessor().setHederaState(state); daggerApp.platformStateAccessor().setPlatformState(platformState); diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/HederaInjectionComponent.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/HederaInjectionComponent.java index d490af23c3a8..893e514e0cc1 100644 --- a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/HederaInjectionComponent.java +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/HederaInjectionComponent.java @@ -47,6 +47,7 @@ import com.hedera.node.app.workflows.query.QueryWorkflow; import com.hedera.node.config.ConfigProvider; import com.swirlds.common.crypto.Cryptography; +import com.swirlds.metrics.api.Metrics; import com.swirlds.platform.listeners.ReconnectCompleteListener; import com.swirlds.platform.listeners.StateWriteToDiskCompleteListener; import com.swirlds.platform.system.InitTrigger; @@ -157,6 +158,9 @@ interface Builder { @BindsInstance Builder softwareVersion(SoftwareVersion softwareVersion); + @BindsInstance + Builder metrics(Metrics metrics); + HederaInjectionComponent build(); } } diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/ServicesMain.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/ServicesMain.java index 78371784bae9..943d1c3369b4 100644 --- a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/ServicesMain.java +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/ServicesMain.java @@ -32,8 +32,10 @@ import com.hedera.node.app.services.OrderedServiceMigrator; import com.hedera.node.app.services.ServicesRegistryImpl; import com.hedera.node.app.state.merkle.MerkleHederaState; +import com.swirlds.base.time.Time; import com.swirlds.common.constructable.ConstructableRegistry; import com.swirlds.common.constructable.RuntimeConstructable; +import com.swirlds.common.crypto.CryptographyFactory; import com.swirlds.common.io.utility.FileUtils; import com.swirlds.common.platform.NodeId; import com.swirlds.config.api.Configuration; @@ -174,10 +176,15 @@ public static void main(final String... args) throws Exception { final SoftwareVersion version = hedera.getSoftwareVersion(); logger.info("Starting node {} with version {}", selfId, version); - final PlatformBuilder builder = + final PlatformBuilder platformBuilder = PlatformBuilder.create(Hedera.APP_NAME, Hedera.SWIRLD_NAME, version, hedera::newState, selfId); - builder.withConfiguration(buildConfiguration()); + // Add additional configuration to the platform + final Configuration configuration = buildConfiguration(); + platformBuilder.withConfiguration(configuration); + + platformBuilder.withCryptography(CryptographyFactory.create()); + platformBuilder.withTime(Time.getCurrent()); // IMPORTANT: A surface-level reading of this method will undersell the centrality // of the Hedera instance. It is actually omnipresent throughout both the startup @@ -202,7 +209,7 @@ public static void main(final String... args) throws Exception { // whose object graph roots include the Ingest, PreHandle, Handle, and Query workflows; // as well as other infrastructure components that need to be initialized or accessed // at specific points in the Swirlds application lifecycle. - final Platform platform = builder.build(); + final Platform platform = platformBuilder.build(); hedera.init(platform, selfId); platform.start(); hedera.run(); diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/metrics/MetricsInjectionModule.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/metrics/MetricsInjectionModule.java index 637c16624e80..da74e23d15f4 100644 --- a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/metrics/MetricsInjectionModule.java +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/metrics/MetricsInjectionModule.java @@ -18,21 +18,13 @@ import com.hedera.node.app.spi.metrics.StoreMetricsService; import com.swirlds.metrics.api.Metrics; -import com.swirlds.platform.system.Platform; import dagger.Binds; import dagger.Module; -import dagger.Provides; import edu.umd.cs.findbugs.annotations.NonNull; -import javax.inject.Singleton; /** A Dagger module for providing dependencies based on {@link Metrics}. */ @Module public interface MetricsInjectionModule { - @Provides - @Singleton - static Metrics provideMetrics(@NonNull final Platform platform) { - return platform.getContext().getMetrics(); - } @Binds StoreMetricsService bindStoreMetricsService(@NonNull StoreMetricsServiceImpl storeMetricsService); diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/components/IngestComponentTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/components/IngestComponentTest.java index 3e5eb3025845..83b1f94ddbba 100644 --- a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/components/IngestComponentTest.java +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/components/IngestComponentTest.java @@ -17,18 +17,14 @@ package com.hedera.node.app.components; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import com.hedera.hapi.node.base.AccountID; import com.hedera.hapi.node.base.SemanticVersion; import com.hedera.node.app.DaggerHederaInjectionComponent; import com.hedera.node.app.HederaInjectionComponent; import com.hedera.node.app.config.ConfigProviderImpl; -import com.hedera.node.app.fees.congestion.ThrottleMultiplier; -import com.hedera.node.app.fees.congestion.UtilizationScaledThrottleMultiplier; import com.hedera.node.app.fixtures.state.FakeHederaState; import com.hedera.node.app.info.SelfNodeInfoImpl; import com.hedera.node.app.services.ServicesRegistry; @@ -38,6 +34,7 @@ import com.hedera.pbj.runtime.io.buffer.Bytes; import com.swirlds.common.context.PlatformContext; import com.swirlds.common.crypto.CryptographyHolder; +import com.swirlds.common.metrics.noop.NoOpMetrics; import com.swirlds.config.api.Configuration; import com.swirlds.metrics.api.Metrics; import com.swirlds.platform.system.InitTrigger; @@ -58,26 +55,14 @@ class IngestComponentTest { @Mock private Platform platform; - @Mock - private PlatformContext platformContext; - - @Mock - private Metrics metrics; - - @Mock - UtilizationScaledThrottleMultiplier genericFeeMultiplier; - - @Mock - ThrottleMultiplier gasFeeMultiplier; - private HederaInjectionComponent app; @BeforeEach void setUp() { final Configuration configuration = HederaTestConfigBuilder.createConfig(); final PlatformContext platformContext = mock(PlatformContext.class); + final Metrics metrics = new NoOpMetrics(); lenient().when(platformContext.getConfiguration()).thenReturn(configuration); - when(platform.getContext()).thenReturn(platformContext); final var selfNodeInfo = new SelfNodeInfoImpl( 1L, @@ -108,6 +93,7 @@ void setUp() { .servicesRegistry(mock(ServicesRegistry.class)) .instantSource(InstantSource.system()) .softwareVersion(mock(HederaSoftwareVersion.class)) + .metrics(metrics) .build(); final var state = new FakeHederaState(); @@ -117,9 +103,6 @@ void setUp() { @Test void objectGraphRootsAreAvailable() { - given(platform.getContext()).willReturn(platformContext); - given(platformContext.getMetrics()).willReturn(metrics); - final IngestInjectionComponent subject = app.ingestComponentFactory().get().create();