Skip to content

Commit

Permalink
Merge pull request #2704 from aeternity/PT-167889960-disallow-claimin…
Browse files Browse the repository at this point in the history
…g-test-registrar-ta

Pt 167889960 disallow claiming test registrar
  • Loading branch information
ThomasArts committed Sep 2, 2019
2 parents d05785c + 0f45ee7 commit 3d5959f
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 56 deletions.
8 changes: 4 additions & 4 deletions apps/aechannel/test/aesc_txs_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,7 @@ fp_use_onchain_name_resolution(Cfg) ->
FPRound = 20,
LockPeriod = 10,
FPHeight0 = 20,
Name = <<"lorem.test">>,
Name = aens_test_utils:fullname(<<"lorem">>),
ForceCallCheckName =
fun(Forcer, K, Found) when is_binary(K) andalso is_boolean(Found) ->
fun(Props) ->
Expand Down Expand Up @@ -3518,7 +3518,8 @@ fp_insufficent_gas_price(Cfg) ->
ok.

fp_register_name(Cfg) ->
Name = <<"bla.test">>,
%% protocol version in config is latest version w.r.t. 'make' target
Name = aens_test_utils:fullname(<<"bla">>),
Salt = 42,
{ok, NameAscii} = aens_utils:to_ascii(Name),
CHash = address_encode(hash, aens_hash:commitment_hash(NameAscii, Salt)),
Expand Down Expand Up @@ -5492,7 +5493,7 @@ fp_sophia_versions(Cfg) ->
OK %% Lima
},
%% AEVM 2
{?VM_AEVM_SOPHIA_2, SophiaVsn1,
{?VM_AEVM_SOPHIA_2, SophiaVsn1,
ErrUnknown, %% Roma
OK, %% Minerva
OK, %% Fortuna
Expand Down Expand Up @@ -5618,4 +5619,3 @@ aevm_type(Type) -> Type.
encode_sig(Sig) ->
<<"0x", Hex/binary>> = aeu_hex:hexstring_encode(Sig),
binary_to_list(<<"#", Hex/binary>>).

11 changes: 5 additions & 6 deletions apps/aecontract/test/aecontract_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3538,7 +3538,7 @@ sophia_signatures_aens(Cfg) ->
Acc = ?call(new_account, 20000000 * aec_test_utils:min_gas_price()),
NameAcc = ?call(new_account, 20000000 * aec_test_utils:min_gas_price()),
Ct = ?call(create_contract, NameAcc, aens, {}, #{ amount => 100000 }),
Name1 = <<"bla.test">>,
Name1 = aens_test_utils:fullname(<<"bla">>),
Salt1 = rand:uniform(10000),
{ok, NameAscii} = aens_utils:to_ascii(Name1),
CHash = ?hsh(aens_hash:commitment_hash(NameAscii, Salt1)),
Expand All @@ -3549,9 +3549,8 @@ sophia_signatures_aens(Cfg) ->
VMVersion when ?IS_FATE_SOPHIA(VMVersion) -> Name1
end,
NameAccSig = sign(<<NameAcc/binary, Ct/binary>>, NameAcc),
{ok, NameHash} = aens:get_name_hash(<<"bla.test">>),
NameSig = sign(<<NameAcc/binary, NameHash/binary, Ct/binary>>, NameAcc),
AccSig = sign(<<Acc/binary, NameHash/binary, Ct/binary>>, Acc),
NameSig = sign(<<NameAcc/binary, NHash/binary, Ct/binary>>, NameAcc),
AccSig = sign(<<Acc/binary, NHash/binary, Ct/binary>>, Acc),
APubkey = 1,
OPubkey = 2,
Pointers = [aens_pointer:new(<<"account_pubkey">>, aeser_id:create(account, <<APubkey:256>>)),
Expand Down Expand Up @@ -5123,7 +5122,7 @@ sophia_aens_resolve(_Cfg) ->
state(aect_test_utils:new_state()),
Acc = ?call(new_account, 20000000 * aec_test_utils:min_gas_price()),
Ct = ?call(create_contract, Acc, aens, {}, #{ amount => 100000 }),
Name = <<"foo.test">>,
Name = aens_test_utils:fullname(<<"foo">>),
APubkey = 1,
OPubkey = 2,
CPubkey = 3,
Expand Down Expand Up @@ -5185,7 +5184,7 @@ sophia_aens_transactions(Cfg) ->
aens_pointer:new(<<"oracle_pubkey">>, aeser_id:create(oracle, <<OPubkey:256>>)),
aens_pointer:new(<<"contract_pubkey">>, aeser_id:create(contract, <<CPubkey:256>>))
],
Name1 = <<"bla.test">>,
Name1 = aens_test_utils:fullname(<<"bla">>),
Salt1 = rand:uniform(10000),
{ok, NameAscii} = aens_utils:to_ascii(Name1),
CHash = aens_hash:commitment_hash(NameAscii, Salt1),
Expand Down
14 changes: 11 additions & 3 deletions apps/aecore/src/aec_governance.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
name_claim_max_expiration/0,
name_protection_period/0,
name_claim_preclaim_delta/0,
name_registrars/0,
name_registrars/1,
possible_name_registrars/0,
micro_block_cycle/0,
accepted_future_block_time_shift/0,
fraud_report_reward/1,
Expand Down Expand Up @@ -243,10 +244,17 @@ name_protection_period() ->
name_claim_preclaim_delta() ->
1.

-spec name_registrars() -> list(binary()).
name_registrars() ->
-spec name_registrars(aec_hard_forks:protocol_vsn()) -> list(binary()).
name_registrars(Protocol) when Protocol >= ?LIMA_PROTOCOL_VSN ->
[<<"aet">>];
name_registrars(_Protocol) ->
[<<"test">>].

%% union of all name_registrars above disregarding the height
possible_name_registrars() ->
[<<"aet">>, <<"test">>].


fraud_report_reward(Height) ->
Coinbase = block_mine_reward(Height),
%% Assert that the coinbase is always even dividable.
Expand Down
16 changes: 10 additions & 6 deletions apps/aehttp/test/aehttp_integration_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2234,10 +2234,11 @@ oracle_transactions(_Config) ->
%% GET revoke_tx unsigned transaction
nameservice_transactions(_Config) ->
{ok, 200, _} = get_balance_at_top(),
{ok, 200, #{<<"top_block_height">> := Height}} = get_status_sut(),
MinerAddress = get_pubkey(),
{ok, MinerPubkey} = aeser_api_encoder:safe_decode(account_pubkey, MinerAddress),
nameservice_transaction_preclaim(MinerAddress, MinerPubkey),
nameservice_transaction_claim(MinerAddress, MinerPubkey),
nameservice_transaction_claim(MinerAddress, MinerPubkey, Height),
nameservice_transaction_update(MinerAddress, MinerPubkey),
nameservice_transaction_transfer(MinerAddress, MinerPubkey),
nameservice_transaction_revoke(MinerAddress, MinerPubkey),
Expand Down Expand Up @@ -2294,8 +2295,8 @@ test_missing_address(Key, Encoded, APIFun) ->
APIFun(maps:put(Key, RandAddress, Encoded)),
ok.

nameservice_transaction_claim(MinerAddress, MinerPubkey) ->
Name = <<"name.test">>,
nameservice_transaction_claim(MinerAddress, MinerPubkey, Height) ->
Name = aens_test_utils:fullname(<<"name">>, Height),
Salt = 1234,

{ok, 200, #{<<"commitment_id">> := EncodedCHash}} = get_commitment_id(Name, Salt),
Expand Down Expand Up @@ -3018,9 +3019,12 @@ naming_system_manage_name(_Config) ->
{PubKey, PrivKey} = initialize_account(1000000000 *
aec_test_utils:min_gas_price()),
PubKeyEnc = aeser_api_encoder:encode(account_pubkey, PubKey),

{ok, 200, #{<<"top_block_height">> := Height}} = get_status_sut(),

%% TODO: find out how to craete HTTP path with unicode chars
%%Name = <<"詹姆斯詹姆斯.test"/utf8>>,
Name = <<"without-unicode.test">>,
Name = aens_test_utils:fullname(<<"without-unicode">>, Height),
NameSalt = 12345,
NameTTL = 20000,
Pointers = [#{<<"key">> => <<"account_pubkey">>, <<"id">> => PubKeyEnc}],
Expand Down Expand Up @@ -3151,7 +3155,8 @@ naming_system_manage_name(_Config) ->
ok.

naming_system_broken_txs(_Config) ->
Name = <<"fooo.test">>,
%% We reset the chain and hence on latest_protocol_height
Name = aens_test_utils:fullname(<<"fooo">>),
NameSalt = 12345,
{ok, NHash} = aens:get_name_hash(Name),
CHash = aens_hash:commitment_hash(Name, NameSalt),
Expand Down Expand Up @@ -3899,4 +3904,3 @@ http_datetime_to_unixtime(S) ->
BaseSecs = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}),
ExpiresDt = httpd_util:convert_request_date(S),
calendar:datetime_to_gregorian_seconds(ExpiresDt) - BaseSecs.

4 changes: 1 addition & 3 deletions apps/aehttp/test/aehttp_sc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,7 @@ random_unused_name(Attempts) when Attempts < 1->
random_unused_name(Attempts) ->
Size = 10,
RandStr = base58:binary_to_base58(crypto:strong_rand_bytes(Size)),
NameL = RandStr ++ ".test",
Name = list_to_binary(NameL),
Name = aens_test_utils:fullname(list_to_binary(RandStr)),
case get_names_entry_by_name_sut(Name) of
{ok, 404, _Error} -> Name; % name not used yet
_ -> random_unused_name(Attempts - 1)
Expand Down Expand Up @@ -3651,4 +3650,3 @@ sc_ws_broken_open_params(Config) ->
#{lock_period => -1}, Config),
Test(ChannelOpts8, <<"Value too low">>),
ok.

12 changes: 8 additions & 4 deletions apps/aens/src/aens_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

%% API
-export([check_name_claimed_and_owned/3,
name_parts/1,
to_ascii/1]).

%%%===================================================================
Expand Down Expand Up @@ -59,12 +60,15 @@ check_claimed_status(Name) ->
revoked -> {error, name_revoked}
end.

name_parts(Name) ->
binary:split(Name, ?LABEL_SEPARATOR, [global, trim]).

validate_name(Name) ->
case binary:split(Name, ?LABEL_SEPARATOR, [global, trim]) of
case name_parts(Name) of
[_Label, RegistrarNS] ->
case [RN || RN <- aec_governance:name_registrars(), RN =:= RegistrarNS] of
[] -> {error, registrar_unknown};
_ -> ok
case lists:member(RegistrarNS, aec_governance:possible_name_registrars()) of
true -> ok;
false -> {error, registrar_unknown}
end;
[_Name] ->
{error, no_registrar};
Expand Down
64 changes: 54 additions & 10 deletions apps/aens/test/aens_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

%% common_test exports
-export([all/0,
groups/0
groups/0,
init_per_group/2,
end_per_group/2
]).

%% test case exports
Expand All @@ -26,11 +28,13 @@
transfer_negative/1,
revoke/1,
revoke_negative/1,
prune_preclaim/1]).
prune_preclaim/1,
registrar_change/1]).

-include_lib("common_test/include/ct.hrl").

-include("../../aecore/include/blocks.hrl").
-include_lib("aecontract/include/hard_forks.hrl").


%%%===================================================================
%%% Common test framework
Expand All @@ -56,12 +60,20 @@ groups() ->
transfer,
transfer_negative,
revoke,
revoke_negative]}
revoke_negative,
registrar_change]}
].

-define(NAME, <<"詹姆斯詹姆斯.test"/utf8>>).
-define(NAME, <<"詹姆斯詹姆斯"/utf8>>).
-define(PRE_CLAIM_HEIGHT, 1).

init_per_group(Group, Cfg) ->
%% dependening on how we call 'make' we get different protocols
[{protocol, aec_hard_forks:protocol_effective_at_height(1)} | Cfg].

end_per_group(_Grp, Cfg) ->
Cfg.

%%%===================================================================
%%% Preclaim
%%%===================================================================
Expand All @@ -75,7 +87,7 @@ preclaim(Cfg) ->
PrivKey = aens_test_utils:priv_key(PubKey, S1),
Trees = aens_test_utils:trees(S1),
Height = ?PRE_CLAIM_HEIGHT,
Name = ?NAME,
Name = aens_test_utils:fullname(?NAME),
NameSalt = rand:uniform(10000),
{ok, NameAscii} = aens_utils:to_ascii(Name),
CHash = aens_hash:commitment_hash(NameAscii, NameSalt),
Expand Down Expand Up @@ -103,7 +115,7 @@ preclaim_negative(Cfg) ->
Height = 1,
Env = aetx_env:tx_env(Height),

{ok, NameAscii} = aens_utils:to_ascii(<<"詹姆斯詹姆斯.test"/utf8>>),
{ok, NameAscii} = aens_utils:to_ascii(aens_test_utils:fullname(<<"詹姆斯詹姆斯"/utf8>>)),
CHash = aens_hash:commitment_hash(NameAscii, 123),

%% Test bad account key
Expand Down Expand Up @@ -338,7 +350,7 @@ update_negative(Cfg) ->
{error, account_nonce_too_high} = aetx:process(Tx4, Trees, Env),

%% Test name not present
{ok, NHash2} = aens:get_name_hash(<<"othername.test">>),
{ok, NHash2} = aens:get_name_hash(aens_test_utils:fullname(<<"othername">>)),
TxSpec5 = aens_test_utils:update_tx_spec(PubKey, NHash2, S1),
{ok, Tx5} = aens_update_tx:new(TxSpec5),
{error, name_does_not_exist} = aetx:process(Tx5, Trees, Env),
Expand Down Expand Up @@ -415,7 +427,7 @@ transfer_negative(Cfg) ->
{error, account_nonce_too_high} = aetx:process(Tx3, Trees, Env),

%% Test name not present
{ok, NHash2} = aens:get_name_hash(<<"othername.test">>),
{ok, NHash2} = aens:get_name_hash(aens_test_utils:fullname(<<"othername">>)),
TxSpec4 = aens_test_utils:transfer_tx_spec(PubKey, NHash2, PubKey, S1),
{ok, Tx4} = aens_transfer_tx:new(TxSpec4),
{error, name_does_not_exist} = aetx:process(Tx4, Trees, Env),
Expand Down Expand Up @@ -489,7 +501,7 @@ revoke_negative(Cfg) ->
{error, account_nonce_too_high} = aetx:process(Tx3, Trees, Env),

%% Test name not present
{ok, NHash2} = aens:get_name_hash(<<"othername.test">>),
{ok, NHash2} = aens:get_name_hash(aens_test_utils:fullname(<<"othername">>)),
TxSpec4 = aens_test_utils:revoke_tx_spec(PubKey, NHash2, S1),
{ok, Tx4} = aens_revoke_tx:new(TxSpec4),
{error, name_does_not_exist} = aetx:process(Tx4, Trees, Env),
Expand Down Expand Up @@ -560,3 +572,35 @@ do_prune_until(N1, N1, OTree) ->
aens_state_tree:prune(N1, OTree);
do_prune_until(N1, N2, OTree) ->
do_prune_until(N1 + 1, N2, aens_state_tree:prune(N1, OTree)).

%%%===================================================================
%%% Change of registrar
%%%===================================================================

registrar_change(Cfg) ->
State = aens_test_utils:new_state(),
{PubKey, S1} = aens_test_utils:setup_new_account(State),
PrivKey = aens_test_utils:priv_key(PubKey, S1),
NameSalt = rand:uniform(10000),
%% invalid name for protocol
Name = case ?config(protocol, Cfg) >= ?LIMA_PROTOCOL_VSN of
false -> <<"asdf.aet">>; %% we can't claim ".aet" name yet
true -> <<"asdf.test">> %% we no longer can claim ".test" name
end,

%% preclaim
Trees1 = aens_test_utils:trees(S1),
{ok, NameAscii} = aens_utils:to_ascii(Name),
CHash = aens_hash:commitment_hash(NameAscii, NameSalt),
TxSpec1 = aens_test_utils:preclaim_tx_spec(PubKey, CHash, S1),
{ok, Tx1} = aens_preclaim_tx:new(TxSpec1),
SignedTx1 = aec_test_utils:sign_tx(Tx1, PrivKey),
{ok, [_SignedTx1], Trees2, _} =
aec_block_micro_candidate:apply_block_txs(
[SignedTx1], Trees1, aetx_env:tx_env(1)),

%% claim
S2 = aens_test_utils:set_trees(Trees2, S1),
TxSpec2 = aens_test_utils:claim_tx_spec(PubKey, Name, NameSalt, S2),
{ok, Tx2} = aens_claim_tx:new(TxSpec2),
{error, invalid_registrar} = aetx:process(Tx2, Trees2, aetx_env:tx_env(2)).
24 changes: 23 additions & 1 deletion apps/aens/test/aens_test_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
transfer_tx_spec/4,
transfer_tx_spec/5,
revoke_tx_spec/3,
revoke_tx_spec/4]).
revoke_tx_spec/4,
fullname/1,
fullname/2 ]).

-include_lib("aecontract/include/hard_forks.hrl").

%%%===================================================================
%%% Test state
Expand Down Expand Up @@ -98,6 +102,24 @@ set_account(Account, State) ->
AccTree = aec_accounts_trees:enter(Account, aec_trees:accounts(Trees)),
set_trees(aec_trees:set_accounts(Trees, AccTree), State).

%% protocol dependent

latest_protocol_version() ->
lists:last(aec_hard_forks:sorted_protocol_versions()).

fullname(RootName) ->
fullname_in_protocol(RootName, latest_protocol_version()).

fullname(RootName, Height) ->
fullname_in_protocol(RootName, aec_hard_forks:protocol_effective_at_height(Height)).

fullname_in_protocol(RootName, Protocol) ->
Reg = case Protocol >= ?LIMA_PROTOCOL_VSN of
true -> <<"aet">>;
false -> <<"test">>
end,
<<RootName/binary, ".", Reg/binary>>.

%%%===================================================================
%%% Names utils
%%%===================================================================
Expand Down
12 changes: 6 additions & 6 deletions apps/aens/test/aens_utils_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ to_ascii_test() ->
?assertEqual({error, no_registrar},
aens_utils:to_ascii(<<"abcdefg">>)),
?assertEqual({error, multiple_namespaces},
aens_utils:to_ascii(<<"abcd.efgh.test">>)),
aens_utils:to_ascii(<<"abcd.efgh.aet">>)),
?assertEqual({error, registrar_unknown},
aens_utils:to_ascii(<<"abcd.aettt">>)),
LongName = list_to_binary(string:lowercase(base58:binary_to_base58(crypto:strong_rand_bytes(1000)))),
?assertEqual({error, no_label_in_registrar},
aens_utils:to_ascii(<<".test">>)),
aens_utils:to_ascii(<<".aet">>)),
?assertMatch({error, {bad_label, {too_long, _}}},
aens_utils:to_ascii(<<LongName/binary, ".test">>)),
aens_utils:to_ascii(<<LongName/binary, ".aet">>)),
?assertEqual({error, {invalid_codepoint, 65533}},
aens_utils:to_ascii(<<"�姆.test"/utf8>>)),
?assertEqual({ok, <<"xn--8wsa062gba0028bca.test">>},
aens_utils:to_ascii(<<"詹姆斯詹姆斯.test"/utf8>>)).
aens_utils:to_ascii(<<"�姆.aet"/utf8>>)),
?assertEqual({ok, <<"xn--8wsa062gba0028bca.aet">>},
aens_utils:to_ascii(<<"詹姆斯詹姆斯.aet"/utf8>>)).

0 comments on commit 3d5959f

Please sign in to comment.