Skip to content

Commit

Permalink
feat: make transactions pcv computed on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent 53d5611 commit c5b0510
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 165 deletions.
1 change: 1 addition & 0 deletions internal/api/v1/controllers_transactions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
Metadata: payload.Metadata,
}

// todo: handle missing error cases
res, err := l.CreateTransaction(r.Context(), getCommandParameters(r), runScript)
if err != nil {
switch {
Expand Down
2 changes: 2 additions & 0 deletions internal/api/v1/controllers_transactions_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"encoding/json"
"github.com/formancehq/go-libs/time"
"math/big"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -221,6 +222,7 @@ func TestTransactionsCreate(t *testing.T) {

systemController, ledgerController := newTestingSystemController(t, true)
if testCase.expectedStatusCode < 300 && testCase.expectedStatusCode >= 200 {
testCase.expectedRunScript.Timestamp = time.Time{}
ledgerController.EXPECT().
CreateTransaction(gomock.Any(), ledgercontroller.Parameters{
DryRun: tc.expectedPreview,
Expand Down
2 changes: 2 additions & 0 deletions internal/api/v2/controllers_transactions_create_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v2

import (
"github.com/formancehq/go-libs/time"
"math/big"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -401,6 +402,7 @@ func TestTransactionCreate(t *testing.T) {

systemController, ledgerController := newTestingSystemController(t, true)
if testCase.expectControllerCall {
testCase.expectedRunScript.Timestamp = time.Time{}
expect := ledgerController.EXPECT().
CreateTransaction(gomock.Any(), ledgercontroller.Parameters{
DryRun: tc.expectedDryRun,
Expand Down
12 changes: 7 additions & 5 deletions internal/storage/bucket/migrations/0-init-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,13 @@ as
$$
begin
insert into "{{.Bucket}}".accounts_metadata (ledger, accounts_seq, revision, date, metadata)
values (new.ledger, new.seq, (select revision + 1
from "{{.Bucket}}".accounts_metadata
where accounts_metadata.accounts_seq = new.seq
order by revision desc
limit 1), new.updated_at, new.metadata);
values (new.ledger, new.seq, (
select revision + 1
from "{{.Bucket}}".accounts_metadata
where accounts_metadata.accounts_seq = new.seq
order by revision desc
limit 1
), new.updated_at, new.metadata);

return new;
end;
Expand Down
4 changes: 3 additions & 1 deletion internal/storage/bucket/migrations/11-stateless.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ rename attribute inputs to input;
alter type "{{.Bucket}}".volumes
rename attribute outputs to output;

alter table "{{.Bucket}}".transactions
add column post_commit_volumes jsonb not null ;

alter table "{{.Bucket}}".moves
alter column post_commit_volumes
drop not null,
Expand Down Expand Up @@ -119,7 +122,6 @@ create function "{{.Bucket}}".set_volumes()
as
$$
begin
--todo: use balances table directly...
new.post_commit_volumes = coalesce((
select (
(post_commit_volumes).input + case when new.is_source then 0 else new.amount end,
Expand Down
1 change: 1 addition & 0 deletions internal/storage/ledger/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func TestGetAccount(t *testing.T) {

t.Run("find account in past", func(t *testing.T) {
t.Parallel()

account, err := store.GetAccount(ctx, ledgercontroller.NewGetAccountQuery("multi").WithPIT(now.Add(-30*time.Second)))
require.NoError(t, err)
require.Equal(t, ledger.ExpandedAccount{
Expand Down
6 changes: 3 additions & 3 deletions internal/storage/ledger/balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestUpdateBalances(t *testing.T) {
AccountsSeq: world.Seq,
})
require.NoError(t, err)
require.Equal(t, map[string]map[string]ledger.Volumes{
require.Equal(t, ledger.PostCommitVolumes{
"world": {
"USD/2": ledger.NewVolumesInt64(0, 100),
},
Expand All @@ -295,7 +295,7 @@ func TestUpdateBalances(t *testing.T) {
Output: big.NewInt(0),
})
require.NoError(t, err)
require.Equal(t, map[string]map[string]ledger.Volumes{
require.Equal(t, ledger.PostCommitVolumes{
"world": {
"USD/2": ledger.NewVolumesInt64(50, 100),
},
Expand All @@ -310,7 +310,7 @@ func TestUpdateBalances(t *testing.T) {
AccountsSeq: world.Seq,
})
require.NoError(t, err)
require.Equal(t, map[string]map[string]ledger.Volumes{
require.Equal(t, ledger.PostCommitVolumes{
"world": {
"USD/2": ledger.NewVolumesInt64(100, 150),
},
Expand Down
26 changes: 5 additions & 21 deletions internal/storage/ledger/moves.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,13 @@ func (m Moves) volumeUpdates() []AccountsVolumes {
return ret
}

func (m Moves) ComputePostCommitVolumes() TransactionsPostCommitVolumes {
ret := TransactionsPostCommitVolumes{}
func (m Moves) ComputePostCommitEffectiveVolumes() ledger.PostCommitVolumes {
ret := ledger.PostCommitVolumes{}
for _, move := range m {
ret = append(ret, TransactionPostCommitVolume{
AggregatedAccountVolume: AggregatedAccountVolume{
Volumes: *move.PostCommitVolumes,
Asset: move.Asset,
ret = ret.Merge(ledger.PostCommitVolumes{
move.Account: ledger.VolumesByAssets{
move.Asset: *move.PostCommitEffectiveVolumes,
},
Account: move.Account,
})
}
return ret
}

func (m Moves) ComputePostCommitEffectiveVolumes() TransactionsPostCommitVolumes {
ret := TransactionsPostCommitVolumes{}
for _, move := range m {
ret = append(ret, TransactionPostCommitVolume{
AggregatedAccountVolume: AggregatedAccountVolume{
Volumes: *move.PostCommitEffectiveVolumes,
Asset: move.Asset,
},
Account: move.Account,
})
}
return ret
Expand Down
Loading

0 comments on commit c5b0510

Please sign in to comment.