Skip to content

Commit

Permalink
fix: transactions commit with special chars in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Jan 20, 2025
1 parent fe38474 commit b0c5ea5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/controller/ledger/log_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (lp *logProcessor[INPUT, OUTPUT]) fetchLogWithIK(ctx context.Context, store
if err == nil {
// notes(gfyrag): idempotency hash should never be empty in this case, but data from previous
// ledger version does not have this field and it cannot be recomputed
if log.IdempotencyHash != "" {
if len(log.IdempotencyHash) > 0 {
if computedHash := ledger.ComputeIdempotencyHash(parameters.Input); log.IdempotencyHash != computedHash {
return nil, nil, newErrInvalidIdempotencyInputs(log.IdempotencyKey, log.IdempotencyHash, computedHash)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/ledger/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Log struct {

Ledger string `bun:"ledger,type:varchar"`
Data RawMessage `bun:"data,type:jsonb"`
Memento RawMessage `bun:"memento,type:bytea"`
Memento []byte `bun:"memento,type:bytea"`
}

func (log Log) ToCore() ledger.Log {
Expand Down
32 changes: 30 additions & 2 deletions internal/storage/ledger/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestInsertLog(t *testing.T) {
func TestLogsInsert(t *testing.T) {
t.Parallel()

store := newLedgerStore(t)
Expand Down Expand Up @@ -138,9 +138,37 @@ func TestInsertLog(t *testing.T) {
previous = &chainedLog
}
})

t.Run("insert with special characters", func(t *testing.T) {

type testCase struct {
name string
metadata map[string]string
}

testCases := []testCase{
{name: "with escaped quotes", metadata: map[string]string{"key": "value with \"quotes\""}},
{name: "with utf-8 characters", metadata: map[string]string{"rate": "½"}},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

log := ledger.NewLog(ledger.CreatedTransaction{
Transaction: ledger.NewTransaction().
WithPostings(ledger.NewPosting("world", "bank", "USD", big.NewInt(100))).
WithMetadata(testCase.metadata),
})

err := store.InsertLog(ctx, &log)
require.NoError(t, err)
})
}
})
}

func TestReadLogWithIdempotencyKey(t *testing.T) {
func TestLogsReadWithIdempotencyKey(t *testing.T) {
t.Parallel()

store := newLedgerStore(t)
Expand Down
4 changes: 1 addition & 3 deletions internal/storage/ledger/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,6 @@ func TestTransactionsList(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

store.DumpTables(ctx, "transactions")

tc.query.Options.Expand = []string{"volumes", "effectiveVolumes"}

cursor, err := store.Transactions().Paginate(ctx, tc.query)
Expand All @@ -871,4 +869,4 @@ func TestTransactionsList(t *testing.T) {
}
})
}
}
}
7 changes: 5 additions & 2 deletions test/e2e/api_transactions_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
. "github.com/onsi/gomega"
)

var _ = Context("Ledger accounts list API tests", func() {
var _ = Context("Ledger transactions create API tests", func() {
for _, data := range []struct {
description string
numscriptRewrite bool
Expand Down Expand Up @@ -82,7 +82,10 @@ var _ = Context("Ledger accounts list API tests", func() {
BeforeEach(func() {
req = operations.V2CreateTransactionRequest{
V2PostTransaction: components.V2PostTransaction{
Metadata: map[string]string{},
Metadata: map[string]string{
"add some quotes": "\" quoted value\"",
"add some utf-8 characters": "½",
},
Postings: []components.V2Posting{
{
Amount: big.NewInt(100),
Expand Down

0 comments on commit b0c5ea5

Please sign in to comment.