Add tags for successfully delivered transactions

parent 6b9ad4ce
Pipeline #37257033 passed with stages
in 20 minutes and 10 seconds
......@@ -162,6 +162,15 @@ Fee: per tx + per byte
The proof is padded from the right by zero bits to form full bytes.
### Tags
The following tags are set for successfully delivered transactions:
* From (Base64-encoded address).
* To (Base64-encoded address) — if present.
* Value.
* Fee.
### Account serialization format
Key:
......
......@@ -92,7 +92,15 @@ handle_event(
{keep_state, Data1, {reply, From, #'abci.ResponseEndBlock'{validator_updates=Diffs}}};
handle_event({call, From}, #'abci.RequestDeliverTx'{tx=MaybeTxBin}, committing, Data)->
{ErrorCode, NewData} = ercoin_tx:handle_bin(MaybeTxBin, Data),
{keep_state, NewData, {reply, From, #'abci.ResponseDeliverTx'{code=ErrorCode}}};
Tags =
case ErrorCode of
?OK ->
[#'common.KVPair'{key=Key, value=Value}
|| {Key, Value} <- maps:to_list(ercoin_tx:tags(MaybeTxBin, Data))];
_ ->
[]
end,
{keep_state, NewData, {reply, From, #'abci.ResponseDeliverTx'{code=ErrorCode, tags=Tags}}};
handle_event({call, _}, _, committing, _) ->
{keep_state_and_data, postpone};
handle_event(
......
......@@ -29,9 +29,14 @@
handle_bin/2,
handle_bin/3,
required_balance/2,
tags/2,
timestamp/1,
value/1]).
-export_type([age_margin/0]).
-export_type([tags/0]).
-type tags() :: #{binary() => binary()}.
-type age_margin() :: non_neg_integer().
-spec timestamp(tx()) -> ercoin_timestamp:timestamp() | none.
......@@ -56,6 +61,14 @@ from(#vote_tx{address=Address}) ->
from(#burn_tx{address=Address}) ->
Address.
-spec to(tx()) -> address() | none.
to(#transfer_tx{to=To}) ->
To;
to(#account_tx{to=To}) ->
To;
to(_) ->
none.
-spec value(tx()) -> non_neg_integer().
value(Tx) when is_record(Tx, burn_tx) orelse is_record(Tx, transfer_tx) ->
get_value(value, Tx);
......@@ -388,3 +401,18 @@ add_to_fresh_txs(Tx, Data=#data{fresh_txs=FreshTxs, fresh_txs_hash=FreshTxsHash}
NewFreshTxs = ?SETS:add_element({ercoin_tx:timestamp(Tx), ?HASH(TxBin)}, FreshTxs),
NewFreshTxsHash = ?HASH(<<FreshTxsHash/binary, TxBin/binary>>),
Data#data{fresh_txs=NewFreshTxs, fresh_txs_hash=NewFreshTxsHash}.
-spec tags(tx() | binary(), data()) -> tags().
tags(TxBin, Data) when is_binary(TxBin) ->
tags(deserialize(TxBin), Data);
tags(Tx, Data) ->
BaseTags =
#{<<"fee">> => ercoin_fee:total(Tx, Data),
<<"from">> => base64:encode(from(Tx)),
<<"value">> => integer_to_binary(value(Tx))},
case to(Tx) of
none ->
BaseTags;
To ->
BaseTags#{<<"to">> => base64:encode(To)}
end.
......@@ -121,6 +121,22 @@ prop_deliver_tx_handles_binary_in_regard_to_data() ->
{ErrorCode, NewData} =:= ercoin_tx:handle_bin(MaybeTxBin, Data)
end).
prop_deliver_tx_sets_tags_for_successful_transactions() ->
?FORALL(
{Data, TxBin},
ercoin_tx_gen:data_with_tx_bin(),
begin
{keep_state, _, {reply, "from", #'abci.ResponseDeliverTx'{code=?OK, tags=Tags}}} =
ercoin_abci:handle_event(
{call, "from"},
#'abci.RequestDeliverTx'{
tx=TxBin},
committing,
Data),
maps:from_list([{Key, Value} || #'common.KVPair'{key=Key, value=Value} <- Tags]) =:=
ercoin_tx:tags(ercoin_tx:deserialize(TxBin), Data)
end).
%% TODO: Slash validators that propose invalid transactions.
%% A transaction may have a higher timestamp than the timestamp of last block created, so we want to check it against current timestamp, not the timestamp of last block.
......
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