From d759e857355c41be33b03878abfd53df45d43b30 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 22 Apr 2018 18:56:58 +0200 Subject: [PATCH] [client.keyset] added 'Rescue()' method to keyset --- .../schastsp/client/keyset/keyset.go | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/git.xdrm.io/schastsp/client/keyset/keyset.go b/src/git.xdrm.io/schastsp/client/keyset/keyset.go index dcc1b53..ed382ab 100644 --- a/src/git.xdrm.io/schastsp/client/keyset/keyset.go +++ b/src/git.xdrm.io/schastsp/client/keyset/keyset.go @@ -206,3 +206,45 @@ func (s T) MigrationCode(optional... uint8) uint8 { return s.mcode } + + + +/* (7) Updates depth for rescuing from desynchroisation +* +* @lastHash<[]byte> Last received hash +* +* @return error Error or NIL if not +* +* @description We must update @depth so the next sent hash h_depth(k) +* hashed MIN times equals the received hash @lastHash +* We must find depth = i-MIN+1 +* with i so that h_MIN( h_i(k) ) = lastHash +* +---------------------------------------------------------*/ +func (s *T) Rescue(lastHash []byte) error { + + /* (1) Browse possible values + ---------------------------------------------------------*/ + for i := s.depth ; i <= s.depth+s.ctx.MinDepth() ; i++ { + + /* (1) Process hash */ + currentHash, err := s.Hash(); + if err != nil { return err } + + /* (2) If not found -> try again */ + if string(currentHash) != string(lastHash) { continue } + + /* (3) Store new depth */ + s.depth = i - s.ctx.MinDepth() + 1 + + /* (4) Update migration code */ + s.mcode = 2 + + return nil + + } + + return errors.New("Cannot find an available rescue depth"); + + +} \ No newline at end of file