<commit_before>-module(aecore_sync_SUITE).

%% common_test exports
-export(
   [
    all/0, groups/0, suite/0,
    init_per_suite/1, end_per_suite/1,
    init_per_group/2, end_per_group/2,
    init_per_testcase/2, end_per_testcase/2
   ]).

%% test case exports
-export(
   [
    start_first_node/1,
    start_second_node/1,
    start_third_node/1,
    start_blocked_second/1,
    mine_on_first_up_to_latest_consensus_protocol/1,
    mine_on_first/1,
    mine_on_second/1,
    mine_on_third/1,
    mine_again_on_first/1,
    restart_first/1,
    restart_second/1,
    restart_third/1,
    tx_first_pays_second/1,
    tx_first_pays_second_more_it_can_afford/1,
    ensure_tx_pools_empty/1,
    ensure_tx_pools_one_tx/1,
    ensure_tx_pools_one_tx_first_node/1,
    ensure_tx_pools_one_tx_second_node/1,
    ensure_tx_pools_one_tx_third_node/1,
    report_metrics/1,
    check_metrics_logged/1,
    crash_syncing_worker/1,
    large_msgs/1
   ]).

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

-import(aecore_suite_utils, [patron/0]).

all() ->
    [ {group, all_nodes} ].

groups() ->
    [
     {all_nodes, [sequence], [{group, two_nodes},
                              {group, three_nodes},
                              {group, semantically_invalid_tx},
                              {group, mempool_sync},
                              {group, one_blocked},
                              {group, large_msgs}
                             ]},
     {two_nodes, [sequence],
      [start_first_node,
       mine_on_first_up_to_latest_consensus_protocol,
       mine_on_first,
       start_second_node,
       tx_first_pays_second,
       mine_again_on_first,
       ensure_tx_pools_empty,
       mine_on_second,
       tx_first_pays_second,
       restart_second,
       restart_first,
       report_metrics,
       check_metrics_logged,
       crash_syncing_worker]},
     {three_nodes, [sequence],
      [start_first_node,
       mine_on_first,
       start_second_node,
       start_third_node,
       mine_again_on_first,
       mine_on_second,
       mine_on_third,
       restart_third,
       report_metrics,
       check_metrics_logged]},
     {semantically_invalid_tx, [sequence],
      [start_first_node,
       mine_on_first_up_to_latest_consensus_protocol,
       mine_on_first,
       start_second_node,
       mine_on_second, %% We need to make sure first
       mine_on_second, %% doesn't get a delayed reward
       ensure_tx_pools_empty,
       tx_first_pays_second_more_it_can_afford,
       mine_on_second,
       ensure_tx_pools_one_tx]},
     {mempool_sync, [sequence],
      [start_first_node,
       mine_on_first_up_to_latest_consensus_protocol,
       mine_on_first,
       start_second_node,
       tx_first_pays_second,   %% Tx nonce = 1
       ensure_tx_pools_one_tx, %% for dev1 & dev2
       mine_again_on_first,
       ensure_tx_pools_empty,  %% for dev1 & dev2
       tx_first_pays_second,
       ensure_tx_pools_one_tx, %% Tx nonce = 2,
       start_third_node,
       ensure_tx_pools_one_tx, %% for dev1 & dev2
       ensure_tx_pools_one_tx_third_node %% check mempool on dev3
      ]},
     {one_blocked, [sequence],
      [start_first_node,
       mine_on_first,
       start_blocked_second]},
     {large_msgs, [sequence],
      [large_msgs]}
    ].

suite() ->
    [].

init_per_suite(Config) ->
    DataDir = ?config(data_dir, Config),
    TopDir = aecore_suite_utils:top_dir(DataDir),
    Config1 = [{symlink_name, "latest.sync"},
               {top_dir, TopDir},
               {test_module, ?MODULE}] ++ Config,
    aecore_suite_utils:make_shortcut(Config1),
    ct:log("Environment = ~p", [[{args, init:get_arguments()},
                                 {node, node()},
                                 {cookie, erlang:get_cookie()}]]),
    Forks = aecore_suite_utils:forks(),
    DefCfg = #{
        <<"metrics">> => #{
            <<"rules">> => [
                #{<<"name">> => <<"ae.epoch.system.**">>,
                  <<"actions">> => <<"log">>},
                #{<<"name">> => <<"ae.epoch.aecore.**">>,
                  <<"actions">> => <<"log,send">>}
            ]
        },
        <<"chain">> => #{
            <<"persist">> => true,
            <<"hard_forks">> => Forks
        },
        <<"sync">> => #{
            <<"single_outbound_per_group">> => false
        },
        <<"mempool">> => #{
            <<"tx_ttl">> => 100
        },
        <<"mining">> => #{
            <<"micro_block_cycle">> => 100
        }
    },
    Config2 = aec_metrics_test_utils:make_port_map([dev1, dev2, dev3], Config1),
    aecore_suite_utils:create_configs(Config2, DefCfg, [{add_peers, true}]),
    aecore_suite_utils:make_multi(Config2),
    Config2.

end_per_suite(Config) ->
    stop_devs(Config).

init_per_group(TwoNodes, Config) when
        TwoNodes == two_nodes; TwoNodes == semantically_invalid_tx;
        TwoNodes == large_msgs; TwoNodes == mempool_sync ->
    config({devs, [dev1, dev2]}, Config);<commit_msg>Refactor setting logger_port_map in the test config<commit_after>-module(aecore_sync_SUITE).

%% common_test exports
-export(
   [
    all/0, groups/0, suite/0,
    init_per_suite/1, end_per_suite/1,
    init_per_group/2, end_per_group/2,
    init_per_testcase/2, end_per_testcase/2
   ]).

%% test case exports
-export(
   [
    start_first_node/1,
    start_second_node/1,
    start_third_node/1,
    start_blocked_second/1,
    mine_on_first_up_to_latest_consensus_protocol/1,
    mine_on_first/1,
    mine_on_second/1,
    mine_on_third/1,
    mine_again_on_first/1,
    restart_first/1,
    restart_second/1,
    restart_third/1,
    tx_first_pays_second/1,
    tx_first_pays_second_more_it_can_afford/1,
    ensure_tx_pools_empty/1,
    ensure_tx_pools_one_tx/1,
    ensure_tx_pools_one_tx_first_node/1,
    ensure_tx_pools_one_tx_second_node/1,
    ensure_tx_pools_one_tx_third_node/1,
    report_metrics/1,
    check_metrics_logged/1,
    crash_syncing_worker/1,
    large_msgs/1
   ]).

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

-import(aecore_suite_utils, [patron/0]).

all() ->
    [ {group, all_nodes} ].

groups() ->
    [
     {all_nodes, [sequence], [{group, two_nodes},
                              {group, three_nodes},
                              {group, semantically_invalid_tx},
                              {group, mempool_sync},
                              {group, one_blocked},
                              {group, large_msgs}
                             ]},
     {two_nodes, [sequence],
      [start_first_node,
       mine_on_first_up_to_latest_consensus_protocol,
       mine_on_first,
       start_second_node,
       tx_first_pays_second,
       mine_again_on_first,
       ensure_tx_pools_empty,
       mine_on_second,
       tx_first_pays_second,
       restart_second,
       restart_first,
       report_metrics,
       check_metrics_logged,
       crash_syncing_worker]},
     {three_nodes, [sequence],
      [start_first_node,
       mine_on_first,
       start_second_node,
       start_third_node,
       mine_again_on_first,
       mine_on_second,
       mine_on_third,
       restart_third,
       report_metrics,
       check_metrics_logged]},
     {semantically_invalid_tx, [sequence],
      [start_first_node,
       mine_on_first_up_to_latest_consensus_protocol,
       mine_on_first,
       start_second_node,
       mine_on_second, %% We need to make sure first
       mine_on_second, %% doesn't get a delayed reward
       ensure_tx_pools_empty,
       tx_first_pays_second_more_it_can_afford,
       mine_on_second,
       ensure_tx_pools_one_tx]},
     {mempool_sync, [sequence],
      [start_first_node,
       mine_on_first_up_to_latest_consensus_protocol,
       mine_on_first,
       start_second_node,
       tx_first_pays_second,   %% Tx nonce = 1
       ensure_tx_pools_one_tx, %% for dev1 & dev2
       mine_again_on_first,
       ensure_tx_pools_empty,  %% for dev1 & dev2
       tx_first_pays_second,
       ensure_tx_pools_one_tx, %% Tx nonce = 2,
       start_third_node,
       ensure_tx_pools_one_tx, %% for dev1 & dev2
       ensure_tx_pools_one_tx_third_node %% check mempool on dev3
      ]},
     {one_blocked, [sequence],
      [start_first_node,
       mine_on_first,
       start_blocked_second]},
     {large_msgs, [sequence],
      [large_msgs]}
    ].

suite() ->
    [].

init_per_suite(Config0) ->
    Config = aec_metrics_test_utils:make_port_map([dev1, dev2, dev3], Config0),
    DataDir = ?config(data_dir, Config),
    TopDir = aecore_suite_utils:top_dir(DataDir),
    Config1 = [{symlink_name, "latest.sync"},
               {top_dir, TopDir},
               {test_module, ?MODULE}] ++ Config,
    aecore_suite_utils:make_shortcut(Config1),
    ct:log("Environment = ~p", [[{args, init:get_arguments()},
                                 {node, node()},
                                 {cookie, erlang:get_cookie()}]]),
    Forks = aecore_suite_utils:forks(),
    DefCfg = #{
        <<"metrics">> => #{
            <<"rules">> => [
                #{<<"name">> => <<"ae.epoch.system.**">>,
                  <<"actions">> => <<"log">>},
                #{<<"name">> => <<"ae.epoch.aecore.**">>,
                  <<"actions">> => <<"log,send">>}
            ]
        },
        <<"chain">> => #{
            <<"persist">> => true,
            <<"hard_forks">> => Forks
        },
        <<"sync">> => #{
            <<"single_outbound_per_group">> => false
        },
        <<"mempool">> => #{
            <<"tx_ttl">> => 100
        },
        <<"mining">> => #{
            <<"micro_block_cycle">> => 100
        }
    },
    aecore_suite_utils:create_configs(Config1, DefCfg, [{add_peers, true}]),
    aecore_suite_utils:make_multi(Config1),
    Config1.

end_per_suite(Config) ->
    stop_devs(Config).

init_per_group(TwoNodes, Config) when
        TwoNodes == two_nodes; TwoNodes == semantically_invalid_tx;
        TwoNodes == large_msgs; TwoNodes == mempool_sync ->
    config({devs, [dev1, dev2]}, Config);
{'fraction_non_alphanumeric': 0.12144288577154308, 'fraction_numerical': 0.012024048096192385, 'mean_word_length': 2.3437185929648243, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1351947', 'n_tokens_mistral': 3584, 'n_tokens_neox': 3539, 'n_words': 471}