diff options
Diffstat (limited to 'shard/lib/app/chat.ex')
-rw-r--r-- | shard/lib/app/chat.ex | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex index 52ad920..054d7f7 100644 --- a/shard/lib/app/chat.ex +++ b/shard/lib/app/chat.ex @@ -175,12 +175,12 @@ defmodule SApp.Chat do {pk, bin, sign} = msgitem if Shard.Keys.verify(pk, bin, sign) == :ok do if prev_root == state.mst.root do + # Only one new message, insert it directly mst2 = MST.insert(state.mst, msgitem) if mst2.root == new_root do - # This was the only message missing, we are happy! state = %{state | mst: mst2} - Shard.Manager.save_state(state.id, mst2.root) GenServer.cast(state.page_store, {:set_roots, [mst2.root]}) + Shard.Manager.save_state(state.id, mst2.root) msg_callback(state, msgitem) state else @@ -188,9 +188,11 @@ defmodule SApp.Chat do state end else + # Not a simple one-insertion transition, look at the whole tree init_merge(state, new_root, peer_id) end else + Logger.warn("Received message with invalid signature") state end end @@ -222,17 +224,19 @@ defmodule SApp.Chat do mgmst = put_in(mgmst.store.prefer_ask, [source_peer]) mst = MST.merge(state.mst, mgmst) - correct = for {x, true} <- MST.last(mst, nil, 100) do - if not MapSet.member? prev_last, x do - msg_callback(state, x) - {pk, bin, sign} = x - Shard.Keys.verify(pk, bin, sign) - else - true - end + new = for {x, true} <- MST.last(mst, nil, 100), + not MapSet.member?(prev_last, x) + do x end + + correct = for x <- new do + {pk, bin, sign} = x + Shard.Keys.verify(pk, bin, sign) end if Enum.all? correct do + for x <- new do + msg_callback(state, x) + end GenServer.cast(state.page_store, {:set_roots, [mst.root]}) Shard.Manager.save_state(state.id, mst.root) %{state | mst: mst} |