From 6af30c6b3c8895bb05249a74754dc7014eeed3ef Mon Sep 17 00:00:00 2001 From: ascandone Date: Wed, 2 Oct 2024 18:36:58 +0200 Subject: [PATCH] feat: make MachineFactory take TX instead of vm.Store --- README.md | 2 +- internal/controller/ledger/README.md | 58 +++++++++---------- .../controller/ledger/controller_default.go | 6 +- .../ledger/controller_default_test.go | 7 ++- internal/controller/ledger/machine.go | 6 +- .../ledger/machine_generated_test.go | 3 +- 6 files changed, 43 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 93e9fbb60..4dcbcb839 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# src +# ledger ```go import "github.com/formancehq/ledger" diff --git a/internal/controller/ledger/README.md b/internal/controller/ledger/README.md index d18774bdc..274f02178 100644 --- a/internal/controller/ledger/README.md +++ b/internal/controller/ledger/README.md @@ -80,7 +80,7 @@ Ledger Controller doc - [func \(ctrl \*DefaultController\) SaveTransactionMetadata\(ctx context.Context, parameters Parameters\[SaveTransactionMetadata\]\) error](<#DefaultController.SaveTransactionMetadata>) - [type DefaultMachineAdapter](<#DefaultMachineAdapter>) - [func NewDefaultMachine\(p program.Program\) \*DefaultMachineAdapter](<#NewDefaultMachine>) - - [func \(d \*DefaultMachineAdapter\) Execute\(ctx context.Context, store vm.Store, vars map\[string\]string\) \(\*MachineResult, error\)](<#DefaultMachineAdapter.Execute>) + - [func \(d \*DefaultMachineAdapter\) Execute\(ctx context.Context, tx TX, vars map\[string\]string\) \(\*MachineResult, error\)](<#DefaultMachineAdapter.Execute>) - [type DefaultMachineFactory](<#DefaultMachineFactory>) - [func NewDefaultMachineFactory\(compiler Compiler\) \*DefaultMachineFactory](<#NewDefaultMachineFactory>) - [func \(d \*DefaultMachineFactory\) Make\(script string\) \(Machine, error\)](<#DefaultMachineFactory.Make>) @@ -674,7 +674,7 @@ func (ctrl *ControllerWithTraces) SaveTransactionMetadata(ctx context.Context, p -## type [DefaultController]() +## type [DefaultController]() @@ -685,7 +685,7 @@ type DefaultController struct { ``` -### func [NewDefaultController]() +### func [NewDefaultController]() ```go func NewDefaultController(ledger ledger.Ledger, store Store, machineFactory MachineFactory) *DefaultController @@ -694,7 +694,7 @@ func NewDefaultController(ledger ledger.Ledger, store Store, machineFactory Mach -### func \(\*DefaultController\) [CountAccounts]() +### func \(\*DefaultController\) [CountAccounts]() ```go func (ctrl *DefaultController) CountAccounts(ctx context.Context, a ListAccountsQuery) (int, error) @@ -703,7 +703,7 @@ func (ctrl *DefaultController) CountAccounts(ctx context.Context, a ListAccounts -### func \(\*DefaultController\) [CountTransactions]() +### func \(\*DefaultController\) [CountTransactions]() ```go func (ctrl *DefaultController) CountTransactions(ctx context.Context, q ListTransactionsQuery) (int, error) @@ -712,7 +712,7 @@ func (ctrl *DefaultController) CountTransactions(ctx context.Context, q ListTran -### func \(\*DefaultController\) [CreateTransaction]() +### func \(\*DefaultController\) [CreateTransaction]() ```go func (ctrl *DefaultController) CreateTransaction(ctx context.Context, parameters Parameters[RunScript]) (*ledger.CreatedTransaction, error) @@ -721,7 +721,7 @@ func (ctrl *DefaultController) CreateTransaction(ctx context.Context, parameters -### func \(\*DefaultController\) [DeleteAccountMetadata]() +### func \(\*DefaultController\) [DeleteAccountMetadata]() ```go func (ctrl *DefaultController) DeleteAccountMetadata(ctx context.Context, parameters Parameters[DeleteAccountMetadata]) error @@ -730,7 +730,7 @@ func (ctrl *DefaultController) DeleteAccountMetadata(ctx context.Context, parame -### func \(\*DefaultController\) [DeleteTransactionMetadata]() +### func \(\*DefaultController\) [DeleteTransactionMetadata]() ```go func (ctrl *DefaultController) DeleteTransactionMetadata(ctx context.Context, parameters Parameters[DeleteTransactionMetadata]) error @@ -739,7 +739,7 @@ func (ctrl *DefaultController) DeleteTransactionMetadata(ctx context.Context, pa -### func \(\*DefaultController\) [Export]() +### func \(\*DefaultController\) [Export]() ```go func (ctrl *DefaultController) Export(ctx context.Context, w ExportWriter) error @@ -748,7 +748,7 @@ func (ctrl *DefaultController) Export(ctx context.Context, w ExportWriter) error -### func \(\*DefaultController\) [GetAccount]() +### func \(\*DefaultController\) [GetAccount]() ```go func (ctrl *DefaultController) GetAccount(ctx context.Context, q GetAccountQuery) (*ledger.Account, error) @@ -757,7 +757,7 @@ func (ctrl *DefaultController) GetAccount(ctx context.Context, q GetAccountQuery -### func \(\*DefaultController\) [GetAggregatedBalances]() +### func \(\*DefaultController\) [GetAggregatedBalances]() ```go func (ctrl *DefaultController) GetAggregatedBalances(ctx context.Context, q GetAggregatedBalanceQuery) (ledger.BalancesByAssets, error) @@ -766,7 +766,7 @@ func (ctrl *DefaultController) GetAggregatedBalances(ctx context.Context, q GetA -### func \(\*DefaultController\) [GetMigrationsInfo]() +### func \(\*DefaultController\) [GetMigrationsInfo]() ```go func (ctrl *DefaultController) GetMigrationsInfo(ctx context.Context) ([]migrations.Info, error) @@ -784,7 +784,7 @@ func (ctrl *DefaultController) GetStats(ctx context.Context) (Stats, error) -### func \(\*DefaultController\) [GetTransaction]() +### func \(\*DefaultController\) [GetTransaction]() ```go func (ctrl *DefaultController) GetTransaction(ctx context.Context, query GetTransactionQuery) (*ledger.Transaction, error) @@ -793,7 +793,7 @@ func (ctrl *DefaultController) GetTransaction(ctx context.Context, query GetTran -### func \(\*DefaultController\) [GetVolumesWithBalances]() +### func \(\*DefaultController\) [GetVolumesWithBalances]() ```go func (ctrl *DefaultController) GetVolumesWithBalances(ctx context.Context, q GetVolumesWithBalancesQuery) (*bunpaginate.Cursor[ledger.VolumesWithBalanceByAssetByAccount], error) @@ -802,7 +802,7 @@ func (ctrl *DefaultController) GetVolumesWithBalances(ctx context.Context, q Get -### func \(\*DefaultController\) [Import]() +### func \(\*DefaultController\) [Import]() ```go func (ctrl *DefaultController) Import(ctx context.Context, stream chan ledger.Log) error @@ -811,7 +811,7 @@ func (ctrl *DefaultController) Import(ctx context.Context, stream chan ledger.Lo -### func \(\*DefaultController\) [IsDatabaseUpToDate]() +### func \(\*DefaultController\) [IsDatabaseUpToDate]() ```go func (ctrl *DefaultController) IsDatabaseUpToDate(ctx context.Context) (bool, error) @@ -820,7 +820,7 @@ func (ctrl *DefaultController) IsDatabaseUpToDate(ctx context.Context) (bool, er -### func \(\*DefaultController\) [ListAccounts]() +### func \(\*DefaultController\) [ListAccounts]() ```go func (ctrl *DefaultController) ListAccounts(ctx context.Context, a ListAccountsQuery) (*bunpaginate.Cursor[ledger.Account], error) @@ -829,7 +829,7 @@ func (ctrl *DefaultController) ListAccounts(ctx context.Context, a ListAccountsQ -### func \(\*DefaultController\) [ListLogs]() +### func \(\*DefaultController\) [ListLogs]() ```go func (ctrl *DefaultController) ListLogs(ctx context.Context, q GetLogsQuery) (*bunpaginate.Cursor[ledger.Log], error) @@ -838,7 +838,7 @@ func (ctrl *DefaultController) ListLogs(ctx context.Context, q GetLogsQuery) (*b -### func \(\*DefaultController\) [ListTransactions]() +### func \(\*DefaultController\) [ListTransactions]() ```go func (ctrl *DefaultController) ListTransactions(ctx context.Context, q ListTransactionsQuery) (*bunpaginate.Cursor[ledger.Transaction], error) @@ -847,7 +847,7 @@ func (ctrl *DefaultController) ListTransactions(ctx context.Context, q ListTrans -### func \(\*DefaultController\) [RevertTransaction]() +### func \(\*DefaultController\) [RevertTransaction]() ```go func (ctrl *DefaultController) RevertTransaction(ctx context.Context, parameters Parameters[RevertTransaction]) (*ledger.RevertedTransaction, error) @@ -856,7 +856,7 @@ func (ctrl *DefaultController) RevertTransaction(ctx context.Context, parameters -### func \(\*DefaultController\) [SaveAccountMetadata]() +### func \(\*DefaultController\) [SaveAccountMetadata]() ```go func (ctrl *DefaultController) SaveAccountMetadata(ctx context.Context, parameters Parameters[SaveAccountMetadata]) error @@ -865,7 +865,7 @@ func (ctrl *DefaultController) SaveAccountMetadata(ctx context.Context, paramete -### func \(\*DefaultController\) [SaveTransactionMetadata]() +### func \(\*DefaultController\) [SaveTransactionMetadata]() ```go func (ctrl *DefaultController) SaveTransactionMetadata(ctx context.Context, parameters Parameters[SaveTransactionMetadata]) error @@ -874,7 +874,7 @@ func (ctrl *DefaultController) SaveTransactionMetadata(ctx context.Context, para -## type [DefaultMachineAdapter]() +## type [DefaultMachineAdapter]() @@ -885,7 +885,7 @@ type DefaultMachineAdapter struct { ``` -### func [NewDefaultMachine]() +### func [NewDefaultMachine]() ```go func NewDefaultMachine(p program.Program) *DefaultMachineAdapter @@ -894,10 +894,10 @@ func NewDefaultMachine(p program.Program) *DefaultMachineAdapter -### func \(\*DefaultMachineAdapter\) [Execute]() +### func \(\*DefaultMachineAdapter\) [Execute]() ```go -func (d *DefaultMachineAdapter) Execute(ctx context.Context, store vm.Store, vars map[string]string) (*MachineResult, error) +func (d *DefaultMachineAdapter) Execute(ctx context.Context, tx TX, vars map[string]string) (*MachineResult, error) ``` @@ -1582,13 +1582,13 @@ type Listener interface { ``` -## type [Machine]() +## type [Machine]() ```go type Machine interface { - Execute(context.Context, vm.Store, map[string]string) (*MachineResult, error) + Execute(context.Context, TX, map[string]string) (*MachineResult, error) } ``` @@ -1606,7 +1606,7 @@ type MachineFactory interface { ``` -## type [MachineResult]() +## type [MachineResult]() diff --git a/internal/controller/ledger/controller_default.go b/internal/controller/ledger/controller_default.go index 8c1da03c8..6794a323e 100644 --- a/internal/controller/ledger/controller_default.go +++ b/internal/controller/ledger/controller_default.go @@ -4,10 +4,11 @@ import ( "context" "database/sql" "fmt" - "github.com/formancehq/go-libs/time" "math/big" "reflect" + "github.com/formancehq/go-libs/time" + "github.com/formancehq/go-libs/migrations" "github.com/formancehq/ledger/internal/tracing" @@ -17,6 +18,7 @@ import ( "github.com/formancehq/go-libs/platform/postgres" "errors" + "github.com/formancehq/go-libs/bun/bunpaginate" "github.com/formancehq/go-libs/logging" "github.com/formancehq/go-libs/metadata" @@ -217,7 +219,7 @@ func (ctrl *DefaultController) CreateTransaction(ctx context.Context, parameters output, err := forgeLog(ctx, ctrl.store, parameters, func(ctx context.Context, sqlTX TX, input RunScript) (*ledger.CreatedTransaction, error) { result, err := tracing.TraceWithLatency(ctx, "ExecuteMachine", func(ctx context.Context) (*MachineResult, error) { - return m.Execute(ctx, newVmStoreAdapter(sqlTX), input.Vars) + return m.Execute(ctx, sqlTX, input.Vars) }) if err != nil { return nil, fmt.Errorf("failed to execute program: %w", err) diff --git a/internal/controller/ledger/controller_default_test.go b/internal/controller/ledger/controller_default_test.go index 565eab166..935508aae 100644 --- a/internal/controller/ledger/controller_default_test.go +++ b/internal/controller/ledger/controller_default_test.go @@ -3,11 +3,12 @@ package ledger import ( "context" "database/sql" - "github.com/formancehq/go-libs/pointer" - "github.com/formancehq/go-libs/time" "math/big" "testing" + "github.com/formancehq/go-libs/pointer" + "github.com/formancehq/go-libs/time" + "github.com/formancehq/go-libs/bun/bunpaginate" "github.com/formancehq/go-libs/migrations" @@ -43,7 +44,7 @@ func TestCreateTransaction(t *testing.T) { posting := ledger.NewPosting("world", "bank", "USD", big.NewInt(100)) machine.EXPECT(). - Execute(gomock.Any(), newVmStoreAdapter(sqlTX), runScript.Vars). + Execute(gomock.Any(), sqlTX, runScript.Vars). Return(&MachineResult{ Postings: ledger.Postings{posting}, }, nil) diff --git a/internal/controller/ledger/machine.go b/internal/controller/ledger/machine.go index 163e3ff9d..adeb99556 100644 --- a/internal/controller/ledger/machine.go +++ b/internal/controller/ledger/machine.go @@ -7,6 +7,7 @@ import ( "github.com/formancehq/ledger/internal/machine" "errors" + "github.com/formancehq/go-libs/collectionutils" "github.com/formancehq/go-libs/metadata" ledger "github.com/formancehq/ledger/internal" @@ -22,7 +23,7 @@ type MachineResult struct { //go:generate mockgen -write_source_comment=false -write_package_comment=false -source machine.go -destination machine_generated_test.go -package ledger . Machine type Machine interface { - Execute(context.Context, vm.Store, map[string]string) (*MachineResult, error) + Execute(context.Context, TX, map[string]string) (*MachineResult, error) } type DefaultMachineAdapter struct { @@ -30,7 +31,8 @@ type DefaultMachineAdapter struct { machine *vm.Machine } -func (d *DefaultMachineAdapter) Execute(ctx context.Context, store vm.Store, vars map[string]string) (*MachineResult, error) { +func (d *DefaultMachineAdapter) Execute(ctx context.Context, tx TX, vars map[string]string) (*MachineResult, error) { + store := newVmStoreAdapter(tx) d.machine = vm.NewMachine(d.program) diff --git a/internal/controller/ledger/machine_generated_test.go b/internal/controller/ledger/machine_generated_test.go index f7fcee03f..049b6690d 100644 --- a/internal/controller/ledger/machine_generated_test.go +++ b/internal/controller/ledger/machine_generated_test.go @@ -9,7 +9,6 @@ import ( context "context" reflect "reflect" - vm "github.com/formancehq/ledger/internal/machine/vm" gomock "go.uber.org/mock/gomock" ) @@ -37,7 +36,7 @@ func (m *MockMachine) EXPECT() *MockMachineMockRecorder { } // Execute mocks base method. -func (m *MockMachine) Execute(arg0 context.Context, arg1 vm.Store, arg2 map[string]string) (*MachineResult, error) { +func (m *MockMachine) Execute(arg0 context.Context, arg1 TX, arg2 map[string]string) (*MachineResult, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Execute", arg0, arg1, arg2) ret0, _ := ret[0].(*MachineResult)