-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: log open/closed channels together with their locked AE (#840)
* feat: log open/closed channels together with their locked AE * chore: add channel stats migration (~30min RUN) This migration will avoid requiring a full-sync, but it will take ~30mins to run.
- Loading branch information
Showing
12 changed files
with
503 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule AeMdw.Channels do | ||
@moduledoc """ | ||
Main channels module. | ||
""" | ||
|
||
alias AeMdw.Collection | ||
alias AeMdw.Db.ChannelCloseMutation | ||
alias AeMdw.Db.ChannelOpenMutation | ||
alias AeMdw.Db.ChannelSpendMutation | ||
alias AeMdw.Db.Model | ||
alias AeMdw.Db.State | ||
alias AeMdw.Node | ||
alias AeMdw.Txs | ||
|
||
require Model | ||
|
||
@typep state() :: State.t() | ||
@type closing_type() :: | ||
:channel_close_solo_tx | ||
| :channel_close_mutual_tx | ||
| :channel_settle_tx | ||
|
||
@spec close_mutation(closing_type(), Node.aetx()) :: ChannelCloseMutation.t() | ||
def close_mutation(tx_type, tx), do: ChannelCloseMutation.new(tx_type, tx) | ||
|
||
@spec open_mutation(Node.aetx()) :: ChannelOpenMutation.t() | ||
def open_mutation(tx), do: ChannelOpenMutation.new(tx) | ||
|
||
@spec deposit_mutation(Node.aetx()) :: ChannelSpendMutation.t() | ||
def deposit_mutation(tx), do: ChannelSpendMutation.new(:aesc_deposit_tx.amount(tx)) | ||
|
||
@spec withdraw_mutation(Node.aetx()) :: ChannelSpendMutation.t() | ||
def withdraw_mutation(tx), do: ChannelSpendMutation.new(-:aesc_withdraw_tx.amount(tx)) | ||
|
||
@spec channels_opened_count(state(), Txs.txi(), Txs.txi()) :: non_neg_integer() | ||
def channels_opened_count(state, from_txi, next_txi), | ||
do: type_count(state, :channel_create_tx, from_txi, next_txi) | ||
|
||
@spec channels_closed_count(state(), Txs.txi(), Txs.txi()) :: non_neg_integer() | ||
def channels_closed_count(state, from_txi, next_txi) do | ||
type_count(state, :channel_close_solo_tx, from_txi, next_txi) + | ||
type_count(state, :channel_close_mutual_tx, from_txi, next_txi) + | ||
type_count(state, :channel_settle_tx, from_txi, next_txi) | ||
end | ||
|
||
defp type_count(state, type, from_txi, next_txi) do | ||
state | ||
|> Collection.stream(Model.Type, {type, from_txi}) | ||
|> Stream.take_while(&match?({^type, txi} when txi < next_txi, &1)) | ||
|> Enum.count() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule AeMdw.Db.ChannelCloseMutation do | ||
@moduledoc """ | ||
Increases channels_closed stat and refund locked AE. | ||
""" | ||
|
||
alias AeMdw.Channels | ||
alias AeMdw.Db.State | ||
alias AeMdw.Node | ||
|
||
@derive AeMdw.Db.Mutation | ||
defstruct [:tx_type, :tx] | ||
|
||
@typep tx_type() :: Channels.closing_type() | ||
|
||
@opaque t() :: %__MODULE__{tx_type: tx_type(), tx: Node.aetx()} | ||
|
||
@spec new(tx_type(), Node.aetx()) :: t() | ||
def new(tx_type, tx), do: %__MODULE__{tx_type: tx_type, tx: tx} | ||
|
||
@spec execute(t(), State.t()) :: State.t() | ||
def execute(%__MODULE__{tx_type: tx_type, tx: tx}, state) do | ||
state | ||
|> State.inc_stat(:channels_closed) | ||
|> State.inc_stat(:locked_in_channels, -released_amount(tx_type, tx)) | ||
end | ||
|
||
defp released_amount(:channel_close_solo_tx, _tx), do: 0 | ||
|
||
defp released_amount(:channel_close_mutual_tx, tx) do | ||
:aesc_close_mutual_tx.initiator_amount_final(tx) + | ||
:aesc_close_mutual_tx.responder_amount_final(tx) | ||
end | ||
|
||
defp released_amount(:channel_settle_tx, tx) do | ||
%{"initiator_amount_final" => initiator_amount, "responder_amount_final" => responder_amount} = | ||
:aesc_settle_tx.for_client(tx) | ||
|
||
initiator_amount + responder_amount | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule AeMdw.Db.ChannelOpenMutation do | ||
@moduledoc """ | ||
Increases channels_opened stat. | ||
""" | ||
|
||
alias AeMdw.Db.State | ||
alias AeMdw.Node | ||
|
||
@derive AeMdw.Db.Mutation | ||
defstruct [:tx] | ||
|
||
@opaque t() :: %__MODULE__{ | ||
tx: Node.aetx() | ||
} | ||
|
||
@spec new(Node.aetx()) :: t() | ||
def new(tx), do: %__MODULE__{tx: tx} | ||
|
||
@spec execute(t(), State.t()) :: State.t() | ||
def execute(%__MODULE__{tx: tx}, state) do | ||
initiator_amount = :aesc_create_tx.initiator_amount(tx) | ||
responder_amount = :aesc_create_tx.responder_amount(tx) | ||
|
||
state | ||
|> State.inc_stat(:channels_opened) | ||
|> State.inc_stat(:locked_in_channels, initiator_amount + responder_amount) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule AeMdw.Db.ChannelSpendMutation do | ||
@moduledoc """ | ||
Logs withdraws/deposists from channels. | ||
""" | ||
|
||
alias AeMdw.Db.State | ||
|
||
@derive AeMdw.Db.Mutation | ||
defstruct [:amount] | ||
|
||
@opaque t() :: %__MODULE__{amount: integer()} | ||
|
||
@spec new(integer()) :: t() | ||
def new(amount), do: %__MODULE__{amount: amount} | ||
|
||
@spec execute(t(), State.t()) :: State.t() | ||
def execute(%__MODULE__{amount: amount}, state) do | ||
State.inc_stat(state, :locked_in_channels, amount) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.