TD-674: Adds wallet balance notice event (#65)

Updates docker compose images
Makes composed machinegun to log to stdout

* TD-675: Adds limit change log notice

---------

Co-authored-by: Артем <WWW_cool@inbox.ru>
This commit is contained in:
Aleksey Kashapov 2023-08-02 18:25:02 +03:00 committed by GitHub
parent f1c188e42a
commit d21e440a22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 101 additions and 13 deletions

View File

@ -516,6 +516,7 @@ do_process_transfer(p_transfer_prepare, Deposit) ->
{continue, Events};
do_process_transfer(p_transfer_commit, Deposit) ->
{ok, Events} = ff_pipeline:with(p_transfer, Deposit, fun ff_postings_transfer:commit/1),
ok = ff_wallet:log_balance(wallet_id(Deposit)),
{continue, Events};
do_process_transfer(p_transfer_cancel, Deposit) ->
{ok, Events} = ff_pipeline:with(p_transfer, Deposit, fun ff_postings_transfer:cancel/1),
@ -617,6 +618,7 @@ handle_child_result({undefined, Events} = Result, Deposit) ->
true ->
{continue, Events};
false ->
ok = ff_wallet:log_balance(wallet_id(Deposit)),
Result
end;
handle_child_result({_OtherAction, _Events} = Result, _Deposit) ->

View File

@ -381,6 +381,7 @@ do_process_transfer(p_transfer_prepare, Revert) ->
{continue, Events};
do_process_transfer(p_transfer_commit, Revert) ->
{ok, Events} = ff_pipeline:with(p_transfer, Revert, fun ff_postings_transfer:commit/1),
ok = ff_wallet:log_balance(wallet_id(Revert)),
{continue, Events};
do_process_transfer(p_transfer_cancel, Revert) ->
{ok, Events} = ff_pipeline:with(p_transfer, Revert, fun ff_postings_transfer:cancel/1),

View File

@ -75,7 +75,9 @@ hold_withdrawal_limits(TurnoverLimits, Route, Withdrawal, Iter) ->
commit_withdrawal_limits(TurnoverLimits, Route, Withdrawal, Iter) ->
LimitChanges = gen_limit_changes(TurnoverLimits, Route, Withdrawal, Iter),
Context = gen_limit_context(Route, Withdrawal),
commit(LimitChanges, get_latest_clock(), Context).
Clock = get_latest_clock(),
ok = commit(LimitChanges, Clock, Context),
ok = log_limit_changes(TurnoverLimits, Clock, Context).
-spec rollback_withdrawal_limits([turnover_limit()], route(), withdrawal(), pos_integer()) -> ok.
rollback_withdrawal_limits(TurnoverLimits, Route, Withdrawal, Iter) ->
@ -235,3 +237,42 @@ call(Func, Args) ->
Service = {limproto_limiter_thrift, 'Limiter'},
Request = {Service, Func, Args},
ff_woody_client:call(limiter, Request).
log_limit_changes(TurnoverLimits, Clock, Context) ->
Attrs = mk_limit_log_attributes(Context),
lists:foreach(
fun(#domain_TurnoverLimit{id = ID, upper_boundary = UpperBoundary, domain_revision = DomainRevision}) ->
#limiter_Limit{amount = LimitAmount} = get(ID, DomainRevision, Clock, Context),
ok = logger:log(notice, "Limit change commited", [], #{
limit => Attrs#{config_id => ID, boundary => UpperBoundary, amount => LimitAmount}
})
end,
TurnoverLimits
).
mk_limit_log_attributes(#limiter_LimitContext{
withdrawal_processing = #context_withdrawal_Context{withdrawal = Wthd}
}) ->
#context_withdrawal_Withdrawal{
withdrawal = #wthd_domain_Withdrawal{
body = #domain_Cash{amount = Amount, currency = Currency}
},
wallet_id = WalletID,
route = #base_Route{provider = Provider, terminal = Terminal}
} = Wthd,
#{
config_id => undefined,
%% Limit boundary amount
boundary => undefined,
%% Current amount with accounted change
amount => undefined,
route => #{
provider_id => Provider#domain_ProviderRef.id,
terminal_id => Terminal#domain_TerminalRef.id
},
wallet_id => WalletID,
change => #{
amount => Amount,
currency => Currency
}
}.

View File

@ -759,6 +759,7 @@ do_process_transfer(p_transfer_commit, Withdrawal) ->
ok = commit_routes_limits([route(Withdrawal)], Withdrawal),
Tr = ff_withdrawal_route_attempt_utils:get_current_p_transfer(attempts(Withdrawal)),
{ok, Events} = ff_postings_transfer:commit(Tr),
ok = ff_wallet:log_balance(wallet_id(Withdrawal)),
{continue, [{p_transfer, Ev} || Ev <- Events]};
do_process_transfer(p_transfer_cancel, Withdrawal) ->
ok = rollback_routes_limits([route(Withdrawal)], Withdrawal),
@ -1023,6 +1024,7 @@ handle_child_result({undefined, Events} = Result, Withdrawal) ->
true ->
{continue, Events};
false ->
ok = ff_wallet:log_balance(wallet_id(Withdrawal)),
Result
end;
handle_child_result({_OtherAction, _Events} = Result, _Withdrawal) ->

View File

@ -6,6 +6,7 @@
-export([make_empty_final/0]).
-export([gather_used_accounts/1]).
-export([find_account/2]).
-export([finalize/3]).
-export([add_fee/2]).
-export([combine/2]).
@ -131,6 +132,14 @@ make_empty_final() ->
gather_used_accounts(#{postings := Postings}) ->
lists:usort(lists:flatten([[S, D] || #{sender := #{account := S}, receiver := #{account := D}} <- Postings])).
-spec find_account(ff_account:id(), final_cash_flow()) -> account() | undefined.
find_account(AccountID, FinalCashFlow) ->
Accounts = gather_used_accounts(FinalCashFlow),
case lists:filter(fun(A) -> ff_account:id(A) =:= AccountID end, Accounts) of
[Account | _] -> Account;
_Else -> undefined
end.
-spec finalize(cash_flow_plan(), account_mapping(), constant_mapping()) ->
{ok, final_cash_flow()} | {error, finalize_error()}.
finalize(Plan, Accounts, Constants) ->

View File

@ -4,7 +4,8 @@
-module(ff_wallet).
-type id() :: ff_account:id().
-type id() :: binary().
-type account_id() :: ff_account:id().
-type external_id() :: id() | undefined.
-type metadata() :: ff_entity_context:md().
@ -14,7 +15,7 @@
name := binary(),
blocking := blocking(),
account => account(),
external_id => id(),
external_id => account_id(),
metadata => metadata(),
created_at => ff_time:timestamp_ms()
}.
@ -23,7 +24,7 @@
version := ?ACTUAL_FORMAT_VERSION,
name := binary(),
blocking := blocking(),
external_id => id(),
external_id => account_id(),
metadata => metadata(),
created_at => ff_time:timestamp_ms()
}.
@ -33,11 +34,11 @@
| {account, ff_account:event()}.
-type params() :: #{
id := id(),
id := account_id(),
identity := ff_identity_machine:id(),
name := binary(),
currency := ff_currency:id(),
external_id => id(),
external_id => account_id(),
metadata => metadata()
}.
@ -56,6 +57,7 @@
| {currency, notfound}.
-export_type([id/0]).
-export_type([account_id/0]).
-export_type([wallet/0]).
-export_type([wallet_state/0]).
-export_type([event/0]).
@ -82,6 +84,7 @@
-export([close/1]).
-export([get_account_balance/1]).
-export([check_creation/1]).
-export([log_balance/1]).
-export([apply_event/2]).
@ -100,7 +103,7 @@
-spec account(wallet_state()) -> account().
-spec id(wallet_state()) -> id().
-spec id(wallet_state()) -> account_id().
-spec identity(wallet_state()) -> identity().
-spec name(wallet_state()) -> binary().
-spec currency(wallet_state()) -> currency().
@ -191,6 +194,26 @@ check_creation(#{identity := IdentityID, currency := CurrencyID}) ->
{Identity, Currency}
end).
-spec log_balance(id()) -> ok.
log_balance(WalletID) ->
case ff_wallet_machine:get(WalletID) of
{ok, Machine} ->
Wallet = ff_wallet_machine:wallet(Machine),
{ok, {Amounts, Currency}} = ff_accounting:balance(account(Wallet)),
logger:log(notice, "Wallet balance", [], #{
wallet => #{
id => WalletID,
balance => #{
amount => ff_indef:current(Amounts),
currency => Currency
}
}
}),
ok;
{error, notfound} ->
ok
end.
%%
-spec apply_event(event(), undefined | wallet_state()) -> wallet_state().

View File

@ -410,6 +410,7 @@ do_process_transfer(p_transfer_prepare, W2WTransferState) ->
{continue, Events};
do_process_transfer(p_transfer_commit, W2WTransferState) ->
{ok, Events} = ff_pipeline:with(p_transfer, W2WTransferState, fun ff_postings_transfer:commit/1),
ok = log_wallet_balance(W2WTransferState),
{continue, Events};
do_process_transfer(p_transfer_cancel, W2WTransferState) ->
{ok, Events} = ff_pipeline:with(p_transfer, W2WTransferState, fun ff_postings_transfer:cancel/1),
@ -507,11 +508,16 @@ handle_child_result({undefined, Events} = Result, W2WTransferState) ->
true ->
{continue, Events};
false ->
ok = log_wallet_balance(W2WTransferState),
Result
end;
handle_child_result({_OtherAction, _Events} = Result, _W2WTransfer) ->
Result.
log_wallet_balance(W2WTransferState) ->
ok = ff_wallet:log_balance(wallet_to_id(W2WTransferState)),
ok = ff_wallet:log_balance(wallet_from_id(W2WTransferState)).
%% Internal getters and setters
-spec p_transfer_status(w2w_transfer_state()) -> ff_postings_transfer:status() | undefined.

View File

@ -1,6 +1,6 @@
[
{kernel, [
{log_level, info},
{logger_level, info},
{logger, [
{handler, default, logger_std_h, #{
level => debug,

View File

@ -28,7 +28,7 @@ services:
command: /sbin/init
dominant:
image: ghcr.io/valitydev/dominant:sha-ecd7531
image: ghcr.io/valitydev/dominant:sha-fdf5277
command: /opt/dominant/bin/dominant foreground
depends_on:
machinegun:
@ -40,7 +40,7 @@ services:
retries: 10
machinegun:
image: ghcr.io/valitydev/machinegun:sha-058bada
image: ghcr.io/valitydev/machinegun:sha-ed72eec
command: /opt/machinegun/bin/machinegun foreground
volumes:
- ./test/machinegun/config.yaml:/opt/machinegun/etc/config.yaml
@ -52,7 +52,7 @@ services:
retries: 10
limiter:
image: ghcr.io/valitydev/limiter:sha-2b8723b
image: ghcr.io/valitydev/limiter:sha-6c3bfe3
command: /opt/limiter/bin/limiter foreground
depends_on:
machinegun:
@ -87,7 +87,7 @@ services:
retries: 20
party-management:
image: ghcr.io/valitydev/party-management:sha-4a94036
image: ghcr.io/valitydev/party-management:sha-18bba50
command: /opt/party-management/bin/party-management foreground
depends_on:
machinegun:
@ -103,7 +103,7 @@ services:
retries: 10
bender:
image: ghcr.io/valitydev/bender:sha-d05ea29
image: ghcr.io/valitydev/bender:sha-c9ea00f
command: /opt/bender/bin/bender foreground
depends_on:
machinegun:

View File

@ -89,3 +89,7 @@ namespaces:
storage:
type: memory
logging:
out_type: stdout
level: info