Add a query for fetching fees

parent 293db410
Pipeline #30005641 passed with stages
in 19 minutes and 28 seconds
......@@ -13,12 +13,28 @@
-module(ercoin_fee).
-export(
[long/2,
[deserialize/1,
fees/1,
long/2,
serialize/1,
short/2,
total/2]).
-include_lib("include/ercoin.hrl").
-spec serialize(map()) -> binary().
serialize(
#{fee_per_tx := FeePerTx,
fee_per_256_bytes := FeePer256B,
fee_per_account_day := FeePerAccountDay}) ->
<<FeePerTx:8/unit:8, FeePer256B:8/unit:8, FeePerAccountDay:8/unit:8>>.
-spec deserialize(binary()) -> map().
deserialize(<<FeePerTx:8/unit:8, FeePer256B:8/unit:8, FeePerAccountDay:8/unit:8>>) ->
#{fee_per_tx => FeePerTx,
fee_per_256_bytes => FeePer256B,
fee_per_account_day => FeePerAccountDay}.
-spec div_ceil(integer(), integer()) -> integer().
%% @doc Divide integers with rounding up.
div_ceil(Dividend, Divisor) ->
......
......@@ -19,7 +19,7 @@
[perform/2]).
-spec perform(abci:'abci.RequestQuery'(), data()) -> abci:'abci.ResponseQuery'().
perform(#'abci.RequestQuery'{path=Path, data=QueryData}, #data{accounts=Accounts}) ->
perform(#'abci.RequestQuery'{path=Path, data=QueryData}, Data=#data{accounts=Accounts}) ->
{Code, ResponseValue, Error} =
case iolist_to_binary(Path) of
<<"account">> ->
......@@ -29,6 +29,8 @@ perform(#'abci.RequestQuery'{path=Path, data=QueryData}, #data{accounts=Accounts
AccountBin ->
{?OK, AccountBin, []}
end;
<<"fees">> ->
{?OK, ercoin_fee:serialize(ercoin_fee:fees(Data)), []};
_ ->
{?BAD_REQUEST, <<>>, []}
end,
......
......@@ -25,35 +25,20 @@
-include_lib("include/ercoin.hrl").
-spec choices_serialize(choices()) -> binary().
choices_serialize(
#{fee_per_tx := FeePerTx,
fee_per_256_bytes := FeePer256B,
fee_per_account_day := FeePerAccountDay,
protocol := Protocol}) ->
<<FeePerTx:8/unit:8, FeePer256B:8/unit:8, FeePerAccountDay:8/unit:8, Protocol>>.
choices_serialize(Choices=#{protocol := Protocol}) ->
<<(ercoin_fee:serialize(Choices))/binary, Protocol>>.
-spec serialize(vote() | none) -> binary().
serialize(
#vote{
valid_since=ValidSince,
choices=
#{fee_per_tx := FeePerTx,
fee_per_256_bytes := FeePer256B,
fee_per_account_day := FeePerAccountDay,
protocol := Protocol}}) ->
<<ValidSince:4/unit:8, FeePerTx:8/unit:8, FeePer256B:8/unit:8, FeePerAccountDay:8/unit:8, Protocol>>;
serialize(#vote{valid_since=ValidSince, choices=Choices}) ->
<<ValidSince:4/unit:8, (choices_serialize(Choices))/binary>>;
serialize(none) ->
<<>>.
-spec deserialize(binary()) -> vote() | none.
deserialize(<<ValidSince:4/unit:8, FeePerTx:8/unit:8, FeePer256B:8/unit:8, FeePerAccountDay:8/unit:8, Protocol>>) ->
deserialize(<<ValidSince:4/unit:8, FeesBin:24/binary, Protocol>>) ->
#vote{
valid_since=ValidSince,
choices=
#{fee_per_tx => FeePerTx,
fee_per_256_bytes => FeePer256B,
fee_per_account_day => FeePerAccountDay,
protocol => Protocol}};
choices=maps:put(protocol, Protocol, ercoin_fee:deserialize(FeesBin))};
deserialize(<<>>) ->
none.
......
......@@ -19,6 +19,7 @@
[data_with_query/0,
data_with_query_account_not_existing/0,
data_with_query_account_valid/0,
data_with_query_fees/0,
data_with_query_unknown/0]).
data_with_query_unknown() ->
......@@ -42,8 +43,15 @@ data_with_query_account_not_existing() ->
path="account",
data=Bin}}).
data_with_query_fees() ->
?LET(
Data,
data(),
{Data, #'abci.RequestQuery'{path="fees"}}).
data_with_query() ->
oneof(
[fun data_with_query_unknown/0,
fun data_with_query_account_valid/0,
fun data_with_query_account_not_existing/0]).
fun data_with_query_account_not_existing/0,
fun data_with_query_fees/0]).
......@@ -21,6 +21,7 @@
ercoin_query_gen,
[data_with_query_account_not_existing/0,
data_with_query_account_valid/0,
data_with_query_fees/0,
data_with_query_unknown/0]).
prop_account_query_returns_serialized_account() ->
......@@ -46,3 +47,13 @@ prop_unknown_query_returns_bad_request_code() ->
{Data, Query},
data_with_query_unknown(),
?BAD_REQUEST =:= get_value(code, ercoin_query:perform(Query, Data))).
prop_fees_query_returns_fees() ->
?FORALL(
{Data, Query},
data_with_query_fees(),
begin
#'abci.ResponseQuery'{value=ResponseValue, code=ResponseCode} =
ercoin_query:perform(Query, Data),
ResponseCode =:= ?OK andalso ResponseValue =:= ercoin_fee:serialize(ercoin_fee:fees(Data))
end).
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment