Rename “valid_since” record fields to “timestamp”

parent 0c6cedda
Pipeline #36627929 passed with stages
in 20 minutes and 29 seconds
......@@ -124,12 +124,12 @@ Fee: per tx + per byte + per time.
* Transaction type (3, 1 byte).
* Timestamp (4 bytes).
* Address (32 bytes).
* Votes.
* Choices.
* Signature of all the previous fields.
Fee: none
### Votes format
### Choices format
* Price per tx (8 bytes).
* Price per 256 bytes per tx (8 bytes).
......@@ -139,7 +139,7 @@ Fee: none
### Burn transaction format
* Transaction type (4, 1 byte).
* Valid Since (4 bytes) — earliest block height at which tx can be included.
* Timestamp (4 bytes).
* Address (32 bytes).
* Value (8 bytes).
* Message length (1 byte).
......
......@@ -39,7 +39,7 @@
-type message() :: binary().
-record(
transfer_tx,
{valid_since :: block_height(),
{timestamp :: ercoin_timestamp:timestamp(),
from :: pk(),
to :: pk(),
value :: tx_value(),
......@@ -48,7 +48,7 @@
-type transfer_tx() :: #transfer_tx{}.
-record(
burn_tx,
{valid_since :: block_height(),
{timestamp :: ercoin_timestamp:timestamp(),
address :: pk(),
value :: tx_value(),
message :: message(),
......@@ -57,7 +57,7 @@
-record(
lock_tx,
{address :: pk(),
locked_until :: block_height(),
locked_until :: ercoin_timestamp:timestamp(),
validator_pk :: pk(),
signature :: signature()}).
-type lock_tx() :: #lock_tx{}.
......@@ -65,13 +65,13 @@
account_tx,
{from :: pk(),
to :: pk(),
valid_until :: block_height(),
valid_until :: ercoin_timestamp:timestamp(),
signature :: signature()}).
-type account_tx() :: #account_tx{}.
-type choices() :: map().
-record(
vote,
{valid_since :: block_height(),
{timestamp :: ercoin_timestamp:timestamp(),
choices :: choices()}).
-type vote() :: #vote{}.
-record(
......
......@@ -92,7 +92,7 @@ handle_event(
_ ->
[]
end,
NewFreshTxs = lists:dropwhile(fun ({ValidSince, _}) -> ValidSince < Timestamp - EpochLength end, FreshTxs),
NewFreshTxs = lists:dropwhile(fun ({TxTimestamp, _}) -> TxTimestamp < Timestamp - EpochLength end, FreshTxs),
Data1 = Data#data{height=NewHeight, fresh_txs=NewFreshTxs},
{keep_state, Data1, {reply, From, #'abci.ResponseEndBlock'{validator_updates=Diffs}}};
handle_event({call, From}, #'abci.RequestDeliverTx'{tx=MaybeTxBin}, committing, Data)->
......
......@@ -29,19 +29,19 @@
handle_bin/2,
handle_bin/3,
required_balance/2,
valid_since/1,
timestamp/1,
value/1]).
-type age_margin() :: non_neg_integer().
-spec valid_since(tx()) -> ercoin_timestamp:timestamp() | none.
valid_since(#transfer_tx{valid_since=ValidSince}) ->
ValidSince;
valid_since(#vote_tx{vote=#vote{valid_since=ValidSince}}) ->
ValidSince;
valid_since(#burn_tx{valid_since=ValidSince}) ->
ValidSince;
valid_since(_) ->
-spec timestamp(tx()) -> ercoin_timestamp:timestamp() | none.
timestamp(#transfer_tx{timestamp=Timestamp}) ->
Timestamp;
timestamp(#vote_tx{vote=#vote{timestamp=Timestamp}}) ->
Timestamp;
timestamp(#burn_tx{timestamp=Timestamp}) ->
Timestamp;
timestamp(_) ->
none.
-spec from(tx()) -> address().
......@@ -69,14 +69,14 @@ required_balance(Tx, Data) ->
-spec serialize(tx()) -> binary().
serialize(
#transfer_tx{
valid_since=ValidSince,
timestamp=Timestamp,
from=From,
to=To,
value=Value,
message=Message,
signature=Signature}) ->
MessageLength = byte_size(Message),
<<0, ValidSince:4/unit:8, From/binary, To/binary, Value:8/unit:8, MessageLength, Message/binary, Signature/binary>>;
<<0, Timestamp:4/unit:8, From/binary, To/binary, Value:8/unit:8, MessageLength, Message/binary, Signature/binary>>;
serialize(
#account_tx{
valid_until=ValidUntil,
......@@ -95,29 +95,29 @@ serialize(
#vote_tx{
vote=
#vote{
valid_since=ValidSince,
timestamp=Timestamp,
choices=Choices},
address=Address,
signature=Signature}) ->
ChoicesBin = ercoin_vote:choices_serialize(Choices),
<<3, ValidSince:4/unit:8, Address/binary, ChoicesBin/binary, Signature/binary>>;
<<3, Timestamp:4/unit:8, Address/binary, ChoicesBin/binary, Signature/binary>>;
serialize(
#burn_tx{
valid_since=ValidSince,
timestamp=Timestamp,
address=Address,
value=Value,
message=Message,
signature=Signature}) ->
MsgLength = byte_size(Message),
<<4, ValidSince:4/unit:8, Address/binary, Value:8/unit:8, MsgLength, Message/binary, Signature/binary>>.
<<4, Timestamp:4/unit:8, Address/binary, Value:8/unit:8, MsgLength, Message/binary, Signature/binary>>.
-spec deserialize(binary()) -> tx() | none.
deserialize(TxBin) ->
Tx =
case TxBin of
<<0, ValidSince:4/unit:8, From:32/binary, To:32/binary, Value:8/unit:8, MsgLength/integer, Msg:MsgLength/binary, Signature/binary>> ->
<<0, Timestamp:4/unit:8, From:32/binary, To:32/binary, Value:8/unit:8, MsgLength/integer, Msg:MsgLength/binary, Signature/binary>> ->
#transfer_tx{
valid_since=ValidSince,
timestamp=Timestamp,
from=From,
to=To,
value=Value,
......@@ -135,21 +135,21 @@ deserialize(TxBin) ->
address=Address,
validator_pk=ValidatorPK,
signature=Signature};
<<3, ValidSince:4/unit:8, Address:32/binary, FeePerTx:8/unit:8, FeePer256B:8/unit:8, FeePerAccountDay:8/unit:8, Protocol, Signature/binary>> ->
<<3, Timestamp:4/unit:8, Address:32/binary, FeePerTx:8/unit:8, FeePer256B:8/unit:8, FeePerAccountDay:8/unit:8, Protocol, Signature/binary>> ->
#vote_tx{
address=Address,
vote=
#vote{
valid_since=ValidSince,
timestamp=Timestamp,
choices=#{
fee_per_tx => FeePerTx,
fee_per_256_bytes => FeePer256B,
fee_per_account_day => FeePerAccountDay,
protocol => Protocol}},
signature=Signature};
<<4, ValidSince:4/unit:8, Address:32/binary, Value:8/unit:8, MsgLength, Msg:MsgLength/binary, Signature/binary>> ->
<<4, Timestamp:4/unit:8, Address:32/binary, Value:8/unit:8, MsgLength, Msg:MsgLength/binary, Signature/binary>> ->
#burn_tx{
valid_since=ValidSince,
timestamp=Timestamp,
address=Address,
value=Value,
message=Msg,
......@@ -182,8 +182,8 @@ error_code(Tx, Data) ->
-spec error_code(tx(), data(), age_margin()) -> error_code().
error_code(Tx, Data=#data{timestamp=Timestamp}, AgeMargin) ->
ValidSince = ercoin_tx:valid_since(Tx),
case ValidSince =:= none orelse ValidSince =< Timestamp andalso ValidSince >= Timestamp - AgeMargin of
TxTimestamp = ercoin_tx:timestamp(Tx),
case TxTimestamp =:= none orelse TxTimestamp =< Timestamp andalso TxTimestamp >= Timestamp - AgeMargin of
true ->
case is_record(Tx, vote_tx) of
true ->
......@@ -297,7 +297,7 @@ error_code_1(Tx=#burn_tx{}, Data, #account{locked_until=LockedUntil}) ->
-spec in_fresh_txs(tx(), data()) -> boolean().
in_fresh_txs(Tx, Data) ->
?SETS:is_element({ercoin_tx:valid_since(Tx), ?HASH(ercoin_tx:serialize(Tx))}, Data#data.fresh_txs).
?SETS:is_element({ercoin_tx:timestamp(Tx), ?HASH(ercoin_tx:serialize(Tx))}, Data#data.fresh_txs).
-spec unpack_binary(binary(), data()) -> {error_code(), tx() | none}.
unpack_binary(MaybeTxBin, Data) ->
......@@ -385,6 +385,6 @@ apply_1(#burn_tx{address=Address, value=Value}, Data) ->
%% @doc Add a transaction to fresh_txs and to fresh_txs_hash.
add_to_fresh_txs(Tx, Data=#data{fresh_txs=FreshTxs, fresh_txs_hash=FreshTxsHash}) ->
TxBin = ercoin_tx:serialize(Tx),
NewFreshTxs = ?SETS:add_element({ercoin_tx:valid_since(Tx), ?HASH(TxBin)}, FreshTxs),
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}.
......@@ -29,15 +29,15 @@ choices_serialize(Choices=#{protocol := Protocol}) ->
<<(ercoin_fee:serialize(Choices))/binary, Protocol>>.
-spec serialize(vote() | none) -> binary().
serialize(#vote{valid_since=ValidSince, choices=Choices}) ->
<<ValidSince:4/unit:8, (choices_serialize(Choices))/binary>>;
serialize(#vote{timestamp=Timestamp, choices=Choices}) ->
<<Timestamp:4/unit:8, (choices_serialize(Choices))/binary>>;
serialize(none) ->
<<>>.
-spec deserialize(binary()) -> vote() | none.
deserialize(<<ValidSince:4/unit:8, FeesBin:24/binary, Protocol>>) ->
deserialize(<<Timestamp:4/unit:8, FeesBin:24/binary, Protocol>>) ->
#vote{
valid_since=ValidSince,
timestamp=Timestamp,
choices=maps:put(protocol, Protocol, ercoin_fee:deserialize(FeesBin))};
deserialize(<<>>) ->
none.
......
......@@ -114,7 +114,7 @@ prop_end_block_truncates_fresh_txs() ->
begin
FreshTxs = Data#data.fresh_txs,
#data{fresh_txs=NewFreshTxs, timestamp=Timestamp} = end_block(Data),
NewFreshTxs =:= ?SETS:filter(fun ({ValidSince, _Tx}) -> ValidSince >= Timestamp - Data#data.epoch_length end, FreshTxs)
NewFreshTxs =:= ?SETS:filter(fun ({TxTimestamp, _Tx}) -> TxTimestamp >= Timestamp - Data#data.epoch_length end, FreshTxs)
end).
prop_deliver_tx_handles_binary_in_regard_to_data() ->
......
......@@ -25,7 +25,6 @@
address/0,
address_secret/0,
begin_block/1,
block_height/0,
choices/0,
data/0,
data_sks/0,
......@@ -33,8 +32,8 @@
data_sks_and_account/1,
data_with_account/0,
data_with_begin_block/0,
valid_since/1,
valid_since/2,
tx_timestamp/1,
tx_timestamp/2,
vote/1,
vote/2]).
......@@ -286,10 +285,10 @@ fee() ->
balance() ->
non_neg_integer().
valid_since(#data{timestamp=Timestamp, epoch_length=EpochLength}) ->
valid_since(Timestamp, EpochLength).
tx_timestamp(#data{timestamp=Timestamp, epoch_length=EpochLength}) ->
tx_timestamp(Timestamp, EpochLength).
valid_since(Timestamp, EpochLength) ->
tx_timestamp(Timestamp, EpochLength) ->
choose(max(Timestamp - EpochLength + 1, 0), Timestamp).
choices() ->
......@@ -306,19 +305,19 @@ vote(#data{timestamp=Timestamp, epoch_length=EpochLength}) ->
vote(Timestamp, EpochLength) ->
?LET(
{ValidSince, Choices},
{valid_since(Timestamp, EpochLength), choices()},
{TxTimestamp, Choices},
{tx_timestamp(Timestamp, EpochLength), choices()},
#vote{
valid_since=ValidSince,
timestamp=TxTimestamp,
choices=Choices}).
vote_maybe_old(Timestamp) ->
%% No epoch length is given, so we create a vote that possibly will not pass current validation when embedded in a tx.
?LET(
{ValidSince, Choices},
{TxTimestamp, Choices},
{choose(0, Timestamp), choices()},
#vote{
valid_since=ValidSince,
timestamp=TxTimestamp,
choices=Choices}).
-spec accounts_tree_from_list(list(account())) -> gb_merkle_trees:tree().
......
......@@ -34,15 +34,15 @@ delete_account(Address, Data=#data{accounts=Accounts}) ->
NewAccounts = gb_merkle_trees:delete(Address, Accounts),
Data#data{accounts=NewAccounts}.
set_valid_since(ValidSince, Tx, SKs) ->
sign_tx(set_valid_since_1(ValidSince, Tx), SKs).
set_timestamp(Timestamp, Tx, SKs) ->
sign_tx(set_timestamp_1(Timestamp, Tx), SKs).
set_valid_since_1(ValidSince, Tx=#transfer_tx{}) ->
Tx#transfer_tx{valid_since=ValidSince};
set_valid_since_1(ValidSince, Tx=#vote_tx{}) ->
Tx#vote_tx{vote=Tx#vote_tx.vote#vote{valid_since=ValidSince}};
set_valid_since_1(ValidSince, Tx=#burn_tx{}) ->
Tx#burn_tx{valid_since=ValidSince}.
set_timestamp_1(Timestamp, Tx=#transfer_tx{}) ->
Tx#transfer_tx{timestamp=Timestamp};
set_timestamp_1(Timestamp, Tx=#vote_tx{}) ->
Tx#vote_tx{vote=Tx#vote_tx.vote#vote{timestamp=Timestamp}};
set_timestamp_1(Timestamp, Tx=#burn_tx{}) ->
Tx#burn_tx{timestamp=Timestamp}.
-spec sign_tx(tx(), secret() | map()) -> tx().
sign_tx(Tx, Secrets) when is_map(Secrets) ->
......@@ -56,13 +56,13 @@ sign_tx(Tx, Secret) ->
-spec to_sign(tx()) -> binary().
to_sign(
#transfer_tx{
valid_since=ValidSince,
timestamp=Timestamp,
from=From,
to=To,
value=Value,
message=Message}) ->
MessageLength = byte_size(Message),
<<0, ValidSince:4/unit:8, From/binary, To/binary, Value:8/unit:8, MessageLength, Message/binary>>;
<<0, Timestamp:4/unit:8, From/binary, To/binary, Value:8/unit:8, MessageLength, Message/binary>>;
to_sign(#account_tx{valid_until=ValidUntil, from=From, to=To}) ->
<<1, ValidUntil:4/unit:8, From/binary, To/binary>>;
to_sign(#lock_tx{address=Address, locked_until=LockedUntil, validator_pk=ValidatorPK}) ->
......@@ -72,18 +72,18 @@ to_sign(
address=Address,
vote=
#vote{
valid_since=ValidSince,
timestamp=Timestamp,
choices=Choices}}) ->
ChoicesBin = ercoin_vote:choices_serialize(Choices),
<<3, ValidSince:4/unit:8, Address/binary, ChoicesBin/binary>>;
<<3, Timestamp:4/unit:8, Address/binary, ChoicesBin/binary>>;
to_sign(
#burn_tx{
valid_since=ValidSince,
timestamp=Timestamp,
address=Address,
value=Value,
message=Message}) ->
MsgLength = byte_size(Message),
<<4, ValidSince:4/unit:8, Address/binary, Value:8/unit:8, MsgLength, Message/binary>>.
<<4, Timestamp:4/unit:8, Address/binary, Value:8/unit:8, MsgLength, Message/binary>>.
-spec sign_detached(binary(), secret()) -> binary().
sign_detached(Msg, {SK, Proof}) ->
......@@ -170,11 +170,11 @@ make_sufficient_balance(Data, Tx) ->
transfer_tx({Data, SKs}, From, To) ->
?LET(
{Value, ValidSince, Message},
{choose(0, From#account.balance), ercoin_gen:valid_since(Data), message()},
{Value, Timestamp, Message},
{choose(0, From#account.balance), ercoin_gen:tx_timestamp(Data), message()},
sign_tx(
#transfer_tx{
valid_since=ValidSince,
timestamp=Timestamp,
from=From#account.address,
to=To#account.address,
message=Message,
......@@ -185,11 +185,11 @@ transfer_tx({Data, SKs}, From, To) ->
burn_tx({Data, SKs}, #account{address=Address, balance=Balance}) ->
?LET(
{Value, ValidSince, Message},
{choose(0, Balance), ercoin_gen:valid_since(Data), message()},
{Value, Timestamp, Message},
{choose(0, Balance), ercoin_gen:tx_timestamp(Data), message()},
sign_tx(
#burn_tx{
valid_since=ValidSince,
timestamp=Timestamp,
address=Address,
message=Message,
value=Value,
......@@ -442,7 +442,7 @@ data_with_invalid_tx_bin() ->
?LET(
ValidIn,
pos_integer(),
{Data, ercoin_tx:serialize(set_valid_since(Data#data.timestamp + ValidIn, Tx, SKs))})),
{Data, ercoin_tx:serialize(set_timestamp(Data#data.timestamp + ValidIn, Tx, SKs))})),
%% Tx outdated.
?LET(
{{Data, SKs}, Tx},
......@@ -456,7 +456,7 @@ data_with_invalid_tx_bin() ->
?LET(
ExpiredBy,
pos_integer(),
{Data, ercoin_tx:serialize(set_valid_since(Data#data.timestamp - Data#data.epoch_length - ExpiredBy, Tx, SKs))})),
{Data, ercoin_tx:serialize(set_timestamp(Data#data.timestamp - Data#data.epoch_length - ExpiredBy, Tx, SKs))})),
%% Incorrectly specified message length.
?LET(
{{{Data, SKs}, ValidTx}, InvalidLength},
......@@ -485,7 +485,7 @@ data_with_invalid_tx_bin() ->
begin
TxBin = ercoin_tx:serialize(Tx),
#data{fresh_txs=FreshTxs} = Data,
NewFreshTxs = ?SETS:add_element({ercoin_tx:valid_since(Tx), ?HASH(TxBin)}, FreshTxs),
NewFreshTxs = ?SETS:add_element({ercoin_tx:timestamp(Tx), ?HASH(TxBin)}, FreshTxs),
{Data#data{fresh_txs=NewFreshTxs}, TxBin}
end)
%% TODO: Maybe ensure that no more than one vote tx per validator can be included in one block.
......
......@@ -30,7 +30,7 @@
-define(
SAMPLE_TRANSFER_TX,
#transfer_tx{
valid_since=1234,
timestamp=1234,
%% Secret key: <<241,233,155,25,231,68,253,237,185,176,117,196,7,138,248,241,14,148,35,27,241,3,132,194,142,70,50,81,135,104,72,21,236,168,42,39,132,221,157,252,126,127,245,49,190,10,84,207,79,253,85,117,4,82,163,98,146,75,91,17,19,215,36,37>>.
from= <<236,168,42,39,132,221,157,252,126,127,245,49,190,10,84,207,79,253,85,117,4,82,163,98,146,75,91,17,19,215,36,37>>,
to= <<0:32/unit:8>>,
......@@ -167,7 +167,7 @@ prop_transfer_tx_and_vote_tx_are_added_to_fresh_txs_and_fresh_txs_hash() ->
begin
TxBin = ercoin_tx:serialize(Tx),
#data{fresh_txs=NewFreshTxs, fresh_txs_hash=NewFreshTxsHash} = ercoin_tx:apply(Tx, Data),
?SETS:is_element({ercoin_tx:valid_since(Tx), ?HASH(TxBin)}, NewFreshTxs) andalso
?SETS:is_element({ercoin_tx:timestamp(Tx), ?HASH(TxBin)}, NewFreshTxs) andalso
NewFreshTxsHash =:= ?HASH(<<FreshTxsHash/binary, TxBin/binary>>)
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