Skip to content

Commit

Permalink
Merge pull request #101 from blinklabs-io/feat/db-config-fingerprint
Browse files Browse the repository at this point in the history
feat: store/compare config fingerprint in DB
  • Loading branch information
agaffney authored Dec 23, 2023
2 parents c1570bd + d9176cb commit 6ce5457
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

const (
chainsyncCursorKey = "chainsync_cursor"
fingerprintKey = "config_fingerprint"
)

type State struct {
Expand All @@ -39,6 +40,45 @@ func (s *State) Load() error {
}
s.db = db
//defer db.Close()
if err := s.compareFingerprint(); err != nil {
return err
}
return nil
}

func (s *State) compareFingerprint() error {
cfg := config.GetConfig()
fingerprint := fmt.Sprintf(
"network=%s,network-magic=%d",
cfg.Indexer.Network,
cfg.Indexer.NetworkMagic,
)
err := s.db.Update(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(fingerprintKey))
if err != nil {
if err == badger.ErrKeyNotFound {
if err := txn.Set([]byte(fingerprintKey), []byte(fingerprint)); err != nil {
return err
}
return nil
} else {
return err
}
}
err = item.Value(func(v []byte) error {
if string(v) != fingerprint {
return fmt.Errorf("config fingerprint in DB doesn't match current config: %s", v)
}
return nil
})
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit 6ce5457

Please sign in to comment.