summaryrefslogtreecommitdiff
path: root/src/plop.erl
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2014-05-29 12:38:41 +0200
committerLinus Nordberg <linus@nordberg.se>2014-05-29 12:38:41 +0200
commit5fc1ec8ac6d76f5798373658b06021696f5e1e02 (patch)
tree766fc67e69e3d5177817dddf7cc80bc972cfc9b8 /src/plop.erl
parent2812418feb2aa76c3b6ee9de0ca0b469907fa3ca (diff)
Add db:size/0 and some error checking.
Diffstat (limited to 'src/plop.erl')
-rw-r--r--src/plop.erl28
1 files changed, 22 insertions, 6 deletions
diff --git a/src/plop.erl b/src/plop.erl
index 08c74c8..a28f915 100644
--- a/src/plop.erl
+++ b/src/plop.erl
@@ -71,6 +71,7 @@ stop() ->
%%%%%%%%%%%%%%%%%%%%
init([PrivKeyfile, PubKeyfile]) ->
+ io:format("plop starting~n"),
%% Read RSA keypair.
%% {Private_key, Public_key} = read_keyfile_rsa(Keyfile, Passphrase),
%% LogID = crypto:hash(sha256,
@@ -80,10 +81,13 @@ init([PrivKeyfile, PubKeyfile]) ->
LogID = crypto:hash(sha256, public_key:der_encode(
'ECPoint',
element(2, element(1, Public_key)))), % FIXME!
+ %% WARNING FIXME: The building of the tree is immensely expensive
+ %% and slow -- don't do this with more than a couple of hundred of
+ %% entries!
{ok, #state{pubkey = Public_key,
privkey = Private_key,
logid = LogID,
- hashtree = ht:create()}}.
+ hashtree = build_tree_from_db()}}.
handle_cast(_Request, State) ->
{noreply, State}.
@@ -142,6 +146,20 @@ handle_call({test, pubkey}, _From,
{reply, PK, Plop}.
%%%%%%%%%%%%%%%%%%%%
+-spec build_tree_from_db() -> ht:head().
+build_tree_from_db() ->
+ build_tree(ht:create(), lists:seq(0, db:size() - 1)).
+-spec build_tree(ht:head(), list()) -> ht:head().
+build_tree(Tree, []) ->
+ Tree;
+build_tree(Tree, [H|T]) ->
+ Data = db_get_single_entry(H),
+ build_tree(ht:append(Tree, Data), T).
+
+db_get_single_entry(N) ->
+ [#mtl{entry = #timestamped_entry{entry = #plop_entry{data = Data}}}] =
+ db:get_by_index(N, N),
+ Data.
-spec do_add(timestamped_entry(), public_key:rsa_private_key(),
binary(), any()) -> {any(), binary()}.
@@ -151,8 +169,7 @@ do_add(TimestampedEntry = #timestamped_entry{entry = PlopEntry},
Record = db:find(DB_hash),
case Record of
#plop{index = I, mtl = #mtl{entry = E}, spt = SPT} ->
- io:format("Found entry: index=~p, data=~p~n",
- [I, E#timestamped_entry.entry#plop_entry.data]),
+ io:format("Found entry: index=~p~n", [I]),
%% Database consistency checking. FIXME: Remove.
Record = Record#plop{
hash = DB_hash,
@@ -164,13 +181,12 @@ do_add(TimestampedEntry = #timestamped_entry{entry = PlopEntry},
[] ->
NewSPT = spt(LogID, Privkey, TimestampedEntry),
MTL = #mtl{entry = TimestampedEntry},
- io:format("Creating new entry: index=~p,data=~p~n",
- [ht:size(Tree), PlopEntry#plop_entry.data]),
+ io:format("Creating new entry: index=~p~n", [ht:size(Tree)]),
DB_data = #plop{index = ht:size(Tree),
hash = DB_hash,
mtl = MTL,
spt = NewSPT},
- ok = db:add(DB_data),
+ {atomic, ok} = db:add(DB_data),
{ht:append(Tree, serialise(MTL)), % New tree.
NewSPT}; % New SPT.
Err -> {error, Err}