-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add
StateHashedNotification
(#15395)
Signed-off-by: Michael Tinker <[email protected]>
- Loading branch information
1 parent
3a62199
commit c7f12c7
Showing
6 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...re/src/main/java/com/swirlds/platform/system/state/notifications/StateHashedListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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.swirlds.platform.system.state.notifications; | ||
|
||
import com.swirlds.common.notification.DispatchMode; | ||
import com.swirlds.common.notification.DispatchModel; | ||
import com.swirlds.common.notification.DispatchOrder; | ||
import com.swirlds.common.notification.Listener; | ||
|
||
/** | ||
* A method that listens for the newly hashed states. | ||
*/ | ||
@DispatchModel(mode = DispatchMode.SYNC, order = DispatchOrder.UNORDERED) | ||
public interface StateHashedListener extends Listener<StateHashedNotification> {} |
59 changes: 59 additions & 0 deletions
59
...rc/main/java/com/swirlds/platform/system/state/notifications/StateHashedNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.swirlds.platform.system.state.notifications; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.swirlds.common.crypto.Hash; | ||
import com.swirlds.common.notification.AbstractNotification; | ||
import com.swirlds.common.notification.Notification; | ||
import com.swirlds.platform.wiring.components.StateAndRound; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
/** | ||
* A {@link Notification} that a state hash has been computed. | ||
*/ | ||
public class StateHashedNotification extends AbstractNotification { | ||
private final long roundNumber; | ||
private final Hash hash; | ||
|
||
/** | ||
* Create a notification for a newly hashed state. | ||
* @param stateAndRound the state and round that is now hashed | ||
* @return a new notification | ||
*/ | ||
public static StateHashedNotification from(@NonNull final StateAndRound stateAndRound) { | ||
try (final var state = stateAndRound.reservedSignedState()) { | ||
return new StateHashedNotification( | ||
stateAndRound.round().getRoundNum(), | ||
requireNonNull(state.get().getState().getHash())); | ||
} | ||
} | ||
|
||
private StateHashedNotification(final long roundNumber, @NonNull final Hash hash) { | ||
this.roundNumber = roundNumber; | ||
this.hash = requireNonNull(hash); | ||
} | ||
|
||
public long round() { | ||
return roundNumber; | ||
} | ||
|
||
public @NonNull Hash hash() { | ||
return hash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...est/java/com/swirlds/platform/system/state/notifications/StateHashedNotificationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.swirlds.platform.system.state.notifications; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
import com.swirlds.common.crypto.Hash; | ||
import com.swirlds.platform.internal.ConsensusRound; | ||
import com.swirlds.platform.state.MerkleRoot; | ||
import com.swirlds.platform.state.signed.ReservedSignedState; | ||
import com.swirlds.platform.state.signed.SignedState; | ||
import com.swirlds.platform.wiring.components.StateAndRound; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class StateHashedNotificationTest { | ||
private static final long ROUND = 123L; | ||
private static final Hash HASH = new Hash(new byte[48]); | ||
|
||
@Mock | ||
private MerkleRoot merkleRoot; | ||
|
||
@Mock | ||
private SignedState signedState; | ||
|
||
@Mock | ||
private ConsensusRound round; | ||
|
||
@Mock | ||
private ReservedSignedState reservedSignedState; | ||
|
||
@Test | ||
void factoryWorksAsExpected() { | ||
given(round.getRoundNum()).willReturn(ROUND); | ||
given(reservedSignedState.get()).willReturn(signedState); | ||
given(signedState.getState()).willReturn(merkleRoot); | ||
given(merkleRoot.getHash()).willReturn(HASH); | ||
|
||
final var notification = StateHashedNotification.from(new StateAndRound(reservedSignedState, round)); | ||
|
||
assertEquals(ROUND, notification.round()); | ||
assertEquals(HASH, notification.hash()); | ||
} | ||
} |