summaryrefslogtreecommitdiff
path: root/src/plop.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2015-08-17 16:02:42 +0200
committerLinus Nordberg <linus@nordu.net>2015-08-19 16:28:35 +0200
commitc853ee34a2d2d047cc456a9fc78b2904b22dad9c (patch)
tree03e892ffd4ad25ebe23e69275dd725421654b26a /src/plop.erl
parent85411c74332775b6b2dbc06f40074a26e7f6ba3e (diff)
Wrap entries in plop wrapper
Diffstat (limited to 'src/plop.erl')
-rw-r--r--src/plop.erl46
1 files changed, 42 insertions, 4 deletions
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).
%%%%%%%%%%%%%%%%%%%%