Skip to content

Commit ed4bac7

Browse files
Merge branch 'develop' into 13679-tm-upgrade-netty
2 parents fcc34b4 + 621c8fe commit ed4bac7

File tree

28 files changed

+2342
-73
lines changed

28 files changed

+2342
-73
lines changed

hapi/build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ tasks.cloneHederaProtobufs {
3232
// uncomment below to use a specific tag
3333
// tag = "v0.51.0"
3434
// uncomment below to use a specific branch
35-
// branch = "main"
36-
branch = "add-response"
35+
branch = "main"
3736
}
3837

3938
sourceSets {

hedera-node/hedera-addressbook-service-impl/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ tasks.withType<JavaCompile>().configureEach { options.compilerArgs.add("-Xlint:-
2828
mainModuleInfo { annotationProcessor("dagger.compiler") }
2929

3030
testModuleInfo {
31-
requires("com.hedera.node.app.service.addressbook.impl")
31+
requires("com.hedera.node.app")
3232
requires("com.hedera.node.app.service.mono")
3333
requires("com.hedera.node.config.test.fixtures")
3434
requires("com.swirlds.config.extensions.test.fixtures")

hedera-node/hedera-addressbook-service-impl/src/main/java/com/hedera/node/app/service/addressbook/impl/AddressBookServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.hedera.node.app.service.addressbook.impl;
1818

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

3131
@Override
3232
public void registerSchemas(@NonNull SchemaRegistry registry) {
33-
registry.register(new V050AddressBookSchema());
33+
registry.register(new V052AddressBookSchema());
3434
}
3535
}

hedera-node/hedera-addressbook-service-impl/src/main/java/com/hedera/node/app/service/addressbook/impl/handlers/NodeUpdateHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void handle(@NonNull final HandleContext handleContext) {
8686
final var existingNode = nodeStore.get(op.nodeId());
8787
validateFalse(existingNode == null, INVALID_NODE_ID);
8888
if (op.hasAccountId()) {
89-
final var accountId = op.accountIdOrElse(AccountID.DEFAULT);
89+
final var accountId = op.accountIdOrThrow();
9090
validateTrue(accountStore.contains(accountId), INVALID_NODE_ACCOUNT_ID);
9191
}
9292
if (op.hasDescription()) addressBookValidator.validateDescription(op.description(), nodeConfig);

hedera-node/hedera-addressbook-service-impl/src/main/java/com/hedera/node/app/service/addressbook/impl/schemas/V050AddressBookSchema.java

Lines changed: 0 additions & 60 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (C) 2020-2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hedera.node.app.service.addressbook.impl.schemas;
18+
19+
import static com.hedera.node.app.service.addressbook.impl.AddressBookServiceImpl.NODES_KEY;
20+
import static java.util.Objects.requireNonNull;
21+
22+
import com.hedera.hapi.node.base.SemanticVersion;
23+
import com.hedera.hapi.node.base.ServiceEndpoint;
24+
import com.hedera.hapi.node.state.addressbook.Node;
25+
import com.hedera.hapi.node.state.common.EntityNumber;
26+
import com.hedera.pbj.runtime.io.buffer.Bytes;
27+
import com.swirlds.platform.state.spi.WritableKVStateBase;
28+
import com.swirlds.state.spi.MigrationContext;
29+
import com.swirlds.state.spi.Schema;
30+
import com.swirlds.state.spi.StateDefinition;
31+
import com.swirlds.state.spi.WritableKVState;
32+
import edu.umd.cs.findbugs.annotations.NonNull;
33+
import java.util.List;
34+
import java.util.Set;
35+
import org.apache.logging.log4j.LogManager;
36+
import org.apache.logging.log4j.Logger;
37+
38+
/**
39+
* General schema for the addressbook service
40+
* {@code V052AddressBookSchema} is used for migrating the address book on Version 0.52.0
41+
*/
42+
public class V052AddressBookSchema extends Schema {
43+
private static final Logger log = LogManager.getLogger(V052AddressBookSchema.class);
44+
45+
private static final long MAX_NODES = 100L;
46+
private static final SemanticVersion VERSION =
47+
SemanticVersion.newBuilder().major(0).minor(52).patch(0).build();
48+
49+
public V052AddressBookSchema() {
50+
super(VERSION);
51+
}
52+
53+
@NonNull
54+
@Override
55+
public Set<StateDefinition> statesToCreate() {
56+
return Set.of(StateDefinition.onDisk(NODES_KEY, EntityNumber.PROTOBUF, Node.PROTOBUF, MAX_NODES));
57+
}
58+
59+
@Override
60+
public void migrate(@NonNull final MigrationContext ctx) {
61+
requireNonNull(ctx);
62+
final WritableKVState<EntityNumber, Node> writableNodes =
63+
ctx.newStates().get(NODES_KEY);
64+
final var networkInfo = ctx.networkInfo();
65+
final var addressBook = networkInfo.addressBook();
66+
log.info("Started migrating nodes from address book");
67+
68+
addressBook.forEach(nodeInfo -> {
69+
final var node = Node.newBuilder()
70+
.nodeId(nodeInfo.nodeId())
71+
.accountId(nodeInfo.accountId())
72+
.description(nodeInfo.memo())
73+
.gossipEndpoint(List.of(
74+
ServiceEndpoint.newBuilder()
75+
.ipAddressV4(Bytes.wrap(nodeInfo.internalHostName()))
76+
.port(nodeInfo.internalPort())
77+
.build(),
78+
ServiceEndpoint.newBuilder()
79+
.ipAddressV4(Bytes.wrap(nodeInfo.externalHostName()))
80+
.port(nodeInfo.externalPort())
81+
.build()))
82+
.gossipCaCertificate(nodeInfo.sigCertBytes())
83+
.weight(nodeInfo.stake())
84+
.build();
85+
writableNodes.put(
86+
EntityNumber.newBuilder().number(nodeInfo.nodeId()).build(), node);
87+
});
88+
89+
if (writableNodes.isModified()) {
90+
((WritableKVStateBase) writableNodes).commit();
91+
}
92+
log.info("Migrated {} nodes from address book", addressBook.size());
93+
}
94+
}

hedera-node/hedera-addressbook-service-impl/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
requires transitive javax.inject;
1111
requires com.hedera.node.app.service.token;
1212
requires com.hedera.node.config;
13+
requires com.swirlds.platform.core;
1314
requires com.hedera.pbj.runtime;
1415
requires org.apache.logging.log4j;
1516
requires static com.github.spotbugs.annotations;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hedera.node.app.service.addressbook.impl.test.schemas;
18+
19+
import static com.hedera.node.app.service.addressbook.impl.AddressBookServiceImpl.NODES_KEY;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.mockito.BDDMockito.given;
24+
25+
import com.hedera.node.app.info.NodeInfoImpl;
26+
import com.hedera.node.app.service.addressbook.impl.schemas.V052AddressBookSchema;
27+
import com.hedera.node.app.service.addressbook.impl.test.handlers.AddressBookTestBase;
28+
import com.hedera.node.app.spi.fixtures.util.LogCaptor;
29+
import com.hedera.node.app.spi.fixtures.util.LogCaptureExtension;
30+
import com.hedera.node.app.spi.fixtures.util.LoggingSubject;
31+
import com.hedera.node.app.spi.fixtures.util.LoggingTarget;
32+
import com.hedera.pbj.runtime.io.buffer.Bytes;
33+
import com.swirlds.state.spi.MigrationContext;
34+
import com.swirlds.state.spi.StateDefinition;
35+
import com.swirlds.state.spi.WritableKVState;
36+
import com.swirlds.state.spi.WritableStates;
37+
import com.swirlds.state.spi.info.NetworkInfo;
38+
import java.util.List;
39+
import org.junit.jupiter.api.BeforeEach;
40+
import org.junit.jupiter.api.Test;
41+
import org.junit.jupiter.api.extension.ExtendWith;
42+
import org.mockito.Mock;
43+
import org.mockito.junit.jupiter.MockitoExtension;
44+
45+
@ExtendWith({MockitoExtension.class, LogCaptureExtension.class})
46+
class V052AddressBookSchemaTest extends AddressBookTestBase {
47+
@LoggingTarget
48+
private LogCaptor logCaptor;
49+
50+
@Mock
51+
private MigrationContext migrationContext;
52+
53+
@Mock
54+
private WritableStates writableStates;
55+
56+
@Mock
57+
private NetworkInfo networkInfo;
58+
59+
@Mock
60+
private WritableKVState writableKVState;
61+
62+
@LoggingSubject
63+
private V052AddressBookSchema subject;
64+
65+
@BeforeEach
66+
void setUp() {
67+
subject = new V052AddressBookSchema();
68+
}
69+
70+
@Test
71+
void registersExpectedSchema() {
72+
final var statesToCreate = subject.statesToCreate();
73+
assertThat(statesToCreate).hasSize(1);
74+
final var iter =
75+
statesToCreate.stream().map(StateDefinition::stateKey).sorted().iterator();
76+
assertEquals(NODES_KEY, iter.next());
77+
}
78+
79+
@Test
80+
void migrateAsExpected() {
81+
setupMigrationContext();
82+
83+
assertThatCode(() -> subject.migrate(migrationContext)).doesNotThrowAnyException();
84+
assertThat(logCaptor.infoLogs()).contains("Started migrating nodes from address book");
85+
assertThat(logCaptor.infoLogs()).contains("Migrated 2 nodes from address book");
86+
}
87+
88+
private void setupMigrationContext() {
89+
given(migrationContext.newStates()).willReturn(writableStates);
90+
given(writableStates.get(NODES_KEY)).willReturn(writableKVState);
91+
92+
final var nodeInfo1 = new NodeInfoImpl(
93+
1,
94+
payerId,
95+
0,
96+
"23.45.34.245",
97+
22,
98+
"127.0.0.1",
99+
123,
100+
"pubKey1",
101+
"memo1",
102+
Bytes.wrap(gossipCaCertificate));
103+
final var nodeInfo2 = new NodeInfoImpl(
104+
2,
105+
accountId,
106+
1,
107+
"23.45.34.240",
108+
23,
109+
"127.0.0.2",
110+
123,
111+
"pubKey2",
112+
"memo2",
113+
Bytes.wrap(grpcCertificateHash));
114+
given(networkInfo.addressBook()).willReturn(List.of(nodeInfo1, nodeInfo2));
115+
given(migrationContext.networkInfo()).willReturn(networkInfo);
116+
}
117+
}

0 commit comments

Comments
 (0)