diff options
Diffstat (limited to 'src/plop.erl')
-rw-r--r-- | src/plop.erl | 61 |
1 files changed, 26 insertions, 35 deletions
diff --git a/src/plop.erl b/src/plop.erl index 61af616..cfb62fc 100644 --- a/src/plop.erl +++ b/src/plop.erl @@ -21,18 +21,14 @@ %%% - get rid of CT-specific stuff that has creeped in -module(plop). --behaviour(gen_server). %% API. --export([start_link/0, stop/0]). +-export([initsize/0]). -export([get_logid/0, serialise/1]). -export([add/3, sth/0, get/1, get/2, spt/1, consistency/2, inclusion/2, inclusion_and_entry/2]). -export([generate_timestamp/0, save_sth/1, verify_sth/4]). %% API for tests. -export([testing_get_pubkey/0]). -%% gen_server callbacks. --export([init/1, handle_call/3, terminate/2, - handle_cast/2, handle_info/2, code_change/3]). -import(stacktrace, [call/2]). -include("plop.hrl"). @@ -57,20 +53,6 @@ }). -type sth_signed() :: #sth_signed{}. -start_link() -> - gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). - -stop() -> - call(?MODULE, stop). - -%%%%%%%%%%%%%%%%%%%% -init([]) -> - _Tree = ht:reset_tree([db:size() - 1]), - {ok, []}. - -handle_cast(_Request, State) -> - {noreply, State}. - handle_http_reply(TreeLeafHash, RepliesUntilQuorum, StatusCode, Body) -> lager:debug("leafhash ~s: http_reply: ~p", @@ -92,16 +74,6 @@ handle_http_reply(TreeLeafHash, RepliesUntilQuorum, end end. -handle_info(_Info, State) -> - {noreply, State}. - -code_change(_OldVsn, State, _Extra) -> - {ok, State}. - -terminate(_Reason, _State) -> - io:format("~p terminating~n", [?MODULE]), - ok. - %%%%%%%%%%%%%%%%%%%% -spec add(binary(), binary(), binary()) -> ok. add(LogEntry, TreeLeafHash, EntryHash) -> @@ -117,12 +89,34 @@ add(LogEntry, TreeLeafHash, EntryHash) -> save_sth(STH) -> {ok, STHFile} = application:get_env(plop, sth_path), + {struct, PropList} = STH, + Treesize = proplists:get_value(<<"tree_size">>, PropList), lager:debug("writing new sth to ~p: ~p", [STHFile, STH]), - atomic:replacefile(STHFile, mochijson2:encode(STH)). + ok = atomic:replacefile(STHFile, mochijson2:encode(STH)), + ok = db:set_treesize(Treesize). + +initsize() -> + db:create_size_table(), + Treesize = case sth() of + noentry -> + 0; + {struct, PropList} -> + proplists:get_value(<<"tree_size">>, PropList) + end, + db:set_treesize(Treesize). sth() -> - {ok, STHFile} = application:get_env(plop, sth_path), - mochijson2:decode(atomic:readfile(STHFile)). + case application:get_env(plop, sth_path) of + {ok, STHFile} -> + case atomic:readfile(STHFile) of + noentry -> + noentry; + Contents -> + mochijson2:decode(Contents) + end; + undefined -> + noentry + end. -spec get(non_neg_integer(), non_neg_integer()) -> [{non_neg_integer(), binary(), binary()}]. @@ -296,9 +290,6 @@ fill_in_entry({_Index, LeafHash, notfetched}) -> db:get_by_leaf_hash(LeafHash). %%%%%%%%%%%%%%%%%%%% -handle_call(stop, _From, Plop) -> - {stop, normal, stopped, Plop}. - verify_sth(Treesize, Timestamp, Roothash, PackedSignature) -> STH = serialise(#sth_signed{ |