summaryrefslogtreecommitdiff
path: root/src/plop.erl
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2014-09-20 15:33:06 +0200
committerLinus Nordberg <linus@nordberg.se>2014-09-20 15:33:06 +0200
commit4f1a715a3f2a6b049dcb9dcc5b38f6f2716e4101 (patch)
tree035e8e4e93da8629de125d5036b1f5c8c3b0b6cb /src/plop.erl
parent209fc7ed4d44f0d613aabdb9b1c59b8621dc1339 (diff)
Store and retrieve extra-data in/from db.
Also, add more specs to db.
Diffstat (limited to 'src/plop.erl')
-rw-r--r--src/plop.erl46
1 files changed, 26 insertions, 20 deletions
diff --git a/src/plop.erl b/src/plop.erl
index abd7d87..0cb29f5 100644
--- a/src/plop.erl
+++ b/src/plop.erl
@@ -26,7 +26,7 @@
%% API.
-export([start_link/2, stop/0]).
-export([get_logid/0, serialise/1]).
--export([add/1, sth/0, get/2, consistency/2, inclusion/2, inclusion_and_leaf/2]).
+-export([add/2, sth/0, get/2, consistency/2, inclusion/2, inclusion_and_more/2]).
%% API for tests.
-export([read_keyfile_rsa/2, read_keyfiles_ec/2]).
-export([testing_get_pubkey/0]).
@@ -104,23 +104,24 @@ terminate(_Reason, _State) ->
ok.
%%%%%%%%%%%%%%%%%%%%
--spec add(timestamped_entry()) -> spt().
-add(Data) ->
- gen_server:call(?MODULE, {add, Data}).
+-spec add(timestamped_entry(), binary()) -> spt().
+add(Entry, ExtraData) ->
+ gen_server:call(?MODULE, {add, {Entry, ExtraData}}).
sth() ->
gen_server:call(?MODULE, {sth, []}).
+-spec get(non_neg_integer(), non_neg_integer()) -> [{mtl(), binary()}].
get(Start, End) ->
gen_server:call(?MODULE, {get, {Start, End}}).
consistency(TreeSizeFirst, TreeSizeSecond) ->
gen_server:call(?MODULE, {consistency, {TreeSizeFirst, TreeSizeSecond}}).
-spec inclusion(binary(), non_neg_integer()) ->
- {ok|notfound, plop_entry() | string()}.
+ {ok, mtl()} | {notfound, string()}.
inclusion(Hash, TreeSize) ->
gen_server:call(?MODULE, {inclusion, {Hash, TreeSize}}).
--spec inclusion_and_leaf(non_neg_integer(), non_neg_integer()) ->
- {ok|notfound, plop_entry() | string()}.
-inclusion_and_leaf(Index, TreeSize) ->
- gen_server:call(?MODULE, {inclusion_and_leaf, {Index, TreeSize}}).
+-spec inclusion_and_more(non_neg_integer(), non_neg_integer()) ->
+ {ok, {mtl(), binary()}} | {notfound, string()}.
+inclusion_and_more(Index, TreeSize) ->
+ gen_server:call(?MODULE, {inclusion_and_more, {Index, TreeSize}}).
get_logid() ->
gen_server:call(?MODULE, {get, logid}).
testing_get_pubkey() ->
@@ -132,15 +133,15 @@ handle_call(stop, _From, State) ->
%% FIXME: What's the right interface for add()? Need to be able to set
%% version and signature type, at least. That's missing from
%% #timestamped_entry, so add it somehow.
-handle_call({add, #timestamped_entry{
- timestamp = Timestamp_in, entry = Entry}},
+handle_call({add,
+ {#timestamped_entry{timestamp = Timestamp_in, entry = Entry},
+ ExtraData}},
_From,
- State = #state{privkey = Privkey,
- logid = LogID}) ->
+ State = #state{privkey = Privkey, logid = LogID}) ->
TimestampedEntry = #timestamped_entry{
timestamp = timestamp(Timestamp_in),
entry = Entry},
- {ok, SPT} = do_add(TimestampedEntry, Privkey, LogID),
+ {ok, SPT} = do_add(TimestampedEntry, ExtraData, Privkey, LogID),
{reply, SPT, State};
handle_call({sth, Data}, _From,
@@ -161,17 +162,17 @@ handle_call({inclusion, {Hash, TreeSize}}, _From, Plop) ->
R = case db:find(mtlhash, Hash) of
[] ->
{notfound, "Unknown hash"}; % FIXME: include Hash
- {plop, I, _EntryHash, _MTLHash, _MTL, _SPT} ->
+ {plop, I, _EntryHash, _MTLHash, _MTL, _ExtraData, _SPT} ->
{ok, I, ht:path(I, TreeSize - 1)}
end,
{reply, R, Plop};
-handle_call({inclusion_and_leaf, {Index, TreeSize}}, _From, Plop) ->
+handle_call({inclusion_and_more, {Index, TreeSize}}, _From, Plop) ->
R = case db:find(index, Index) of
[] ->
{notfound, "Unknown index"}; % FIXME: include Index
- {plop, I, _EntryHash, _MTLHash, MTL, _SPT} ->
- {ok, MTL, ht:path(I, TreeSize - 1)}
+ {plop, I, _EntryHash, _MTLHash, MTL, ExtraData, _SPT} ->
+ {ok, MTL, ExtraData, ht:path(I, TreeSize - 1)}
end,
{reply, R, Plop};
@@ -186,9 +187,10 @@ handle_call({test, pubkey}, _From,
%% Data.
-spec do_add(timestamped_entry(),
+ binary(),
public_key:rsa_private_key(),
- binary()) -> {ok|error, binary()}.
-do_add(TimestampedEntry, Privkey, LogID) ->
+ binary()) -> {ok, spt()} | {error, any()}.
+do_add(TimestampedEntry, ExtraData, Privkey, LogID) ->
DB_hash = crypto:hash(sha256,
serialise(TimestampedEntry#timestamped_entry.entry)),
Record = db:find(entryhash, DB_hash),
@@ -208,6 +210,7 @@ do_add(TimestampedEntry, Privkey, LogID) ->
entryhash = DB_hash,
mtlhash = ht:leaf_hash(MTLtext),
mtl = MTL,
+ extra_data = ExtraData,
spt = NewSPT},
{atomic, ok} = db:add(DB_data),
{ht:add(MTLtext), NewSPT};
@@ -458,16 +461,19 @@ add_test() ->
{ok, S} = init([?TESTPRIVKEYFILE, ?TESTPUBKEYFILE]),
Data1 = <<"some data">>,
+ ExtraData1 = <<"some extra data">>,
{_Tree, SPT} =
do_add(#timestamped_entry{
timestamp = 4711,
entry = #plop_entry{type = test, data = Data1}},
+ ExtraData1,
S#state.privkey,
S#state.logid),
{_Tree1, SPT1} =
do_add(#timestamped_entry{
timestamp = 4712,
entry = #plop_entry{type = test, data = Data1}},
+ ExtraData1,
S#state.privkey,
S#state.logid),
?assertEqual(SPT, SPT1),