Skip to content

Commit

Permalink
fix: various
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent 36112d0 commit 0f57064
Show file tree
Hide file tree
Showing 37 changed files with 707 additions and 742 deletions.
18 changes: 7 additions & 11 deletions internal/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ const (
)

type Account struct {
Address string `json:"address"`
Metadata metadata.Metadata `json:"metadata"`
FirstUsage time.Time `json:"-"`
InsertionDate time.Time `json:"_"`
UpdatedAt time.Time `json:"-"`
}

type ExpandedAccount struct {
Account `bun:",extend"`
Volumes VolumesByAssets `json:"volumes,omitempty"`
EffectiveVolumes VolumesByAssets `json:"effectiveVolumes,omitempty"`
Address string `json:"address"`
Metadata metadata.Metadata `json:"metadata"`
FirstUsage time.Time `json:"-"`
InsertionDate time.Time `json:"_"`
UpdatedAt time.Time `json:"-"`
Volumes VolumesByAssets `json:"volumes,omitempty"`
EffectiveVolumes VolumesByAssets `json:"effectiveVolumes,omitempty"`
}
8 changes: 4 additions & 4 deletions internal/api/v1/controllers_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
ledger "github.com/formancehq/ledger/internal"
)

type accountWithVolumesAndBalances ledger.ExpandedAccount
type accountWithVolumesAndBalances ledger.Account

func (a accountWithVolumesAndBalances) MarshalJSON() ([]byte, error) {
type aux struct {
ledger.ExpandedAccount
ledger.Account
Balances map[string]*big.Int `json:"balances"`
}
return json.Marshal(aux{
ExpandedAccount: ledger.ExpandedAccount(a),
Balances: a.Volumes.Balances(),
Account: ledger.Account(a),
Balances: a.Volumes.Balances(),
})
}

Expand Down
12 changes: 5 additions & 7 deletions internal/api/v1/controllers_accounts_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,11 @@ func TestAccountsList(t *testing.T) {
testCase.expectStatusCode = http.StatusOK
}

expectedCursor := bunpaginate.Cursor[ledger.ExpandedAccount]{
Data: []ledger.ExpandedAccount{
expectedCursor := bunpaginate.Cursor[ledger.Account]{
Data: []ledger.Account{
{
Account: ledger.Account{
Address: "world",
Metadata: metadata.Metadata{},
},
Address: "world",
Metadata: metadata.Metadata{},
},
},
}
Expand All @@ -147,7 +145,7 @@ func TestAccountsList(t *testing.T) {

require.Equal(t, testCase.expectStatusCode, rec.Code)
if testCase.expectStatusCode < 300 && testCase.expectStatusCode >= 200 {
cursor := api.DecodeCursorResponse[ledger.ExpandedAccount](t, rec.Body)
cursor := api.DecodeCursorResponse[ledger.Account](t, rec.Body)
require.Equal(t, expectedCursor, *cursor)
} else {
err := api.ErrorResponse{}
Expand Down
8 changes: 3 additions & 5 deletions internal/api/v1/controllers_accounts_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ func getAccount(w http.ResponseWriter, r *http.Request) {
if err != nil {
switch {
case postgres.IsNotFoundError(err):
acc = &ledger.ExpandedAccount{
Account: ledger.Account{
Address: address,
Metadata: metadata.Metadata{},
},
acc = &ledger.Account{
Address: address,
Metadata: metadata.Metadata{},
Volumes: ledger.VolumesByAssets{},
EffectiveVolumes: ledger.VolumesByAssets{},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/api/v1/controllers_accounts_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestAccountsRead(t *testing.T) {
if tc.expectBackendCall {
ledgerController.EXPECT().
GetAccount(gomock.Any(), tc.expectQuery).
Return(&ledger.ExpandedAccount{}, tc.returnErr)
Return(&ledger.Account{}, tc.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
Expand All @@ -91,7 +91,7 @@ func TestAccountsRead(t *testing.T) {

require.Equal(t, tc.expectStatusCode, rec.Code)
if tc.expectStatusCode < 300 && tc.expectStatusCode >= 200 {
_, ok := api.DecodeSingleResponse[ledger.ExpandedAccount](t, rec.Body)
_, ok := api.DecodeSingleResponse[ledger.Account](t, rec.Body)
require.True(t, ok)
} else {
err := api.ErrorResponse{}
Expand Down
12 changes: 5 additions & 7 deletions internal/api/v2/controllers_accounts_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,11 @@ func TestAccountsList(t *testing.T) {
tc.expectStatusCode = http.StatusOK
}

expectedCursor := bunpaginate.Cursor[ledger.ExpandedAccount]{
Data: []ledger.ExpandedAccount{
expectedCursor := bunpaginate.Cursor[ledger.Account]{
Data: []ledger.Account{
{
Account: ledger.Account{
Address: "world",
Metadata: metadata.Metadata{},
},
Address: "world",
Metadata: metadata.Metadata{},
},
},
}
Expand All @@ -218,7 +216,7 @@ func TestAccountsList(t *testing.T) {

require.Equal(t, tc.expectStatusCode, rec.Code)
if tc.expectStatusCode < 300 && tc.expectStatusCode >= 200 {
cursor := api.DecodeCursorResponse[ledger.ExpandedAccount](t, rec.Body)
cursor := api.DecodeCursorResponse[ledger.Account](t, rec.Body)
require.Equal(t, expectedCursor, *cursor)
} else {
err := api.ErrorResponse{}
Expand Down
4 changes: 2 additions & 2 deletions internal/api/v2/controllers_accounts_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestAccountsRead(t *testing.T) {
if tc.expectBackendCall {
ledgerController.EXPECT().
GetAccount(gomock.Any(), tc.expectQuery).
Return(&ledger.ExpandedAccount{}, tc.returnErr)
Return(&ledger.Account{}, tc.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
Expand All @@ -95,7 +95,7 @@ func TestAccountsRead(t *testing.T) {

require.Equal(t, tc.expectStatusCode, rec.Code)
if tc.expectStatusCode < 300 && tc.expectStatusCode >= 200 {
_, ok := api.DecodeSingleResponse[ledger.ExpandedAccount](t, rec.Body)
_, ok := api.DecodeSingleResponse[ledger.Account](t, rec.Body)
require.True(t, ok)
} else {
err := api.ErrorResponse{}
Expand Down
20 changes: 11 additions & 9 deletions internal/api/v2/controllers_bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ func TestBulk(t *testing.T) {
"asset": "USD/2",
},
},
"timestamp": now.Format(time.RFC3339Nano),
"metadata": map[string]any{},
"reverted": false,
"id": float64(0),
"timestamp": now.Format(time.RFC3339Nano),
"metadata": map[string]any{},
"reverted": false,
"revertedAt": nil,
"id": float64(0),
},
ResponseType: ActionCreateTransaction,
}},
Expand Down Expand Up @@ -151,11 +152,12 @@ func TestBulk(t *testing.T) {
},
expectResults: []Result{{
Data: map[string]any{
"id": float64(0),
"metadata": nil,
"postings": nil,
"reverted": false,
"timestamp": "0001-01-01T00:00:00Z",
"id": float64(0),
"metadata": nil,
"postings": nil,
"reverted": false,
"revertedAt": nil,
"timestamp": "0001-01-01T00:00:00Z",
},
ResponseType: ActionRevertTransaction,
}},
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/ledger/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Controller interface {
GetMigrationsInfo(ctx context.Context) ([]migrations.Info, error)
GetStats(ctx context.Context) (Stats, error)

GetAccount(ctx context.Context, query GetAccountQuery) (*ledger.ExpandedAccount, error)
ListAccounts(ctx context.Context, query ListAccountsQuery) (*bunpaginate.Cursor[ledger.ExpandedAccount], error)
GetAccount(ctx context.Context, query GetAccountQuery) (*ledger.Account, error)
ListAccounts(ctx context.Context, query ListAccountsQuery) (*bunpaginate.Cursor[ledger.Account], error)
CountAccounts(ctx context.Context, query ListAccountsQuery) (int, error)
ListLogs(ctx context.Context, query GetLogsQuery) (*bunpaginate.Cursor[ledger.Log], error)
CountTransactions(ctx context.Context, query ListTransactionsQuery) (int, error)
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ func (ctrl *DefaultController) CountAccounts(ctx context.Context, a ListAccounts
})
}

func (ctrl *DefaultController) ListAccounts(ctx context.Context, a ListAccountsQuery) (*bunpaginate.Cursor[ledger.ExpandedAccount], error) {
return tracing.Trace(ctx, "ListAccounts", func(ctx context.Context) (*bunpaginate.Cursor[ledger.ExpandedAccount], error) {
func (ctrl *DefaultController) ListAccounts(ctx context.Context, a ListAccountsQuery) (*bunpaginate.Cursor[ledger.Account], error) {
return tracing.Trace(ctx, "ListAccounts", func(ctx context.Context) (*bunpaginate.Cursor[ledger.Account], error) {
accounts, err := ctrl.store.ListAccounts(ctx, a)
return accounts, err
})
}

func (ctrl *DefaultController) GetAccount(ctx context.Context, q GetAccountQuery) (*ledger.ExpandedAccount, error) {
return tracing.Trace(ctx, "GetAccount", func(ctx context.Context) (*ledger.ExpandedAccount, error) {
func (ctrl *DefaultController) GetAccount(ctx context.Context, q GetAccountQuery) (*ledger.Account, error) {
return tracing.Trace(ctx, "GetAccount", func(ctx context.Context) (*ledger.Account, error) {
accounts, err := ctrl.store.GetAccount(ctx, q)
return accounts, err
})
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/ledger/controller_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestGetAccount(t *testing.T) {
listener := NewMockListener(ctrl)
ctx := logging.TestingContext()

account := ledger.ExpandedAccount{}
account := ledger.Account{}
query := NewGetAccountQuery("world")
store.EXPECT().
GetAccount(gomock.Any(), query).
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestListAccounts(t *testing.T) {
listener := NewMockListener(ctrl)
ctx := logging.TestingContext()

cursor := &bunpaginate.Cursor[ledger.ExpandedAccount]{}
cursor := &bunpaginate.Cursor[ledger.Account]{}
query := NewListAccountsQuery(NewPaginatedQueryOptions[PITFilterWithVolumes](PITFilterWithVolumes{}))
store.EXPECT().
ListAccounts(gomock.Any(), query).
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/ledger/controller_generated.go

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

8 changes: 4 additions & 4 deletions internal/controller/ledger/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Balances = vm.Balances

//go:generate mockgen -source store.go -destination store_generated.go -package ledger . TX
type TX interface {
GetAccount(ctx context.Context, query GetAccountQuery) (*ledger.ExpandedAccount, error)
GetAccount(ctx context.Context, query GetAccountQuery) (*ledger.Account, error)
// GetBalances must returns balance and lock account until the end of the TX
GetBalances(ctx context.Context, query BalanceQuery) (Balances, error)
CommitTransaction(ctx context.Context, transaction *ledger.Transaction) error
Expand Down Expand Up @@ -61,8 +61,8 @@ type Store interface {
CountTransactions(ctx context.Context, q ListTransactionsQuery) (int, error)
GetTransaction(ctx context.Context, query GetTransactionQuery) (*ledger.Transaction, error)
CountAccounts(ctx context.Context, a ListAccountsQuery) (int, error)
ListAccounts(ctx context.Context, a ListAccountsQuery) (*bunpaginate.Cursor[ledger.ExpandedAccount], error)
GetAccount(ctx context.Context, q GetAccountQuery) (*ledger.ExpandedAccount, error)
ListAccounts(ctx context.Context, a ListAccountsQuery) (*bunpaginate.Cursor[ledger.Account], error)
GetAccount(ctx context.Context, q GetAccountQuery) (*ledger.Account, error)
GetAggregatedBalances(ctx context.Context, q GetAggregatedBalanceQuery) (ledger.BalancesByAssets, error)
GetVolumesWithBalances(ctx context.Context, q GetVolumesWithBalancesQuery) (*bunpaginate.Cursor[ledger.VolumesWithBalanceByAssetByAccount], error)
IsUpToDate(ctx context.Context) (bool, error)
Expand Down Expand Up @@ -280,7 +280,7 @@ func (v *vmStoreAdapter) GetAccount(ctx context.Context, address string) (*ledge
if err != nil {
return nil, err
}
return &account.Account, nil
return account, nil
}

var _ vm.Store = (*vmStoreAdapter)(nil)
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/ledger/store_generated.go

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

3 changes: 3 additions & 0 deletions internal/storage/bucket/bucket_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bucket

import (
systemstore "github.com/formancehq/ledger/internal/storage/system"
"testing"

"github.com/formancehq/go-libs/bun/bunconnect"
Expand All @@ -20,6 +21,8 @@ func TestBuckets(t *testing.T) {
db, err := bunconnect.OpenSQLDB(ctx, pgDatabase.ConnectionOptions())
require.NoError(t, err)

require.NoError(t, systemstore.Migrate(ctx, db))

bucket := New(db, name)
require.NoError(t, bucket.Migrate(ctx))
}
24 changes: 11 additions & 13 deletions internal/storage/ledger/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ type Account struct {
PostCommitEffectiveVolumes AggregatedAccountVolumes `bun:"pcev,scanonly"`
}

func (account Account) toCore() ledger.ExpandedAccount {
return ledger.ExpandedAccount{
Account: ledger.Account{
Address: account.Address,
Metadata: account.Metadata,
FirstUsage: account.FirstUsage,
InsertionDate: account.InsertionDate,
UpdatedAt: account.UpdatedAt,
},
func (account Account) toCore() ledger.Account {
return ledger.Account{
Address: account.Address,
Metadata: account.Metadata,
FirstUsage: account.FirstUsage,
InsertionDate: account.InsertionDate,
UpdatedAt: account.UpdatedAt,
Volumes: account.PostCommitVolumes.toCore(),
EffectiveVolumes: account.PostCommitEffectiveVolumes.toCore(),
}
Expand Down Expand Up @@ -275,8 +273,8 @@ func (s *Store) selectAccounts(date *time.Time, expandVolumes, expandEffectiveVo
return ret
}

func (s *Store) ListAccounts(ctx context.Context, q ledgercontroller.ListAccountsQuery) (*Cursor[ledger.ExpandedAccount], error) {
return tracing.TraceWithLatency(ctx, "ListAccounts", func(ctx context.Context) (*Cursor[ledger.ExpandedAccount], error) {
func (s *Store) ListAccounts(ctx context.Context, q ledgercontroller.ListAccountsQuery) (*Cursor[ledger.Account], error) {
return tracing.TraceWithLatency(ctx, "ListAccounts", func(ctx context.Context) (*Cursor[ledger.Account], error) {
ret, err := UsingOffset[ledgercontroller.PaginatedQueryOptions[ledgercontroller.PITFilterWithVolumes], Account](
ctx,
s.selectAccounts(
Expand All @@ -296,8 +294,8 @@ func (s *Store) ListAccounts(ctx context.Context, q ledgercontroller.ListAccount
})
}

func (s *Store) GetAccount(ctx context.Context, q ledgercontroller.GetAccountQuery) (*ledger.ExpandedAccount, error) {
return tracing.TraceWithLatency(ctx, "GetAccount", func(ctx context.Context) (*ledger.ExpandedAccount, error) {
func (s *Store) GetAccount(ctx context.Context, q ledgercontroller.GetAccountQuery) (*ledger.Account, error) {
return tracing.TraceWithLatency(ctx, "GetAccount", func(ctx context.Context) (*ledger.Account, error) {
ret := &Account{}
if err := s.selectAccounts(q.PIT, q.ExpandVolumes, q.ExpandEffectiveVolumes, nil).
Model(ret).
Expand Down
Loading

0 comments on commit 0f57064

Please sign in to comment.