Skip to content

Commit

Permalink
Merge pull request #2718 from aeternity/fix_error_messages
Browse files Browse the repository at this point in the history
Replace confusing account_nonce_too_high/low errors
  • Loading branch information
hanssv committed Sep 3, 2019
2 parents 6880ce5 + 8732bfb commit 47d74f1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
24 changes: 12 additions & 12 deletions apps/aechannel/test/aesc_txs_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,9 @@ create_wrong_nonce(_Cfg) ->
Env = aetx_env:set_signed_tx(Env0, {value, SignedTx}),
{error, Err} = aetx:process(Tx, Trees, Env)
end,
Test(Nonce - 1, account_nonce_too_high),
Test(Nonce, account_nonce_too_high),
Test(Nonce + 2, account_nonce_too_low),
Test(Nonce - 1, tx_nonce_already_used_for_account),
Test(Nonce, tx_nonce_already_used_for_account),
Test(Nonce + 2, tx_nonce_too_high_for_account),
ok.

create_exsisting(Cfg) ->
Expand Down Expand Up @@ -883,9 +883,9 @@ close_mutual_wrong_nonce(Cfg) ->
prepare_balances_for_mutual_close(),
negative(fun close_mutual_/2, {error, Error})])
end,
Test(InitiatorNonce - 1, account_nonce_too_high),
Test(InitiatorNonce, account_nonce_too_high),
Test(InitiatorNonce + 2, account_nonce_too_low),
Test(InitiatorNonce - 1, tx_nonce_already_used_for_account),
Test(InitiatorNonce, tx_nonce_already_used_for_account),
Test(InitiatorNonce + 2, tx_nonce_too_high_for_account),
ok.


Expand Down Expand Up @@ -1609,9 +1609,9 @@ settle_wrong_nonce(Cfg) ->
% settle tx amounts must be equal to the last on-chain tx
ActualTest =
fun(Closer, Setler) ->
Test(Closer, Setler, Nonce - 1, account_nonce_too_high),
Test(Closer, Setler, Nonce , account_nonce_too_high),
Test(Closer, Setler, Nonce + 2, account_nonce_too_low)
Test(Closer, Setler, Nonce - 1, tx_nonce_already_used_for_account),
Test(Closer, Setler, Nonce , tx_nonce_already_used_for_account),
Test(Closer, Setler, Nonce + 2, tx_nonce_too_high_for_account)
end,
[ActualTest(Closer, Setler) || Closer <- ?ROLES,
Setler <- RolesWithKeys],
Expand Down Expand Up @@ -4954,9 +4954,9 @@ test_both_wrong_nonce(Cfg, Fun, InitProps) ->
end,
lists:foreach(
fun(Poster) ->
Test(Poster, AccountNonce - 1, account_nonce_too_high),
Test(Poster, AccountNonce, account_nonce_too_high),
Test(Poster, AccountNonce + 2, account_nonce_too_low)
Test(Poster, AccountNonce - 1, tx_nonce_already_used_for_account),
Test(Poster, AccountNonce, tx_nonce_already_used_for_account),
Test(Poster, AccountNonce + 2, tx_nonce_too_high_for_account)
end,
?ROLES),
ok.
Expand Down
2 changes: 1 addition & 1 deletion apps/aecontract/test/aecontract_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ create_contract_negative(_Cfg) ->
%% Test too high account nonce
RTx3 = create_tx(PubKey, #{nonce => 0}, S1),
{error, _, S1} = sign_and_apply_transaction(RTx3, PrivKey, S1),
{error, account_nonce_too_high} = aetx:process(RTx3, Trees, Env),
{error, tx_nonce_already_used_for_account} = aetx:process(RTx3, Trees, Env),

ok.

Expand Down
2 changes: 1 addition & 1 deletion apps/aecore/test/aec_spend_tx_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ check_test_() ->
SenderAccount = new_account(#{pubkey => ?SENDER_PUBKEY, balance => 1000000, nonce => AccountNonce}),
StateTree = aec_test_utils:create_state_tree_with_account(SenderAccount),
Env = aetx_env:tx_env(20),
?assertEqual({error, account_nonce_too_high},
?assertEqual({error, tx_nonce_already_used_for_account},
aetx:process(SpendTx, StateTree, Env))
end},
{"TX TTL is too small",
Expand Down
10 changes: 5 additions & 5 deletions apps/aens/test/aens_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ preclaim_negative(Cfg) ->
%% Test too high account nonce
TxSpec3 = aens_test_utils:preclaim_tx_spec(PubKey, CHash, #{nonce => 0}, S1),
{ok, Tx3} = aens_preclaim_tx:new(TxSpec3),
{error, account_nonce_too_high} = aetx:process(Tx3, Trees, Env),
{error, tx_nonce_already_used_for_account} = aetx:process(Tx3, Trees, Env),

%% Test commitment already present
{PubKey2, Name, NameSalt, S3} = preclaim(Cfg),
Expand Down Expand Up @@ -366,7 +366,7 @@ claim_negative(Cfg) ->
%% Test too high account nonce
TxSpec3 = aens_test_utils:claim_tx_spec(PubKey, Name, NameSalt, namefee(Name, Cfg), #{nonce => 0}, S1),
{ok, Tx3} = aens_claim_tx:new(TxSpec3),
{error, account_nonce_too_high} = aetx:process(Tx3, Trees, Env),
{error, tx_nonce_already_used_for_account} = aetx:process(Tx3, Trees, Env),

%% Test commitment not found
TxSpec4 = aens_test_utils:claim_tx_spec(PubKey, Name, NameSalt + 1, namefee(Name, Cfg), S1),
Expand Down Expand Up @@ -549,7 +549,7 @@ update_negative(Cfg) ->
%% Test too high account nonce
TxSpec4 = aens_test_utils:update_tx_spec(PubKey, NHash, #{nonce => 0}, S1),
{ok, Tx4} = aens_update_tx:new(TxSpec4),
{error, account_nonce_too_high} = aetx:process(Tx4, Trees, Env),
{error, tx_nonce_already_used_for_account} = aetx:process(Tx4, Trees, Env),

%% Test name not present
{ok, NHash2} = aens:get_name_hash(aens_test_utils:fullname(<<"othername">>)),
Expand Down Expand Up @@ -626,7 +626,7 @@ transfer_negative(Cfg) ->
%% Test too high account nonce
TxSpec3 = aens_test_utils:transfer_tx_spec(PubKey, NHash, PubKey, #{nonce => 0}, S1),
{ok, Tx3} = aens_transfer_tx:new(TxSpec3),
{error, account_nonce_too_high} = aetx:process(Tx3, Trees, Env),
{error, tx_nonce_already_used_for_account} = aetx:process(Tx3, Trees, Env),

%% Test name not present
{ok, NHash2} = aens:get_name_hash(aens_test_utils:fullname(<<"othername">>)),
Expand Down Expand Up @@ -700,7 +700,7 @@ revoke_negative(Cfg) ->
%% Test too high account nonce
TxSpec3 = aens_test_utils:revoke_tx_spec(PubKey, NHash, #{nonce => 0}, S1),
{ok, Tx3} = aens_revoke_tx:new(TxSpec3),
{error, account_nonce_too_high} = aetx:process(Tx3, Trees, Env),
{error, tx_nonce_already_used_for_account} = aetx:process(Tx3, Trees, Env),

%% Test name not present
{ok, NHash2} = aens:get_name_hash(aens_test_utils:fullname(<<"othername">>)),
Expand Down
8 changes: 4 additions & 4 deletions apps/aeoracle/test/aeoracle_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ register_oracle_negative(_Cfg) ->

%% Test too high account nonce
RTx3 = aeo_test_utils:register_tx(PubKey, #{nonce => 0}, S1),
{error, account_nonce_too_high} = aetx:process(RTx3, Trees, Env),
{error, tx_nonce_already_used_for_account} = aetx:process(RTx3, Trees, Env),

%% Test too low fee
RTx4 = aeo_test_utils:register_tx(PubKey, #{fee => 0}, S1),
Expand Down Expand Up @@ -293,7 +293,7 @@ extend_oracle_negative(Cfg) ->

%% Test too high account nonce
RTx4 = aeo_test_utils:extend_tx(OracleKey, #{nonce => 0}, S2),
{error, account_nonce_too_high} = aetx:process(RTx4, Trees2, Env2),
{error, tx_nonce_already_used_for_account} = aetx:process(RTx4, Trees2, Env2),

%% Test too low fee
RTx5 = aeo_test_utils:extend_tx(OracleKey, #{fee => 0}, S2),
Expand Down Expand Up @@ -390,7 +390,7 @@ query_oracle_negative(Cfg) ->

%% Test too high nonce in account
Q3 = aeo_test_utils:query_tx(SenderKey, OracleId, #{nonce => 0}, S2),
{error, account_nonce_too_high} = aetx:process(Q3, Trees, Env),
{error, tx_nonce_already_used_for_account} = aetx:process(Q3, Trees, Env),

%% Test too low fee
Q4 = aeo_test_utils:query_tx(SenderKey, OracleId, #{fee => 0}, S2),
Expand Down Expand Up @@ -528,7 +528,7 @@ query_response_negative(Cfg) ->

%% Test too high nonce for account
RTx2 = aeo_test_utils:response_tx(OracleKey, ID, <<"42">>, #{nonce => 0}, S1),
{error, account_nonce_too_high} = aetx:process(RTx2, Trees, Env),
{error, tx_nonce_already_used_for_account} = aetx:process(RTx2, Trees, Env),

%% Test fee too low
RTx3 = aeo_test_utils:response_tx(OracleKey, ID, <<"42">>, #{fee => 0}, S1),
Expand Down
4 changes: 2 additions & 2 deletions apps/aeprimop/src/aeprimop.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1496,8 +1496,8 @@ assert_ga_env(Pubkey, Nonce, #state{tx_env = Env}) ->
assert_account_nonce(Account, Nonce) ->
case aec_accounts:nonce(Account) of
N when N + 1 =:= Nonce -> ok;
N when N >= Nonce -> runtime_error(account_nonce_too_high);
N when N < Nonce -> runtime_error(account_nonce_too_low)
N when N >= Nonce -> runtime_error(tx_nonce_already_used_for_account);
N when N < Nonce -> runtime_error(tx_nonce_too_high_for_account)
end.

assert_account_balance(Account, Balance) ->
Expand Down
10 changes: 5 additions & 5 deletions apps/aetx/src/aetx_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ check_balance(Account, Amount) ->
end.

-spec check_nonce(aec_accounts:account(), non_neg_integer(), aetx_env:env()) ->
ok | {error, account_nonce_too_high | account_nonce_too_low}.
ok | {error, atom()}.
check_nonce(Account, Nonce, Env) ->
case aec_accounts:type(Account) of
basic ->
AccountNonce = aec_accounts:nonce(Account),
ANonce = aec_accounts:nonce(Account),
if
Nonce =:= (AccountNonce + 1) -> ok;
Nonce =< AccountNonce -> {error, account_nonce_too_high};
Nonce > AccountNonce -> {error, account_nonce_too_low}
Nonce == (ANonce + 1) -> ok;
Nonce =< ANonce -> {error, tx_nonce_already_used_for_account};
Nonce > ANonce -> {error, tx_nonce_too_high_for_account}
end;
generalized ->
GANonce = aetx_env:ga_nonce(Env, aec_accounts:pubkey(Account)),
Expand Down

0 comments on commit 47d74f1

Please sign in to comment.