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(btcstaking-tracker): fix race condition #35

Merged
merged 6 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ $(BUILDDIR)/:
mkdir -p $(BUILDDIR)/

test:
go test ./...
go test -race ./...

test-e2e:
cd $(TOOLS_DIR); go install -trimpath $(BABYLON_PKG);
go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e
go test -race -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e

build-docker:
$(DOCKER) build --tag babylonlabs-io/vigilante -f Dockerfile \
Expand Down
7 changes: 7 additions & 0 deletions btcstaking-tracker/btcslasher/slasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,18 @@ 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
Lazar955 marked this conversation as resolved.
Show resolved Hide resolved

// try to slash both staking and unbonding txs for each BTC delegation
// sign and submit slashing tx for each active delegation
// TODO: use semaphore to prevent spamming BTC node
for _, del := range activeBTCDels {
bs.wg.Add(1)
go func(d *bstypes.BTCDelegationResponse) {
defer bs.wg.Done()
mu.Lock()
defer mu.Unlock()
bs.slashBTCDelegation(fpBTCPK, extractedfpBTCSK, d)
}(del)
}
Expand All @@ -262,6 +267,8 @@ 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)
}(del)
}
Expand Down
6 changes: 6 additions & 0 deletions btcstaking-tracker/btcslasher/slasher_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ func findFPIdxInWitness(fpBTCPK *bbn.BIP340PubKey, fpBtcPkList []bbn.BIP340PubKe
return 0, fmt.Errorf("the given finality provider's PK is not found in the BTC delegation")
}

// BuildSlashingTxWithWitness constructs a Bitcoin slashing transaction with the required witness data
// using the provided finality provider's private key. It handles the conversion and validation of
// various parameters needed for slashing a Bitcoin delegation, including the staking transaction,
// finality provider public keys, and covenant public keys.
// Note: this function is UNSAFE for concurrent accesses as slashTx.BuildSlashingTxWithWitness is not safe for
// concurrent access inside it's calling asig.NewDecyptionKeyFromBTCSK
func BuildSlashingTxWithWitness(
d *bstypes.BTCDelegationResponse,
bsParams *bstypes.Params,
Expand Down
Loading