Store data in ~/.ercoin/

parent df40db72
Pipeline #17434564 passed with stage
in 16 minutes 57 seconds
......@@ -15,5 +15,5 @@
IPFS_BOOTSTRAP_HASH=QmSykgTV3Pdg9EsrdQDFkJpKMemaB1xQCbXN4uAqV5xmLS
mkdir -p priv
ipfs get -o priv/current_data.bin /ipfs/$IPFS_BOOTSTRAP_HASH/genesis_data
ipfs get -o ~/.ercoin /ipfs/$IPFS_BOOTSTRAP_HASH/tendermint
ipfs get -o ~/.ercoin/current_data.bin /ipfs/$IPFS_BOOTSTRAP_HASH/genesis_data
......@@ -62,15 +62,13 @@ code_change(_OldVsn, OldState, OldData, _Extra) ->
{ok, OldState, OldData}.
init(_Args) ->
{ok, gossiping, ercoin_genesis:current_data()}.
{ok, gossiping, ercoin_persistence:current_data()}.
terminate(_Reason, _State, _Data) ->
ok.
gossiping(internal, dump_data, Data) ->
%% We create a binary here to not copy large term (data record) between processes.
DataBin = term_to_binary(Data, [compressed]),
ok = gen_server:cast(ercoin_persistence, {store_data_bin, DataBin}),
ok = ercoin_persistence:dump_data_async(Data),
keep_state_and_data;
gossiping(
{call, From},
......
......@@ -13,8 +13,7 @@
-module(ercoin_genesis).
-export(
[current_data/0,
data_to_genesis_json/1,
[data_to_genesis_json/1,
sk_to_priv_validator_json/1]).
-include_lib("include/ercoin.hrl").
......@@ -37,12 +36,6 @@ binary_to_iolist(<<Head/integer, Rest/binary>>) ->
binary_to_hex(Bin) ->
iolist_to_binary(binary_to_iolist(Bin)).
-spec current_data() -> data().
current_data() ->
DataPath = filename:absname_join(code:priv_dir(ercoin), "current_data.bin"),
{ok, DataBin} = file:read_file(DataPath),
binary_to_term(DataBin).
%% @doc Convert data to a genesis.json file for Tendermint.
-spec data_to_genesis_json(data()) -> binary().
data_to_genesis_json(Genesis) ->
......
......@@ -3,6 +3,8 @@
%% API.
-export([start_link/0]).
-export([current_data/0]).
-export([dump_data_async/1]).
%% gen_server.
-export([init/1]).
......@@ -21,6 +23,29 @@
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
data_dir() ->
case os:getenv("ERCOIN_HOME") of
false ->
Home = os:getenv("HOME"),
filename:absname_join(Home, ".ercoin");
ErcoinHome ->
ErcoinHome
end.
-spec current_data() -> term().
current_data() ->
DataPath = filename:absname_join(data_dir(), "current_data.bin"),
{ok, DataBin} = file:read_file(DataPath),
binary_to_term(DataBin).
%% @doc Dump data asynchronously.
%% This function converts term to binary and schedules writing it to disc.
-spec dump_data_async(term()) -> ok.
dump_data_async(Data) ->
%% We create a binary here to not copy large term between processes.
DataBin = term_to_binary(Data),
gen_server:cast(?MODULE, {store_data_bin, DataBin}).
%% gen_server.
init([]) ->
......@@ -30,8 +55,7 @@ handle_call(_Request, _From, State) ->
{reply, ignored, State}.
handle_cast({store_data_bin, DataBin}, State) ->
%% TODO: Do not store data inside priv/ directory.
DataPath = filename:absname_join(code:priv_dir(ercoin), "current_data.bin"),
DataPath = filename:absname_join(data_dir(), "current_data.bin"),
ok = write_atomically(DataPath, DataBin),
{noreply, State};
handle_cast(_Msg, State) ->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment