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

feat: add opcc slashing event #139

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

* [#135](https://github.com/babylonlabs-io/vigilante/pull/135) add opcc slashing event

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be #139

## v0.18.0

### Improvements
Expand Down
64 changes: 42 additions & 22 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 @@ -132,10 +135,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
}
SebastianElvis marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down Expand Up @@ -200,6 +211,31 @@ 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 @@ -215,25 +251,9 @@ func (bs *BTCSlasher) equivocationTracker() {
bs.logger.Debug("handle delegations loop quit")
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
Loading