Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add network info subcomponent needed for the state #9673

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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() {
steven-sheehy marked this conversation as resolved.
Show resolved Hide resolved
throw new UnsupportedOperationException("Ledger ID is not supported.");
}

@Nonnull
@Override
public SelfNodeInfo selfNodeInfo() {
return mockSelfNodeInfo();
}

@Nonnull
@Override
public List<NodeInfo> 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(),
"");
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading