Skip to content

Commit

Permalink
Merge branch 'develop' into 13679-tm-upgrade-netty
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-swirlds-labs authored Jun 13, 2024
2 parents fcc34b4 + 621c8fe commit ed4bac7
Show file tree
Hide file tree
Showing 28 changed files with 2,342 additions and 73 deletions.
3 changes: 1 addition & 2 deletions hapi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ tasks.cloneHederaProtobufs {
// uncomment below to use a specific tag
// tag = "v0.51.0"
// uncomment below to use a specific branch
// branch = "main"
branch = "add-response"
branch = "main"
}

sourceSets {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tasks.withType<JavaCompile>().configureEach { options.compilerArgs.add("-Xlint:-
mainModuleInfo { annotationProcessor("dagger.compiler") }

testModuleInfo {
requires("com.hedera.node.app.service.addressbook.impl")
requires("com.hedera.node.app")
requires("com.hedera.node.app.service.mono")
requires("com.hedera.node.config.test.fixtures")
requires("com.swirlds.config.extensions.test.fixtures")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.hedera.node.app.service.addressbook.impl;

import com.hedera.node.app.service.addressbook.AddressBookService;
import com.hedera.node.app.service.addressbook.impl.schemas.V050AddressBookSchema;
import com.hedera.node.app.service.addressbook.impl.schemas.V052AddressBookSchema;
import com.hedera.node.app.spi.RpcService;
import com.swirlds.state.spi.SchemaRegistry;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -30,6 +30,6 @@ public final class AddressBookServiceImpl implements AddressBookService {

@Override
public void registerSchemas(@NonNull SchemaRegistry registry) {
registry.register(new V050AddressBookSchema());
registry.register(new V052AddressBookSchema());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void handle(@NonNull final HandleContext handleContext) {
final var existingNode = nodeStore.get(op.nodeId());
validateFalse(existingNode == null, INVALID_NODE_ID);
if (op.hasAccountId()) {
final var accountId = op.accountIdOrElse(AccountID.DEFAULT);
final var accountId = op.accountIdOrThrow();
validateTrue(accountStore.contains(accountId), INVALID_NODE_ACCOUNT_ID);
}
if (op.hasDescription()) addressBookValidator.validateDescription(op.description(), nodeConfig);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2020-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.node.app.service.addressbook.impl.schemas;

import static com.hedera.node.app.service.addressbook.impl.AddressBookServiceImpl.NODES_KEY;
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.node.base.ServiceEndpoint;
import com.hedera.hapi.node.state.addressbook.Node;
import com.hedera.hapi.node.state.common.EntityNumber;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.platform.state.spi.WritableKVStateBase;
import com.swirlds.state.spi.MigrationContext;
import com.swirlds.state.spi.Schema;
import com.swirlds.state.spi.StateDefinition;
import com.swirlds.state.spi.WritableKVState;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.List;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* General schema for the addressbook service
* {@code V052AddressBookSchema} is used for migrating the address book on Version 0.52.0
*/
public class V052AddressBookSchema extends Schema {
private static final Logger log = LogManager.getLogger(V052AddressBookSchema.class);

private static final long MAX_NODES = 100L;
private static final SemanticVersion VERSION =
SemanticVersion.newBuilder().major(0).minor(52).patch(0).build();

public V052AddressBookSchema() {
super(VERSION);
}

@NonNull
@Override
public Set<StateDefinition> statesToCreate() {
return Set.of(StateDefinition.onDisk(NODES_KEY, EntityNumber.PROTOBUF, Node.PROTOBUF, MAX_NODES));
}

@Override
public void migrate(@NonNull final MigrationContext ctx) {
requireNonNull(ctx);
final WritableKVState<EntityNumber, Node> writableNodes =
ctx.newStates().get(NODES_KEY);
final var networkInfo = ctx.networkInfo();
final var addressBook = networkInfo.addressBook();
log.info("Started migrating nodes from address book");

addressBook.forEach(nodeInfo -> {
final var node = Node.newBuilder()
.nodeId(nodeInfo.nodeId())
.accountId(nodeInfo.accountId())
.description(nodeInfo.memo())
.gossipEndpoint(List.of(
ServiceEndpoint.newBuilder()
.ipAddressV4(Bytes.wrap(nodeInfo.internalHostName()))
.port(nodeInfo.internalPort())
.build(),
ServiceEndpoint.newBuilder()
.ipAddressV4(Bytes.wrap(nodeInfo.externalHostName()))
.port(nodeInfo.externalPort())
.build()))
.gossipCaCertificate(nodeInfo.sigCertBytes())
.weight(nodeInfo.stake())
.build();
writableNodes.put(
EntityNumber.newBuilder().number(nodeInfo.nodeId()).build(), node);
});

if (writableNodes.isModified()) {
((WritableKVStateBase) writableNodes).commit();
}
log.info("Migrated {} nodes from address book", addressBook.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
requires transitive javax.inject;
requires com.hedera.node.app.service.token;
requires com.hedera.node.config;
requires com.swirlds.platform.core;
requires com.hedera.pbj.runtime;
requires org.apache.logging.log4j;
requires static com.github.spotbugs.annotations;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.node.app.service.addressbook.impl.test.schemas;

import static com.hedera.node.app.service.addressbook.impl.AddressBookServiceImpl.NODES_KEY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.given;

import com.hedera.node.app.info.NodeInfoImpl;
import com.hedera.node.app.service.addressbook.impl.schemas.V052AddressBookSchema;
import com.hedera.node.app.service.addressbook.impl.test.handlers.AddressBookTestBase;
import com.hedera.node.app.spi.fixtures.util.LogCaptor;
import com.hedera.node.app.spi.fixtures.util.LogCaptureExtension;
import com.hedera.node.app.spi.fixtures.util.LoggingSubject;
import com.hedera.node.app.spi.fixtures.util.LoggingTarget;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.state.spi.MigrationContext;
import com.swirlds.state.spi.StateDefinition;
import com.swirlds.state.spi.WritableKVState;
import com.swirlds.state.spi.WritableStates;
import com.swirlds.state.spi.info.NetworkInfo;
import java.util.List;
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, LogCaptureExtension.class})
class V052AddressBookSchemaTest extends AddressBookTestBase {
@LoggingTarget
private LogCaptor logCaptor;

@Mock
private MigrationContext migrationContext;

@Mock
private WritableStates writableStates;

@Mock
private NetworkInfo networkInfo;

@Mock
private WritableKVState writableKVState;

@LoggingSubject
private V052AddressBookSchema subject;

@BeforeEach
void setUp() {
subject = new V052AddressBookSchema();
}

@Test
void registersExpectedSchema() {
final var statesToCreate = subject.statesToCreate();
assertThat(statesToCreate).hasSize(1);
final var iter =
statesToCreate.stream().map(StateDefinition::stateKey).sorted().iterator();
assertEquals(NODES_KEY, iter.next());
}

@Test
void migrateAsExpected() {
setupMigrationContext();

assertThatCode(() -> subject.migrate(migrationContext)).doesNotThrowAnyException();
assertThat(logCaptor.infoLogs()).contains("Started migrating nodes from address book");
assertThat(logCaptor.infoLogs()).contains("Migrated 2 nodes from address book");
}

private void setupMigrationContext() {
given(migrationContext.newStates()).willReturn(writableStates);
given(writableStates.get(NODES_KEY)).willReturn(writableKVState);

final var nodeInfo1 = new NodeInfoImpl(
1,
payerId,
0,
"23.45.34.245",
22,
"127.0.0.1",
123,
"pubKey1",
"memo1",
Bytes.wrap(gossipCaCertificate));
final var nodeInfo2 = new NodeInfoImpl(
2,
accountId,
1,
"23.45.34.240",
23,
"127.0.0.2",
123,
"pubKey2",
"memo2",
Bytes.wrap(grpcCertificateHash));
given(networkInfo.addressBook()).willReturn(List.of(nodeInfo1, nodeInfo2));
given(migrationContext.networkInfo()).willReturn(networkInfo);
}
}
Loading

0 comments on commit ed4bac7

Please sign in to comment.