package mtproto

import (
	
	

	
	

	
)

func ( *Conn) ( int64) {
	.sessionMux.Lock()
	// Copy to log.
	 := .salt
	.salt = 
	.sessionMux.Unlock()

	if  !=  {
		.log.Info(context.Background(), "Salt updated", log.Int64("old", ), log.Int64("new", ))
		.traceEvent(WireEvent{Type: WireSaltChange, Old: , New: })
	}
}

func ( *Conn) () {
	,  := .salts.Get(.clock.Now().Add(time.Minute * 5))
	if ! {
		return
	}

	.storeSalt()
}

const defaultSaltsNum = 4

func ( *Conn) ( context.Context) error {
	 := &mt.GetFutureSaltsRequest{
		Num: defaultSaltsNum,
	}
	,  := context.WithTimeout(, .getTimeout(.TypeID()))
	defer ()

	if  := .writeServiceMessage(, );  != nil {
		return errors.Wrap(, "request salts")
	}

	return nil
}

// saltLoop fetches future salts every saltFetchInterval. It runs only if
// the interval is set.
//
// Nothing is fetched at start: Telegram Desktop never sends
// get_future_salts, it uses the salt of new_session_created and the one
// bad_server_salt brings when the current salt expires.
func ( *Conn) ( context.Context) error {
	select {
	case <-.gotSession.Ready():
	case <-.Done():
		return .Err()
	}

	 := .clock.Ticker(.saltFetchInterval)
	defer .Stop()
	for {
		select {
		case <-.C():
			if  := .getSalts();  != nil {
				return 
			}
		case <-.Done():
			return .Err()
		}
	}
}