Add message and type to transaction tags

parent 387f2365
Pipeline #37320920 passed with stages
in 20 minutes and 48 seconds
......@@ -167,9 +167,11 @@ The proof is padded from the right by zero bits to form full bytes.
The following tags are set for successfully delivered transactions:
* From (Base64-encoded address).
* Fee.
* Message (Base64-encoded) — if present.
* To (Base64-encoded address) — if present.
* Type.
* Value.
* Fee.
### Account serialization format
......
......@@ -13,6 +13,7 @@
-module(ercoin_tx).
-compile({parse_transform, dynarec}).
-compile({parse_transform, category}).
-include_lib("include/ercoin.hrl").
......@@ -39,7 +40,7 @@
-type tags() :: #{binary() => binary()}.
-type age_margin() :: non_neg_integer().
-spec timestamp(tx()) -> ercoin_timestamp:timestamp() | none.
-spec timestamp(tx()) -> datum:option(ercoin_timestamp:timestamp()).
timestamp(#transfer_tx{timestamp=Timestamp}) ->
Timestamp;
timestamp(#vote_tx{vote=#vote{timestamp=Timestamp}}) ->
......@@ -47,7 +48,7 @@ timestamp(#vote_tx{vote=#vote{timestamp=Timestamp}}) ->
timestamp(#burn_tx{timestamp=Timestamp}) ->
Timestamp;
timestamp(_) ->
none.
undefined.
-spec from(tx()) -> address().
from(#transfer_tx{from=From}) ->
......@@ -61,20 +62,31 @@ from(#vote_tx{address=Address}) ->
from(#burn_tx{address=Address}) ->
Address.
-spec to(tx()) -> address() | none.
-spec to(tx()) -> datum:option(address()).
to(#transfer_tx{to=To}) ->
To;
to(#account_tx{to=To}) ->
To;
to(_) ->
none.
undefined.
-spec message(tx()) -> datum:option(message()).
message(Tx) when is_record(Tx, burn_tx); is_record(Tx, transfer_tx) ->
get_value(message, Tx);
message(_) ->
undefined.
-spec value(tx()) -> non_neg_integer().
value(Tx) when is_record(Tx, burn_tx) orelse is_record(Tx, transfer_tx) ->
value(Tx) when is_record(Tx, burn_tx); is_record(Tx, transfer_tx) ->
get_value(value, Tx);
value(_) ->
0.
-spec type_no(tx()) -> non_neg_integer().
type_no(Tx) ->
<<TypeNo, _/binary>> = serialize(Tx),
TypeNo.
-spec required_balance(tx(), data()) -> non_neg_integer().
required_balance(Tx, Data) ->
value(Tx) + ercoin_fee:total(Tx, Data).
......@@ -196,7 +208,7 @@ error_code(Tx, Data) ->
-spec error_code(tx(), data(), age_margin()) -> error_code().
error_code(Tx, Data=#data{timestamp=Timestamp}, AgeMargin) ->
TxTimestamp = ercoin_tx:timestamp(Tx),
case TxTimestamp =:= none orelse TxTimestamp =< Timestamp andalso TxTimestamp >= Timestamp - AgeMargin of
case TxTimestamp =:= undefined orelse TxTimestamp =< Timestamp andalso TxTimestamp >= Timestamp - AgeMargin of
true ->
case is_record(Tx, vote_tx) of
true ->
......@@ -406,13 +418,17 @@ add_to_fresh_txs(Tx, Data=#data{fresh_txs=FreshTxs, fresh_txs_hash=FreshTxsHash}
tags(TxBin, Data) when is_binary(TxBin) ->
tags(deserialize(TxBin), Data);
tags(Tx, Data) ->
BaseTags =
#{<<"fee">> => ercoin_fee:total(Tx, Data),
MaybeTags =
#{<<"fee">> => integer_to_binary(ercoin_fee:total(Tx, Data)),
<<"from">> => base64:encode(from(Tx)),
<<"message">> => [option || message(Tx), base64:encode(_)],
<<"to">> => [option || to(Tx), base64:encode(_)],
<<"type">> => integer_to_binary(type_no(Tx)),
<<"value">> => integer_to_binary(value(Tx))},
case to(Tx) of
none ->
BaseTags;
To ->
BaseTags#{<<"to">> => base64:encode(To)}
end.
maps:filter(
fun (_, Value) when is_binary(Value) ->
true;
(_, _) ->
false
end,
MaybeTags).
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