From 63d054b97f7b75ae8e0d02472a30fd2f6196bfd6 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Sun, 23 Nov 2014 16:15:34 +0200 Subject: Deserialize all of plop --- src/plop.erl | 80 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/src/plop.erl b/src/plop.erl index 161faf8..38d7047 100644 --- a/src/plop.erl +++ b/src/plop.erl @@ -117,10 +117,12 @@ sth() -> -spec get(non_neg_integer(), non_neg_integer()) -> [{non_neg_integer(), binary(), binary()}]. get(Start, End) -> - call(?MODULE, {get, {index, Start, End}}). + EndBound = min(End, db:size() - 1), + lists:map(fun (E) -> fill_in_entry(E) end, + db:get_by_indices(Start, EndBound, {sorted, false})). get(Hash) -> - call(?MODULE, {get, {hash, Hash}}). + db:get_by_entry_hash(Hash). spt(Data) -> #signature{algorithm = #sig_and_hash_alg{ @@ -129,16 +131,52 @@ spt(Data) -> signature = sign:sign(Data)}. consistency(TreeSizeFirst, TreeSizeSecond) -> - call(?MODULE, {consistency, {TreeSizeFirst, TreeSizeSecond}}). + LastAllowedEntry = db:size() - 1, + if + TreeSizeFirst > LastAllowedEntry -> + []; + TreeSizeSecond > LastAllowedEntry -> + []; + true -> + ht:consistency(TreeSizeFirst - 1, TreeSizeSecond - 1) + end. + -spec inclusion(binary(), non_neg_integer()) -> {ok, {binary(), binary()}} | {notfound, string()}. inclusion(Hash, TreeSize) -> - call(?MODULE, {inclusion, {Hash, TreeSize}}). + LastAllowedTreeSize = db:size(), + if + TreeSize > LastAllowedTreeSize -> + {notfound, "Unknown tree size"}; + true -> + case db:get_by_leaf_hash(Hash) of + notfound -> + {notfound, "Unknown hash"}; % FIXME: include Hash + {I, _MTLHash, _Entry} -> + {ok, I, ht:path(I, TreeSize - 1)} + end + end. + -spec inclusion_and_entry(non_neg_integer(), non_neg_integer()) -> {ok, {binary(), binary()}} | {notfound, string()}. inclusion_and_entry(Index, TreeSize) -> - call(?MODULE, {inclusion_and_entry, {Index, TreeSize}}). + LastAllowedTreeSize = db:size(), + LastAllowedEntry = db:size() - 1, + if + TreeSize > LastAllowedTreeSize -> + {notfound, "Unknown tree size"}; + Index > LastAllowedEntry -> + {notfound, "Unknown index"}; + true -> + case db:get_by_index(Index) of + notfound -> + {notfound, "Unknown index"}; % FIXME: include Index + {I, _MTLHash, Entry} -> + {ok, Entry, ht:path(I, TreeSize - 1)} + end + end. + get_logid() -> sign:get_logid(). testing_get_pubkey() -> @@ -214,38 +252,8 @@ fill_in_entry({_Index, LeafHash, notfetched}) -> %%%%%%%%%%%%%%%%%%%% handle_call(stop, _From, Plop) -> - {stop, normal, stopped, Plop}; + {stop, normal, stopped, Plop}. -handle_call({get, {index, Start, End}}, _From, Plop) -> - EndBound = min(End, db:size() - 1), - {reply, lists:map(fun (E) -> fill_in_entry(E) end, - db:get_by_indices(Start, EndBound, {sorted, false})), - Plop}; - -handle_call({get, {hash, EntryHash}}, _From, Plop) -> - {reply, db:get_by_entry_hash(EntryHash), Plop}; - - -handle_call({consistency, {First, Second}}, _From, Plop) -> - {reply, ht:consistency(First - 1, Second - 1), Plop}; - -handle_call({inclusion, {Hash, TreeSize}}, _From, Plop) -> - R = case db:get_by_leaf_hash(Hash) of - notfound -> - {notfound, "Unknown hash"}; % FIXME: include Hash - {I, _MTLHash, _Entry} -> - {ok, I, ht:path(I, TreeSize - 1)} - end, - {reply, R, Plop}; - -handle_call({inclusion_and_entry, {Index, TreeSize}}, _From, Plop) -> - R = case db:get_by_index(Index) of - notfound -> - {notfound, "Unknown index"}; % FIXME: include Index - {I, _MTLHash, Entry} -> - {ok, Entry, ht:path(I, TreeSize - 1)} - end, - {reply, R, Plop}. %% @doc Signed Plop Timestamp, conformant to an SCT in RFC6962 3.2 and %% RFC5246 4.7. -- cgit v1.1