[client.keyset] added 'Rescue()' method to keyset

This commit is contained in:
xdrm-brackets 2018-04-22 18:56:58 +02:00
parent 3b282e397b
commit d759e85735
1 changed files with 42 additions and 0 deletions

View File

@ -206,3 +206,45 @@ func (s T) MigrationCode(optional... uint8) uint8 {
return s.mcode return s.mcode
} }
/* (7) Updates depth for rescuing from desynchroisation
*
* @lastHash<[]byte> Last received hash
*
* @return error<err> 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");
}