Skip to content

Commit

Permalink
feat: add opcc slashing event (#139)
Browse files Browse the repository at this point in the history
## Summary

This is a PR to update the BTC Slasher program to support and handle OP
stack consumer chain slashing events.

It accompanies
babylonlabs-io/babylon-contract#92.

## Test plan

Before running integration tests, I needed to set `DOCKER_HOST`:
```bash
export DOCKER_HOST="unix:///Users/parkyeung/.docker/run/docker.sock"
```

To run tests:

```bash
# unit tests
make test

# integration tests
make test-e2e
```
  • Loading branch information
parketh authored Dec 12, 2024
1 parent 242fd29 commit b6e3b69
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Improvements

* [#139](https://github.com/babylonlabs-io/vigilante/pull/139) add opcc slashing event
* [#136](https://github.com/babylonlabs-io/vigilante/pull/136) rate limit activations
* [#141](https://github.com/babylonlabs-io/vigilante/pull/141) decrement tracked delegations in atomic slasher
* [#143](https://github.com/babylonlabs-io/vigilante/pull/143) adds nlreturn linter rule
Expand Down
66 changes: 43 additions & 23 deletions btcstaking-tracker/btcslasher/slasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
)

const (
txSubscriberName = "tx-subscriber"
messageActionName = "/babylon.finality.v1.MsgAddFinalitySig"
evidenceEventName = "babylon.finality.v1.EventSlashedFinalityProvider.evidence"
txSubscriberName = "tx-subscriber"
messageActionName = "/babylon.finality.v1.MsgAddFinalitySig"
consumerMessageActionName = "/babylon.finality.v1.MsgEquivocationEvidence"
evidenceEventName = "babylon.finality.v1.EventSlashedFinalityProvider.evidence"
)

type BTCSlasher struct {
Expand All @@ -43,6 +44,8 @@ type BTCSlasher struct {
// channel for finality signature messages, which might include
// equivocation evidences
finalitySigChan <-chan coretypes.ResultEvent
// channel for consumer fp equivocation evidences
equivocationEvidenceChan <-chan coretypes.ResultEvent
// channel for SKs of slashed finality providers
slashedFPSKChan chan *btcec.PrivateKey
// channel for receiving the slash result of each BTC delegation
Expand Down Expand Up @@ -133,10 +136,18 @@ func (bs *BTCSlasher) Start() error {
// start the subscriber to slashing events
// NOTE: at this point monitor has already started the Babylon querier routine
queryName := fmt.Sprintf("tm.event = 'Tx' AND message.action='%s'", messageActionName)
// subscribe to babylon fp slashing events
bs.finalitySigChan, startErr = bs.BBNQuerier.Subscribe(txSubscriberName, queryName)
if startErr != nil {
return
}
// subscribe to consumer fp slashing events
queryName = fmt.Sprintf("tm.event = 'Tx' AND message.action='%s'", consumerMessageActionName)
bs.equivocationEvidenceChan, startErr = bs.BBNQuerier.Subscribe(txSubscriberName, queryName)
if startErr != nil {
return
}

// BTC slasher has started
bs.logger.Debugf("slasher routine has started subscribing %s", queryName)

Expand Down Expand Up @@ -203,6 +214,32 @@ func (bs *BTCSlasher) slashingEnforcer() {
}
}

func (bs *BTCSlasher) handleEvidence(evt *coretypes.ResultEvent, isConsumer bool) {
evidence := filterEvidence(evt)

if evidence == nil {
return
}

fpBTCPKHex := evidence.FpBtcPk.MarshalHex()
fpType := "babylon"
if isConsumer {
fpType = "consumer"
}
bs.logger.Infof("new equivocating %s finality provider %s to be slashed", fpType, fpBTCPKHex)
bs.logger.Debugf("found equivocation evidence of %s finality provider %s: %v", fpType, fpBTCPKHex, evidence)

// extract the SK of the slashed finality provider
fpBTCSK, err := evidence.ExtractBTCSK()
if err != nil {
bs.logger.Errorf("failed to extract BTC SK of the slashed %s finality provider %s: %v", fpType, fpBTCPKHex, err)

return
}

bs.slashedFPSKChan <- fpBTCSK
}

// equivocationTracker is a routine to track the equivocation events on Babylon,
// extract equivocating finality providers' SKs, and sen to slashing enforcer
// routine
Expand All @@ -219,26 +256,9 @@ func (bs *BTCSlasher) equivocationTracker() {

return
case resultEvent := <-bs.finalitySigChan:
evidence := filterEvidence(&resultEvent)

if evidence == nil {
// this event does not contain equivocation evidence, skip
continue
}

fpBTCPKHex := evidence.FpBtcPk.MarshalHex()
bs.logger.Infof("new equivocating finality provider %s to be slashed", fpBTCPKHex)
bs.logger.Debugf("found equivocation evidence of finality provider %s: %v", fpBTCPKHex, evidence)

// extract the SK of the slashed finality provider
fpBTCSK, err := evidence.ExtractBTCSK()
if err != nil {
bs.logger.Errorf("failed to extract BTC SK of the slashed finality provider %s: %v", fpBTCPKHex, err)

continue
}

bs.slashedFPSKChan <- fpBTCSK
bs.handleEvidence(&resultEvent, false)
case resultEvent := <-bs.equivocationEvidenceChan:
bs.handleEvidence(&resultEvent, true)
}
}
}
Expand Down

0 comments on commit b6e3b69

Please sign in to comment.