diff --git a/lib/ae_mdw/oracles.ex b/lib/ae_mdw/oracles.ex index 369e9481f..6f7651366 100644 --- a/lib/ae_mdw/oracles.ex +++ b/lib/ae_mdw/oracles.ex @@ -385,27 +385,25 @@ defmodule AeMdw.Oracles do query_format = :aeo_oracles.query_format(oracle_rec) response_format = :aeo_oracles.response_format(oracle_rec) query_fee = :aeo_oracles.query_fee(oracle_rec) - v3? = Keyword.get(opts, :v3?, false) - oracle = - %{ - oracle: Enc.encode(:oracle_pubkey, pk), - active: is_active?, - active_from: register_height, - register_time: DbUtil.block_index_to_time(state, register_bi), - expire_height: expire_height, - approximate_expire_time: - DbUtil.height_to_time(state, expire_height, last_gen, last_micro_time), - register: expand_bi_txi_idx(state, register_bi_txi_idx, opts), - register_tx_hash: Enc.encode(:tx_hash, Txs.txi_to_hash(state, register_txi)), - query_fee: query_fee, - format: %{ - query: query_format, - response: response_format - } + oracle = %{ + oracle: Enc.encode(:oracle_pubkey, pk), + active: is_active?, + active_from: register_height, + register_time: DbUtil.block_index_to_time(state, register_bi), + expire_height: expire_height, + approximate_expire_time: + DbUtil.height_to_time(state, expire_height, last_gen, last_micro_time), + register: expand_bi_txi_idx(state, register_bi_txi_idx, opts), + register_tx_hash: Enc.encode(:tx_hash, Txs.txi_to_hash(state, register_txi)), + query_fee: query_fee, + format: %{ + query: query_format, + response: response_format } + } - if v3? do + if Keyword.get(opts, :v3?, false) do oracle else Map.put(oracle, :extends, Enum.map(extends, &expand_bi_txi_idx(state, &1, opts))) @@ -447,7 +445,7 @@ defmodule AeMdw.Oracles do Keyword.get(opts, :v3?, false) -> state |> Txs.fetch!(txi) - |> Map.put("tx_hash", Enc.encode(:tx_hash, Txs.txi_to_hash(state, txi))) + |> put_in(["tx", "tx_hash"], Enc.encode(:tx_hash, Txs.txi_to_hash(state, txi))) |> Map.drop(["tx_index"]) Keyword.get(opts, :expand?, false) -> diff --git a/lib/ae_mdw_web/controllers/oracle_controller.ex b/lib/ae_mdw_web/controllers/oracle_controller.ex index 41731b443..a50d2ee50 100644 --- a/lib/ae_mdw_web/controllers/oracle_controller.ex +++ b/lib/ae_mdw_web/controllers/oracle_controller.ex @@ -15,7 +15,7 @@ defmodule AeMdwWeb.OracleController do @spec oracle(Conn.t(), map()) :: Conn.t() def oracle(%Conn{assigns: %{state: state, opts: opts}} = conn, %{"id" => id}) do - with opts <- [{:v3, true} | opts], + with opts <- [{:v3?, true} | opts], {:ok, oracle_pk} <- Validate.id(id, [:oracle_pubkey]), {:ok, oracle} <- Oracles.fetch(state, oracle_pk, opts) do format_json(conn, oracle) diff --git a/test/ae_mdw_web/controllers/oracle_controller_test.exs b/test/ae_mdw_web/controllers/oracle_controller_test.exs index b05018960..e4ecaf3f3 100644 --- a/test/ae_mdw_web/controllers/oracle_controller_test.exs +++ b/test/ae_mdw_web/controllers/oracle_controller_test.exs @@ -1,4 +1,5 @@ defmodule AeMdwWeb.OracleControllerTest do + alias AeMdw.Db.Format use AeMdwWeb.ConnCase @moduletag skip_store: true @@ -156,6 +157,83 @@ defmodule AeMdwWeb.OracleControllerTest do end describe "oracles" do + test "it retrieves an oracle", %{ + conn: conn, + store: store, + inactive_oracles: [oracle_id1 | _], + encoded_pks: [encoded_oracle_id | _] + } do + account_pk1 = <<7::256>> + account_id1 = :aeser_id.create(:account, account_pk1) + oracle_id = :aeser_id.create(:oracle, oracle_id1) + + {:ok, oracle_query_aetx1} = + :aeo_query_tx.new(%{ + sender_id: account_id1, + nonce: 1, + oracle_id: oracle_id, + query: "query-1", + query_fee: 11, + query_ttl: {:delta, 111}, + response_ttl: {:delta, 1_111}, + fee: 11_111 + }) + + signed_tx1 = :aetx_sign.new(oracle_query_aetx1, []) + block_hash = <<950::256>> + + with_mocks [ + {Oracle, [:passthrough], [oracle_tree!: fn _block_hash -> :aeo_state_tree.empty() end]}, + {Format, [:passthrough], + [ + to_map: fn _state, {:tx, _index, hash, {_kb_index, _mb_index}, _mb_time} -> + %{ + "hash" => Enc.encode(:tx_hash, hash), + "tx" => %{ + "abi_version" => 0, + "account_id" => Enc.encode(:account_pubkey, account_pk1), + "nonce" => 1, + "fee" => 11_111, + "oracle_ttl" => %{"type" => "delta", "value" => 111}, + "query_fee" => 11, + "type" => "OracleRegisterTx", + "version" => 1, + "tx_hash" => Enc.encode(:tx_hash, hash) + } + } + end + ]}, + {:aeo_state_tree, [:passthrough], [get_oracle: fn _pk, _tree -> TS.core_oracle() end]}, + {:aec_db, [], + [ + get_header: fn <> -> <> end, + find_tx_with_location: fn _tx_hash -> {block_hash, signed_tx1} end + ]}, + {:aec_headers, [:passthrough], + [time_in_msecs: fn <> -> @node_times[height] end]} + ] do + assert oracle = + conn + |> with_store(store) + |> get("/v3/oracles/#{encoded_oracle_id}") + |> json_response(200) + + exp1_time = @node_times[@exp1] + reg_time1 = @node_times[901] + + assert %{ + "oracle" => ^encoded_oracle_id, + "approximate_expire_time" => ^exp1_time, + "register_time" => ^reg_time1, + "register_tx_hash" => tx_hash, + "register" => %{ + "hash" => tx_hash, + "tx" => %{"type" => "OracleRegisterTx", "tx_hash" => tx_hash} + } + } = oracle + end + end + test "it retrieves active oracles first", %{ conn: conn, store: store,