Skip to content

Commit 8383c71

Browse files
authored
feat: render call details for ga_attach and ga_meta (#972)
1 parent 3e83163 commit 8383c71

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

lib/ae_mdw/contract.ex

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ defmodule AeMdw.Contract do
4949
return: any()
5050
}
5151
@type fun_arg_res_or_error :: fun_arg_res() | {:error, any()}
52-
@type local_idx() :: non_neg_integer()
52+
@type local_idx :: non_neg_integer()
53+
@typep pubkey :: DBN.pubkey()
5354
@typep tx :: Node.tx()
5455
@typep signed_tx :: Node.signed_tx()
5556
@typep block_hash :: <<_::256>>
@@ -298,6 +299,18 @@ defmodule AeMdw.Contract do
298299
|> Map.put("source_hash", source_hash && Base.encode64(source_hash))
299300
end
300301

302+
@spec get_ga_attach_call_details(signed_tx(), pubkey(), block_hash()) :: serialized_call()
303+
def get_ga_attach_call_details(signed_tx, contract_pk, block_hash) do
304+
call_rec = call_rec(signed_tx, contract_pk, block_hash)
305+
{mod, tx_rec} = signed_tx |> :aetx_sign.tx() |> :aetx.specialize_callback()
306+
307+
%{
308+
"args" => contract_init_args(contract_pk, tx_rec, mod),
309+
"gas_used" => :aect_call.gas_used(call_rec),
310+
"return_type" => :aect_call.return_type(call_rec)
311+
}
312+
end
313+
301314
@spec stringfy_log_topics([map()]) :: [map()]
302315
def stringfy_log_topics(logs) do
303316
Enum.map(logs, fn log ->
@@ -430,9 +443,9 @@ defmodule AeMdw.Contract do
430443
end
431444
end
432445

433-
defp contract_init_args(contract_pk, tx_rec) do
446+
defp contract_init_args(contract_pk, tx_rec, mod \\ :aect_create_tx) do
434447
with {:ok, {type_info, _compiler_vsn, _source_hash}} <- get_info(contract_pk),
435-
call_data <- :aect_create_tx.call_data(tx_rec),
448+
call_data <- mod.call_data(tx_rec),
436449
{"init", args} <- decode_call_data(type_info, call_data) do
437450
args_type_value(args)
438451
else

lib/ae_mdw/db/format.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ defmodule AeMdw.Db.Format do
285285

286286
defp custom_encode(_state, :ga_attach_tx, tx, tx_rec, signed_tx, _txi, block_hash) do
287287
contract_pk = :aega_attach_tx.contract_pubkey(tx_rec)
288-
call_rec = Contract.call_rec(signed_tx, contract_pk, block_hash)
288+
call_details = Contract.get_ga_attach_call_details(signed_tx, contract_pk, block_hash)
289289

290290
tx
291291
|> Map.put("contract_id", Enc.encode(:contract_pubkey, contract_pk))
292-
|> Map.put("return_type", :aect_call.return_type(call_rec))
292+
|> Map.merge(call_details)
293293
end
294294

295295
defp custom_encode(_state, :ga_meta_tx, tx, tx_rec, _signed_tx, _txi, block_hash) do
@@ -298,10 +298,14 @@ defmodule AeMdw.Db.Format do
298298

299299
case :aec_chain.get_ga_call(owner_pk, auth_id, block_hash) do
300300
{:ok, ga_object} ->
301-
Map.put(tx, "return_type", :aega_call.return_type(ga_object))
301+
tx
302+
|> Map.put("return_type", :aega_call.return_type(ga_object))
303+
|> Map.put("gas_used", :aega_call.gas_used(ga_object))
302304

303305
_error_revert ->
304-
Map.put(tx, "return_type", :unknown)
306+
tx
307+
|> Map.put("return_type", :unknown)
308+
|> Map.put("gas_used", nil)
305309
end
306310
end
307311

test/ae_mdw_web/controllers/tx_controller_test.exs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule AeMdwWeb.TxControllerTest do
99
alias AeMdw.TestSamples, as: TS
1010

1111
import AeMdwWeb.BlockchainSim, only: [with_blockchain: 3, tx: 3]
12+
import AeMdw.Util.Encoding
1213
import Mock
1314

1415
require Model
@@ -52,35 +53,67 @@ defmodule AeMdwWeb.TxControllerTest do
5253
end
5354
end
5455

55-
test "returns an ga_attach_tx with return_type", %{
56+
test "returns an ga_attach_tx with call details", %{
5657
conn: conn,
5758
store: store
5859
} do
5960
nonce = 3
6061

62+
call_data =
63+
<<43, 17, 68, 214, 68, 31, 27, 159, 1, 81, 36, 174, 134, 247, 199, 24, 36, 209, 93, 111,
64+
71, 91, 175, 20, 235, 201, 171, 175, 148, 138>>
65+
6166
with_blockchain %{ga: 10_000},
6267
mb: [
63-
ga_tx: tx(:ga_attach_tx, :ga, %{nonce: nonce})
68+
ga_tx: tx(:ga_attach_tx, :ga, %{nonce: nonce, call_data: call_data})
6469
] do
65-
%{txs: [tx]} = blocks[:mb]
70+
%{txs: [signed_tx]} = blocks[:mb]
6671
{:id, :account, account_pk} = accounts[:ga]
6772
account_id = encode(:account_pubkey, account_pk)
6873
mb_hash = :crypto.strong_rand_bytes(32)
6974

7075
store =
7176
store
72-
|> Store.put(Model.Tx, Model.tx(index: 1, block_index: {0, 0}, id: :aetx_sign.hash(tx)))
77+
|> Store.put(
78+
Model.Tx,
79+
Model.tx(index: 1, block_index: {0, 0}, id: :aetx_sign.hash(signed_tx))
80+
)
7381
|> Store.put(Model.Block, Model.block(index: {0, -1}, tx_index: 1))
7482
|> Store.put(Model.Block, Model.block(index: {0, 0}, hash: mb_hash, tx_index: 1))
7583
|> Store.put(Model.Block, Model.block(index: {1, -1}, tx_index: 2))
7684

77-
tx_hash = encode(:tx_hash, :aetx_sign.hash(tx))
85+
functions = %{
86+
<<68, 214, 68, 31>> =>
87+
{[], {[bytes: 20], {:tuple, []}},
88+
%{
89+
0 => [
90+
{:STORE, {:var, -1}, {:immediate, 1}},
91+
{:STORE, {:var, -2}, {:arg, 0}},
92+
{:RETURNR, {:immediate, {:tuple, {}}}}
93+
]
94+
}}
95+
}
96+
97+
type_info = {:fcode, functions, %{<<68, 214, 68, 31>> => "init"}, nil}
98+
{_mod, tx} = signed_tx |> :aetx_sign.tx() |> :aetx.specialize_callback()
99+
contract_pk = :aega_attach_tx.contract_pubkey(tx)
100+
101+
AeMdw.EtsCache.put(AeMdw.Contract, contract_pk, {type_info, nil, nil})
102+
103+
tx_hash = encode(:tx_hash, :aetx_sign.hash(signed_tx))
104+
105+
{:tuple, {_fun_hash, {:tuple, {{:bytes, arg_bytes}}}}} =
106+
:aeb_fate_encoding.deserialize(call_data)
107+
108+
bytes_arg = encode(:bytearray, arg_bytes)
78109

79110
assert %{
80111
"hash" => ^tx_hash,
81112
"tx" => %{
82113
"nonce" => ^nonce,
83114
"owner_id" => ^account_id,
115+
"args" => [%{"type" => "bytes", "value" => ^bytes_arg}],
116+
"gas_used" => 1_000,
84117
"return_type" => "ok",
85118
"type" => "GAAttachTx"
86119
}
@@ -120,7 +153,8 @@ defmodule AeMdwWeb.TxControllerTest do
120153
"hash" => ^tx_hash,
121154
"tx" => %{
122155
"ga_id" => ^account_id,
123-
"return_type" => "some_return_type",
156+
"gas_used" => 2_000,
157+
"return_type" => "ok",
124158
"tx" => %{
125159
"tx" => %{
126160
"sender_id" => ^account_id,

test/support/blockchain_sim.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ defmodule AeMdwWeb.BlockchainSim do
5454
if ga_tx_hash do
5555
[
5656
{:aec_chain, [:passthrough],
57-
get_ga_call: fn ^ga_pk, _auth_id, _block_hash -> {:ok, ga_tx_hash} end,
58-
get_contract_call: fn _pk, _call_id, ^block_hash ->
57+
get_ga_call: fn ^ga_pk, _auth_id, _block_hash ->
58+
{:ok, :aega_call.new({:id, :account, ga_pk}, ga_pk, 1, 10_000, 2_000, :ok, "")}
59+
end,
60+
get_contract_call: fn _ga_pk, _call_id, ^block_hash ->
5961
{:ok, call_rec("attach", ga_pk)}
60-
end},
61-
{:aega_call, [:passthrough], return_type: fn ^ga_tx_hash -> :some_return_type end}
62+
end}
6263
]
6364
else
6465
[]

0 commit comments

Comments
 (0)