Skip to content

Commit

Permalink
feat: Reduce services-hedera's usage of PlatformContext (#13652)
Browse files Browse the repository at this point in the history
  • Loading branch information
kfa-aguda authored Jun 24, 2024
1 parent bc59304 commit b11b628
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ public final class Hedera implements SwirldMain {
*/
private PlatformStatus platformStatus = STARTING_UP;

private Metrics metrics;

/*==================================================================================================================
*
* Hedera Object Construction.
Expand Down Expand Up @@ -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 {}",
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,6 +158,9 @@ interface Builder {
@BindsInstance
Builder softwareVersion(SoftwareVersion softwareVersion);

@BindsInstance
Builder metrics(Metrics metrics);

HederaInjectionComponent build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand Down

0 comments on commit b11b628

Please sign in to comment.