From b101b2095fe406511559bac22e2d50ba5bea092e Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Fri, 12 Sep 2014 17:52:34 +0200 Subject: Add docu in comments, rename two external functions, add a larger test. --- src/ht.erl | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/ht.erl b/src/ht.erl index a51924d..8118274 100644 --- a/src/ht.erl +++ b/src/ht.erl @@ -23,7 +23,7 @@ -behaviour(gen_server). -export([size/0, add/1, tree_hash/0, tree_hash/1]). --export([get_incl/2, get_cons/2]). +-export([path/2, consistency/2]). -export([start_link/0, start_link/1, stop/0]). -export([init/1, handle_call/3, terminate/2, handle_cast/2, handle_info/2, code_change/3]). @@ -54,9 +54,9 @@ tree_hash() -> gen_server:call(?MODULE, tree_hash). tree_hash(Version) -> gen_server:call(?MODULE, {tree_hash, Version}). -get_incl(I, V) -> - gen_server:call(?MODULE, {inclusion, I, V}). -get_cons(V1, V2) -> +path(I, V) -> + gen_server:call(?MODULE, {path, I, V}). +consistency(V1, V2) -> gen_server:call(?MODULE, {consistency, V1, V2}). %% gen_server callbacks @@ -84,7 +84,7 @@ handle_call(tree_hash, _From, State) -> handle_call({tree_hash, Version}, _From, State) -> {NewState, Hash} = tree_hash(State, Version), {reply, Hash, NewState}; -handle_call({inclusion, _Index, _Version}, _From, State) -> +handle_call({path, _Index, _Version}, _From, State) -> {reply, nyi, State}; handle_call({consistency, _Version1, _Version2}, _From, State) -> {reply, nyi, State}. @@ -206,16 +206,22 @@ update_layer(Tree, Layer, ICur, ILast) -> update_parent(S, Layer, I, ILast) when I >= ILast -> %% We're done updating parents. If ILast is a left child, copy it %% to where its parent would've been were it a right child. This - %% is a "placeholder node" which simplifies when creating - %% incomplete ("non-frozen") trees. + %% is a "placeholder node" which simplifies creating incomplete + %% ("non-frozen") trees. case right_node_p(ILast) of true -> S; _ -> ts:append(S, Layer + 1, ts:retrieve_hash(S, {ILast, Layer})) end; update_parent(S, Layer, I, ILast) -> false = right_node_p(I), % ASSERTION - %% Make an inner node hash of I and sibling. Store it as + %% Make an inner node hash of I and its sibling. Store it as %% parent. Recurse with next pair of leaves. + %% TODO: This is the only place where we store rather than + %% append. Consider changing this to an append. This would make it + %% possible for ts to use other data structures for the storage + %% (like a binary per layer). If so, don't forget to truncate + %% Layer+1 before appending if there's already a parent for I + %% there. update_parent(ts:store(S, {parent(I), Layer + 1}, mkinnerhash(ts:retrieve_hash(S, {I, Layer}), ts:retrieve_hash(S, {I + 1, Layer}))), @@ -304,6 +310,15 @@ old_versions_test() -> tree_hash(X - 1)) end, random_entries(length(?TEST_VECTOR_LEAVES))). +old_versions_bigger_test() -> + LEAVES = [<> || X <- lists:seq(0, 512)], + test_init(LEAVES), + ?assertEqual(mth(LEAVES), tree_hash()), + lists:foreach( + fun(X) -> ?assertEqual(mth(lists:sublist(LEAVES, X)), + tree_hash(X - 1)) end, + random_entries(length(LEAVES))). + %%%%%%%%%%%%%%%%%%%% %% Testing the test helpers. mth_test() -> -- cgit v1.1