Add missing validations of transaction’s from address

parent 13369699
Pipeline #22147507 passed with stages
in 15 minutes and 49 seconds
......@@ -343,40 +343,40 @@ tx_error_code(Tx, Data=#data{height=Height, epoch_length=EpochLength}) ->
ValidSince = get_valid_since(Tx),
case ValidSince =:= none orelse ValidSince =< Height + 1 andalso ValidSince > Height - EpochLength + 1 of
true ->
tx_error_code_1(Tx, Data);
case get_account(from(Tx), Data) of
none ->
?NOT_FOUND;
From ->
tx_error_code_1(Tx, Data, From)
end;
_ ->
?INVALID_TIMESTAMP
end.
-spec tx_error_code_1(tx(), data()) -> error_code().
tx_error_code_1(Tx=#transfer_tx{from=From, to=To, value=Value}, Data) ->
-spec tx_error_code_1(tx(), data(), account()) -> error_code().
tx_error_code_1(Tx=#transfer_tx{to=To, value=Value}, Data, #account{locked_until=LockedUntil, balance=Balance}) ->
case ?SETS:is_element({get_valid_since(Tx), ?HASH(tx_serialize(Tx))}, Data#data.fresh_txs) of
false ->
case get_account(To, Data) of
none ->
?NOT_FOUND;
#account{} ->
case get_account(From, Data) of
case LockedUntil of
none ->
?NOT_FOUND;
#account{balance=Balance, locked_until=LockedUntil} ->
case LockedUntil of
none ->
case Balance < fee(Tx, Data) + Value of
false ->
?OK;
true ->
?INSUFFICIENT_FUNDS
end;
_ ->
?FORBIDDEN
end
case Balance < fee(Tx, Data) + Value of
false ->
?OK;
true ->
?INSUFFICIENT_FUNDS
end;
_ ->
?FORBIDDEN
end
end;
true ->
?ALREADY_EXECUTED
end;
tx_error_code_1(#account_tx{valid_until=ValidUntil, to=To, from=From}, Data) ->
tx_error_code_1(#account_tx{valid_until=ValidUntil, to=To, from=From}, Data, _) ->
case From =:= To orelse not is_locked(get_account(From, Data)) of
true ->
case get_account(To, Data) of
......@@ -393,7 +393,7 @@ tx_error_code_1(#account_tx{valid_until=ValidUntil, to=To, from=From}, Data) ->
false ->
?FORBIDDEN
end;
tx_error_code_1(#lock_tx{address=Address, locked_until=LockedUntil}, Data) ->
tx_error_code_1(#lock_tx{address=Address, locked_until=LockedUntil}, Data, _) ->
#account{locked_until=CurrentLockedUntil, valid_until=ValidUntil} = get_account(Address, Data),
case (CurrentLockedUntil =:= none orelse LockedUntil > CurrentLockedUntil) andalso LockedUntil =< ValidUntil of
true ->
......@@ -401,10 +401,27 @@ tx_error_code_1(#lock_tx{address=Address, locked_until=LockedUntil}, Data) ->
false ->
?FORBIDDEN
end;
tx_error_code_1(Tx=#vote_tx{}, #data{fresh_txs=FreshTxs}) ->
tx_error_code_1(Tx=#vote_tx{address=Address}, #data{fresh_txs=FreshTxs, validators=Validators, future_validators=FutureValidators}, _) ->
case ?SETS:is_element({get_valid_since(Tx), ?HASH(tx_serialize(Tx))}, FreshTxs) of
false ->
?OK;
case gb_merkle_trees:lookup(Address, Validators) of
none ->
case FutureValidators of
undefined ->
?NOT_FOUND;
{promise, _, _} ->
?NOT_FOUND;
_ ->
case gb_merkle_trees:lookup(Address, FutureValidators) of
none ->
?NOT_FOUND;
_ ->
?OK
end
end;
_ ->
?OK
end;
_ ->
?ALREADY_EXECUTED
end.
......@@ -1619,8 +1636,37 @@ data_with_invalid_tx_bin() ->
%% From not existing.
?LET(
{Data, ValidTx},
data_with_transfer_tx(),
{delete_account(ValidTx#transfer_tx.from, Data), tx_serialize(ValidTx)}),
data_with_tx(),
{delete_account(from(ValidTx), Data), tx_serialize(ValidTx)}),
%% Vote tx: From not a validator and not a future validator.
?LET(
{Data=#data{validators=Validators, future_validators=FutureValidators}, ValidTx=#vote_tx{address=Address}},
data_with_vote_tx(),
begin
NewValidators =
case gb_merkle_trees:lookup(Address, Validators) of
none ->
Validators;
_ ->
gb_merkle_trees:delete(Address, Validators)
end,
NewFutureValidators =
case FutureValidators of
undefined ->
FutureValidators;
{promise, _, _} ->
FutureValidators;
_ ->
case gb_merkle_trees:lookup(Address, FutureValidators) of
none ->
FutureValidators;
_ ->
gb_merkle_trees:delete(Address, FutureValidators)
end
end,
{Data#data{validators=NewValidators, future_validators=NewFutureValidators},
tx_serialize(ValidTx)}
end),
%% Transfer tx: From locked.
?LET(
{Data, SKs},
......
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