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: unbonding state transitions #27

Merged
merged 15 commits into from
Oct 23, 2024
Merged

Conversation

gusin13
Copy link
Collaborator

@gusin13 gusin13 commented Oct 22, 2024

This PR

Sets up internal expiry checker service which reads the timelock_queue table and mark the delegations as Withdrawable

Note -
ran into this issue - ref tl;dr - this pr only marks delegations which are naturally expired as Unbonding -> Withdrawable.
for manual unbonding some extra work is needed as logged in above issue

i couldn't properly test the expiry checker as i can't access btc node locally, have pinged devops

@gusin13 gusin13 marked this pull request as ready for review October 22, 2024 22:16
@jrwbabylonlab
Copy link
Collaborator

jrwbabylonlab commented Oct 23, 2024

This PR

Sets up internal expiry checker service which reads the timelock_queue table and mark the delegations as Withdrawable

Note - ran into this issue - ref tl;dr - this pr only marks delegations which are naturally expired as Unbonding -> Withdrawable. for manual unbonding some extra work is needed as logged in above issue

i couldn't properly test the expiry checker as i can't access btc node locally, have pinged devops

@gusin13 "marks delegations which are naturally expired as Unbonding -> Withdrawable." this statement seems wrong to me. The BBN will emit EventBTCDelegationExpired when the BTC timelock is expired. That means we do not need to derive the withdrawable state in indexer for natural expired delegation. it should be marked to withdrawable as soon as we received the EventBTCDelegationExpired event.

I was referring to this comment in the babylon code

// EventBTCDelegationExpired is the event emitted when a BTC delegation
// is unbonded by expiration of the staking tx timelock

If this is correct then my above assumption should hold. But if not, then core team need to fix this comment and explicity point out the EventBTCDelegationExpired are emitted at the time of VP lost, rather than timelock expired.

if err := s.db.UpdateBTCDelegationState(
ctx, unbondedEarlyEvent.StakingTxHash, types.DelegationState(unbondedEarlyEvent.NewState),
ctx, unbondedEarlyEvent.StakingTxHash, types.StateUnbonding,
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we have a method in the utils to map the BBN state into web state? i.e types.DelegationState(unbondedEarlyEvent.NewState) seems ok to me, but not sure why it's removed?

Copy link
Collaborator Author

@gusin13 gusin13 Oct 23, 2024

Choose a reason for hiding this comment

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

the state sent in Babylon event is Unbonded, types.DelegationState(unbondedEarlyEvent.NewState) would give us Unbonded

but the indexer needs to treat this as Unbonding

}

// previous state should be unbonding
if delegation.State != types.StateUnbonding {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this validation is too strong which will leads to issues when we have duplicated entries in db upon re-bootstrap of the service.

We have this set of methods in the phase-1 which i think we can re-use in phase-2 https://github.com/babylonlabs-io/staking-api-service/blob/main/internal/shared/utils/state_transition.go#L34
i.e ignore if the state is already beyond the withdrawable state

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i see, sure i have removed the check.
this might required lot of change in code for the outdated/qualified state transitions, would prefer to do in separate pr

#29

Copy link
Collaborator

@jrwbabylonlab jrwbabylonlab left a comment

Choose a reason for hiding this comment

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

Approved with some comments

@gusin13
Copy link
Collaborator Author

gusin13 commented Oct 23, 2024

This PR
Sets up internal expiry checker service which reads the timelock_queue table and mark the delegations as Withdrawable
Note - ran into this issue - ref tl;dr - this pr only marks delegations which are naturally expired as Unbonding -> Withdrawable. for manual unbonding some extra work is needed as logged in above issue
i couldn't properly test the expiry checker as i can't access btc node locally, have pinged devops

@gusin13 "marks delegations which are naturally expired as Unbonding -> Withdrawable." this statement seems wrong to me. The BBN will emit EventBTCDelegationExpired when the BTC timelock is expired. That means we do not need to derive the withdrawable state in indexer for natural expired delegation. it should be marked to withdrawable as soon as we received the EventBTCDelegationExpired event.

I was referring to this comment in the babylon code

// EventBTCDelegationExpired is the event emitted when a BTC delegation
// is unbonded by expiration of the staking tx timelock

If this is correct then my above assumption should hold. But if not, then core team need to fix this comment and explicity point out the EventBTCDelegationExpired are emitted at the time of VP lost, rather than timelock expired.

@jrwbabylonlab

That means we do not need to derive the withdrawable state in indexer for natural expired delegation

https://github.com/babylonlabs-io/babylon/blob/6a0ccacc41435a249316a77fe6e1e06aeb654d13/x/btcstaking/keeper/msg_server.go#L323-L332

https://github.com/babylonlabs-io/babylon/blob/6a0ccacc41435a249316a77fe6e1e06aeb654d13/x/btcstaking/types/btc_delegation.go#L122-L126

In Babylon this event is sent at btcDel.EndHeight-wValue which is basically when the VP is lost, but for our case we need to make sure
current btc tip > btcDel.EndHeight to mark it as Withdrawable.
Maybe comments can be improved in that event

Copy link
Member

@gitferry gitferry left a comment

Choose a reason for hiding this comment

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

Great work! Some nits and questions:

internal/config/poller.go Outdated Show resolved Hide resolved
internal/config/poller.go Outdated Show resolved Hide resolved
internal/db/model/setup.go Outdated Show resolved Hide resolved
internal/services/delegation.go Show resolved Hide resolved
internal/db/timelock.go Outdated Show resolved Hide resolved
internal/services/expiry-checker.go Show resolved Hide resolved
@jrwbabylonlab
Copy link
Collaborator

This PR
Sets up internal expiry checker service which reads the timelock_queue table and mark the delegations as Withdrawable
Note - ran into this issue - ref tl;dr - this pr only marks delegations which are naturally expired as Unbonding -> Withdrawable. for manual unbonding some extra work is needed as logged in above issue
i couldn't properly test the expiry checker as i can't access btc node locally, have pinged devops

@gusin13 "marks delegations which are naturally expired as Unbonding -> Withdrawable." this statement seems wrong to me. The BBN will emit EventBTCDelegationExpired when the BTC timelock is expired. That means we do not need to derive the withdrawable state in indexer for natural expired delegation. it should be marked to withdrawable as soon as we received the EventBTCDelegationExpired event.
I was referring to this comment in the babylon code

// EventBTCDelegationExpired is the event emitted when a BTC delegation
// is unbonded by expiration of the staking tx timelock

If this is correct then my above assumption should hold. But if not, then core team need to fix this comment and explicity point out the EventBTCDelegationExpired are emitted at the time of VP lost, rather than timelock expired.

@jrwbabylonlab

That means we do not need to derive the withdrawable state in indexer for natural expired delegation

https://github.com/babylonlabs-io/babylon/blob/6a0ccacc41435a249316a77fe6e1e06aeb654d13/x/btcstaking/keeper/msg_server.go#L323-L332

https://github.com/babylonlabs-io/babylon/blob/6a0ccacc41435a249316a77fe6e1e06aeb654d13/x/btcstaking/types/btc_delegation.go#L122-L126

In Babylon this event is sent at btcDel.EndHeight-wValue which is basically when the VP is lost, but for our case we need to make sure current btc tip > btcDel.EndHeight to mark it as Withdrawable. Maybe comments can be improved in that event

@gusin13 gotcha. In that case, yeah, make sense to have the natural unbonded event to be tracked in the expiry process so we can transition to the withdrawable state once timelock expires.
However, i do not think we need an unbonding state for the natural expire delegation. There is no such thing from BTC staking point of view.

@gusin13
Copy link
Collaborator Author

gusin13 commented Oct 23, 2024

However, i do not think we need an unbonding state for the natural expire delegation. There is no such thing from BTC staking point of view.

@jrwbabylonlab i found some comments here which explains this case. Basically after the VP is lost, the delegation is neither active nor withdrawable, there is a middle state which can be referred as unbonding

@gusin13 gusin13 merged commit 22ed334 into main Oct 23, 2024
11 checks passed
@gusin13 gusin13 deleted the gusin13/unbonding-transition branch October 23, 2024 16:49
@jrwbabylonlab
Copy link
Collaborator

However, i do not think we need an unbonding state for the natural expire delegation. There is no such thing from BTC staking point of view.

@jrwbabylonlab i found some comments here which explains this case. Basically after the VP is lost, the delegation is neither active nor withdrawable, there is a middle state which can be referred as unbonding

let's move this conversation to slack

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