Skip to content

Commit

Permalink
pr comment, wrap pk
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Sep 10, 2024
1 parent 0cd6256 commit f4c1fad
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
22 changes: 12 additions & 10 deletions btcstaking-tracker/btcslasher/slasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package btcslasher
import (
"context"
"fmt"
"github.com/babylonlabs-io/vigilante/types"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"sync"
"time"

Expand Down Expand Up @@ -233,8 +235,8 @@ func (bs *BTCSlasher) equivocationTracker() {
// SlashFinalityProvider slashes all BTC delegations under a given finality provider
// the checkBTC option indicates whether to check the slashing tx's input is still spendable
// on Bitcoin (including mempool txs).
func (bs *BTCSlasher) SlashFinalityProvider(extractedfpBTCSK *btcec.PrivateKey) error {
fpBTCPK := bbn.NewBIP340PubKeyFromBTCPK(extractedfpBTCSK.PubKey())
func (bs *BTCSlasher) SlashFinalityProvider(extractedFpBTCSK *btcec.PrivateKey) error {
fpBTCPK := bbn.NewBIP340PubKeyFromBTCPK(extractedFpBTCSK.PubKey())
bs.logger.Infof("start slashing finality provider %s", fpBTCPK.MarshalHex())

// get all active and unbonded BTC delegations at the current BTC height
Expand All @@ -246,8 +248,8 @@ func (bs *BTCSlasher) SlashFinalityProvider(extractedfpBTCSK *btcec.PrivateKey)
return fmt.Errorf("failed to get BTC delegations under finality provider %s: %w", fpBTCPK.MarshalHex(), err)
}

// Initialize a mutex to protect access to extractedfpBTCSK *btcec.PrivateKey
var mu sync.Mutex
// Initialize a mutex protected *btcec.PrivateKey
safeExtractedFpBTCSK := types.NewPrivateKeyWithMutex(extractedFpBTCSK)

// try to slash both staking and unbonding txs for each BTC delegation
// sign and submit slashing tx for each active delegation
Expand All @@ -256,9 +258,9 @@ func (bs *BTCSlasher) SlashFinalityProvider(extractedfpBTCSK *btcec.PrivateKey)
bs.wg.Add(1)
go func(d *bstypes.BTCDelegationResponse) {
defer bs.wg.Done()
mu.Lock()
defer mu.Unlock()
bs.slashBTCDelegation(fpBTCPK, extractedfpBTCSK, d)
safeExtractedFpBTCSK.UseKey(func(key *secp256k1.PrivateKey) {
bs.slashBTCDelegation(fpBTCPK, key, d)
})
}(del)
}
// sign and submit slashing tx for each unbonded delegation
Expand All @@ -267,9 +269,9 @@ func (bs *BTCSlasher) SlashFinalityProvider(extractedfpBTCSK *btcec.PrivateKey)
bs.wg.Add(1)
go func(d *bstypes.BTCDelegationResponse) {
defer bs.wg.Done()
mu.Lock()
defer mu.Unlock()
bs.slashBTCDelegation(fpBTCPK, extractedfpBTCSK, d)
safeExtractedFpBTCSK.UseKey(func(key *secp256k1.PrivateKey) {
bs.slashBTCDelegation(fpBTCPK, key, d)
})
}(del)
}

Expand Down
33 changes: 33 additions & 0 deletions types/safeprivatekey.go
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)
}

0 comments on commit f4c1fad

Please sign in to comment.