From fe5a771cb73a0010abecff181c2d462c68ad378a Mon Sep 17 00:00:00 2001 From: Vanessa Violet Date: Sat, 12 Aug 2023 06:59:13 +0000 Subject: [PATCH] Revert "badger mutex lock not needed because of ghost locker" This reverts commit ef383c80fdf46d2fde5fd549ad3ddab13d413dd9. --- storage/badger.go | 3 +++ storage/badger_deposit.go | 6 ++++++ storage/badger_graph.go | 3 +++ storage/badger_mint.go | 6 ++++++ storage/badger_utxo.go | 9 +++++++++ 5 files changed, 27 insertions(+) diff --git a/storage/badger.go b/storage/badger.go index 873a34484..5d3f9ec67 100644 --- a/storage/badger.go +++ b/storage/badger.go @@ -1,6 +1,7 @@ package storage import ( + "sync" "time" "github.com/MixinNetwork/mixin/config" @@ -13,6 +14,7 @@ type BadgerStore struct { custom *config.Custom snapshotsDB *badger.DB cacheDB *badger.DB + mutex *sync.RWMutex closing bool } @@ -29,6 +31,7 @@ func NewBadgerStore(custom *config.Custom, dir string) (*BadgerStore, error) { custom: custom, snapshotsDB: snapshotsDB, cacheDB: cacheDB, + mutex: new(sync.RWMutex), closing: false, }, nil } diff --git a/storage/badger_deposit.go b/storage/badger_deposit.go index da92d945d..777622572 100644 --- a/storage/badger_deposit.go +++ b/storage/badger_deposit.go @@ -11,6 +11,9 @@ import ( ) func (s *BadgerStore) CheckDepositInput(deposit *common.DepositData, tx crypto.Hash) error { + s.mutex.RLock() + defer s.mutex.RUnlock() + txn := s.snapshotsDB.NewTransaction(false) defer txn.Discard() @@ -27,6 +30,9 @@ func (s *BadgerStore) CheckDepositInput(deposit *common.DepositData, tx crypto.H } func (s *BadgerStore) LockDepositInput(deposit *common.DepositData, tx crypto.Hash, fork bool) error { + s.mutex.Lock() + defer s.mutex.Unlock() + return s.snapshotsDB.Update(func(txn *badger.Txn) error { ival, err := readDepositInput(txn, deposit) if err == badger.ErrKeyNotFound { diff --git a/storage/badger_graph.go b/storage/badger_graph.go index 24a113660..9889d00ad 100644 --- a/storage/badger_graph.go +++ b/storage/badger_graph.go @@ -97,6 +97,9 @@ func readSnapshotsForNodeRound(txn *badger.Txn, nodeId crypto.Hash, round uint64 func (s *BadgerStore) WriteSnapshot(snap *common.SnapshotWithTopologicalOrder, signers []crypto.Hash) error { logger.Debugf("BadgerStore.WriteSnapshot(%v)", snap.Snapshot) + s.mutex.Lock() + defer s.mutex.Unlock() + txn := s.snapshotsDB.NewTransaction(true) defer txn.Discard() diff --git a/storage/badger_mint.go b/storage/badger_mint.go index 0b7f0dc77..885958fdc 100644 --- a/storage/badger_mint.go +++ b/storage/badger_mint.go @@ -63,6 +63,9 @@ func (s *BadgerStore) ReadMintDistributions(offset, count uint64) ([]*common.Min } func (s *BadgerStore) ReadLastMintDistribution(batch uint64) (*common.MintDistribution, error) { + s.mutex.RLock() + defer s.mutex.RUnlock() + txn := s.snapshotsDB.NewTransaction(false) defer txn.Discard() @@ -100,6 +103,9 @@ func (s *BadgerStore) ReadLastMintDistribution(batch uint64) (*common.MintDistri } func (s *BadgerStore) LockMintInput(mint *common.MintData, tx crypto.Hash, fork bool) error { + s.mutex.Lock() + defer s.mutex.Unlock() + return s.snapshotsDB.Update(func(txn *badger.Txn) error { dist, err := readMintInput(txn, mint) if err == badger.ErrKeyNotFound { diff --git a/storage/badger_utxo.go b/storage/badger_utxo.go index 3c4b87a3f..93407b543 100644 --- a/storage/badger_utxo.go +++ b/storage/badger_utxo.go @@ -24,6 +24,9 @@ func (s *BadgerStore) ReadUTXOKeys(hash crypto.Hash, index int) (*common.UTXOKey } func (s *BadgerStore) ReadUTXOLock(hash crypto.Hash, index int) (*common.UTXOWithLock, error) { + s.mutex.RLock() + defer s.mutex.RUnlock() + txn := s.snapshotsDB.NewTransaction(false) defer txn.Discard() @@ -48,6 +51,9 @@ func (s *BadgerStore) readUTXOLock(txn *badger.Txn, hash crypto.Hash, index int) } func (s *BadgerStore) LockUTXOs(inputs []*common.Input, tx crypto.Hash, fork bool) error { + s.mutex.Lock() + defer s.mutex.Unlock() + return s.snapshotsDB.Update(func(txn *badger.Txn) error { for _, in := range inputs { err := lockUTXO(txn, in.Hash, in.Index, tx, fork) @@ -105,6 +111,9 @@ func (s *BadgerStore) ReadGhostKeyLock(key crypto.Key) (*crypto.Hash, error) { } func (s *BadgerStore) LockGhostKeys(keys []*crypto.Key, tx crypto.Hash, fork bool) error { + s.mutex.RLock() + defer s.mutex.RUnlock() + return s.snapshotsDB.Update(func(txn *badger.Txn) error { filter := make(map[crypto.Key]bool) for _, ghost := range keys {