Skip to content

Commit

Permalink
fix: oracles v3 endpoint was returning v2 data (#1874)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaboiishere authored Aug 12, 2024
1 parent 27d1901 commit f44d45a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 20 deletions.
36 changes: 17 additions & 19 deletions lib/ae_mdw/oracles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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) ->
Expand Down
2 changes: 1 addition & 1 deletion lib/ae_mdw_web/controllers/oracle_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
78 changes: 78 additions & 0 deletions test/ae_mdw_web/controllers/oracle_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule AeMdwWeb.OracleControllerTest do
alias AeMdw.Db.Format
use AeMdwWeb.ConnCase
@moduletag skip_store: true

Expand Down Expand Up @@ -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 <<height::256>> -> <<height::256>> end,
find_tx_with_location: fn _tx_hash -> {block_hash, signed_tx1} end
]},
{:aec_headers, [:passthrough],
[time_in_msecs: fn <<height::256>> -> @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,
Expand Down

0 comments on commit f44d45a

Please sign in to comment.