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

Migrated ConsensusService to new design #6239

Merged
merged 1 commit into from
Apr 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.hedera.hapi.node.base.Timestamp;
import com.hedera.hapi.node.transaction.Query;
import com.hedera.node.app.hapi.utils.fee.FeeObject;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.mono.context.primitives.StateView;
import com.hedera.node.app.service.mono.fees.calculation.UsageBasedFeeCalculator;
import com.hedera.node.app.service.mono.fees.calculation.UsagePricesProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import com.hedera.hapi.node.base.Key;
import com.hedera.hapi.node.state.consensus.Topic;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.mono.fees.calculation.consensus.queries.GetTopicInfoResourceUsage;
import com.hedera.node.app.service.mono.legacy.core.jproto.JKey;
import com.hedera.node.app.service.mono.pbj.PbjConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import com.hedera.node.app.service.admin.FreezeService;
import com.hedera.node.app.service.admin.impl.ReadableSpecialFileStore;
import com.hedera.node.app.service.consensus.ConsensusService;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStoreImpl;
import com.hedera.node.app.service.schedule.ScheduleService;
import com.hedera.node.app.service.schedule.impl.ReadableScheduleStore;
import com.hedera.node.app.service.token.ReadableAccountStore;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class ReadableStoreFactory {
AccountAccess.class, new StoreEntry(TokenService.NAME, ReadableAccountStoreImpl::new),
ReadableAccountStore.class, new StoreEntry(TokenService.NAME, ReadableAccountStoreImpl::new),
ReadableTokenStore.class, new StoreEntry(TokenService.NAME, ReadableTokenStoreImpl::new),
ReadableTopicStore.class, new StoreEntry(ConsensusService.NAME, ReadableTopicStore::new),
ReadableTopicStore.class, new StoreEntry(ConsensusService.NAME, ReadableTopicStoreImpl::new),
ReadableScheduleStore.class, new StoreEntry(ScheduleService.NAME, ReadableScheduleStore::new),
ReadableSpecialFileStore.class, new StoreEntry(FreezeService.NAME, ReadableSpecialFileStore::new));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.hedera.hapi.node.consensus.ConsensusUpdateTopicTransactionBody;
import com.hedera.hapi.node.transaction.TransactionBody;
import com.hedera.node.app.service.admin.impl.ReadableSpecialFileStore;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.impl.WritableTopicStore;
import com.hedera.node.app.service.consensus.impl.config.ConsensusServiceConfig;
import com.hedera.node.app.service.consensus.impl.records.ConsensusCreateTopicRecordBuilder;
Expand Down Expand Up @@ -123,11 +122,11 @@ public void dispatchPreHandle(@NonNull final PreHandleContext context) throws Pr
case CONSENSUS_CREATE_TOPIC -> handlers.consensusCreateTopicHandler()
.preHandle(context);
case CONSENSUS_UPDATE_TOPIC -> handlers.consensusUpdateTopicHandler()
.preHandle(context, context.createStore(ReadableTopicStore.class));
.preHandle(context);
case CONSENSUS_DELETE_TOPIC -> handlers.consensusDeleteTopicHandler()
.preHandle(context, context.createStore(ReadableTopicStore.class));
.preHandle(context);
case CONSENSUS_SUBMIT_MESSAGE -> handlers.consensusSubmitMessageHandler()
.preHandle(context, context.createStore(ReadableTopicStore.class));
.preHandle(context);

case CONTRACT_CREATE_INSTANCE -> handlers.contractCreateHandler().preHandle(context);
case CONTRACT_UPDATE_INSTANCE -> handlers.contractUpdateHandler().preHandle(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.hedera.node.app.fees.MonoGetTopicInfoUsage;
import com.hedera.node.app.hapi.utils.exception.InvalidTxBodyException;
import com.hedera.node.app.hapi.utils.fee.FeeObject;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.mono.context.primitives.StateView;
import com.hedera.node.app.service.mono.fees.FeeCalculator;
import com.hedera.node.app.service.mono.fees.HbarCentExchange;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.hedera.hapi.node.base.Timestamp;
import com.hedera.hapi.node.transaction.Query;
import com.hedera.node.app.hapi.utils.fee.FeeObject;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.mono.context.primitives.StateView;
import com.hedera.node.app.service.mono.fees.calculation.UsageBasedFeeCalculator;
import com.hedera.node.app.service.mono.fees.calculation.UsagePricesProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.hedera.hapi.node.base.TopicID;
import com.hedera.hapi.node.consensus.ConsensusGetTopicInfoQuery;
import com.hedera.hapi.node.state.consensus.Topic;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.mono.fees.calculation.consensus.queries.GetTopicInfoResourceUsage;
import com.hedera.node.app.service.mono.legacy.core.jproto.JKey;
import com.hedera.node.app.service.mono.pbj.PbjConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,32 @@ void testPreHandleDispatch(

private static Stream<Arguments> getDispatchParameters() {
return Stream.of(
// consensus
Arguments.of(
TransactionBody.newBuilder()
.consensusCreateTopic(ConsensusCreateTopicTransactionBody.DEFAULT)
.build(),
(Function<TransactionHandlers, TransactionHandler>)
TransactionHandlers::consensusCreateTopicHandler),
Arguments.of(
TransactionBody.newBuilder()
.consensusUpdateTopic(ConsensusUpdateTopicTransactionBody.DEFAULT)
.build(),
(Function<TransactionHandlers, TransactionHandler>)
TransactionHandlers::consensusUpdateTopicHandler),
Arguments.of(
TransactionBody.newBuilder()
.consensusDeleteTopic(ConsensusDeleteTopicTransactionBody.DEFAULT)
.build(),
(Function<TransactionHandlers, TransactionHandler>)
TransactionHandlers::consensusDeleteTopicHandler),
Arguments.of(
TransactionBody.newBuilder()
.consensusSubmitMessage(ConsensusSubmitMessageTransactionBody.DEFAULT)
.build(),
(Function<TransactionHandlers, TransactionHandler>)
TransactionHandlers::consensusSubmitMessageHandler),

// crypto
Arguments.of(
TransactionBody.newBuilder()
Expand Down Expand Up @@ -729,32 +755,6 @@ void testPreHandleWithPayer(final TransactionBody txBody, final DispatchToHandle

private static Stream<Arguments> getDispatchParametersOld() {
return Stream.of(
// consensus
Arguments.of(
TransactionBody.newBuilder()
.consensusCreateTopic(ConsensusCreateTopicTransactionBody.DEFAULT)
.build(),
(DispatchToHandler) (handlers, meta) ->
verify(handlers.consensusCreateTopicHandler()).preHandle(meta)),
Arguments.of(
TransactionBody.newBuilder()
.consensusUpdateTopic(ConsensusUpdateTopicTransactionBody.DEFAULT)
.build(),
(DispatchToHandler) (handlers, meta) ->
verify(handlers.consensusUpdateTopicHandler()).preHandle(eq(meta), any())),
Arguments.of(
TransactionBody.newBuilder()
.consensusDeleteTopic(ConsensusDeleteTopicTransactionBody.DEFAULT)
.build(),
(DispatchToHandler) (handlers, meta) ->
verify(handlers.consensusDeleteTopicHandler()).preHandle(eq(meta), any())),
Arguments.of(
TransactionBody.newBuilder()
.consensusSubmitMessage(ConsensusSubmitMessageTransactionBody.DEFAULT)
.build(),
(DispatchToHandler) (handlers, meta) ->
verify(handlers.consensusSubmitMessageHandler()).preHandle(eq(meta), any())),

// contract
Arguments.of(
TransactionBody.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.BDDMockito.given;

import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.spi.state.ReadableStates;
import com.hedera.node.app.state.HederaState;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ dependencies {
testImplementation(testLibs.bundles.testing)
testImplementation(testFixtures(project(":hedera-node:hedera-mono-service")))
testImplementation(testFixtures(project(":hedera-node:hedera-app-spi")))
testImplementation(project(":hedera-node:hedera-token-service"))
testImplementation(testLibs.mockito.inline)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 Hedera Hashgraph, LLC
* Copyright (C) 2023 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.
Expand All @@ -20,6 +20,8 @@

import com.hedera.hapi.node.base.TopicID;
import com.hedera.hapi.node.state.consensus.Topic;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.consensus.TopicMetadata;
import com.hedera.node.app.service.mono.utils.EntityNum;
import com.hedera.node.app.spi.state.ReadableKVState;
import com.hedera.node.app.spi.state.ReadableStates;
Expand All @@ -34,16 +36,16 @@
*
* <p>This class is not exported from the module. It is an internal implementation detail.
*/
public class ReadableTopicStore extends TopicStore {
public class ReadableTopicStoreImpl extends TopicStore implements ReadableTopicStore {
/** The underlying data storage class that holds the topic data. */
private final ReadableKVState<EntityNum, Topic> topicState;

/**
* Create a new {@link ReadableTopicStore} instance.
* Create a new {@link ReadableTopicStoreImpl} instance.
*
* @param states The state to use.
*/
public ReadableTopicStore(@NonNull final ReadableStates states) {
public ReadableTopicStoreImpl(@NonNull final ReadableStates states) {
requireNonNull(states);

this.topicState = states.get("TOPICS");
Expand All @@ -57,12 +59,14 @@ public ReadableTopicStore(@NonNull final ReadableStates states) {
* @return topic's metadata
*/
// TODO : Change to return Topic instead of TopicMetadata
@Nullable
public TopicMetadata getTopicMetadata(@Nullable final TopicID id) {
final var topic = getTopicLeaf(id);
return topic.map(TopicStore::topicMetaFrom).orElse(null);
}

public Optional<Topic> getTopicLeaf(TopicID id) {
@NonNull
public Optional<Topic> getTopicLeaf(@NonNull final TopicID id) {
return Optional.ofNullable(Objects.requireNonNull(topicState).get(EntityNum.fromTopicId(id)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.hedera.hapi.node.base.Key;
import com.hedera.hapi.node.base.Timestamp;
import com.hedera.hapi.node.state.consensus.Topic;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.consensus.TopicMetadata;
import java.util.Optional;
import java.util.OptionalLong;

Expand All @@ -44,32 +46,4 @@ public static TopicMetadata topicMetaFrom(final Topic topic) {
topic.topicNumber(),
topic.deleted());
}

// TODO : Remove use of TopicMetadata and change to use Topic instead

/**
* Topic metadata
*
* @param memo topic's memo
* @param adminKey topic's admin key
* @param submitKey topic's submit key
* @param autoRenewDurationSeconds topic's auto-renew duration in seconds
* @param autoRenewAccountId topic's auto-renew account id
* @param expirationTimestamp topic's expiration timestamp
* @param sequenceNumber topic's sequence number
* @param runningHash topic's running hash
* @param key topic's key
* @param isDeleted topic's deleted flag
*/
public record TopicMetadata(
Optional<String> memo,
Key adminKey,
Key submitKey,
long autoRenewDurationSeconds,
OptionalLong autoRenewAccountId,
Timestamp expirationTimestamp,
long sequenceNumber,
byte[] runningHash,
long key,
boolean isDeleted) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.hedera.hapi.node.base.HederaFunctionality;
import com.hedera.hapi.node.consensus.ConsensusCreateTopicTransactionBody;
import com.hedera.hapi.node.state.consensus.Topic;
import com.hedera.hapi.node.transaction.TransactionBody;
import com.hedera.node.app.service.consensus.impl.WritableTopicStore;
import com.hedera.node.app.service.consensus.impl.config.ConsensusServiceConfig;
import com.hedera.node.app.service.consensus.impl.records.ConsensusCreateTopicRecordBuilder;
Expand All @@ -55,19 +54,7 @@ public ConsensusCreateTopicHandler() {
// Exists for injection
}

/**
* This method is called during the pre-handle workflow.
*
* <p>Typically, this method validates the {@link TransactionBody} semantically, gathers all
* required keys, and warms the cache.
*
* <p>Please note: the method signature is just a placeholder which is most likely going to
* change.
*
* @param context the {@link PreHandleContext} which collects all information that will be
* passed to the handle stage
* @throws NullPointerException if one of the arguments is {@code null}
*/
@Override
public void preHandle(@NonNull final PreHandleContext context) throws PreCheckException {
requireNonNull(context);
final var op = context.body().consensusCreateTopicOrThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.hedera.hapi.node.base.TopicID;
import com.hedera.hapi.node.consensus.ConsensusDeleteTopicTransactionBody;
import com.hedera.hapi.node.state.consensus.Topic;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.consensus.impl.WritableTopicStore;
import com.hedera.node.app.service.consensus.impl.records.ConsensusDeleteTopicRecordBuilder;
import com.hedera.node.app.service.consensus.impl.records.DeleteTopicRecordBuilder;
Expand All @@ -47,22 +47,12 @@ public ConsensusDeleteTopicHandler() {
// Exists for injection
}

/**
* This method is called during the pre-handle workflow.
*
* <p>Determines signatures needed for deleting a consensus topic
*
* @param context the {@link PreHandleContext} which collects all information that will be
* passed to {@code handle()}
* @param topicStore the {@link ReadableTopicStore} to use to resolve topic metadata
* @throws NullPointerException if any of the arguments are {@code null}
*/
public void preHandle(@NonNull final PreHandleContext context, @NonNull ReadableTopicStore topicStore)
throws PreCheckException {
@Override
public void preHandle(@NonNull final PreHandleContext context) throws PreCheckException {
requireNonNull(context);
requireNonNull(topicStore);

final var op = context.body().consensusDeleteTopicOrThrow();
final var topicStore = context.createStore(ReadableTopicStore.class);
// The topic ID must be present on the transaction and the topic must exist.
final var topic = topicStore.getTopicMetadata(op.topicID());
mustExist(topic, INVALID_TOPIC_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.hedera.hapi.node.consensus.ConsensusTopicInfo;
import com.hedera.hapi.node.transaction.Query;
import com.hedera.hapi.node.transaction.Response;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.spi.info.NetworkInfo;
import com.hedera.node.app.spi.workflows.PaidQueryHandler;
import com.hedera.node.app.spi.workflows.PreCheckException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.hedera.hapi.node.consensus.ConsensusSubmitMessageTransactionBody;
import com.hedera.hapi.node.state.consensus.Topic;
import com.hedera.hapi.node.transaction.TransactionBody;
import com.hedera.node.app.service.consensus.impl.ReadableTopicStore;
import com.hedera.node.app.service.consensus.ReadableTopicStore;
import com.hedera.node.app.service.consensus.impl.WritableTopicStore;
import com.hedera.node.app.service.consensus.impl.config.ConsensusServiceConfig;
import com.hedera.node.app.service.consensus.impl.records.ConsensusSubmitMessageRecordBuilder;
Expand Down Expand Up @@ -67,22 +67,12 @@ public ConsensusSubmitMessageHandler() {
// Exists for injection
}

/**
* This method is called during the pre-handle workflow.
*
* <p>Determines signatures needed for submitting a new message to a consensus topic
*
* @param context the {@link PreHandleContext} which collects all information that will be
* passed to {@code handle()}
* @param topicStore the {@link ReadableTopicStore} to use to resolve topic metadata
* @throws NullPointerException if one of the arguments is {@code null}
*/
public void preHandle(@NonNull final PreHandleContext context, @NonNull ReadableTopicStore topicStore)
throws PreCheckException {
@Override
public void preHandle(@NonNull final PreHandleContext context) throws PreCheckException {
requireNonNull(context);
requireNonNull(topicStore);

final var op = context.body().consensusSubmitMessageOrThrow();
final var topicStore = context.createStore(ReadableTopicStore.class);
// The topic ID must be present on the transaction and the topic must exist.
final var topic = topicStore.getTopicMetadata(op.topicID());
mustExist(topic, INVALID_TOPIC_ID);
Expand Down