diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/storage.erl | 12 | ||||
-rw-r--r-- | src/storagedb.erl | 17 |
2 files changed, 26 insertions, 3 deletions
diff --git a/src/storage.erl b/src/storage.erl index e208d9b..5774f2a 100644 --- a/src/storage.erl +++ b/src/storage.erl @@ -30,8 +30,16 @@ request(post, "ct/storage/entrycommitted", Input) -> success({[{result, <<"ok">>}]}) end; request(get, "ct/storage/fetchnewentries", _Input) -> - NewHashes = storagedb:fetchnewhashes(0), - % XXX send only hashes, implement getentry + {LastIndexOrZero, LastHash} = storagedb:lastverifiednewentry(), + LastVerifiedAndNewHashes = storagedb:fetchnewhashes(LastIndexOrZero), + NewHashes = case {LastHash, LastVerifiedAndNewHashes} of + {none, LastVerifiedAndNewHashes} -> + LastVerifiedAndNewHashes; + {Hash1, [Hash2|Rest]} when Hash1 == Hash2 -> + Rest; + _ -> + exit({internalerror, "Incorrect lastverifiedentry"}) + end, Entries = lists:map(fun(LeafHash) -> base64:encode(LeafHash) end, NewHashes), diff --git a/src/storagedb.erl b/src/storagedb.erl index c94a13e..d781033 100644 --- a/src/storagedb.erl +++ b/src/storagedb.erl @@ -7,7 +7,7 @@ -behaviour(gen_server). %% API -export([start_link/0, stop/0]). --export([add/1, fetchnewhashes/1]). +-export([add/1, fetchnewhashes/1, lastverifiednewentry/0]). %% gen_server callbacks. -export([init/1, handle_call/3, terminate/2, handle_cast/2, handle_info/2, code_change/3]). @@ -23,6 +23,10 @@ start_link() -> stop() -> call(?MODULE, stop). +lastverifiednewentry_path() -> + {ok, Value} = application:get_env(plop, lastverifiednewentry_path), + Value. + %%%%%%%%%%%%%%%%%%%% %% Public API. @@ -34,6 +38,17 @@ fetchnewhashes(Index) -> index:getrange(newentries_path(), Index, Size - 1) end. +lastverifiednewentry() -> + case atomic:readfile(lastverifiednewentry_path()) of + noentry -> + {0, none}; + Contents -> + {struct, PropList} = mochijson2:decode(Contents), + Index = proplists:get_value(<<"index">>, PropList), + Hash = proplists:get_value(<<"hash">>, PropList), + {Index, mochihex:to_bin(binary_to_list(Hash))} + end. + -spec add(binary()) -> ok. add(LeafHash) -> ok = call(?MODULE, {add_nosync, LeafHash}), |