This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
/
type_url.ex
287 lines (239 loc) · 7.63 KB
/
type_url.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
defmodule ForgeAbi.Util.TypeUrl do
@moduledoc """
quick convertion among type, type_url and type_mod
"""
defmodule DummyCodec do
@moduledoc false
def encode(data), do: data
def decode(data), do: data
end
use GenServer
require Logger
# tx
alias ForgeAbi.{
AccountMigrateTx,
ConsensusUpgradeTx,
ConsumeAssetTx,
CreateAssetTx,
DeclareFileTx,
DeclareTx,
ExchangeTx,
PokeTx,
StakeTx,
SysUpgradeTx,
TransferTx,
UpdateAssetTx
}
# state
alias ForgeAbi.{
AccountState,
AssetState,
ForgeState,
RootState,
StakeState,
StatisticsState
}
# stake
alias ForgeAbi.{
StakeForAsset,
StakeForChain,
StakeForNode,
StakeForUser
}
# other
alias ForgeAbi.{
BlockInfo,
Transaction,
TransactionInfo,
TxStatus
}
alias Google.Protobuf.Any
@base_types [
# forge tx
{:account_migrate, "fg:t:account_migrate", AccountMigrateTx},
{:poke, "fg:t:poke", PokeTx},
{:consume_asset, "fg:t:consume_asset", ConsumeAssetTx},
{:create_asset, "fg:t:create_asset", CreateAssetTx},
{:consensus_upgrade, "fg:t:consensus_upgrade", ConsensusUpgradeTx},
{:declare, "fg:t:declare", DeclareTx},
{:declare_file, "fg:t:declare_file", DeclareFileTx},
{:exchange, "fg:t:exchange", ExchangeTx},
{:stake, "fg:t:stake", StakeTx},
{:sys_upgrade, "fg:t:sys_upgrade", SysUpgradeTx},
{:transfer, "fg:t:transfer", TransferTx},
{:update_asset, "fg:t:update_asset", UpdateAssetTx},
# forge state
{:account_state, "fg:s:account", AccountState},
{:asset_state, "fg:s:asset", AssetState},
{:forge_state, "fg:s:forge", ForgeState},
{:stake_state, "fg:s:stake", StakeState},
{:statistics_state, "fg:s:statistics", StatisticsState},
{:root_state, "fg:s:root", RootState},
# forge tx stake
{:stake_for_node, "fg:x:stake_node", StakeForNode},
{:stake_for_user, "fg:x:stake_user", StakeForUser},
{:stake_for_asset, "fg:x:stake_asset", StakeForAsset},
{:stake_for_chain, "fg:x:stake_chain", StakeForChain},
# other type url
{:block_info, "fg:x:block_info", BlockInfo},
{:transaction, "fg:x:tx", Transaction},
{:transaction_info, "fg:x:tx_info", TransactionInfo},
{:tx_status, "fg:x:tx_status", TxStatus},
# dummy codec
{:address, "fg:x:address", DummyCodec}
]
def start_link(_) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end
@doc """
retrieve all types for introspection.
"""
@spec all :: [{atom(), String.t(), module()}]
def all, do: GenServer.call(__MODULE__, :all)
@doc """
Retrieve tuple by type or type_url
iex> ForgeAbi.Util.TypeUrl.get(:declare)
{"fg:t:declare", ForgeAbi.DeclareTx}
iex> ForgeAbi.Util.TypeUrl.get("fg:t:declare")
{:declare, ForgeAbi.DeclareTx}
iex> ForgeAbi.Util.TypeUrl.get(:account_state)
{"fg:s:account", ForgeAbi.AccountState}
iex> ForgeAbi.Util.TypeUrl.get("fg:t:transfer")
{:transfer, ForgeAbi.TransferTx}
"""
@spec get(atom() | String.t()) :: {atom() | String.t(), module()} | {:error, term()}
def get(key), do: GenServer.call(__MODULE__, {:get, key})
@spec extend([{atom(), String.t(), module()}]) :: :ok
def extend(type_urls), do: GenServer.call(__MODULE__, {:extend, type_urls})
@doc """
retrieve extended types for introspection.
"""
@spec get_extended :: [{atom(), String.t(), module()}]
def get_extended, do: GenServer.call(__MODULE__, :get_extended)
@spec remove(atom()) :: :ok
def remove(type), do: GenServer.call(__MODULE__, {:remove, type})
@doc """
Decode the binary inside the Any.
"""
@spec decode(Any.t() | nil) :: {:error, term()} | {atom(), any()}
def decode(nil), do: {nil, nil}
def decode(any), do: GenServer.call(__MODULE__, {:decode, any})
@doc """
Encode a struct and wrap it with Any.
"""
@spec encode(atom(), any()) :: {:ok, Any.t()} | {:error, term()}
def encode(type, data), do: GenServer.call(__MODULE__, {:encode, type, data})
@doc """
Encode a struct and wrap it with Any. Throw exception on error.
"""
@spec encode!(atom(), any()) :: Any.t() | no_return()
def encode!(type, data) do
case encode(type, data) do
{:ok, result} -> result
{:error, reason} -> raise reason
end
end
# callbacks
def init(_) do
# The state of this GenServer is like:
# %{
# types: [
# {:account_state, "fg:s:ccount", AccountState},
# {:asset_state, "fg:s:Asset", AssetState}
# ],
# t1: %{
# :acount_state => {"fg:s:account", AccountState},
# :asset_state => {"fg:s:asset", AssetState}
# },
# t2: %{
# "fg:s:account" => {:account_state, AccountState}
# "fg:s:asset" => {asset_state, AssetState}
# }
# }
new_state = add(@base_types, %{types: [], extended: [], t1: %{}, t2: %{}})
{:ok, new_state}
end
def handle_call(:all, _from, %{types: types} = state) do
{:reply, types, state}
end
def handle_call(:get_extended, _from, %{extended: extended} = state) do
{:reply, extended, state}
end
def handle_call({:get, key}, _from, state) do
{:reply, get_key(key, state), state}
end
def handle_call({:extend, type_urls}, _from, state) do
new_state = add(type_urls, state, true)
{:reply, :ok, new_state}
end
def handle_call({:remove, type}, _from, state) do
new_state =
case get_key(type, state) do
{:error, _} -> state
{url, _} -> remove(type, url, state)
end
{:reply, :ok, new_state}
end
def handle_call({:decode, %{type_url: type_url, value: value}}, _from, state) do
decoded =
case get_key(type_url, state) do
{:error, reason} ->
Logger.info("Failed to find #{type_url}. Reason: #{inspect(reason)}.")
{:error, reason}
{type, mod} ->
{type, mod.decode(value)}
end
{:reply, decoded, state}
rescue
e ->
Logger.warn("Failed to decode data: Error: #{inspect(e)}")
{:reply, nil, state}
end
def handle_call({:encode, type, data}, _from, state) do
encoded =
case get_key(type, state) do
{:error, reason} ->
Logger.info("Failed to find #{type} in TypeUrl server. Reason: #{inspect(reason)}.")
{:error, reason}
{type_url, mod} ->
{:ok, %Any{type_url: type_url, value: mod.encode(data)}}
end
{:reply, encoded, state}
end
def handle_call(msg, _from, state) do
Logger.warn("Unrecognized message: #{inspect(msg)}")
{:reply, msg, state}
end
# private function
defp get_key(key, %{t1: t1}) when is_atom(key),
do: t1 |> Map.get(key, {:error, "Unknown type #{key}"}) |> to_mod()
defp get_key(key, %{t2: t2}) when is_binary(key),
do: t2 |> Map.get(key, {:error, "Unknown type #{key}"}) |> to_mod()
defp to_mod({:error, _} = error), do: error
defp to_mod({t, m}) when is_atom(m), do: {t, m}
defp to_mod({t, m}), do: {t, to_atom(m)}
defp to_atom(m), do: String.to_existing_atom("Elixir." <> m)
defp add(type_urls, state, extend? \\ false) do
Enum.reduce(type_urls, state, fn {type, url, module} = type_url, acc ->
%{types: types, t1: t1, t2: t2} = acc
extended = Map.get(acc, :extended, [])
result = %{
acc
| types: [type_url | types],
t1: Map.put(t1, type, {url, module}),
t2: Map.put(t2, url, {type, module})
}
case extend? do
true -> Map.put(result, :extended, [type_url | extended])
_ -> result
end
end)
end
defp remove(type, url, %{types: types, t1: t1, t2: t2}) do
%{
types: Enum.reject(types, fn {t, _, _} -> t === type end),
t1: Map.delete(t1, type),
t2: Map.delete(t2, url)
}
end
end