Repository: hashgraph/hedera-services
Issue: avgHdlSubMsgSize stat is not updated after the stat refactoring

**Summary of the defect**
There was a copy-paste issue on lines 61 and 66
https://github.com/hashgraph/hedera-services/blob/d275a47e518b31a30c0689f27acc28d0e4f44479/hedera-node/src/main/java/com/hedera/services/stats/MiscRunningAvgs.java#L57-L66
Also the following function is not hooked up to update the avgHdlSubMsgSize stat when handling submit message transactions
https://github.com/hashgraph/hedera-services/blob/d275a47e518b31a30c0689f27acc28d0e4f44479/hedera-node/src/main/java/com/hedera/services/stats/MiscRunningAvgs.java#L81-L83

**How to reproduce (if possible)**
Run any EET with submit message transactions, the avgHdlSubMsgSize stat is not updated in the csv file(s).

Changed files:
hedera-node/src/main/java/com/hedera/services/stats/HapiOpCounters.java, hedera-node/src/main/java/com/hedera/services/stats/MiscRunningAvgs.java, hedera-node/src/test/java/com/hedera/services/stats/HapiOpCountersTest.java, hedera-node/src/test/java/com/hedera/services/stats/MiscRunningAvgsTest.java, hedera-node/src/main/java/com/hedera/services/context/ServicesContext.java

diff --git a/hedera-node/src/main/java/com/hedera/services/context/ServicesContext.java b/hedera-node/src/main/java/com/hedera/services/context/ServicesContext.java
index 142a9de7ac..9b6277b941 100644
--- a/hedera-node/src/main/java/com/hedera/services/context/ServicesContext.java
+++ b/hedera-node/src/main/java/com/hedera/services/context/ServicesContext.java
@@ -470,7 +470,7 @@ public class ServicesContext {
 
 	public HapiOpCounters opCounters() {
 		if (opCounters == null) {
-			opCounters = new HapiOpCounters(new CounterFactory() {}, MiscUtils::baseStatNameOf);
+			opCounters = new HapiOpCounters(new CounterFactory() {}, runningAvgs(), txnCtx(), MiscUtils::baseStatNameOf);
 		}
 		return opCounters;
 	}
diff --git a/hedera-node/src/main/java/com/hedera/services/stats/HapiOpCounters.java b/hedera-node/src/main/java/com/hedera/services/stats/HapiOpCounters.java
index aae19ab5e3..c8a1a843f2 100644
--- a/hedera-node/src/main/java/com/hedera/services/stats/HapiOpCounters.java
+++ b/hedera-node/src/main/java/com/hedera/services/stats/HapiOpCounters.java
@@ -20,6 +20,7 @@ package com.hedera.services.stats;
  * ‍
  */
 
+import com.hedera.services.context.TransactionContext;
 import com.hederahashgraph.api.proto.java.HederaFunctionality;
 import com.swirlds.common.Platform;
 
@@ -40,11 +41,14 @@ import static com.hedera.services.stats.ServicesStatsConfig.COUNTER_RECEIVED_NAM
 import static com.hedera.services.stats.ServicesStatsConfig.COUNTER_SUBMITTED_DESC_TPL;
 import static com.hedera.services.stats.ServicesStatsConfig.COUNTER_SUBMITTED_NAME_TPL;
 import static com.hedera.services.utils.MiscUtils.QUERY_FUNCTIONS;
+import static com.hederahashgraph.api.proto.java.HederaFunctionality.ConsensusSubmitMessage;
 
 public class HapiOpCounters {
 	static Supplier<HederaFunctionality[]> allFunctions = HederaFunctionality.class::getEnumConstants;
 
 	private final CounterFactory counter;
+	private final MiscRunningAvgs runningAvgs;
+	private final TransactionContext txnCtx;
 	private final Function<HederaFunctionality, String> statNameFn;
 
 	EnumMap<HederaFunctionality, AtomicLong> receivedOps = new EnumMap<>(HederaFunctionality.class);
@@ -52,9 +56,16 @@ public class HapiOpCounters {
 	EnumMap<HederaFunctionality, AtomicLong> submittedTxns = new EnumMap<>(HederaFunctionality.class);
 	EnumMap<HederaFunctionality, AtomicLong> answeredQueries = new EnumMap<>(HederaFunctionality.class);
 
-	public HapiOpCounters(CounterFactory counter, Function<HederaFunctionality, String> statNameFn) {
+	public HapiOpCounters(
+			CounterFactory counter,
+			MiscRunningAvgs runningAvgs,
+			TransactionContext txnCtx,
+			Function<HederaFunctionality, String> statNameFn
+	) {
+		this.txnCtx = txnCtx;
 		this.counter = counter;
 		this.statNameFn = statNameFn;
+		this.runningAvgs = runningAvgs;
 
 		Arrays.stream(allFunctions.get())
 				.filter(function -> !IGNORED_FUNCTIONS.contains(function))
@@ -108,6 +119,10 @@ public class HapiOpCounters {
 
 	public void countHandled(HederaFunctionality txn) {
 		safeIncrement(handledTxns, txn);
+		if (txn == ConsensusSubmitMessage) {
+			int txnBytes = txnCtx.accessor().getTxn().getSerializedSize();
+			runningAvgs.recordHandledSubmitMessageSize(txnBytes);
+		}
 	}
 
 	public long handledSoFar(HederaFunctionality txn) {
diff --git a/hedera-node/src/main/java/com/hedera/services/stats/MiscRunningAvgs.java b/hedera-node/src/main/java/com/hedera/services/stats/MiscRunningAvgs.java
index 81443d939f..5d1d6a2f55 100644
--- a/hedera-node/src/main/java/com/hedera/services/stats/MiscRunningAvgs.java
+++ b/hedera-node/src/main/java/com/hedera/services/stats/MiscRunningAvgs.java
@@ -58,7 +58,7 @@ public class MiscRunningAvgs {
 				runningAvg.from(
 						Names.RECORD_STREAM_QUEUE_SIZE,
 						Descriptions.RECORD_STREAM_QUEUE_SIZE,
-						handledSubmitMessageSize));
+						recordStreamQueueSize));
 		platform.addAppStatEntry(
 				runningAvg.from(
 						Names.HANDLED_SUBMIT_MESSAGE_SIZE,
diff --git a/hedera-node/src/test/java/com/hedera/services/stats/HapiOpCountersTest.java b/hedera-node/src/test/java/com/hedera/services/stats/HapiOpCountersTest.java
index cb0a53f6a7..6f622182a7 100644
--- a/hedera-node/src/test/java/com/hedera/services/stats/HapiOpCountersTest.java
+++ b/hedera-node/src/test/java/com/hedera/services/stats/HapiOpCountersTest.java
@@ -20,7 +20,10 @@ package com.hedera.services.stats;
  * ‍
  */
 
+import com.hedera.services.context.TransactionContext;
+import com.hedera.services.utils.PlatformTxnAccessor;
 import com.hederahashgraph.api.proto.java.HederaFunctionality;
+import com.hederahashgraph.api.proto.java.TransactionBody;
 import com.swirlds.common.Platform;
 import com.swirlds.common.StatEntry;
 import org.junit.jupiter.api.AfterEach;
@@ -31,6 +34,7 @@ import org.junit.runner.RunWith;
 
 import java.util.function.Function;
 
+import static com.hederahashgraph.api.proto.java.HederaFunctionality.ConsensusSubmitMessage;
 import static com.hederahashgraph.api.proto.java.HederaFunctionality.CryptoTransfer;
 import static com.hederahashgraph.api.proto.java.HederaFunctionality.NONE;
 import static com.hederahashgraph.api.proto.java.HederaFunctionality.TokenGetInfo;
@@ -43,11 +47,14 @@ import static org.mockito.BDDMockito.argThat;
 import static org.mockito.BDDMockito.given;
 import static org.mockito.BDDMockito.verify;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 
 @RunWith(JUnitPlatform.class)
 class HapiOpCountersTest {
 	Platform platform;
 	CounterFactory factory;
+	MiscRunningAvgs runningAvgs;
+	TransactionContext txnCtx;
 	Function<HederaFunctionality, String> statNameFn;
 
 	HapiOpCounters subject;
@@ -57,14 +64,17 @@ class HapiOpCountersTest {
 		HapiOpCounters.allFunctions = () -> new HederaFunctionality[] {
 				CryptoTransfer,
 				TokenGetInfo,
+				ConsensusSubmitMessage,
 				NONE
 		};
 
+		txnCtx = mock(TransactionContext.class);
 		platform = mock(Platform.class);
 		factory = mock(CounterFactory.class);
 		statNameFn = HederaFunctionality::toString;
+		runningAvgs = mock(MiscRunningAvgs.class);
 
-		subject = new HapiOpCounters(factory, statNameFn);
+		subject = new HapiOpCounters(factory, runningAvgs, txnCtx, statNameFn);
 	}
 
 	@AfterEach
@@ -147,6 +157,43 @@ class HapiOpCountersTest {
 		verify(platform).addAppStatEntry(tokenInfoAns);
 	}
 
+	@Test
+	public void updatesAvgSubmitMessageHdlSizeForHandled() {
+		// setup:
+		int expectedSize = 12345;
+		TransactionBody txn = mock(TransactionBody.class);
+		PlatformTxnAccessor accessor = mock(PlatformTxnAccessor.class);
+
+		given(txn.getSerializedSize()).willReturn(expectedSize);
+		given(accessor.getTxn()).willReturn(txn);
+		given(txnCtx.accessor()).willReturn(accessor);
+
+		// when:
+		subject.countHandled(ConsensusSubmitMessage);
+
+		// then
+		verify(runningAvgs).recordHandledSubmitMessageSize(expectedSize);
+	}
+
+	@Test
+	public void doesntUpdateAvgSubmitMessageHdlSizeForCountReceivedOrSubmitted() {
+		// setup:
+		int expectedSize = 12345;
+		TransactionBody txn = mock(TransactionBody.class);
+		PlatformTxnAccessor accessor = mock(PlatformTxnAccessor.class);
+
+		given(txn.getSerializedSize()).willReturn(expectedSize);
+		given(accessor.getTxn()).willReturn(txn);
+		given(txnCtx.accessor()).willReturn(accessor);
+
+		// when:
+		subject.countReceived(ConsensusSubmitMessage);
+		subject.countSubmitted(ConsensusSubmitMessage);
+
+		// then
+		verify(runningAvgs, never()).recordHandledSubmitMessageSize(expectedSize);
+	}
+
 	@Test
 	public void updatesExpectedEntries() {
 		// when:
diff --git a/hedera-node/src/test/java/com/hedera/services/stats/MiscRunningAvgsTest.java b/hedera-node/src/test/java/com/hedera/services/stats/MiscRunningAvgsTest.java
index a668059268..ddccae0ad3 100644
--- a/hedera-node/src/test/java/com/hedera/services/stats/MiscRunningAvgsTest.java
+++ b/hedera-node/src/test/java/com/hedera/services/stats/MiscRunningAvgsTest.java
@@ -65,19 +65,19 @@ class MiscRunningAvgsTest {
 		given(factory.from(
 				argThat(MiscRunningAvgs.Names.ACCOUNT_LOOKUP_RETRIES::equals),
 				argThat(MiscRunningAvgs.Descriptions.ACCOUNT_LOOKUP_RETRIES::equals),
-				any())).willReturn(retries);
+				argThat(subject.accountLookupRetries::equals))).willReturn(retries);
 		given(factory.from(
 				argThat(MiscRunningAvgs.Names.ACCOUNT_RETRY_WAIT_MS::equals),
 				argThat(MiscRunningAvgs.Descriptions.ACCOUNT_RETRY_WAIT_MS::equals),
-				any())).willReturn(waitMs);
+				argThat(subject.accountRetryWaitMs::equals))).willReturn(waitMs);
 		given(factory.from(
 				argThat(MiscRunningAvgs.Names.RECORD_STREAM_QUEUE_SIZE::equals),
 				argThat(MiscRunningAvgs.Descriptions.RECORD_STREAM_QUEUE_SIZE::equals),
-				any())).willReturn(queueSizes);
+				argThat(subject.recordStreamQueueSize::equals))).willReturn(queueSizes);
 		given(factory.from(
 				argThat(MiscRunningAvgs.Names.HANDLED_SUBMIT_MESSAGE_SIZE::equals),
 				argThat(MiscRunningAvgs.Descriptions.HANDLED_SUBMIT_MESSAGE_SIZE::equals),
-				any())).willReturn(submitSizes);
+				argThat(subject.handledSubmitMessageSize::equals))).willReturn(submitSizes);
 
 		// when:
 		subject.registerWith(platform);