Skip to content

Commit

Permalink
Ceres: prepare AENS auction changes (#4113)
Browse files Browse the repository at this point in the history
* fix: unused variables/functions + Whitespace

* fix: remove magic number introduce MAX_RAW_POINTER_SIZE

* Add new auction extension logic to Ceres + adjust auction length

* Tests for name auctions with shorter extension in Ceres

Update logic: original TTL can never be reduced.

More future proof by making second claim close to half the extension period

* Add release note

---------

Co-authored-by: Thomas Arts <thomas.arts@quviq.com>
  • Loading branch information
hanssv and ThomasArts committed May 31, 2023
1 parent 906c7d3 commit a6ffb7b
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 112 deletions.
20 changes: 0 additions & 20 deletions apps/aecontract/test/aect_test_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -779,26 +779,6 @@ decode_data_(aevm, Type, Data0) ->
after
cleanup_tempfiles()
end.
%% case get_type(Type) of
%% {ok, SophiaType} ->
%% try aeb_heap:from_binary(SophiaType, Data) of
%% {ok, Term} ->
%% try aect_sophia:prepare_for_json(SophiaType, Term) of
%% R -> {ok, R}
%% catch throw:R -> R
%% end;
%% {error, _} -> {error, <<"bad type/data">>}
%% catch _T:_E -> {error, <<"bad argument">>}
%% end;
%% {error, _} = E -> E
%% end.

get_type(Type) ->
case aeso_compiler:sophia_type_to_typerep(to_str(Type)) of
{ok, _Type} = R -> R;
{error, ErrorAtom} ->
{error, unicode:characters_to_binary(atom_to_list(ErrorAtom))}
end.

%% Convert to old style hex literals.
legacy_args(Vsn, Args) when Vsn =< ?SOPHIA_MINERVA ->
Expand Down
39 changes: 31 additions & 8 deletions apps/aecore/src/aec_governance.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
name_claim_locked_fee/0,
name_claim_fee/2,
name_claim_bid_timeout/2,
name_claim_bid_extension/2,
name_claim_bid_increment/0,
name_claim_max_expiration/1,
name_protection_period/0,
Expand Down Expand Up @@ -73,6 +74,7 @@
-define(MICRO_BLOCK_CYCLE, 3000). %% in msecs
-define(MULTIPLIER_14, 100000000000000).
-define(MULTIPLIER_DAY, 480).
-define(MULTIPLIER_HOUR, 20).

-define(ACCEPTED_FUTURE_BLOCK_TIME_SHIFT, (?EXPECTED_BLOCK_MINE_RATE_MINUTES * 3 * 60 * 1000)). %% 9 min

Expand Down Expand Up @@ -303,23 +305,44 @@ name_max_length_starting_auction() ->

%% Give possibility to have the actual name under consensus,
%% possible to define different bid times for different names.
%% No auction for names of 32 characters or longer
%% No auction for names of 12 characters or longer
name_claim_bid_initial_timeout(_NameSize, Protocol) when Protocol < ?LIMA_PROTOCOL_VSN -> 0;
name_claim_bid_initial_timeout(NameSize, Protocol) when Protocol < ?CERES_PROTOCOL_VSN ->
MaxAuctionName = name_max_length_starting_auction(),
if NameSize < 5 -> 62 * ?MULTIPLIER_DAY; %% 29760 blocks
NameSize < 9 -> 31 * ?MULTIPLIER_DAY; %% 14880 blocks
NameSize =< MaxAuctionName -> 1 * ?MULTIPLIER_DAY; %% 480 blocks
true -> 0
end;
name_claim_bid_initial_timeout(NameSize, _Protocol) ->
MaxAuctionName = name_max_length_starting_auction(),
if NameSize < 5 -> 5 * ?MULTIPLIER_DAY; %% 2400 blocks
NameSize < 9 -> 2 * ?MULTIPLIER_DAY; %% 960 blocks
NameSize =< MaxAuctionName -> 1 * ?MULTIPLIER_DAY; %% 480 blocks
true -> 0
end.

-spec name_claim_bid_timeout(binary(), non_neg_integer()) -> non_neg_integer().
name_claim_bid_timeout(Name, Protocol) when Protocol >= ?LIMA_PROTOCOL_VSN ->
NameSize = name_claim_size(Name),
MaxAuctionName = name_max_length_starting_auction(),
BidTimeout =
if NameSize > MaxAuctionName -> 0;
NameSize > 8 -> 1 * ?MULTIPLIER_DAY; %% 480 blocks
NameSize > 4 -> 31 * ?MULTIPLIER_DAY; %% 14880 blocks
true -> 62 * ?MULTIPLIER_DAY %% 29760 blocks
end,
BidTimeout = name_claim_bid_initial_timeout(NameSize, Protocol),
%% allow overwrite by configuration for test
aeu_env:user_config_or_env([<<"mining">>, <<"name_claim_bid_timeout">>],
aecore, name_claim_bid_timeout, BidTimeout);
name_claim_bid_timeout(_Name, _Protocol) ->
0.

-spec name_claim_bid_extension(binary(), non_neg_integer()) -> non_neg_integer().
name_claim_bid_extension(Name, Protocol) when Protocol < ?CERES_PROTOCOL_VSN ->
name_claim_bid_timeout(Name, Protocol);
name_claim_bid_extension(Name, _Protocol) ->
NameSize = name_claim_size(Name),
MaxAuctionName = name_max_length_starting_auction(),
if NameSize =< MaxAuctionName -> 12 * ?MULTIPLIER_HOUR; %% 240 blocks
true -> 0
end.

%% New/Higher bid has to be at least `name_claim_bid_increment()`% larger
name_claim_bid_increment() ->
5. %% 5%

Expand Down
4 changes: 2 additions & 2 deletions apps/aecore/src/aec_parent_connector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ start_link() ->
%% ParentConnMod :: atom() - module name of the http client module aehttpc_btc | aehttpc_aeternity
%% FetchInterval :: integer() | on_demand - millisecs between parent chain checks or when asked (useful for test)
%% ParentHosts :: [#{host => Host, port => Port, user => User, password => Pass}]
%% NetworkID :: binary() - the parent chain's network id
%% NetworkID :: binary() - the parent chain's network id
%% SignModule :: atom() - module name of the module that keeps the keys for the parent chain transactions to be signed
%% HCPCPairs :: [{binary(), binary()}] - mapping from hyperchain address to child chain address
%% Recipient :: binary() - the parent chain address to which the commitments must be sent to
Expand Down Expand Up @@ -210,7 +210,7 @@ handle_info(check_parent, #state{parent_hosts = ParentNodes,
aec_parent_chain_cache:post_block(NewParentTop),
%_Commitments = fetch_commitments(Mod, Node, Seed,
% aec_parent_chain_block:hash(NewParentTop))
%% Commitments may include varying view on what is the latest
%% Commitments may include varying view on what is the latest
%% block.
%% Commitments include:
%% [{Committer, Committers view of child chain top hash}]
Expand Down
25 changes: 0 additions & 25 deletions apps/aecore/test/aec_parent_chain_cache_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,6 @@ height_to_hash(Height) when is_integer(Height) -> <<Height:32/unit:8>>.
%% {0, 0},
%% MeaningfulBytes),
%% Height.
block_by_height(Height, Commitments) ->
B0 = block_by_height(Height),
aec_parent_chain_block:set_commitments(B0, Commitments).


block_by_height(Height) ->
Hash = height_to_hash(Height),
Expand Down Expand Up @@ -641,24 +637,3 @@ filter_meck_events(Module, Function) ->
M =:= Module andalso F =:= Function
end,
meck:history(Module)).

mock_commitments_list(_BlockHashesMap) ->
meck:expect(aec_parent_connector, request_block_by_height,
fun(Height) ->
spawn(
fun() ->
Block = block_by_height(Height),
?TEST_MODULE:post_block(Block)
end)
end).

mock_commitments_list(all, L) ->
meck:expect(aec_parent_connector, request_block_by_height,
fun(Height) ->
spawn(
fun() ->
Block0 = block_by_height(Height),
Block = aec_parent_chain_block:set_commitments(Block0, L),
?TEST_MODULE:post_block(Block)
end)
end).
5 changes: 1 addition & 4 deletions apps/aecore/test/aecore_suite_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ start_node(N, Config, ExtraEnv) ->
_ ->
["-pa ", MyDir, " -config ./" ++ ConfigFilename]
end,
Env0 =
Env0 =
case ?config(build_to_connect_to_mainnet, Config) of
true ->
[
Expand Down Expand Up @@ -854,9 +854,6 @@ wait_for_new_block_mined(T) when is_integer(T), T >= 0 ->
{error, timeout_waiting_for_block}
end.

wait_for_new_block() ->
wait_for_new_block(30000).

wait_for_new_block(T) when is_integer(T), T >= 0 ->
receive
{gproc_ps_event, top_changed, #{info := #{block_hash := _Hash}}} ->
Expand Down
4 changes: 0 additions & 4 deletions apps/aehttp/test/aehttp_integration_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1996,10 +1996,6 @@ get_status_sut(IntAsString) ->
Parameters = case IntAsString of true -> "?int-as-string"; false -> "" end,
http_request(Host, get, "status" ++ Parameters, []).

prepare_tx(TxType, Args) ->
SignHash = lists:last(aec_hard_forks:sorted_protocol_versions()) >= ?LIMA_PROTOCOL_VSN,
prepare_tx(TxType, Args, SignHash).

prepare_tx(TxType, Args, SignHash) ->
%assert_required_tx_fields(TxType, Args),
{Host, Path} = tx_object_http_path(TxType),
Expand Down
2 changes: 1 addition & 1 deletion apps/aehttp/test/aehttp_parent_connector_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ mock_sign_module(#{pubkey := ExpectedPubKey, privkey := PrivKey}) ->
meck:new(?SIGN_MODULE, []),
meck:expect(?SIGN_MODULE, sign_binary,
fun(Bin, Pubkey) when Pubkey =:= ExpectedPubKey ->
Signature = enacl:sign_detached(Bin, PrivKey),
Signature = enacl:sign_detached(Bin, PrivKey),
{ok, Signature}
end).

Expand Down
39 changes: 4 additions & 35 deletions apps/aehttp/test/aehttp_stake_contract_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ common_tests() ->
].

hc_specific_tests() ->
[
[
verify_commitments,
genesis_has_commitments,
block_difficulty
Expand Down Expand Up @@ -192,7 +192,7 @@ init_per_suite(Config0) ->
ParentPatronPubEnc = aeser_api_encoder:encode(account_pubkey, ParentPatronPub),
aecore_suite_utils:create_seed_file([?PARENT_CHAIN_NODE1],
Config1,
"genesis", "accounts_test.json",
"genesis", "accounts_test.json",
#{ ParentPatronPubEnc =>
100000000000000000000000000000000000000000000000000000000000000000000000,
encoded_pubkey(?DWIGHT) => 2100000000000000000000000000,
Expand Down Expand Up @@ -458,7 +458,7 @@ rpcport=" ++ integer_to_list(?BTC_PARENT_CHAIN_PORT),
end_per_group(pos, Config) ->
aecore_suite_utils:stop_node(?NODE1, Config),
aecore_suite_utils:stop_node(?NODE2, Config);
end_per_group(hc, Config) ->
end_per_group(hc, Config) ->
aecore_suite_utils:stop_node(?NODE1, Config),
aecore_suite_utils:stop_node(?NODE2, Config),
aecore_suite_utils:stop_node(?PARENT_CHAIN_NODE1, Config);
Expand Down Expand Up @@ -1061,11 +1061,6 @@ seed_account(Node, NodeName, RecpipientPubkey, Amount, NetworkId) ->
MineFun = fun(Tx) -> mine_tx_no_cheating(Node, Tx) end,
seed_account(Node, NodeName, RecpipientPubkey, Amount, NetworkId, MineFun).

seed_account_pow(Node, NodeName, RecpipientPubkey, Amount, NetworkId) ->
MineFun = fun(Tx) -> mine_tx(NodeName, Tx) end,
seed_account(Node, NodeName, RecpipientPubkey, Amount, NetworkId, MineFun).


seed_account(Node, NodeName, RecipientPubkey, Amount, NetworkId, MineFun) ->
%% precondition
{ok, []} = rpc:call(NodeName, aec_tx_pool, peek, [infinity]),
Expand All @@ -1088,12 +1083,6 @@ seed_account(Node, NodeName, RecipientPubkey, Amount, NetworkId, MineFun) ->
MineFun(SignedTx),
{ok, SignedTx}.

mine_tx(NodeName, SignedTx) ->
TxHash = aeser_api_encoder:encode(tx_hash, aetx_sign:hash(SignedTx)),
aecore_suite_utils:mine_blocks_until_txs_on_chain(NodeName,
[TxHash],
10). %% max keyblocks

mine_tx_no_cheating(Node, SignedTx) ->
mine_tx_no_cheating(Node, SignedTx, 100).

Expand All @@ -1113,7 +1102,7 @@ mine_tx_no_cheating(Node, SignedTx, Attempts) ->
Retry = fun() -> timer:sleep(100), mine_tx_no_cheating(Node, SignedTx, Attempts - 1) end,
TxHash = aetx_sign:hash(SignedTx),
case rpc(Node, aec_chain, find_tx_location, [TxHash]) of
mempool -> Retry();
mempool -> Retry();
none ->
error({could_not_mine_tx, garbage_collected, SignedTx});
not_found ->
Expand Down Expand Up @@ -1423,7 +1412,6 @@ node_config(PotentialStakers, ReceiveAddress, Consensus) ->
]
}
}

};
_ when Consensus == ?CONSENSUS_HC_BTC; Consensus == ?CONSENSUS_HC_DOGE ->
PCType = case Consensus of
Expand Down Expand Up @@ -1650,25 +1638,6 @@ get_doge_raw_tx(BitcoinCli, TxId) ->
Tx = list_to_binary(string:trim(os:cmd(BitcoinCli ++ "getrawtransaction " ++ binary_to_list(TxId) ++ " true"))),
{ok, jsx:decode(Tx, [return_maps])}.

produce_validator_tx() ->
Who = ?BOB,
SCId = staking_contract_address(),
Tx =
contract_call(SCId, ?STAKING_CONTRACT,
"set_validator_avatar_url",
["\"https://pbs.twimg.com/profile_images/1338340549804380160/A3oKPQuq_400x400.jpg\""], 0,
pubkey(Who), 4),
Bin0 = aetx:serialize_to_binary(Tx),
Bin = aec_hash:hash(signed_tx, Bin0), %% since we are in IRIS or CERES context, we sign th hash
NetworkId = <<"ae_smart_contract_test">>,
BinForNetwork = <<NetworkId/binary, Bin/binary>>,
Signatures = [ enacl:sign_detached(BinForNetwork, privkey(Who))],
SignedTx = aetx_sign:new(Tx, Signatures),
aeser_api_encoder:encode(transaction, aetx_sign:serialize_to_binary(SignedTx)).

get_commitments(GenerationHeight) ->
get_commitments(GenerationHeight, GenerationHeight).

get_commitments(From, To) ->
{ok, Blocks} = get_generations(?PARENT_CHAIN_NODE1, From, To + 1),
MicroBlocks =
Expand Down
15 changes: 15 additions & 0 deletions apps/aens/src/aens_auctions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

%% API
-export([new/5,
extend/6,
serialize/1,
deserialize/2,
deserialize_from_fields/3,
Expand Down Expand Up @@ -74,6 +75,20 @@ new(AuctionHash, Bidder, NameFee, DeltaTTL, BlockHeight) ->
bid = NameFee,
ttl = BlockHeight + DeltaTTL}.

%% NOTE: Pre-Ceres this always hold: OldTTL =< BlockHeight + DeltaTTL
-spec extend(aens_hash:auction_hash(), aec_keys:pubkey(), non_neg_integer(),
non_neg_integer(), non_neg_integer(), aec_blocks:height()) -> auction().
extend(AuctionHash, Bidder, NameFee, OldTTL, DeltaTTL, BlockHeight) ->
NameHash = aens_hash:from_auction_hash(AuctionHash),
TTL = if OldTTL > BlockHeight + DeltaTTL -> OldTTL;
true -> BlockHeight + DeltaTTL
end,
#auction{id = aeser_id:create(name, NameHash),
bidder_id = aeser_id:create(account, Bidder),
started = BlockHeight,
bid = NameFee,
ttl = TTL}.

-spec serialize(auction()) -> serialized().
serialize(#auction{bidder_id = BidderId,
bid = NameFee,
Expand Down
5 changes: 3 additions & 2 deletions apps/aens/src/aens_pointer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ serialize_id_for_client({data, Data}) ->
serialize_id_for_client(Id) ->
aeser_api_encoder:encode(id_hash, Id).

%% TODO: check max length of key?
-define(MAX_RAW_POINTER_SIZE, 1024).

assert_key(Key) when is_binary(Key) -> ok.

assert_data(Data) when is_binary(Data), byte_size(Data) =< 1024 -> ok.
assert_data(Data) when is_binary(Data), byte_size(Data) =< ?MAX_RAW_POINTER_SIZE -> ok.

assert_id(Id) ->
assert_id_type(aeser_id:specialize_type(Id)).
Expand Down

0 comments on commit a6ffb7b

Please sign in to comment.