Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass mutex instead of reading it again from the map #114

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading