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: support phase1 unbonding #23

Merged
merged 35 commits into from
Dec 13, 2024
Merged

Conversation

gusin13
Copy link
Contributor

@gusin13 gusin13 commented Dec 10, 2024

This pr adds phase1 unbonding support in expiry service

https://babylonlabs.atlassian.net/wiki/spaces/BABYLON/pages/86900834/RFC-004+Support+unbonding+of+phase-1+delegation

Fixes -
#19
#18
babylonlabs-io/staking-api-service#177

The following diagram explains the code flow

Screenshot 2024-12-12 at 12 48 39 PM

Simple explanation -

service, on start up spins up expiry poller and btcsubscriber poller
also starts go routine to handle unbonding delegation and withdrawn delegation

the expirty poller will keep looking for expired delegations and mark them as "unbonded" in the db

the btc subscriber poller will keep looking for "unbonded" and "unbonding requested" delegations and register for spend notifications. it also has a internal in memory store where it will keep track of the subscriptions it already made so it doesn't create duplicated subscriptions (the lightenening node library doesn't handle duplications, already discussed offline)

once the spend notifications are received the logic would push the delegations to "unbonding" channel or "withdrawn" channel

the channel handlers will pick up the event from channel,
unbonding channel handler when unbonding is received it marks the delegation as "unbonding" and also creates a new entry in the timelock expiry table so the expiry checker can monitor this and mark as "unbonded" when timelock expires

the withdrawn channel handler will pick up the event from channel and simply mark the delegation as withdrawn

@gusin13 gusin13 marked this pull request as ready for review December 12, 2024 06:57
}
}

func (s *Service) handleSpendingStakingTransaction(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gitferry would appreciate if can you review this logic, its taken from phase1 indexer.

defer s.wg.Done()
for {
select {
case event := <-s.withdrawnDelegationChan:
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm.... could you point out where we pushing event into this chan? or if it will be handled in a seperate PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

// Iterate the list in reverse (i.e. decreasing ActivationHeight)
// and identify the first element that has an activation height below
// the specified BTC height.
for i := len(s.params.Versions) - 1; i >= 0; i-- {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: let's sort the array first. just to be 100% sure it's in the right order for us to loop through

Copy link
Contributor Author

@gusin13 gusin13 Dec 13, 2024

Choose a reason for hiding this comment

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

len(s.params.Versions) -1 assures we are iterating in reverse order?
phase 1 api does it same ref
phase 1 indexer as well ref

@gusin13 gusin13 merged commit 651007c into main Dec 13, 2024
11 checks passed
utils.PushOrQuit(s.unbondingDelegationChan, unbondingEvent, s.quit)

// Register unbonding spend notification
return s.registerUnbondingSpendNotification(delegation)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should pass delegation but we should use unbondingTxHex instead. If I remember correctly the unbonding tx of the delegation is filled when the phase-1 indexer observes it. So it will be empty here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, you are right this seems to be a bug.
I have logged a ticket, will fix in a separate pr soon
#24

tysm 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants