@@ -182,41 +182,42 @@ func getSessionKey(session *Session) []byte {
182182 return session .LocalPublicKey .SerializeCompressed ()
183183}
184184
185- // NewSession creates a new session with the given user-defined parameters.
186- //
187- // NOTE: currently this purely a constructor of the Session type and does not
188- // make any database calls. This will be changed in a future commit.
185+ // NewSession creates and persists a new session with the given user-defined
186+ // parameters. The initial state of the session will be Reserved until
187+ // ShiftState is called with StateCreated.
189188//
190189// NOTE: this is part of the Store interface.
191- func (db * BoltStore ) NewSession (id ID , localPrivKey * btcec.PrivateKey ,
192- label string , typ Type , expiry time.Time , serverAddr string ,
193- devServer bool , perms []bakery.Op , caveats []macaroon.Caveat ,
194- featureConfig FeaturesConfig , privacy bool , linkedGroupID * ID ,
195- flags PrivacyFlags ) (* Session , error ) {
196-
197- return buildSession (
198- id , localPrivKey , label , typ , db .clock .Now (), expiry ,
199- serverAddr , devServer , perms , caveats , featureConfig , privacy ,
200- linkedGroupID , flags ,
201- )
202- }
190+ func (db * BoltStore ) NewSession (label string , typ Type , expiry time.Time ,
191+ serverAddr string , devServer bool , perms []bakery.Op ,
192+ caveats []macaroon.Caveat , featureConfig FeaturesConfig , privacy bool ,
193+ linkedGroupID * ID , flags PrivacyFlags ) (* Session , error ) {
203194
204- // CreateSession adds a new session to the store. If a session with the same
205- // local public key already exists an error is returned.
206- //
207- // NOTE: this is part of the Store interface.
208- func (db * BoltStore ) CreateSession (session * Session ) error {
209- sessionKey := getSessionKey (session )
210-
211- return db .Update (func (tx * bbolt.Tx ) error {
195+ var session * Session
196+ err := db .Update (func (tx * bbolt.Tx ) error {
212197 sessionBucket , err := getBucket (tx , sessionBucketKey )
213198 if err != nil {
214199 return err
215200 }
216201
202+ id , localPrivKey , err := getUnusedIDAndKeyPair (sessionBucket )
203+ if err != nil {
204+ return err
205+ }
206+
207+ session , err = buildSession (
208+ id , localPrivKey , label , typ , db .clock .Now (), expiry ,
209+ serverAddr , devServer , perms , caveats , featureConfig ,
210+ privacy , linkedGroupID , flags ,
211+ )
212+ if err != nil {
213+ return err
214+ }
215+
216+ sessionKey := getSessionKey (session )
217+
217218 if len (sessionBucket .Get (sessionKey )) != 0 {
218- return fmt .Errorf ("session with local public " +
219- "key(%x) already exists" ,
219+ return fmt .Errorf ("session with local public key(%x) " +
220+ "already exists" ,
220221 session .LocalPublicKey .SerializeCompressed ())
221222 }
222223
@@ -275,6 +276,11 @@ func (db *BoltStore) CreateSession(session *Session) error {
275276
276277 return putSession (sessionBucket , session )
277278 })
279+ if err != nil {
280+ return nil , err
281+ }
282+
283+ return session , nil
278284}
279285
280286// UpdateSessionRemotePubKey can be used to add the given remote pub key
@@ -577,53 +583,35 @@ func (db *BoltStore) GetSessionByID(id ID) (*Session, error) {
577583 return session , nil
578584}
579585
580- // GetUnusedIDAndKeyPair can be used to generate a new, unused, local private
586+ // getUnusedIDAndKeyPair can be used to generate a new, unused, local private
581587// key and session ID pair. Care must be taken to ensure that no other thread
582588// calls this before the returned ID and key pair from this method are either
583589// used or discarded.
584- //
585- // NOTE: this is part of the Store interface.
586- func (db * BoltStore ) GetUnusedIDAndKeyPair () (ID , * btcec.PrivateKey , error ) {
587- var (
588- id ID
589- privKey * btcec.PrivateKey
590- )
591- err := db .Update (func (tx * bbolt.Tx ) error {
592- sessionBucket , err := getBucket (tx , sessionBucketKey )
593- if err != nil {
594- return err
595- }
596-
597- idIndexBkt := sessionBucket .Bucket (idIndexKey )
598- if idIndexBkt == nil {
599- return ErrDBInitErr
600- }
590+ func getUnusedIDAndKeyPair (bucket * bbolt.Bucket ) (ID , * btcec.PrivateKey ,
591+ error ) {
601592
602- // Spin until we find a key with an ID that does not collide
603- // with any of our existing IDs.
604- for {
605- // Generate a new private key and ID pair.
606- privKey , id , err = NewSessionPrivKeyAndID ()
607- if err != nil {
608- return err
609- }
593+ idIndexBkt := bucket .Bucket (idIndexKey )
594+ if idIndexBkt == nil {
595+ return ID {}, nil , ErrDBInitErr
596+ }
610597
611- // Check that no such ID exits in our id-to-key index.
612- idBkt := idIndexBkt .Bucket (id [:])
613- if idBkt != nil {
614- continue
615- }
598+ // Spin until we find a key with an ID that does not collide with any of
599+ // our existing IDs.
600+ for {
601+ // Generate a new private key and ID pair.
602+ privKey , id , err := NewSessionPrivKeyAndID ()
603+ if err != nil {
604+ return ID {}, nil , err
605+ }
616606
617- break
607+ // Check that no such ID exits in our id-to-key index.
608+ idBkt := idIndexBkt .Bucket (id [:])
609+ if idBkt != nil {
610+ continue
618611 }
619612
620- return nil
621- })
622- if err != nil {
623- return id , nil , err
613+ return id , privKey , nil
624614 }
625-
626- return id , privKey , nil
627615}
628616
629617// GetGroupID will return the group ID for the given session ID.
0 commit comments