From 8fee4bcdb9edf4d4b9c7798f2bee7d521ea2b485 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 27 Sep 2018 09:33:44 +0200 Subject: I'm stupid --- shard/lib/data/signrev.ex | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 shard/lib/data/signrev.ex (limited to 'shard/lib') diff --git a/shard/lib/data/signrev.ex b/shard/lib/data/signrev.ex new file mode 100644 index 0000000..6360b53 --- /dev/null +++ b/shard/lib/data/signrev.ex @@ -0,0 +1,74 @@ +defmodule SData.SignRev do + @moduledoc""" + Implement a simple signed object with a revision number. + """ + + + defstruct [:term, :rev, :signed] + + @doc""" + New SignRev with an unsigned initial state (use this if initial state is deterministic/not signed). + """ + def new(init_term) do + %__MODULE__{term: init_term, rev: 0, signed: nil} + end + + @doc""" + New SignRev with a signed initial state. + """ + def new(init_term, pk) do + bin = SData.term_bin {0, init_term} + case Shard.Keys.sign(pk, bin) do + {:ok, signed} -> + {:ok, %__MODULE__{term: init_term, rev: 0, signed: signed}} + err -> err + end + end + + @doc""" + Get the current value of the SignRev. + """ + def get(sr) do + sr.term + end + + @doc""" + Update the SignRev with a new value and sign it with this pk + """ + def set(sr, new_term, pk) do + rev = sr.rev + 1 + bin = SData.term_bin {rev, new_term} + case Shard.Keys.sign(pk, bin) do + {:ok, signed} -> + {:ok, %__MODULE__{term: new_term, rev: rev, signed: signed}} + err -> err + end + end + + @doc""" + Get the signed binary for the SignRev. Just send the output of this + to a peer for him to update. + """ + def signed(sr) do + sr.signed + end + + @doc""" + Check that a signed binary is correct and merge it into the SignRev. + + Returns {true, new_sr} if an update happenned, {false, sr} otherwise. + """ + def merge(sr, signed, pk) do + case Shard.Keys.open(pk, signed) do + {:ok, bin} -> + {rev, new_term} = SData.term_unbin bin + if rev > sr.rev do + {true, %__MODULE__{term: new_term, rev: rev, signed: signed}} + else + {false, sr} + end + _ -> + {false, sr} + end + end +end -- cgit v1.2.3