Skip to content

Commit

Permalink
bi-directional translator for NetworkStakingRewards (#7805)
Browse files Browse the repository at this point in the history
Signed-off-by: Lev Povolotsky <[email protected]>
  • Loading branch information
povolev15 authored Aug 3, 2023
1 parent 55bef50 commit 250c753
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ public static Nft nftFromMerkleUniqueToken(
* Converts a {@link com.hedera.node.app.service.mono.state.merkle.MerkleUniqueToken} to a {@link Nft}.
* @param tokenID the {@link NftID}
* @param tokenID the {@link ReadableNftStore}
*
*
*/
public static com.hedera.node.app.service.mono.state.merkle.MerkleUniqueToken merkleUniqueTokenFromNft(
@NonNull NftID tokenID, @NonNull ReadableNftStore readableNftStore) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2023 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.node.app.service.token.impl.codec;

import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.state.token.NetworkStakingRewards;
import edu.umd.cs.findbugs.annotations.NonNull;

public final class NetworkingStakingTranslator {

@NonNull
/**
* Converts a Part of {@link com.hedera.node.app.service.mono.state.merkle.MerkleNetworkContext} to {@link NetworkStakingRewards}.
* @param merkleNetworkContext the {@link com.hedera.node.app.service.mono.state.merkle.MerkleNetworkContext}
* @return the {@link NetworkStakingRewards} converted from the {@link com.hedera.node.app.service.mono.state.merkle.MerkleNetworkContext}
*/
public static NetworkStakingRewards networkStakingRewardsFromMerkleNetworkContext(
@NonNull final com.hedera.node.app.service.mono.state.merkle.MerkleNetworkContext merkleNetworkContext) {
requireNonNull(merkleNetworkContext);
return NetworkStakingRewards.newBuilder()
.stakingRewardsActivated(merkleNetworkContext.areRewardsActivated())
.totalStakedRewardStart(merkleNetworkContext.getTotalStakedRewardStart())
.totalStakedStart(merkleNetworkContext.getTotalStakedStart())
.pendingRewards(merkleNetworkContext.pendingRewards())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2023 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.node.app.service.token.impl.test.codec;

import static com.hedera.node.app.service.token.impl.TokenServiceImpl.STAKING_NETWORK_REWARDS_KEY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.given;

import com.hedera.hapi.node.state.token.NetworkStakingRewards;
import com.hedera.node.app.service.token.impl.ReadableNetworkStakingRewardsStoreImpl;
import com.hedera.node.app.service.token.impl.codec.NetworkingStakingTranslator;
import com.hedera.node.app.spi.state.ReadableSingletonState;
import com.hedera.node.app.spi.state.ReadableStates;
import org.junit.jupiter.api.BeforeEach;
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)
public class NetworkingStakingTranslatorTest {

@Mock(strictness = Mock.Strictness.LENIENT)
private ReadableStates states;

@Mock(strictness = Mock.Strictness.LENIENT)
private ReadableSingletonState stakingRewardsState;

private ReadableNetworkStakingRewardsStoreImpl subject;

@BeforeEach
void setUp() {
given(states.getSingleton(STAKING_NETWORK_REWARDS_KEY)).willReturn(stakingRewardsState);
given(stakingRewardsState.get()).willReturn(new NetworkStakingRewards(true, 1L, 2L, 3L));
subject = new ReadableNetworkStakingRewardsStoreImpl(states);
}

@Test
void createNetworkStakingRewardsFromMerkleNetworkContext() {
final com.hedera.node.app.service.mono.state.merkle.MerkleNetworkContext merkleNetworkContext =
new com.hedera.node.app.service.mono.state.merkle.MerkleNetworkContext();

merkleNetworkContext.setStakingRewardsActivated(true);
merkleNetworkContext.setTotalStakedRewardStart(1L);
merkleNetworkContext.setTotalStakedStart(2L);
merkleNetworkContext.setPendingRewards(3L);

final NetworkStakingRewards convertedNetworkStakingRewards =
NetworkingStakingTranslator.networkStakingRewardsFromMerkleNetworkContext(merkleNetworkContext);

assertEquals(getExpectedNetworkStakingRewards(), convertedNetworkStakingRewards);
}

private com.hedera.node.app.service.mono.state.merkle.MerkleNetworkContext getExpectedMerkleNetworkContext() {
final com.hedera.node.app.service.mono.state.merkle.MerkleNetworkContext merkleNetworkContext =
new com.hedera.node.app.service.mono.state.merkle.MerkleNetworkContext();

merkleNetworkContext.setStakingRewardsActivated(true);
merkleNetworkContext.setTotalStakedRewardStart(1L);
merkleNetworkContext.setTotalStakedStart(2L);
merkleNetworkContext.setPendingRewards(3L);
return merkleNetworkContext;
}

private NetworkStakingRewards getExpectedNetworkStakingRewards() {
return new NetworkStakingRewards(true, 1L, 2L, 3L);
}
}

0 comments on commit 250c753

Please sign in to comment.