From c853ee34a2d2d047cc456a9fc78b2904b22dad9c Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Mon, 17 Aug 2015 16:02:42 +0200 Subject: Wrap entries in plop wrapper --- src/plop.erl | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) (limited to 'src/plop.erl') diff --git a/src/plop.erl b/src/plop.erl index b6d7ff1..b812e4a 100644 --- a/src/plop.erl +++ b/src/plop.erl @@ -27,6 +27,7 @@ -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]). +-export([get_by_leaf_hash/1, entry_for_leafhash/1]). %% API for tests. -export([testing_get_pubkey/0]). @@ -73,16 +74,32 @@ handle_http_reply(TreeLeafHash, RepliesUntilQuorum, end end. +wrap_entry(Entry) -> + EncodedEntry = tlv:encodelist(Entry), + tlv:encodelist([{<<"PLOP">>, EncodedEntry}, + {<<"S256">>, crypto:hash(sha256, EncodedEntry)}]). + +unwrap_entry(WrappedEntry) -> + [{<<"PLOP">>, Entry}, {<<"S256">>, Hash}] = tlv:decodelist(WrappedEntry), + ComputedHash = crypto:hash(sha256, Entry), + case ComputedHash of + Hash -> + tlv:decodelist(Entry); + _ -> + error + end. + %%%%%%%%%%%%%%%%%%%% -spec add(binary(), binary(), binary()) -> ok. add(LogEntry, TreeLeafHash, EntryHash) -> lager:debug("add leafhash ~s", [mochihex:to_hex(TreeLeafHash)]), + WrappedLogEntry = wrap_entry(LogEntry), case storage_nodes() of [] -> exit(internal_merge_not_supported); Nodes -> util:spawn_and_wait(fun () -> - store_at_all_nodes(Nodes, {LogEntry, TreeLeafHash, EntryHash}) + store_at_all_nodes(Nodes, {WrappedLogEntry, TreeLeafHash, EntryHash}) end) end. @@ -126,7 +143,12 @@ get(Start, End) -> -spec get(binary()) -> notfound | {notfetched, binary(), binary()}. get(Hash) -> - db:get_by_entry_hash(Hash). + case db:get_by_entry_hash(Hash) of + notfound -> + notfound; + {notfetched, LeafHash, Entry} -> + {notfetched, LeafHash, unwrap_entry(Entry)} + end. spt(Data) -> #signature{algorithm = #sig_and_hash_alg{ @@ -177,7 +199,7 @@ inclusion_and_entry(Index, TreeSize) -> {I, _MTLHash, noentry} -> {notfound, io:format("Unknown index ~p", [I])}; {I, _MTLHash, Entry} -> - {ok, Entry, ht:path(I, TreeSize - 1)} + {ok, unwrap_entry(Entry), ht:path(I, TreeSize - 1)} end end. @@ -288,8 +310,24 @@ store_at_all_nodes(Nodes, {LogEntry, TreeLeafHash, EntryHash}) -> Any end. +get_by_leaf_hash(LeafHash) -> + case db:get_by_leaf_hash(LeafHash) of + notfound -> + notfound; + {Index, LeafHash, Entry} -> + {Index, LeafHash, unwrap_entry(Entry)} + end. + +entry_for_leafhash(LeafHash) -> + case db:entry_for_leafhash(LeafHash) of + noentry -> + notfound; + Entry -> + unwrap_entry(Entry) + end. + fill_in_entry({_Index, LeafHash, notfetched}) -> - db:get_by_leaf_hash(LeafHash). + get_by_leaf_hash(LeafHash). %%%%%%%%%%%%%%%%%%%% -- cgit v1.1