Skip to content

Commit d28e001

Browse files
authored
fix: cherry-pick: fixed topicMessageSubmit without topicID throws unexpected NPE (#12573)
Signed-off-by: Iris Simon <[email protected]>
1 parent a7a8ce5 commit d28e001

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

hedera-node/hedera-consensus-service-impl/src/main/java/com/hedera/node/app/service/consensus/impl/handlers/ConsensusSubmitMessageHandler.java

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static com.hedera.node.app.service.mono.state.merkle.MerkleTopic.RUNNING_HASH_VERSION;
3232
import static com.hedera.node.app.spi.validation.Validations.mustExist;
3333
import static com.hedera.node.app.spi.workflows.PreCheckException.validateFalsePreCheck;
34+
import static com.hedera.node.app.spi.workflows.PreCheckException.validateTruePreCheck;
3435
import static java.util.Objects.requireNonNull;
3536

3637
import com.hedera.hapi.node.base.AccountID;
@@ -82,6 +83,7 @@ public void preHandle(@NonNull final PreHandleContext context) throws PreCheckEx
8283
final var op = context.body().consensusSubmitMessageOrThrow();
8384
final var topicStore = context.createStore(ReadableTopicStore.class);
8485
// The topic ID must be present on the transaction and the topic must exist.
86+
validateTruePreCheck(op.hasTopicID(), INVALID_TOPIC_ID);
8587
final var topic = topicStore.getTopic(op.topicID());
8688
mustExist(topic, INVALID_TOPIC_ID);
8789
validateFalsePreCheck(topic.deleted(), INVALID_TOPIC_ID);

hedera-node/hedera-consensus-service-impl/src/test/java/com/hedera/node/app/service/consensus/impl/test/handlers/ConsensusSubmitMessageTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ void topicIdNotFound() throws PreCheckException {
152152
assertThrowsPreCheck(() -> subject.preHandle(context), INVALID_TOPIC_ID);
153153
}
154154

155+
@Test
156+
@DisplayName("Topic without id returns error")
157+
void topicWithoutIdNotFound() throws PreCheckException {
158+
mockPayerLookup();
159+
readableTopicState = emptyReadableTopicState();
160+
given(readableStates.<TopicID, Topic>get(TOPICS_KEY)).willReturn(readableTopicState);
161+
readableStore = new ReadableTopicStoreImpl(readableStates);
162+
final var context = new FakePreHandleContext(accountStore, newDefaultSubmitMessageTxn());
163+
context.registerStore(ReadableTopicStore.class, readableStore);
164+
165+
assertThrowsPreCheck(() -> subject.preHandle(context), INVALID_TOPIC_ID);
166+
}
167+
155168
@Test
156169
@DisplayName("Topic without submit key does not error")
157170
void noTopicSubmitKey() throws PreCheckException {
@@ -329,6 +342,21 @@ private TransactionBody newSubmitMessageTxn(final EntityNum topicEntityNum, fina
329342
.build();
330343
}
331344

345+
private TransactionBody newDefaultSubmitMessageTxn() {
346+
return newSubmitMessageTxnWithoutId(
347+
"Message for test-" + Instant.now() + "." + Instant.now().getNano());
348+
}
349+
350+
private TransactionBody newSubmitMessageTxnWithoutId(final String message) {
351+
final var txnId = TransactionID.newBuilder().accountID(payerId).build();
352+
final var submitMessageBuilder =
353+
ConsensusSubmitMessageTransactionBody.newBuilder().message(Bytes.wrap(message));
354+
return TransactionBody.newBuilder()
355+
.transactionID(txnId)
356+
.consensusSubmitMessage(submitMessageBuilder.build())
357+
.build();
358+
}
359+
332360
private TransactionBody newSubmitMessageTxnWithChunks(
333361
final EntityNum topicEntityNum, final int currentChunk, final int totalChunk) {
334362
return newSubmitMessageTxnWithChunksAndPayer(topicEntityNum, currentChunk, totalChunk, null);

0 commit comments

Comments
 (0)