-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
6a42166
commit cb30c52
Showing
2 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
...ra-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/NetworkInfoImpl.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,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<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(), | ||
""); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...irror-web3/src/test/java/com/hedera/mirror/web3/state/components/NetworkInfoImplTest.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,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(); | ||
} | ||
} |