github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/consumer/migration/migrator_storage.go (about)

     1  /*
     2   * Copyright (C) 2022 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package migration
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  
    24  	storm "github.com/asdine/storm/v3"
    25  	"github.com/mysteriumnetwork/node/identity/registry"
    26  	"github.com/rs/zerolog/log"
    27  )
    28  
    29  const hermesMigrationBucketName = "hermes_migration"
    30  const hermesMigrationFinishedKey = "migration_finished"
    31  
    32  // Storage keeps track of migration progress
    33  type Storage struct {
    34  	db respository
    35  	ap registry.AddressProvider
    36  }
    37  
    38  type respository interface {
    39  	SetValue(bucket string, key interface{}, to interface{}) error
    40  	GetValue(bucket string, key interface{}, to interface{}) error
    41  }
    42  
    43  // NewStorage builds and returns a new storage object.
    44  func NewStorage(db respository, ap registry.AddressProvider) *Storage {
    45  	return &Storage{
    46  		db: db,
    47  		ap: ap,
    48  	}
    49  }
    50  
    51  // MarkAsMigrated set migration flag do not try to migrate again
    52  func (s *Storage) MarkAsMigrated(chainID int64, identity string) {
    53  	activeHermes, err := s.ap.GetActiveHermes(chainID)
    54  	if err != nil {
    55  		log.Err(err).Msg("No active hermes set, will skip marking migration status")
    56  		return
    57  	}
    58  
    59  	err = s.db.SetValue(hermesMigrationBucketName, s.getMigrationKey(activeHermes.Hex(), identity), true)
    60  	if err != nil {
    61  		log.Warn().Err(err).Msg("Could not save migration state to local db")
    62  	}
    63  }
    64  
    65  func (s *Storage) isMigrationRequired(chainID int64, identity string) bool {
    66  	activeHermes, err := s.ap.GetActiveHermes(chainID)
    67  	if err != nil {
    68  		log.Err(err).Msg("No active hermes set, will assume required")
    69  		return true
    70  	}
    71  
    72  	var finished bool
    73  	err = s.db.GetValue(hermesMigrationBucketName, s.getMigrationKey(activeHermes.Hex(), identity), &finished)
    74  	if err != nil {
    75  		if !errors.Is(err, storm.ErrNotFound) {
    76  			log.Warn().Err(err).Msg("Could not get migration state from local db")
    77  		}
    78  		return true
    79  	}
    80  
    81  	return !finished
    82  }
    83  
    84  func (s *Storage) getMigrationKey(hermesId, id string) string {
    85  	return fmt.Sprintf("%s_%s_%s", hermesMigrationFinishedKey, hermesId, id)
    86  }