Skip to content

Commit

Permalink
chore: Enable the happy path TSS test (#16406)
Browse files Browse the repository at this point in the history
Signed-off-by: Neeharika-Sompalli <[email protected]>
  • Loading branch information
Neeharika-Sompalli authored Nov 4, 2024
1 parent 4f6fd61 commit f915acc
Show file tree
Hide file tree
Showing 50 changed files with 945 additions and 408 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;

/**
* Provides read-only methods for interacting with the underlying data storage mechanisms for
Expand Down Expand Up @@ -89,22 +89,22 @@ public Iterator<EntityNumber> keys() {

private Roster constructFromNodesState(@NonNull final ReadableKVState<EntityNumber, Node> nodesState) {
final var rosterEntries = new ArrayList<RosterEntry>();
for (final Iterator<EntityNumber> it = nodesState.keys(); it.hasNext(); ) {
for (final var it = nodesState.keys(); it.hasNext(); ) {
final var nodeNumber = it.next();
final var nodeDetail = nodesState.get(nodeNumber);
if (!nodeDetail.deleted()) {
final var node = requireNonNull(nodesState.get(nodeNumber));
if (!node.deleted()) {
final var entry = RosterEntry.newBuilder()
.nodeId(nodeDetail.nodeId())
.weight(nodeDetail.weight())
.gossipCaCertificate(nodeDetail.gossipCaCertificate())
.gossipEndpoint(nodeDetail.gossipEndpoint())
.tssEncryptionKey(nodeDetail.tssEncryptionKey())
.nodeId(node.nodeId())
.weight(node.weight())
.gossipCaCertificate(node.gossipCaCertificate())
.gossipEndpoint(node.gossipEndpoint())
.tssEncryptionKey(node.tssEncryptionKey())
.build();
rosterEntries.add(entry);
}
}

rosterEntries.sort((re1, re2) -> Objects.compare(re1.nodeId(), re2.nodeId(), Long::compareTo));
rosterEntries.sort(Comparator.comparingLong(RosterEntry::nodeId));
return Roster.newBuilder().rosterEntries(rosterEntries).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.hedera.hapi.node.base.AccountID;
import com.hedera.hapi.node.base.ServiceEndpoint;
import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.common.platform.NodeId;
import com.swirlds.state.State;
Expand Down Expand Up @@ -89,6 +90,11 @@ public void updateFrom(final State state) {
throw new UnsupportedOperationException("Not implemented");
}

@Override
public Roster roster() {
return Roster.DEFAULT;
}

private static NodeInfo fakeInfoWith(
final long nodeId,
@NonNull final AccountID nodeAccountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
import com.hedera.node.app.store.ReadableStoreFactory;
import com.hedera.node.app.throttle.CongestionThrottleService;
import com.hedera.node.app.tss.TssBaseService;
import com.hedera.node.app.tss.TssMetrics;
import com.hedera.node.app.version.ServicesSoftwareVersion;
import com.hedera.node.app.workflows.handle.HandleWorkflow;
import com.hedera.node.app.workflows.ingest.IngestWorkflow;
Expand Down Expand Up @@ -858,14 +857,6 @@ public void setInitialStateHash(@NonNull final Hash stateHash) {
initialStateHashFuture = completedFuture(stateHash.getBytes());
}

/**
* @param metrics the metrics object being used to report tss performance
*/
public void registerTssMetrics(@NonNull final Metrics metrics) {
requireNonNull(metrics);
tssBaseService.registerMetrics(new TssMetrics(metrics));
}

/*==================================================================================================================
*
* Exposed for use by embedded Hedera
Expand Down Expand Up @@ -953,7 +944,11 @@ private void initializeDagger(@NonNull final State state, @NonNull final InitTri
.consensusSnapshotOrThrow()
.round();
final var initialStateHash = new InitialStateHash(initialStateHashFuture, roundNum);
final var networkInfo = new StateNetworkInfo(state, platform.getSelfId().id(), configProvider);

final var activeRoster = tssBaseService.chooseRosterForNetwork(
state, trigger, serviceMigrator, version, configProvider.getConfiguration());
final var networkInfo =
new StateNetworkInfo(state, activeRoster, platform.getSelfId().id(), configProvider);
// Fully qualified so as to not confuse javadoc
daggerApp = com.hedera.node.app.DaggerHederaInjectionComponent.builder()
.configProviderImpl(configProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,6 @@ public static void main(final String... args) throws Exception {
final var merkleCryptography = MerkleCryptographyFactory.create(configuration, cryptography);
MerkleCryptoFactory.set(merkleCryptography);

// Register the metrics related to TSS functionalities
hedera.registerTssMetrics(metrics);

// Create the platform context
final var platformContext = PlatformContext.create(
configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@

/**
* Provides information about the network, including the ledger ID. This is constructed from the
* genesis roster on disk.
* roster provided.
*/
@Singleton
public class GenesisNetworkInfo implements NetworkInfo {
private final Map<Long, NodeInfo> nodeInfos;
private final Bytes ledgerId;
private final Roster genesisRoster;

/**
* Constructs a new {@link GenesisNetworkInfo} instance.
Expand All @@ -49,6 +50,7 @@ public class GenesisNetworkInfo implements NetworkInfo {
public GenesisNetworkInfo(final Roster genesisRoster, final Bytes ledgerId) {
this.nodeInfos = buildNodeInfoMap(genesisRoster);
this.ledgerId = ledgerId;
this.genesisRoster = genesisRoster;
}

/**
Expand Down Expand Up @@ -100,6 +102,11 @@ public void updateFrom(final State state) {
throw new UnsupportedOperationException("Not implemented");
}

@Override
public Roster roster() {
return genesisRoster;
}

/**
* Builds a node info from a roster entry from the given roster.
* Since this is only used in the genesis case, the account ID is generated from the node ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@

import static com.hedera.node.app.info.NodeInfoImpl.fromRosterEntry;
import static com.hedera.node.app.service.addressbook.AddressBookHelper.NODES_KEY;
import static com.swirlds.platform.roster.RosterRetriever.retrieve;
import static com.swirlds.platform.roster.RosterRetriever.buildRoster;
import static com.swirlds.platform.roster.RosterRetriever.retrieveActiveOrGenesisRoster;
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.state.addressbook.Node;
import com.hedera.hapi.node.state.common.EntityNumber;
import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.node.app.service.addressbook.AddressBookService;
import com.hedera.node.config.ConfigProvider;
import com.hedera.node.config.data.LedgerConfig;
import com.hedera.node.config.data.TssConfig;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.platform.state.service.PlatformStateService;
import com.swirlds.platform.state.service.ReadablePlatformStateStore;
import com.swirlds.state.State;
import com.swirlds.state.spi.ReadableKVState;
import com.swirlds.state.spi.info.NetworkInfo;
Expand All @@ -47,10 +53,26 @@ public class StateNetworkInfo implements NetworkInfo {
private final Bytes ledgerId;
private final Map<Long, NodeInfo> nodeInfos;
private final long selfId;
private final ConfigProvider configProvider;
private Roster activeRoster;

/**
* Constructs a new network information provider from the given state, roster, selfID, and configuration provider.
*
* @param state the state to retrieve the network information from
* @param roster the roster to retrieve the network information from
* @param selfId the ID of the node
* @param configProvider the configuration provider to retrieve the ledger ID from
*/
public StateNetworkInfo(
@NonNull final State state, final long selfId, @NonNull final ConfigProvider configProvider) {
@NonNull final State state,
@NonNull final Roster roster,
final long selfId,
@NonNull final ConfigProvider configProvider) {
this.selfId = selfId;
this.activeRoster = requireNonNull(roster);
// We keep this for now to check the keyCandidateRoster feature flag in updateFrom()
this.configProvider = requireNonNull(configProvider);
this.nodeInfos = buildNodeInfoMap(state);
// Load the ledger ID from configuration
final var config = configProvider.getConfiguration();
Expand Down Expand Up @@ -89,6 +111,17 @@ public boolean containsNode(final long nodeId) {

@Override
public void updateFrom(@NonNull final State state) {
final var config = configProvider.getConfiguration();
if (config.getConfigData(TssConfig.class).keyCandidateRoster()) {
activeRoster = retrieveActiveOrGenesisRoster(state);
} else {
// When the feature flag is disabled, the rosters in RosterService state are not up-to-date
// FUTURE: Once TSS Roster is implemented in the future, this will be removed and use roster state
// instead of the address book
final var readablePlatformStateStore =
new ReadablePlatformStateStore(state.getReadableStates(PlatformStateService.NAME));
activeRoster = buildRoster(requireNonNull(readablePlatformStateStore.getAddressBook()));
}
nodeInfos.clear();
nodeInfos.putAll(buildNodeInfoMap(state));
}
Expand All @@ -103,16 +136,24 @@ public void updateFrom(@NonNull final State state) {
*/
private Map<Long, NodeInfo> buildNodeInfoMap(final State state) {
final var nodeInfos = new LinkedHashMap<Long, NodeInfo>();
final var rosterEntries = retrieve(state).rosterEntries();
final var rosterEntries = activeRoster.rosterEntries();
final ReadableKVState<EntityNumber, Node> nodeState =
state.getReadableStates(AddressBookService.NAME).get(NODES_KEY);
for (final var rosterEntry : rosterEntries) {
final var node = nodeState.get(
EntityNumber.newBuilder().number(rosterEntry.nodeId()).build());
if (node != null) {
nodeInfos.put(rosterEntry.nodeId(), fromRosterEntry(rosterEntry, node));
}
// At genesis the node store is derived from the roster, hence must have info for every
// node id; and from then on, the roster is derived from the node store, and hence the
// node store must have every node id in the roster.
final var node = requireNonNull(nodeState.get(new EntityNumber(rosterEntry.nodeId())));
// Notice it's possible the node could be deleted here, because a DAB transaction removed
// it from the future address book; that doesn't mean we should stop using it in the current
// version of the software
nodeInfos.put(rosterEntry.nodeId(), fromRosterEntry(rosterEntry, node));
}
return nodeInfos;
}

@Override
public Roster roster() {
return activeRoster;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hedera.node.app.info;

import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.state.State;
import com.swirlds.state.spi.info.NetworkInfo;
Expand Down Expand Up @@ -65,4 +66,9 @@ public boolean containsNode(final long nodeId) {
public void updateFrom(final State state) {
throw new UnsupportedOperationException("Not implemented");
}

@Override
public Roster roster() {
throw new UnsupportedOperationException("Not implemented");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.hedera.node.app.service.schedule.ScheduleService;
import com.hedera.node.app.service.token.TokenService;
import com.hedera.node.app.service.util.UtilService;
import com.hedera.node.app.tss.TssBaseService;
import edu.umd.cs.findbugs.annotations.NonNull;
import javax.inject.Inject;
import javax.inject.Singleton;
Expand Down Expand Up @@ -120,6 +121,7 @@ public String getServiceName(@NonNull final TransactionBody txBody) {
};

case NODE_CREATE, NODE_DELETE, NODE_UPDATE -> AddressBookService.NAME;
case TSS_MESSAGE, TSS_VOTE -> TssBaseService.NAME;

default -> NON_EXISTING_SERVICE;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
import com.hedera.node.app.service.token.impl.ReadableStakingInfoStoreImpl;
import com.hedera.node.app.service.token.impl.ReadableTokenRelationStoreImpl;
import com.hedera.node.app.service.token.impl.ReadableTokenStoreImpl;
import com.hedera.node.app.tss.TssBaseService;
import com.hedera.node.app.tss.stores.ReadableTssStore;
import com.hedera.node.app.tss.stores.ReadableTssStoreImpl;
import com.swirlds.common.RosterStateId;
import com.swirlds.platform.state.MerkleStateRoot;
import com.swirlds.platform.state.service.PlatformStateService;
Expand Down Expand Up @@ -119,6 +122,7 @@ private static Map<Class<?>, StoreEntry> createFactoryMap() {
ReadablePlatformStateStore.class,
new StoreEntry(PlatformStateService.NAME, ReadablePlatformStateStore::new));
newMap.put(ReadableRosterStore.class, new StoreEntry(RosterStateId.NAME, ReadableRosterStoreImpl::new));
newMap.put(ReadableTssStore.class, new StoreEntry(TssBaseService.NAME, ReadableTssStoreImpl::new));
return Collections.unmodifiableMap(newMap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import com.hedera.node.app.service.token.impl.WritableTokenRelationStore;
import com.hedera.node.app.service.token.impl.WritableTokenStore;
import com.hedera.node.app.spi.metrics.StoreMetricsService;
import com.hedera.node.app.tss.TssBaseService;
import com.hedera.node.app.tss.stores.WritableTssStore;
import com.swirlds.common.RosterStateId;
import com.swirlds.config.api.Configuration;
import com.swirlds.platform.state.service.WritableRosterStore;
Expand Down Expand Up @@ -109,6 +111,10 @@ private static Map<Class<?>, StoreEntry> createFactoryMap() {
newMap.put(
WritableRosterStore.class,
new StoreEntry(RosterStateId.NAME, (states, config, metrics) -> new WritableRosterStore(states)));
// TSSBase Service
newMap.put(
WritableTssStore.class,
new StoreEntry(TssBaseService.NAME, (states, config, metrics) -> new WritableTssStore(states)));
return Collections.unmodifiableMap(newMap);
}

Expand Down
Loading

0 comments on commit f915acc

Please sign in to comment.