-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: imports hardforks preset accounts #805
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ffeab9b
feat: imports hardfork preset accounts
jyeshe bb87ad8
docs: add token migration parameter values to transfers
jyeshe 25567be
test: add account param test and validates all fields
jyeshe b6effb0
chore: add genesis accounts to migrated ones
jyeshe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,54 @@ | ||
defmodule AeMdw.Db.HardforkPresets do | ||
@moduledoc """ | ||
Saves hardfork presets into Mdw database. | ||
""" | ||
alias AeMdw.Database | ||
alias AeMdw.Db.IntTransfersMutation | ||
alias AeMdw.Db.Model | ||
alias AeMdw.Db.State | ||
|
||
@doc """ | ||
Imports hardfork migrated accounts. | ||
""" | ||
@spec import_account_presets() :: :ok | ||
def import_account_presets do | ||
case Database.next_key(Model.KindIntTransferTx, {"accounts_lima", {-1, -1}, <<>>, -1}) do | ||
{:ok, {"accounts_lima", _bi, _target, _txi}} -> | ||
:ok | ||
|
||
_missing -> | ||
do_import_account_presets() | ||
end | ||
end | ||
|
||
defp do_import_account_presets() do | ||
minerva_mutations = hardfork_mutations(:minerva, &:aec_fork_block_settings.minerva_accounts/0) | ||
fortuna_mutations = hardfork_mutations(:fortuna, &:aec_fork_block_settings.fortuna_accounts/0) | ||
lima_mutations = hardfork_mutations(:lima, &:aec_fork_block_settings.lima_accounts/0) | ||
|
||
State.commit(State.new(), [minerva_mutations, fortuna_mutations, lima_mutations]) | ||
end | ||
|
||
defp hardfork_mutations(hardfork, fork_settings_accounts_fn) do | ||
transfers = hardfork_transfers("accounts_#{hardfork}", fork_settings_accounts_fn) | ||
|
||
hardfork | ||
|> hardfork_height() | ||
|> IntTransfersMutation.new(transfers) | ||
end | ||
|
||
defp hardfork_transfers(kind, fork_settings_accounts_fn) do | ||
fork_settings_accounts_fn.() | ||
|> Enum.map(fn {account_pk, amount} -> | ||
{kind, account_pk, amount} | ||
end) | ||
end | ||
|
||
defp hardfork_height(hardfork) do | ||
hf_vsn = :aec_hard_forks.protocol_vsn(hardfork) | ||
|
||
:aec_governance.get_network_id() | ||
|> :aec_hard_forks.protocols_from_network_id() | ||
|> Map.get(hf_vsn) | ||
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 was deleted.
Oops, something went wrong.
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,43 @@ | ||
defmodule AeMdw.Db.IntTransfersMutation do | ||
@moduledoc """ | ||
Saves internal transfers rewarded or migrated into the blockchain including block and dev rewards. | ||
""" | ||
|
||
alias AeMdw.Blocks | ||
alias AeMdw.Db.IntTransfer | ||
alias AeMdw.Db.State | ||
|
||
@derive AeMdw.Db.Mutation | ||
defstruct [:height, :transfers] | ||
|
||
@type transfer() :: {IntTransfer.kind(), IntTransfer.target(), IntTransfer.amount()} | ||
|
||
@opaque t() :: %__MODULE__{ | ||
height: Blocks.height(), | ||
transfers: [transfer()] | ||
} | ||
|
||
@ref_txi -1 | ||
@txi_pos -1 | ||
|
||
@spec new(Blocks.height(), [transfer()]) :: t() | ||
def new(height, transfers) do | ||
%__MODULE__{height: height, transfers: transfers} | ||
end | ||
|
||
@spec execute(t(), State.t()) :: State.t() | ||
def execute(%__MODULE__{height: height, transfers: transfers}, state) do | ||
gen_txi_pos = {height, @txi_pos} | ||
|
||
Enum.reduce(transfers, state, fn {kind, target_pk, amount}, state -> | ||
new_state = IntTransfer.write(state, gen_txi_pos, kind, target_pk, @ref_txi, amount) | ||
|
||
if kind in ["reward_dev", "reward_block"] do | ||
stat_kind = if kind == "reward_dev", do: :dev_reward, else: :block_reward | ||
State.inc_stat(new_state, stat_kind, amount) | ||
else | ||
new_state | ||
end | ||
end) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule AeMdw.Db.HardforkPresetsTest do | ||
use ExUnit.Case, async: false | ||
|
||
alias AeMdw.Collection | ||
alias AeMdw.Db.HardforkPresets | ||
alias AeMdw.Db.Model | ||
alias AeMdw.Db.State | ||
|
||
describe "import_account_presets" do | ||
test "saves minerva, fortuna and lima migrated accounts" do | ||
HardforkPresets.import_account_presets() | ||
|
||
assert 325 == | ||
State.new() | ||
|> Collection.stream(Model.KindIntTransferTx, {"accounts_minerva", nil, nil, nil}) | ||
|> Stream.take_while(&match?({"accounts_minerva", _bi, _target, _txi}, &1)) | ||
|> Enum.count() | ||
|
||
assert 304 == | ||
State.new() | ||
|> Collection.stream(Model.KindIntTransferTx, {"accounts_fortuna", nil, nil, nil}) | ||
|> Stream.take_while(&match?({"accounts_fortuna", _bi, _target, _txi}, &1)) | ||
|> Enum.count() | ||
|
||
assert 702 == | ||
State.new() | ||
|> Collection.stream(Model.KindIntTransferTx, {"accounts_lima", nil, nil, nil}) | ||
|> Stream.take_while(&match?({"accounts_lima", _bi, _target, _txi}, &1)) | ||
|> Enum.count() | ||
end | ||
end | ||
end |
113 changes: 113 additions & 0 deletions
113
test/ae_mdw_web/controllers/transfer_controller_test.exs
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,113 @@ | ||
defmodule AeMdwWeb.TransferControllerTest do | ||
use AeMdwWeb.ConnCase | ||
|
||
alias AeMdw.Db.HardforkPresets | ||
|
||
setup_all _ do | ||
HardforkPresets.import_account_presets() | ||
:ok | ||
end | ||
|
||
describe "transfers" do | ||
test "returns hardfork kind transfers with accounts prefix", %{ | ||
conn: conn | ||
} do | ||
kind_prefix = "accounts" | ||
kinds = ~w(accounts_minerva accounts_fortuna accounts_lima) | ||
|
||
conn = get(conn, "/v2/transfers", direction: "forward", kind: kind_prefix, limit: 100) | ||
response = json_response(conn, 200) | ||
|
||
assert Enum.count(response["data"]) == 100 | ||
|
||
assert Enum.all?(response["data"], fn %{"kind" => kind} -> | ||
String.starts_with?(kind, kind_prefix) and kind in kinds | ||
end) | ||
|
||
conn_next = get(conn, response["next"]) | ||
response_next = json_response(conn_next, 200) | ||
|
||
assert Enum.count(response_next["data"]) == 100 | ||
|
||
assert Enum.all?(response_next["data"], fn %{"kind" => kind} -> | ||
String.starts_with?(kind, kind_prefix) and kind in kinds | ||
end) | ||
end | ||
|
||
test "returns fortuna accounts transfers filtering by kind", %{ | ||
conn: conn | ||
} do | ||
kind_prefix = "accounts_fortuna" | ||
|
||
conn = get(conn, "/v2/transfers", direction: "forward", kind: kind_prefix, limit: 100) | ||
response = json_response(conn, 200) | ||
|
||
assert Enum.count(response["data"]) == 100 | ||
|
||
fortuna_height = | ||
:aec_governance.get_network_id() | ||
|> :aec_hard_forks.protocols_from_network_id() | ||
|> Map.get(3) | ||
|
||
assert Enum.all?(response["data"], fn %{ | ||
"kind" => kind, | ||
"amount" => amount, | ||
"height" => height | ||
} -> | ||
kind == kind_prefix and amount > 0 and height == fortuna_height | ||
end) | ||
|
||
conn_next = get(conn, response["next"]) | ||
response_next = json_response(conn_next, 200) | ||
|
||
assert Enum.count(response_next["data"]) == 100 | ||
|
||
assert Enum.all?(response_next["data"], fn %{ | ||
"account_id" => account_id, | ||
"kind" => kind, | ||
"amount" => amount, | ||
"height" => height | ||
} -> | ||
if account_id in [ | ||
"ak_292g7TxJNGFfFXxsvFhJ5MfP6RzMrFGMJxRaY4Tx7byaBFQTg4", | ||
"ak_2qdNcDDcELtsSPwot9ojjojuUxsucpCTcqbqZPT4nKjo1wtEEj" | ||
] do | ||
kind == kind_prefix and amount == 0 and height == fortuna_height | ||
else | ||
kind == kind_prefix and amount > 0 and height == fortuna_height | ||
end | ||
end) | ||
end | ||
|
||
test "returns a lima account transfer filtering by account", %{ | ||
conn: conn | ||
} do | ||
account_id = "ak_zGR3f3QQ3BDAwhuPktLanrqcb6vrbViBr8RzU5VeqGNDbEyx9" | ||
|
||
conn = get(conn, "/v2/transfers", direction: "forward", account: account_id) | ||
response = json_response(conn, 200) | ||
|
||
lima_height = | ||
:aec_governance.get_network_id() | ||
|> :aec_hard_forks.protocols_from_network_id() | ||
|> Map.get(4) | ||
|
||
assert Enum.count(response["data"]) == 1 | ||
|
||
assert %{ | ||
"account_id" => ^account_id, | ||
"kind" => "accounts_lima", | ||
"amount" => 1_186_111_575_000_000_000_000, | ||
"height" => ^lima_height | ||
} = List.first(response["data"]) | ||
end | ||
|
||
test "renders error when the range is invalid", %{conn: conn} do | ||
range = "invalid" | ||
error_msg = "invalid range: #{range}" | ||
conn = get(conn, "/v2/transfers", scope: "gen:#{range}") | ||
|
||
assert %{"error" => ^error_msg} = json_response(conn, 400) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we turn this entirely into a mutation? That way we continue encapsulating Database calls through the State
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Database
is used because the goal is to check if the data is already persisted.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that's what it does. I was suggesting changing how it does it. It's optional anyway, feel free to merge it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am talking about how not to encapsulate things to make the purpose clearer.