Skip to content

Commit

Permalink
fix to pass mut in stead of reading it again (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored Nov 16, 2024
1 parent f07f8ac commit aef4430
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions jsonrpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ type AccMut struct {

// acquireAccMut acquires the mutex for the account with the given senderHex
// and increments the reference count. If the mutex does not exist, it is created.
func (b *JSONRPCBackend) acquireAccMut(senderHex string) {
func (b *JSONRPCBackend) acquireAccMut(senderHex string) *AccMut {
// critical section for rc and create
b.mut.Lock()
accMut, ok := b.accMuts[senderHex]
Expand All @@ -205,13 +205,13 @@ func (b *JSONRPCBackend) acquireAccMut(senderHex string) {
// critical section end

accMut.mut.Lock()
return accMut
}

// releaseAccMut releases the mutex for the account with the given senderHex
// and decrements the reference count. If the reference count reaches zero,
// the mutex is deleted.
func (b *JSONRPCBackend) releaseAccMut(senderHex string) {
accMut := b.accMuts[senderHex]
func (b *JSONRPCBackend) releaseAccMut(senderHex string, accMut *AccMut) {
accMut.mut.Unlock()

// critical section for rc and delete
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/backend/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func (b *JSONRPCBackend) SendTx(tx *coretypes.Transaction) error {
senderHex := hexutil.Encode(sender.Bytes())

// hold mutex for each sender
b.acquireAccMut(senderHex)
defer b.releaseAccMut(senderHex)
accMut := b.acquireAccMut(senderHex)
defer b.releaseAccMut(senderHex, accMut)

checkCtx := b.app.GetContextForCheckTx(nil)
if acc := b.app.AccountKeeper.GetAccount(checkCtx, sender); acc != nil {
Expand Down

0 comments on commit aef4430

Please sign in to comment.