Skip to content
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: configurable fee specs path from env var #1385

Merged
merged 48 commits into from
Apr 21, 2020

Conversation

mederic-p
Copy link
Contributor

@mederic-p mederic-p commented Mar 11, 2020

Closes #1353

Overview

This PR allows the fee specs path to be configured via the environment variable FEE_SPECS_FILE_PATH.

Changes

  • Updated OMG.ChildChain.Fees.FileAdapter and OMG.ChildChain.ReleaseTasks.SetFeeFileAdapterOpts to support passing FEE_SPECS_FILE_PATH environment variable
  • Updated Fees.FileAdapter and Fees.FeedAdapter to support FEE_ADAPTER=file and FEE_ADAPTER=feed respectively so it can be changed without a new compile & version release.
  • Added tests for SetFeeFeedAdapterOpts and fixed the code resulting from test failures

Usage

On the childchain, set FEE_ADAPTER and FEE_SPECS_FILE_PATH:

export FEE_ADAPTER=file
export FEE_SPECS_FILE_PATH=/path/to/fee/specs.json

Testing

  • Unit tests pass: mix test test/omg_child_chain/release_tasks/set_fee_file_adapter_opts_test.exs test/omg_child_chain/release_tasks/set_fee_feed_adapter_opts_test.exs

  • Specs test pass: cd priv/cabbage && mix test

  • Fees are reflected from the fee specs file e.g. the specs file:

    {
        "1": [{
            "token": "0x0000000000000000000000000000000000000000",
            "amount": 3256762,
            "subunit_to_unit": 1000000000000000000,
            "pegged_amount": null,
            "pegged_currency": null,
            "pegged_subunit_to_unit": null,
            "updated_at": "2019-01-01T10:10:00+00:00"
        }]
    }

    Gives

    $ curl -X POST http://localhost:7534/fees.al
    -H "Content-Type: application/json" \
    -d '{}' \
    -v -w "\n" | jq
    
    {
      "data": {
        "1": [
          {
            "amount": 3256762,
            "currency": "0x0000000000000000000000000000000000000000",
            "pegged_amount": null,
            "pegged_currency": null,
            "pegged_subunit_to_unit": null,
            "subunit_to_unit": 1e+18,
           "updated_at": "2019-01-01T10:10:00Z"
          }
        ]
      },
      "service_name": "watcher_info",
      "success": true,
      "version": "0.4.6+280022b"
    }

@coveralls
Copy link

coveralls commented Mar 11, 2020

Coverage Status

Coverage increased (+0.3%) to 77.917% when pulling 14ee7cd on mederic-p/1353-fee-file-path into 6a26336 on master.

@unnawut unnawut self-assigned this Mar 17, 2020
# Conflicts:
#	apps/omg_child_chain/test/omg_child_chain/integration/fee_server_test.exs
@unnawut unnawut changed the title feat: move fee file path to env var feat: configurable fee specs path from env var Apr 1, 2020
@unnawut unnawut requested review from T-Dnzt and InoMurko April 1, 2020 16:50
@unnawut unnawut marked this pull request as ready for review April 1, 2020 16:51
@spec write_fee_file(%{Crypto.address_t() => map()} | binary(), binary() | nil) :: {:ok, binary, binary}
def write_fee_file(fee_map, file_name \\ nil)
@spec write_fee_file(%{Crypto.address_t() => map()} | binary()) :: {:ok, binary, binary}
def write_fee_file(fee_map, file_path \\ nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what purpose does this serve though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fee specs file is supposed to be updatable but it's residing deep in _build/.../<app-version>/.../omg_child_chain/priv folder making it hard and error prone to update.

So going for a path instead so infra could keep the file somewhere else outside the repo directory and we can refer to it via env var.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we stil need the default arument? file_path \\ nil?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we still do because https://github.com/omisego/elixir-omg/blob/master/apps/omg_child_chain/test/omg_child_chain/integration/fee_server_test.exs specifically tests the behaviour when the same file gets updated.

@@ -90,7 +90,7 @@ defmodule OMG.Watcher.Fixtures do
config :omg_db, path: "#{db_path}"
# this causes the inner test child chain server process to log info. To see these logs adjust test's log level
config :logger, level: :info
config :omg_child_chain, fee_adapter_opts: [specs_file_name: "#{fee_file}"]
config :omg_child_chain, fee_adapter_opts: [specs_file_path: "#{fee_file}"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the last week, this configuration has been changed 4 times :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the other big change to fees was from #1373 which turned the file-only into an adapter for file & feed. I had to rebase and fix conflicts a few times 🤦‍♂

Copy link
Contributor

@pnowosie pnowosie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious was there an issue created for this? It isn't linked.

I want to understand more because what I thought is that having fees in file is hard/impossible to change in the production deployment.

I also thought that fee file become obsolete by the feed service.

Thanks for explaination!

@@ -13,65 +13,86 @@
# limitations under the License.

defmodule OMG.ChildChain.ReleaseTasks.SetFeeFeedAdapterOpts do
@moduledoc false
@moduledoc """
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

@unnawut unnawut requested a review from InoMurko April 20, 2020 12:25
@spec write_fee_file(%{Crypto.address_t() => map()} | binary(), binary() | nil) :: {:ok, binary, binary}
def write_fee_file(fee_map, file_name \\ nil)
@spec write_fee_file(%{Crypto.address_t() => map()} | binary()) :: {:ok, binary, binary}
def write_fee_file(fee_map, file_path \\ nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we stil need the default arument? file_path \\ nil?


def write_fee_file(map, file_name) when is_map(map) do
def write_fee_file(map, file_path) when is_map(map) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this first parameter keep on changing it's name?
fee_map, map, content?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 renamed fee_map to a more accurate map_or_content

full_path = "#{priv_dir}/#{file}"
def write_fee_file(content, file_path) do
file_path =
file_path || "#{:code.priv_dir(:omg_child_chain)}/test_fees_file-#{DateTime.to_unix(DateTime.utc_now())}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this ought to be expanded?
is this a case?

path = case file_path do
nil->"#{:code.priv_dir(:omg_child_chain)}/test_fees_file-#{DateTime.to_unix(DateTime.utc_now())}"
_ -> file_path

|> parse_adapter_value()
|> case do
"FEED" -> configure_feed_adapter(existing_config, config)
_ -> config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like in this case we're not margining into Config.Reader.merge/2?
is this not tested?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case no config is changed so I'm passing the original config back. Refactored in d2b16ca to make this clearer though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh shit. my bad!

|> parse_adapter_value()
|> case do
"FILE" -> configure_file_adapter(config)
_ -> config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, do we not need to Config.Reader.merge/2?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case no config is changed so I'm passing the original config back. Refactored in d2b16ca to make this clearer though.

:ok = System.put_env(@env_fee_adapter, "feed")

:ok = System.put_env(@env_fee_change_tolerance_percent, "1.5")
assert_raise ArgumentError, fn -> SetFeeFeedAdapterOpts.load([], []) end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this raising with a message key? do you want to check if's returning your message?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a plain ArgumentError from String.to_integer/1. I've updated it to include a message + assertions though! f2157dc

@unnawut unnawut merged commit e209c46 into master Apr 21, 2020
@unnawut unnawut deleted the mederic-p/1353-fee-file-path branch April 21, 2020 09:27
@unnawut unnawut added the enhancement New feature or request label Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Present the fee_specs.json in a consistent path between releases
5 participants