Skip to content

Commit

Permalink
feat: add channels last updated time (#1435)
Browse files Browse the repository at this point in the history
  • Loading branch information
sborrazas committed Jul 4, 2023
1 parent d7ab68e commit d922851
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/ae_mdw/channels.ex
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ defmodule AeMdw.Channels do
responder: encode_account(responder_pk),
state_hash: encode(:state, state_hash),
last_updated_height: last_updated_height,
last_updated_time: DbUtil.block_time(update_block_hash),
last_updated_tx_hash: encode(:tx_hash, tx_hash),
last_updated_tx_type: Format.type_to_swagger_name(tx_type),
updates_count: length(updates),
Expand Down
3 changes: 2 additions & 1 deletion lib/ae_mdw/db/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ defmodule AeMdw.Db.Util do
def height_to_time(_state, height, last_height, last_micro_time),
do: last_micro_time + (height - last_height) * @approximate_key_block_rate

defp block_time(block_hash) do
@spec block_time(Blocks.block_hash()) :: time()
def block_time(block_hash) do
block_hash
|> :aec_db.get_block()
|> :aec_blocks.time_in_msecs()
Expand Down
19 changes: 16 additions & 3 deletions test/ae_mdw_web/controllers/channel_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ defmodule AeMdwWeb.ChannelControllerTest do

_state, {3_000, -1} ->
{:tx, :channel_deposit_tx, tx_hash, :channel_deposit_tx, block_hash}
end,
block_time: fn ^block_hash ->
456
end
]},
{:aec_chain, [:passthrough],
Expand Down Expand Up @@ -107,6 +110,7 @@ defmodule AeMdwWeb.ChannelControllerTest do
"active" => false,
"amount" => 8_000_000,
"last_updated_height" => 600_000,
"last_updated_time" => 456,
"last_updated_tx_hash" => ^enc_tx_hash,
"last_updated_tx_type" => "ChannelDepositTx",
"updates_count" => 1,
Expand All @@ -129,6 +133,7 @@ defmodule AeMdwWeb.ChannelControllerTest do
"last_updated_height" => 500_000,
"last_updated_tx_hash" => ^enc_tx_hash,
"last_updated_tx_type" => "ChannelCloseMutualTx",
"last_updated_time" => 456,
"updates_count" => 1,
"responder" => ^responder,
"initiator" => ^initiator,
Expand All @@ -152,6 +157,7 @@ defmodule AeMdwWeb.ChannelControllerTest do
"last_updated_height" => 500_000,
"last_updated_tx_hash" => ^enc_tx_hash,
"last_updated_tx_type" => "ChannelWithdrawTx",
"last_updated_time" => 456,
"updates_count" => 1,
"responder" => ^responder,
"initiator" => ^initiator,
Expand Down Expand Up @@ -216,7 +222,8 @@ defmodule AeMdwWeb.ChannelControllerTest do
[
read_node_tx_details: fn _state, {3_000, -1} ->
{:tx, :channel_deposit_tx, tx_hash, :channel_deposit_tx, block_hash}
end
end,
block_time: fn ^block_hash -> 456 end
]},
{:aec_chain, [:passthrough],
get_channel_at_hash: fn pubkey, ^block_hash ->
Expand Down Expand Up @@ -244,6 +251,7 @@ defmodule AeMdwWeb.ChannelControllerTest do
"last_updated_height" => 600_000,
"last_updated_tx_hash" => ^enc_tx_hash,
"last_updated_tx_type" => "ChannelDepositTx",
"last_updated_time" => 456,
"updates_count" => 1,
"responder" => ^responder,
"initiator" => ^initiator,
Expand Down Expand Up @@ -314,7 +322,8 @@ defmodule AeMdwWeb.ChannelControllerTest do

_state, {2_000, -1} ->
{:tx, :channel_close_mutual_tx, tx_hash, :channel_close_mutual_tx, block_hash}
end
end,
block_time: fn ^block_hash -> 456 end
]},
{:aec_chain, [:passthrough],
get_channel_at_hash: fn pubkey, ^block_hash ->
Expand Down Expand Up @@ -350,6 +359,7 @@ defmodule AeMdwWeb.ChannelControllerTest do
"last_updated_height" => 600_000,
"last_updated_tx_hash" => ^enc_tx_hash,
"last_updated_tx_type" => ^tx_type1,
"last_updated_time" => 456,
"updates_count" => 1,
"responder" => ^responder,
"initiator" => ^initiator,
Expand All @@ -376,6 +386,7 @@ defmodule AeMdwWeb.ChannelControllerTest do
"last_updated_height" => 600_000,
"last_updated_tx_hash" => ^enc_tx_hash,
"last_updated_tx_type" => ^tx_type2,
"last_updated_time" => 456,
"updates_count" => 1,
"responder" => ^responder,
"initiator" => ^initiator,
Expand Down Expand Up @@ -453,7 +464,8 @@ defmodule AeMdwWeb.ChannelControllerTest do
{:tx, :channel_close_mutual_tx, tx_hash, :channel_close_mutual_tx,
update_block_hash}
end,
micro_block_height_index: fn _state, ^micro_block_hash -> {:ok, 599_999, 1} end
micro_block_height_index: fn _state, ^micro_block_hash -> {:ok, 599_999, 1} end,
block_time: fn ^update_block_hash -> 456 end
]}
] do
store =
Expand All @@ -474,6 +486,7 @@ defmodule AeMdwWeb.ChannelControllerTest do
"last_updated_height" => 600_000,
"last_updated_tx_hash" => ^enc_tx_hash,
"last_updated_tx_type" => ^tx_type1,
"last_updated_time" => 456,
"updates_count" => 1,
"responder" => ^responder,
"initiator" => ^initiator,
Expand Down

0 comments on commit d922851

Please sign in to comment.