Skip to content

Commit

Permalink
Revert "badger mutex lock not needed because of ghost locker"
Browse files Browse the repository at this point in the history
This reverts commit ef383c8.
  • Loading branch information
vanessaviolet committed Aug 12, 2023
1 parent df54793 commit fe5a771
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions storage/badger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"sync"
"time"

"github.com/MixinNetwork/mixin/config"
Expand All @@ -13,6 +14,7 @@ type BadgerStore struct {
custom *config.Custom
snapshotsDB *badger.DB
cacheDB *badger.DB
mutex *sync.RWMutex
closing bool
}

Expand All @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions storage/badger_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions storage/badger_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 6 additions & 0 deletions storage/badger_mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions storage/badger_utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fe5a771

Please sign in to comment.