Skip to content

Commit

Permalink
reformat logs to use simple concatenation with separators
Browse files Browse the repository at this point in the history
  • Loading branch information
dssei committed Mar 7, 2024
1 parent a84cd86 commit 72e28cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
19 changes: 9 additions & 10 deletions internal/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package mempool
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -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()
}
24 changes: 8 additions & 16 deletions internal/mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mempool
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
Expand Down Expand Up @@ -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")
}
require.Equal(t, newLogData, actualResult)
}

0 comments on commit 72e28cf

Please sign in to comment.