Skip to content

Commit

Permalink
Revert "feat: change transactions ids to uuid"
Browse files Browse the repository at this point in the history
This reverts commit f65b27ce9f09e6660bb061b5733434c66a6acb74.
  • Loading branch information
gfyrag authored and flemzord committed May 12, 2023
1 parent 351ce26 commit fe86393
Show file tree
Hide file tree
Showing 25 changed files with 329 additions and 202 deletions.
23 changes: 15 additions & 8 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,10 @@ paths:
description: Transaction ID.
required: true
schema:
type: string
format: uuid
type: integer
format: int64
minimum: 0
example: 1234
responses:
"200":
description: OK
Expand Down Expand Up @@ -580,8 +582,10 @@ paths:
description: Transaction ID.
required: true
schema:
type: string
format: uuid
type: integer
format: int64
minimum: 0
example: 1234
requestBody:
description: metadata
content:
Expand Down Expand Up @@ -618,8 +622,10 @@ paths:
description: Transaction ID.
required: true
schema:
type: string
format: uuid
type: integer
format: int64
minimum: 0
example: 1234
responses:
"200":
description: OK
Expand Down Expand Up @@ -1117,8 +1123,9 @@ components:
metadata:
$ref: '#/components/schemas/Metadata'
txid:
type: string
format: uuid
type: integer
format: int64
minimum: 0
preCommitVolumes:
$ref: '#/components/schemas/AggregatedVolumes'
postCommitVolumes:
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/controllers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type Ledger interface {
GetAccount(ctx context.Context, param string) (*core.AccountWithVolumes, error)
SaveMeta(ctx context.Context, targetType string, targetID string, m metadata.Metadata) error
SaveMeta(ctx context.Context, targetType string, targetID any, m metadata.Metadata) error
GetAccounts(ctx context.Context, query storage.AccountsQuery) (*api.Cursor[core.Account], error)
CountAccounts(ctx context.Context, query storage.AccountsQuery) (uint64, error)
GetBalancesAggregated(ctx context.Context, q storage.BalancesQuery) (core.AssetsBalances, error)
Expand All @@ -25,8 +25,8 @@ type Ledger interface {
CountTransactions(ctx context.Context, query storage.TransactionsQuery) (uint64, error)
GetTransactions(ctx context.Context, query storage.TransactionsQuery) (*api.Cursor[core.ExpandedTransaction], error)
CreateTransaction(ctx context.Context, preview bool, data core.RunScript) (*core.ExpandedTransaction, error)
GetTransaction(ctx context.Context, id string) (*core.ExpandedTransaction, error)
RevertTransaction(ctx context.Context, id string) (*core.ExpandedTransaction, error)
GetTransaction(ctx context.Context, id uint64) (*core.ExpandedTransaction, error)
RevertTransaction(ctx context.Context, id uint64) (*core.ExpandedTransaction, error)
}

type Backend interface {
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/controllers/api_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 24 additions & 3 deletions pkg/api/controllers/transaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,14 @@ func PostTransaction(w http.ResponseWriter, r *http.Request) {
func GetTransaction(w http.ResponseWriter, r *http.Request) {
l := LedgerFromContext(r.Context())

tx, err := l.GetTransaction(r.Context(), chi.URLParam(r, "txid"))
txId, err := strconv.ParseUint(chi.URLParam(r, "txid"), 10, 64)
if err != nil {
apierrors.ResponseError(w, r, errorsutil.NewError(ledger.ErrValidation,
errors.New("invalid transaction ID")))
return
}

tx, err := l.GetTransaction(r.Context(), txId)
if err != nil {
apierrors.ResponseError(w, r, err)
return
Expand All @@ -221,7 +228,14 @@ func GetTransaction(w http.ResponseWriter, r *http.Request) {
func RevertTransaction(w http.ResponseWriter, r *http.Request) {
l := LedgerFromContext(r.Context())

tx, err := l.RevertTransaction(r.Context(), chi.URLParam(r, "txid"))
txId, err := strconv.ParseUint(chi.URLParam(r, "txid"), 10, 64)
if err != nil {
apierrors.ResponseError(w, r, errorsutil.NewError(ledger.ErrValidation,
errors.New("invalid transaction ID")))
return
}

tx, err := l.RevertTransaction(r.Context(), txId)
if err != nil {
apierrors.ResponseError(w, r, err)
return
Expand All @@ -240,7 +254,14 @@ func PostTransactionMetadata(w http.ResponseWriter, r *http.Request) {
return
}

if err := l.SaveMeta(r.Context(), core.MetaTargetTypeTransaction, chi.URLParam(r, "txid"), m); err != nil {
txId, err := strconv.ParseUint(chi.URLParam(r, "txid"), 10, 64)
if err != nil {
apierrors.ResponseError(w, r, errorsutil.NewError(ledger.ErrValidation,
errors.New("invalid transaction ID")))
return
}

if err := l.SaveMeta(r.Context(), core.MetaTargetTypeTransaction, txId, m); err != nil {
apierrors.ResponseError(w, r, err)
return
}
Expand Down
14 changes: 6 additions & 8 deletions pkg/api/controllers/transaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
sharedapi "github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/metadata"
"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -213,17 +212,16 @@ func TestPostTransactionMetadata(t *testing.T) {
testCase.expectStatusCode = http.StatusNoContent
}

id := uuid.NewString()
backend, mock := newTestingBackend(t)
if testCase.expectStatusCode == http.StatusNoContent {
mock.EXPECT().
SaveMeta(gomock.Any(), core.MetaTargetTypeTransaction, id, testCase.body).
SaveMeta(gomock.Any(), core.MetaTargetTypeTransaction, uint64(0), testCase.body).
Return(nil)
}

router := routes.NewRouter(backend, nil, nil, metrics.NewNoOpMetricsRegistry())

req := httptest.NewRequest(http.MethodPost, "/xxx/transactions/"+id+"/metadata", Buffer(t, testCase.body))
req := httptest.NewRequest(http.MethodPost, "/xxx/transactions/0/metadata", Buffer(t, testCase.body))
rec := httptest.NewRecorder()
req.URL.RawQuery = testCase.queryParams.Encode()

Expand Down Expand Up @@ -251,12 +249,12 @@ func TestGetTransaction(t *testing.T) {

backend, mock := newTestingBackend(t)
mock.EXPECT().
GetTransaction(gomock.Any(), tx.ID).
GetTransaction(gomock.Any(), uint64(0)).
Return(&tx, nil)

router := routes.NewRouter(backend, nil, nil, metrics.NewNoOpMetricsRegistry())

req := httptest.NewRequest(http.MethodGet, "/xxx/transactions/"+tx.ID, nil)
req := httptest.NewRequest(http.MethodGet, "/xxx/transactions/0", nil)
rec := httptest.NewRecorder()

router.ServeHTTP(rec, req)
Expand Down Expand Up @@ -622,12 +620,12 @@ func TestRevertTransaction(t *testing.T) {
backend, mockLedger := newTestingBackend(t)
mockLedger.
EXPECT().
RevertTransaction(gomock.Any(), expectedTx.ID).
RevertTransaction(gomock.Any(), uint64(0)).
Return(&expectedTx, nil)

router := routes.NewRouter(backend, nil, nil, metrics.NewNoOpMetricsRegistry())

req := httptest.NewRequest(http.MethodPost, "/xxx/transactions/"+expectedTx.ID+"/revert", nil)
req := httptest.NewRequest(http.MethodPost, "/xxx/transactions/0/revert", nil)
rec := httptest.NewRecorder()

router.ServeHTTP(rec, req)
Expand Down
50 changes: 45 additions & 5 deletions pkg/core/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"reflect"
"sort"
"strconv"
"strings"
"sync"

"github.com/formancehq/stack/libs/go-libs/collectionutils"
Expand Down Expand Up @@ -152,16 +154,54 @@ func NewTransactionLog(tx Transaction, accountMetadata map[string]metadata.Metad

type SetMetadataLogPayload struct {
TargetType string `json:"targetType"`
TargetID string `json:"targetId"`
TargetID any `json:"targetId"`
Metadata metadata.Metadata `json:"metadata"`
}

func (s SetMetadataLogPayload) hashString(buf *buffer) {
buf.writeString(s.TargetType)
buf.writeString(s.TargetID)
switch targetID := s.TargetID.(type) {
case string:
buf.writeString(targetID)
case uint64:
buf.writeUInt64(targetID)
}
hashStringMetadata(buf, s.Metadata)
}

func (s *SetMetadataLogPayload) UnmarshalJSON(data []byte) error {
type X struct {
TargetType string `json:"targetType"`
TargetID json.RawMessage `json:"targetId"`
Metadata metadata.Metadata `json:"metadata"`
}
x := X{}
err := json.Unmarshal(data, &x)
if err != nil {
return err
}
var id interface{}
switch strings.ToUpper(x.TargetType) {
case strings.ToUpper(MetaTargetTypeAccount):
id = ""
err = json.Unmarshal(x.TargetID, &id)
case strings.ToUpper(MetaTargetTypeTransaction):
id, err = strconv.ParseUint(string(x.TargetID), 10, 64)
default:
panic("unknown type")
}
if err != nil {
return err
}

*s = SetMetadataLogPayload{
TargetType: x.TargetType,
TargetID: id,
Metadata: x.Metadata,
}
return nil
}

func NewSetMetadataLog(at Time, metadata SetMetadataLogPayload) Log {
// Since the id is unique and the hash is a hash of the previous log, they
// will be filled at insertion time during the batch process.
Expand All @@ -173,16 +213,16 @@ func NewSetMetadataLog(at Time, metadata SetMetadataLogPayload) Log {
}

type RevertedTransactionLogPayload struct {
RevertedTransactionID string
RevertedTransactionID uint64
RevertTransaction Transaction
}

func (r RevertedTransactionLogPayload) hashString(buf *buffer) {
buf.writeString(r.RevertedTransactionID)
buf.writeUInt64(r.RevertedTransactionID)
r.RevertTransaction.hashString(buf)
}

func NewRevertedTransactionLog(at Time, revertedTxID string, tx Transaction) Log {
func NewRevertedTransactionLog(at Time, revertedTxID uint64, tx Transaction) Log {
return Log{
Type: RevertedTransactionLogType,
Date: at,
Expand Down
11 changes: 6 additions & 5 deletions pkg/core/metadata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"fmt"
"sort"

"github.com/formancehq/stack/libs/go-libs/collectionutils"
Expand All @@ -19,7 +20,7 @@ func SpecMetadata(name string) string {
return formanceNamespace + name
}

func MarkReverts(m metadata.Metadata, txID string) metadata.Metadata {
func MarkReverts(m metadata.Metadata, txID uint64) metadata.Metadata {
return m.Merge(RevertMetadata(txID))
}

Expand All @@ -37,12 +38,12 @@ func ComputeMetadata(key, value string) metadata.Metadata {
}
}

func RevertedMetadata(by string) metadata.Metadata {
return ComputeMetadata(RevertedMetadataSpecKey(), by)
func RevertedMetadata(by uint64) metadata.Metadata {
return ComputeMetadata(RevertedMetadataSpecKey(), fmt.Sprint(by))
}

func RevertMetadata(tx string) metadata.Metadata {
return ComputeMetadata(RevertMetadataSpecKey(), tx)
func RevertMetadata(tx uint64) metadata.Metadata {
return ComputeMetadata(RevertMetadataSpecKey(), fmt.Sprint(tx))
}

func IsReverted(m metadata.Metadata) bool {
Expand Down
16 changes: 5 additions & 11 deletions pkg/core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ package core

import (
"github.com/formancehq/stack/libs/go-libs/metadata"
"github.com/google/uuid"
)

func init() {
uuid.EnableRandPool()
}

type Transactions struct {
Transactions []TransactionData `json:"transactions"`
}
Expand Down Expand Up @@ -39,7 +34,7 @@ func (t *TransactionData) Reverse() TransactionData {
ret := TransactionData{
Postings: postings,
}
//TODO(gfyrag): Do we keep this for v2?
//TODO(gfyra): Do we keep this for v2?
if t.Reference != "" {
ret.Reference = "revert_" + t.Reference
}
Expand All @@ -57,11 +52,11 @@ func (d TransactionData) hashString(buf *buffer) {

type Transaction struct {
TransactionData
ID string `json:"txid"`
ID uint64 `json:"txid"`
}

type TransactionWithMetadata struct {
ID string
ID uint64
Metadata metadata.Metadata
}

Expand All @@ -80,7 +75,7 @@ func (t Transaction) WithTimestamp(ts Time) Transaction {
return t
}

func (t Transaction) WithID(id string) Transaction {
func (t Transaction) WithID(id uint64) Transaction {
t.ID = id
return t
}
Expand All @@ -91,14 +86,13 @@ func (t Transaction) WithMetadata(m metadata.Metadata) Transaction {
}

func (t Transaction) hashString(buf *buffer) {
buf.writeString(t.ID)
buf.writeUInt64(t.ID)
t.TransactionData.hashString(buf)
}

func NewTransaction() Transaction {
return Transaction{
TransactionData: NewTransactionData(),
ID: uuid.NewString(),
}
}

Expand Down
Loading

0 comments on commit fe86393

Please sign in to comment.