Skip to content

Commit 8061a47

Browse files
authored
reformat logs to use simple concatenation with separators (#207)
1 parent a84cd86 commit 8061a47

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

internal/mempool/mempool.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package mempool
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"errors"
87
"fmt"
8+
"strings"
99
"sync"
1010
"sync/atomic"
1111
"time"
@@ -907,15 +907,14 @@ func (txmp *TxMempool) GetPeerFailedCheckTxCount(nodeID types.NodeID) uint64 {
907907

908908
// AppendCheckTxErr wraps error message into an ABCIMessageLogs json string
909909
func (txmp *TxMempool) AppendCheckTxErr(existingLogs string, log string) string {
910-
var logs []map[string]interface{}
911-
json.Unmarshal([]byte(existingLogs), &logs)
910+
var builder strings.Builder
912911

913-
// Append the new ABCIMessageLog to the slice
914-
logs = append(logs, map[string]interface{}{
915-
"log": log,
916-
})
912+
builder.WriteString(existingLogs)
913+
// If there are already logs, append the new log with a separator
914+
if builder.Len() > 0 {
915+
builder.WriteString("; ")
916+
}
917+
builder.WriteString(log)
917918

918-
// Marshal the updated slice back into a JSON string
919-
jsonData, _ := json.Marshal(logs)
920-
return string(jsonData)
919+
return builder.String()
921920
}

internal/mempool/mempool_test.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package mempool
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"errors"
87
"fmt"
98
"math/rand"
@@ -707,24 +706,17 @@ func TestAppendCheckTxErr(t *testing.T) {
707706
}
708707
t.Cleanup(client.Wait)
709708
txmp := setup(t, client, 500)
710-
existingData := `[{"log":"existing error log"}]`
709+
existingLogData := "existing error log"
710+
newLogData := "sample error log"
711711

712712
// Append new error
713-
result := txmp.AppendCheckTxErr(existingData, "sample error msg")
713+
actualResult := txmp.AppendCheckTxErr(existingLogData, newLogData)
714+
expectedResult := fmt.Sprintf("%s; %s", existingLogData, newLogData)
714715

715-
// Unmarshal the result
716-
var data []map[string]interface{}
717-
err := json.Unmarshal([]byte(result), &data)
718-
require.NoError(t, err)
719-
require.Equal(t, len(data), 2)
720-
require.Equal(t, data[1]["log"], "sample error msg")
716+
require.Equal(t, expectedResult, actualResult)
721717

722718
// Append new error to empty log
723-
result = txmp.AppendCheckTxErr("", "sample error msg")
719+
actualResult = txmp.AppendCheckTxErr("", newLogData)
724720

725-
// Unmarshal the result
726-
err = json.Unmarshal([]byte(result), &data)
727-
require.NoError(t, err)
728-
require.Equal(t, len(data), 1)
729-
require.Equal(t, data[0]["log"], "sample error msg")
730-
}
721+
require.Equal(t, newLogData, actualResult)
722+
}

0 commit comments

Comments
 (0)