Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
Add mutex locks to prevent db transaction conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
hazim-j committed Aug 3, 2023
1 parent 77da1eb commit 334dee6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/modules/paymaster/reputation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package paymaster

import (
"errors"
"sync"

mapset "github.com/deckarep/golang-set/v2"
"github.com/dgraph-io/badger/v3"
Expand All @@ -15,12 +16,14 @@ import (
// UserOperation.
type Reputation struct {
db *badger.DB
mu *sync.Mutex
}

// New returns an instance of a Reputation object to track and appropriately process userOps by paymaster
// status.
func New(db *badger.DB) *Reputation {
return &Reputation{db}
mu := &sync.Mutex{}
return &Reputation{db, mu}
}

// CheckStatus returns a UserOpHandler that is used by the Client to determine if the userOp is allowed based
Expand All @@ -37,7 +40,9 @@ func (r *Reputation) CheckStatus() modules.UserOpHandlerFunc {
return nil
}

r.mu.Lock()
status, err := getStatus(txn, paymaster)
r.mu.Unlock()
if err != nil {
return err
}
Expand All @@ -62,6 +67,8 @@ func (r *Reputation) IncOpsSeen() modules.UserOpHandlerFunc {
return nil
}

r.mu.Lock()
defer r.mu.Unlock()
return incrementOpsSeenByPaymaster(txn, paymaster)
})
}
Expand All @@ -87,6 +94,8 @@ func (r *Reputation) IncOpsIncluded() modules.BatchHandlerFunc {
}
}

r.mu.Lock()
defer r.mu.Unlock()
return incrementOpsIncludedByPaymasters(txn, c, ps.ToSlice()...)
})
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/modules/relay/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/big"
"net/http"
"sync"
"time"

"github.com/dgraph-io/badger/v3"
Expand Down Expand Up @@ -45,6 +46,7 @@ type Relayer struct {
bannedThreshold int
bannedTimeWindow time.Duration
waitTimeout time.Duration
mu *sync.Mutex
}

// New initializes a new EOA relayer for sending batches to the EntryPoint with IP throttling protection.
Expand All @@ -66,6 +68,7 @@ func New(
bannedThreshold: DefaultBanThreshold,
bannedTimeWindow: DefaultBanTimeWindow,
waitTimeout: DefaultWaitTimeout,
mu: &sync.Mutex{},
}
}

Expand Down Expand Up @@ -185,6 +188,8 @@ func (r *Relayer) MapUserOpHashToClientID() gin.HandlerFunc {
return err
}

r.mu.Lock()
defer r.mu.Unlock()
return incrementOpsSeenByClientID(txn, cid, r.bannedTimeWindow)
})
if err != nil {
Expand Down Expand Up @@ -260,6 +265,8 @@ func (r *Relayer) SendUserOperation() modules.BatchHandlerFunc {

hashes := getUserOpHashesFromOps(ctx.EntryPoint, ctx.ChainID, ctx.Batch...)
del = append([]string{}, hashes...)
r.mu.Lock()
defer r.mu.Unlock()
return incrementOpsIncludedByUserOpHashes(txn, r.bannedTimeWindow, hashes...)
})
if err != nil {
Expand Down

0 comments on commit 334dee6

Please sign in to comment.