From 72e28cfc7f69319584201ad20585c52766044e11 Mon Sep 17 00:00:00 2001 From: _dssei_ Date: Wed, 6 Mar 2024 16:50:34 -0800 Subject: [PATCH] reformat logs to use simple concatenation with separators --- internal/mempool/mempool.go | 19 +++++++++---------- internal/mempool/mempool_test.go | 24 ++++++++---------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/internal/mempool/mempool.go b/internal/mempool/mempool.go index ae7b3c7c5..19300d911 100644 --- a/internal/mempool/mempool.go +++ b/internal/mempool/mempool.go @@ -3,9 +3,9 @@ package mempool import ( "bytes" "context" - "encoding/json" "errors" "fmt" + "strings" "sync" "sync/atomic" "time" @@ -907,15 +907,14 @@ func (txmp *TxMempool) GetPeerFailedCheckTxCount(nodeID types.NodeID) uint64 { // AppendCheckTxErr wraps error message into an ABCIMessageLogs json string func (txmp *TxMempool) AppendCheckTxErr(existingLogs string, log string) string { - var logs []map[string]interface{} - json.Unmarshal([]byte(existingLogs), &logs) + var builder strings.Builder - // Append the new ABCIMessageLog to the slice - logs = append(logs, map[string]interface{}{ - "log": log, - }) + builder.WriteString(existingLogs) + // If there are already logs, append the new log with a separator + if builder.Len() > 0 { + builder.WriteString("; ") + } + builder.WriteString(log) - // Marshal the updated slice back into a JSON string - jsonData, _ := json.Marshal(logs) - return string(jsonData) + return builder.String() } diff --git a/internal/mempool/mempool_test.go b/internal/mempool/mempool_test.go index d86ba8f7e..487f00654 100644 --- a/internal/mempool/mempool_test.go +++ b/internal/mempool/mempool_test.go @@ -3,7 +3,6 @@ package mempool import ( "bytes" "context" - "encoding/json" "errors" "fmt" "math/rand" @@ -707,24 +706,17 @@ func TestAppendCheckTxErr(t *testing.T) { } t.Cleanup(client.Wait) txmp := setup(t, client, 500) - existingData := `[{"log":"existing error log"}]` + existingLogData := "existing error log" + newLogData := "sample error log" // Append new error - result := txmp.AppendCheckTxErr(existingData, "sample error msg") + actualResult := txmp.AppendCheckTxErr(existingLogData, newLogData) + expectedResult := fmt.Sprintf("%s; %s", existingLogData, newLogData) - // Unmarshal the result - var data []map[string]interface{} - err := json.Unmarshal([]byte(result), &data) - require.NoError(t, err) - require.Equal(t, len(data), 2) - require.Equal(t, data[1]["log"], "sample error msg") + require.Equal(t, expectedResult, actualResult) // Append new error to empty log - result = txmp.AppendCheckTxErr("", "sample error msg") + actualResult = txmp.AppendCheckTxErr("", newLogData) - // Unmarshal the result - err = json.Unmarshal([]byte(result), &data) - require.NoError(t, err) - require.Equal(t, len(data), 1) - require.Equal(t, data[0]["log"], "sample error msg") -} \ No newline at end of file + require.Equal(t, newLogData, actualResult) +}