From cb30c52da766f95fdde713ce703aa0dd9b5b4693 Mon Sep 17 00:00:00 2001 From: Kristiyan Selveliev Date: Sat, 2 Nov 2024 00:21:57 +0200 Subject: [PATCH] Add network info subcomponent needed for the state (#9673) This PR add NetworkInfo component needed for the state of the reusable services code --------- Signed-off-by: Kristiyan Selveliev --- .../state/components/NetworkInfoImpl.java | 88 +++++++++++++++++++ .../state/components/NetworkInfoImplTest.java | 82 +++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/NetworkInfoImpl.java create mode 100644 hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/components/NetworkInfoImplTest.java diff --git a/hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/NetworkInfoImpl.java b/hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/NetworkInfoImpl.java new file mode 100644 index 0000000000..eaa545279c --- /dev/null +++ b/hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/NetworkInfoImpl.java @@ -0,0 +1,88 @@ +/* + * 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.hedera.mirror.web3.state.components; + +import com.hedera.hapi.node.base.AccountID; +import com.hedera.mirror.web3.evm.properties.MirrorNodeEvmProperties; +import com.hedera.node.app.info.SelfNodeInfoImpl; +import com.hedera.pbj.runtime.io.buffer.Bytes; +import com.swirlds.state.spi.info.NetworkInfo; +import com.swirlds.state.spi.info.NodeInfo; +import com.swirlds.state.spi.info.SelfNodeInfo; +import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; +import jakarta.inject.Named; +import java.util.List; +import lombok.RequiredArgsConstructor; + +@Named +@RequiredArgsConstructor +public class NetworkInfoImpl implements NetworkInfo { + + private final MirrorNodeEvmProperties mirrorNodeEvmProperties; + + @Nonnull + @Override + public Bytes ledgerId() { + throw new UnsupportedOperationException("Ledger ID is not supported."); + } + + @Nonnull + @Override + public SelfNodeInfo selfNodeInfo() { + return mockSelfNodeInfo(); + } + + @Nonnull + @Override + public List addressBook() { + throw new UnsupportedOperationException("Address book is not supported."); + } + + @Nullable + @Override + public NodeInfo nodeInfo(long nodeId) { + return null; + } + + @Override + public boolean containsNode(final long nodeId) { + return nodeInfo(nodeId) != null; + } + + /** + * Returns a {@link SelfNodeInfo} that is a complete mock other than the software version present in the given + * configuration. + * + * @return a mock self node info + */ + private SelfNodeInfo mockSelfNodeInfo() { + return new SelfNodeInfoImpl( + 0, + AccountID.DEFAULT, + 0, + "", + 0, + "", + 0, + "", + "", + Bytes.EMPTY, + mirrorNodeEvmProperties.getSemanticEvmVersion(), + ""); + } +} diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/components/NetworkInfoImplTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/components/NetworkInfoImplTest.java new file mode 100644 index 0000000000..9b7c8dfd11 --- /dev/null +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/components/NetworkInfoImplTest.java @@ -0,0 +1,82 @@ +/* + * 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.hedera.mirror.web3.state.components; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import com.hedera.hapi.node.base.AccountID; +import com.hedera.mirror.web3.Web3IntegrationTest; +import com.hedera.mirror.web3.evm.properties.MirrorNodeEvmProperties; +import com.swirlds.state.spi.info.NodeInfo; +import com.swirlds.state.spi.info.SelfNodeInfo; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Test; + +class NetworkInfoImplTest extends Web3IntegrationTest { + + private static final int NODE_ID = 2; + + @Resource + private MirrorNodeEvmProperties mirrorNodeEvmProperties; + + @Resource + private NetworkInfoImpl networkInfoImpl; + + @Test + void testLedgerId() { + final var exception = assertThrows(UnsupportedOperationException.class, () -> networkInfoImpl.ledgerId()); + assertThat(exception.getMessage()).isEqualTo("Ledger ID is not supported."); + } + + @Test + void testSelfNodeInfo() { + SelfNodeInfo selfNodeInfo = networkInfoImpl.selfNodeInfo(); + assertThat(selfNodeInfo).isNotNull().satisfies(info -> { + assertThat(info.nodeId()).isZero(); + assertThat(info.accountId()).isEqualTo(AccountID.DEFAULT); + assertThat(info.hapiVersion()).isEqualTo(mirrorNodeEvmProperties.getSemanticEvmVersion()); + }); + } + + @Test + void testAddressBook() { + final var exception = assertThrows(UnsupportedOperationException.class, () -> networkInfoImpl.addressBook()); + assertThat(exception.getMessage()).isEqualTo("Address book is not supported."); + } + + @Test + void testNodeInfo() { + assertThat(networkInfoImpl.nodeInfo(NODE_ID)).isNull(); + } + + @Test + void testNodeInfoInvalid() { + NodeInfo nodeInfo = networkInfoImpl.nodeInfo(Integer.MAX_VALUE); + assertThat(nodeInfo).isNull(); + } + + @Test + void testContainsNode() { + assertThat(networkInfoImpl.containsNode(NODE_ID)).isFalse(); + } + + @Test + void testContainsNodeInvalid() { + assertThat(networkInfoImpl.containsNode(Integer.MAX_VALUE)).isFalse(); + } +}