-module(aecore_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

-include("common.hrl").
-include("blocks.hrl").

-define(SERVER, ?MODULE).
-define(CHILD(Mod,N,Type), {Mod,{Mod,start_link,[]},permanent,N,Type,[Mod]}).
-define(CHILD(Mod,N,Type,Params), {Mod,{Mod,start_link,Params},permanent,N,Type,[Mod]}).


%%====================================================================
%% API functions
%%====================================================================

start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%====================================================================
%% Supervisor callbacks
%%====================================================================

init([]) ->
    {ok, {{one_for_one, 5, 10}, [?CHILD(aec_peers, 5000, worker),
                                 ?CHILD(aec_persistence, 5000, worker),
                                 ?CHILD(aec_tx_pool, 5000, worker),
                                 ?CHILD(aec_chain, 5000, worker),
                                 ?CHILD(aec_keys, 5000, worker),
                                 ?CHILD(aec_sync, 5000, worker),
                                 ?CHILD(aec_miner, 5000, worker)]
         }}.

-module(aecore_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

-include("common.hrl").
-include("blocks.hrl").

-define(SERVER, ?MODULE).
-define(CHILD(Mod,N,Type), {Mod,{Mod,start_link,[]},permanent,N,Type,[Mod]}).
-define(CHILD(Mod,N,Type,Params), {Mod,{Mod,start_link,Params},permanent,N,Type,[Mod]}).


%%====================================================================
%% API functions
%%====================================================================

start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%====================================================================
%% Supervisor callbacks
%%====================================================================

init([]) ->
    {ok, {{one_for_one, 5, 10}, [?CHILD(aec_keys, 5000, worker),
                                 ?CHILD(aec_peers, 5000, worker),
                                 ?CHILD(aec_persistence, 5000, worker),
                                 ?CHILD(aec_tx_pool, 5000, worker),
                                 ?CHILD(aec_chain, 5000, worker),
                                 ?CHILD(aec_sync, 5000, worker),
                                 ?CHILD(aec_miner, 5000, worker)]
         }}.

Fix race between chain and keys services when starting with persisted chain
Fix race between chain and keys services when starting with persisted chain

Start process responsible for keys (`aec_keys`) first because it is
used by (at least):

* Chain process (`aec_chain`/`aec_chain_server` - see module
  `aec_chain_state`);

* Mempool process (`aec_tx_pool`).

In particular, chain process needs keys process when loading persisted
chain.

Thanks to Dimitar for reproducing issue (PT-152793276), and Dimitar
and Ulf for proposing fix.
