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