-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(btcstaking-tracker): fix race condition (#35)
- Fixes race condition where `*btcec.PrivateKey` is accessed from multiple go routines where read/write happen in `NewDecyptionKeyFromBTCSK` from pkg `github.com/babylonlabs-io/babylon/crypto/schnorr-adaptor-signature` - Add `-race` flag to detect race conditions early - Fixes [issue](#23) and references [issue](#22)
- Loading branch information
Showing
5 changed files
with
65 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/decred/dcrd/dcrec/secp256k1/v4" | ||
"sync" | ||
) | ||
|
||
// PrivateKeyWithMutex wraps a btcec.PrivateKey with a mutex to ensure thread-safe access. | ||
type PrivateKeyWithMutex struct { | ||
mu sync.Mutex | ||
key *secp256k1.PrivateKey | ||
} | ||
|
||
// NewPrivateKeyWithMutex creates a new PrivateKeyWithMutex. | ||
func NewPrivateKeyWithMutex(key *secp256k1.PrivateKey) *PrivateKeyWithMutex { | ||
return &PrivateKeyWithMutex{ | ||
key: key, | ||
} | ||
} | ||
|
||
// GetKey safely retrieves the private key. | ||
func (p *PrivateKeyWithMutex) GetKey() *secp256k1.PrivateKey { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
return p.key | ||
} | ||
|
||
// UseKey performs an operation with the private key in a thread-safe manner. | ||
func (p *PrivateKeyWithMutex) UseKey(operation func(key *secp256k1.PrivateKey)) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
operation(p.key) | ||
} |