diff options
-rw-r--r-- | shard/lib/app/chat.ex | 24 | ||||
-rw-r--r-- | shard/lib/net/chan.ex | 9 |
2 files changed, 19 insertions, 14 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} diff --git a/shard/lib/net/chan.ex b/shard/lib/net/chan.ex index aa5c186..5aba960 100644 --- a/shard/lib/net/chan.ex +++ b/shard/lib/net/chan.ex @@ -14,7 +14,7 @@ defprotocol SNet.Chan do def handle(chan, func) end -def SNet.PeerChan do +defmodule SNet.PeerChan do @moduledoc""" Direct channel to a peer """ @@ -32,12 +32,13 @@ def SNet.PeerChan do def handle(chan, func) do # DO NOT USE THIS - assert false + raise :do_not_use_this + # assert false end end end -def SNet.FloodChan do +defmodule SNet.FloodChan do @moduledoc""" Channel that send a message to all know peers for shard (floods the network) """ @@ -61,6 +62,6 @@ def SNet.FloodChan do end end -def SNet.CipherChan do +defmodule SNet.CipherChan do # TODO end |