Skip to content

Commit

Permalink
Added check for unstaking operators
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed May 15, 2024
1 parent 01d69fc commit c3664f9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
6 changes: 0 additions & 6 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2363,9 +2363,3 @@ func (tc *TbtcChain) GetRedemptionDelay(
func (tc *TbtcChain) GetDepositMinAge() (uint32, error) {
return tc.walletProposalValidator.DEPOSITMINAGE()
}

func (tc *TbtcChain) IsOperatorUnstaking() (bool, error) {
// TODO: Implement by checking if the operator has deauthorized their entire
// stake.
return false, nil
}
9 changes: 0 additions & 9 deletions pkg/tbtc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ const (
Challenge
)

// StakingChain defines the subset of the TBTC chain interface that pertains to
// the staking activities.
type StakingChain interface {
// IsOperatorUnstaking checks if the operator is unstaking. It returns true
// if the operator has deauthorized their entire stake, false otherwise.
IsOperatorUnstaking() (bool, error)
}

// GroupSelectionChain defines the subset of the TBTC chain interface that
// pertains to the group selection activities.
type GroupSelectionChain interface {
Expand Down Expand Up @@ -539,7 +531,6 @@ type Chain interface {
GetBlockHashByNumber(blockNumber uint64) ([32]byte, error)

sortition.Chain
StakingChain
GroupSelectionChain
DistributedKeyGenerationChain
InactivityClaimChain
Expand Down
5 changes: 0 additions & 5 deletions pkg/tbtc/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,6 @@ func (lc *localChain) IsEligibleForRewards() (bool, error) {
panic("unsupported")
}

func (lc *localChain) IsOperatorUnstaking() (bool, error) {
// TODO: Implement and use in unit tests.
return false, nil
}

func (lc *localChain) CanRestoreRewardEligibility() (bool, error) {
panic("unsupported")
}
Expand Down
28 changes: 26 additions & 2 deletions pkg/tbtc/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,12 @@ func newHeartbeatAction(

func (ha *heartbeatAction) execute() error {
// Do not execute the heartbeat action if the operator is unstaking.
isUnstaking, err := ha.chain.IsOperatorUnstaking()
isUnstaking, err := ha.isOperatorUnstaking()
if err != nil {
return fmt.Errorf("failed to check if the operator is unstaking")
return fmt.Errorf(
"failed to check if the operator is unstaking [%v]",
err,
)
}

if isUnstaking {
Expand Down Expand Up @@ -247,6 +250,27 @@ func (ha *heartbeatAction) actionType() WalletActionType {
return ActionHeartbeat
}

func (ha *heartbeatAction) isOperatorUnstaking() (bool, error) {
stakingProvider, isRegistered, err := ha.chain.OperatorToStakingProvider()
if err != nil {
return false, fmt.Errorf("failed to get staking provider for operator")
}

if !isRegistered {
return false, fmt.Errorf("staking provider not registered for operator")
}

// Eligible stake is defined as the currently authorized stake minus the
// pending authorization decrease.
eligibleStake, err := ha.chain.EligibleStake(stakingProvider)
if err != nil {
return false, fmt.Errorf("failed to check eligible stake for operator")
}

// The operator is considered unstaking if their eligible stake is `0`.
return eligibleStake.Cmp(big.NewInt(0)) == 0, nil
}

// heartbeatFailureCounter holds counters keeping track of consecutive
// heartbeat failures. Each wallet has a separate counter. The key used in
// the map is the uncompressed public key (with 04 prefix) of the wallet.
Expand Down

0 comments on commit c3664f9

Please sign in to comment.