Skip to content

Commit b70e90b

Browse files
authored
refactor: 21898 Remove version management from SwirldStateManager (#21905)
Signed-off-by: Ivan Malygin <[email protected]>
1 parent d04a540 commit b70e90b

File tree

8 files changed

+32
-96
lines changed

8 files changed

+32
-96
lines changed

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/SwirldsPlatform.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public SwirldsPlatform(@NonNull final PlatformComponentBuilder builder) {
211211
// This object makes a copy of the state. After this point, initialState becomes immutable.
212212
final SwirldStateManager swirldStateManager = blocks.swirldStateManager();
213213
swirldStateManager.setState(initialState.getState(), true);
214+
platformStateFacade.setCreationSoftwareVersionTo(swirldStateManager.getConsensusState(), blocks.appVersion());
214215

215216
final EventWindowManager eventWindowManager = new DefaultEventWindowManager();
216217

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformBuilder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,7 @@ public PlatformComponentBuilder buildComponentBuilder() {
433433
new ApplicationCallbacks(preconsensusEventConsumer, snapshotOverrideConsumer, staleEventConsumer);
434434

435435
final AtomicReference<StatusActionSubmitter> statusActionSubmitterAtomicReference = new AtomicReference<>();
436-
final SwirldStateManager swirldStateManager =
437-
new SwirldStateManager(platformContext, currentRoster, softwareVersion, platformStateFacade);
436+
final SwirldStateManager swirldStateManager = new SwirldStateManager(platformContext, currentRoster);
438437

439438
if (model == null) {
440439
final WiringConfig wiringConfig = platformContext.getConfiguration().getConfigData(WiringConfig.class);

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/SwirldStateManager.java

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package com.swirlds.platform.state;
33

4-
import static com.swirlds.platform.state.SwirldStateManagerUtils.fastCopy;
4+
import static com.swirlds.base.units.UnitConstants.NANOSECONDS_TO_MICROSECONDS;
55
import static java.util.Objects.requireNonNull;
66

7-
import com.hedera.hapi.node.base.SemanticVersion;
87
import com.hedera.hapi.node.state.roster.Roster;
98
import com.swirlds.common.context.PlatformContext;
10-
import com.swirlds.platform.state.service.PlatformStateFacade;
119
import com.swirlds.state.MerkleNodeState;
1210
import com.swirlds.state.State;
1311
import com.swirlds.state.merkle.StateMetrics;
@@ -35,31 +33,16 @@ public class SwirldStateManager {
3533
*/
3634
private final AtomicReference<MerkleNodeState> latestImmutableState = new AtomicReference<>();
3735

38-
/**
39-
* The current software version.
40-
*/
41-
private final SemanticVersion softwareVersion;
42-
43-
private final PlatformStateFacade platformStateFacade;
44-
4536
/**
4637
* Constructor.
4738
*
4839
* @param platformContext the platform context
4940
* @param roster the current roster
50-
* @param softwareVersion the current software version
5141
*/
52-
public SwirldStateManager(
53-
@NonNull final PlatformContext platformContext,
54-
@NonNull final Roster roster,
55-
@NonNull final SemanticVersion softwareVersion,
56-
@NonNull final PlatformStateFacade platformStateFacade) {
57-
42+
public SwirldStateManager(@NonNull final PlatformContext platformContext, @NonNull final Roster roster) {
5843
requireNonNull(platformContext);
5944
requireNonNull(roster);
60-
this.platformStateFacade = requireNonNull(platformStateFacade);
6145
this.stateMetrics = new StateMetrics(platformContext.getMetrics());
62-
this.softwareVersion = requireNonNull(softwareVersion);
6346
}
6447

6548
/**
@@ -77,9 +60,7 @@ public void setState(@NonNull final MerkleNodeState state, boolean onInit) {
7760
throw new IllegalStateException("Attempt to set initial state when there is already a state reference.");
7861
}
7962

80-
// Create a fast copy so there is always an immutable state to
81-
// invoke handleTransaction on for pre-consensus transactions
82-
fastCopyAndUpdateRefs(state);
63+
updateStateRefs(state);
8364
}
8465

8566
/**
@@ -90,13 +71,20 @@ public MerkleNodeState getConsensusState() {
9071
return stateRef.get();
9172
}
9273

93-
private void fastCopyAndUpdateRefs(final MerkleNodeState state) {
94-
final MerkleNodeState newState = fastCopy(state, stateMetrics, softwareVersion, platformStateFacade);
95-
74+
private void updateStateRefs(MerkleNodeState state) {
75+
// Create a fast copy so there is always an immutable state to
76+
// invoke handleTransaction on for pre-consensus transactions
77+
final long copyStart = System.nanoTime();
78+
// Create a fast copy
79+
final MerkleNodeState copy = state.copy();
80+
// Increment the reference count because this reference becomes the new value
81+
copy.getRoot().reserve();
82+
final long copyEnd = System.nanoTime();
83+
stateMetrics.stateCopyMicros((copyEnd - copyStart) * NANOSECONDS_TO_MICROSECONDS);
9684
// Set latest immutable first to prevent the newly immutable stateRoot from being deleted between setting the
9785
// stateRef and the latestImmutableState
9886
setLatestImmutableState(state);
99-
updateStateRef(newState);
87+
updateStateRef(copy);
10088
}
10189

10290
/**
@@ -136,7 +124,8 @@ private void setLatestImmutableState(final MerkleNodeState immutableState) {
136124
* @see State#copy()
137125
*/
138126
public MerkleNodeState getStateForSigning() {
139-
fastCopyAndUpdateRefs(stateRef.get());
127+
final MerkleNodeState state = stateRef.get();
128+
updateStateRefs(state);
140129
return latestImmutableState.get();
141130
}
142131
}

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/SwirldStateManagerUtils.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

platform-sdk/swirlds-platform-core/src/main/java/module-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@
116116
requires transitive com.swirlds.config.api;
117117
requires transitive com.swirlds.metrics.api;
118118
requires transitive com.swirlds.state.api;
119-
requires transitive com.swirlds.state.impl;
120119
requires transitive com.swirlds.virtualmap;
121120
requires transitive org.hiero.base.concurrent;
122121
requires transitive org.hiero.base.crypto;
@@ -132,6 +131,7 @@
132131
requires com.swirlds.config.extensions;
133132
requires com.swirlds.logging;
134133
requires com.swirlds.merkledb;
134+
requires com.swirlds.state.impl;
135135
requires com.fasterxml.jackson.core;
136136
requires com.fasterxml.jackson.dataformat.yaml;
137137
requires com.github.spotbugs.annotations;

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/eventhandling/TransactionHandlerTester.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public TransactionHandlerTester(final Roster roster) {
6666
.when(consensusStateEventHandler)
6767
.onHandleConsensusRound(any(), same(consensusState), any());
6868
final StatusActionSubmitter statusActionSubmitter = submittedActions::add;
69-
swirldStateManager = new SwirldStateManager(
70-
platformContext, roster, SemanticVersion.newBuilder().major(1).build(), platformStateFacade);
69+
swirldStateManager = new SwirldStateManager(platformContext, roster);
7170
swirldStateManager.setState(consensusState, true);
7271
defaultTransactionHandler = new DefaultTransactionHandler(
7372
platformContext,

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/state/StateEventHandlerManagerUtilsTests.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package com.swirlds.platform.state;
33

4-
import static com.swirlds.platform.test.fixtures.state.TestPlatformStateFacade.TEST_PLATFORM_STATE_FACADE;
54
import static org.junit.jupiter.api.Assertions.assertEquals;
65
import static org.junit.jupiter.api.Assertions.assertFalse;
7-
import static org.mockito.Mockito.mock;
86

97
import com.hedera.hapi.node.base.SemanticVersion;
108
import com.swirlds.merkledb.test.fixtures.MerkleDbTestUtils;
9+
import com.swirlds.platform.test.fixtures.state.TestPlatformStateFacade;
1110
import com.swirlds.platform.test.fixtures.state.TestingAppStateInitializer;
1211
import com.swirlds.state.MerkleNodeState;
13-
import com.swirlds.state.State;
14-
import com.swirlds.state.merkle.StateMetrics;
1512
import com.swirlds.state.test.fixtures.merkle.TestVirtualMapState;
1613
import org.junit.jupiter.api.AfterEach;
1714
import org.junit.jupiter.api.Test;
@@ -25,11 +22,16 @@ void testFastCopyIsMutable() {
2522
final MerkleNodeState state = TestVirtualMapState.createInstanceWithVirtualMapLabel(virtualMapLabel);
2623
TestingAppStateInitializer.initPlatformState(state);
2724
state.getRoot().reserve();
28-
final StateMetrics stats = mock(StateMetrics.class);
29-
final State result = SwirldStateManagerUtils.fastCopy(
30-
state, stats, SemanticVersion.newBuilder().major(1).build(), TEST_PLATFORM_STATE_FACADE);
3125

32-
assertFalse(result.isImmutable(), "The copy state should be mutable.");
26+
final SemanticVersion softwareVersion =
27+
SemanticVersion.newBuilder().major(1).build();
28+
// Create a fast copy
29+
final MerkleNodeState copy = state.copy();
30+
TestPlatformStateFacade.TEST_PLATFORM_STATE_FACADE.setCreationSoftwareVersionTo(copy, softwareVersion);
31+
// Increment the reference count because this reference becomes the new value
32+
copy.getRoot().reserve();
33+
34+
assertFalse(copy.isImmutable(), "The copy state should be mutable.");
3335
assertEquals(
3436
1,
3537
state.getRoot().getReservationCount(),
@@ -39,7 +41,7 @@ void testFastCopyIsMutable() {
3941
state.getRoot().getReservationCount(),
4042
"Fast copy should return a new state with a reference count of 1.");
4143
state.release();
42-
result.release();
44+
copy.release();
4345
}
4446

4547
@AfterEach

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/state/SwirldsStateManagerTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ void setup() {
4141
final PlatformContext platformContext =
4242
TestPlatformContextBuilder.create().build();
4343

44-
swirldStateManager = new SwirldStateManager(
45-
platformContext, roster, SemanticVersion.newBuilder().major(1).build(), platformStateFacade);
44+
swirldStateManager = new SwirldStateManager(platformContext, roster);
4645
swirldStateManager.setState(initialState, true);
4746
}
4847

0 commit comments

Comments
 (0)