Skip to content

Commit

Permalink
dissalow creating new stake if allow list is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Nov 14, 2024
1 parent 9fa1547 commit 4569d9b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
63 changes: 39 additions & 24 deletions babylonclient/babyloncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,19 @@ func NewBabylonController(
}

type StakingTrackerResponse struct {
SlashingPkScript []byte
SlashingRate sdkmath.LegacyDec
MinComissionRate sdkmath.LegacyDec
CovenantPks []*btcec.PublicKey
CovenantQuruomThreshold uint32
MinSlashingFee btcutil.Amount
MinUnbondingTime uint16
UnbondingFee btcutil.Amount
MinStakingTime uint16
MaxStakingTime uint16
MinStakingValue btcutil.Amount
MaxStakingValue btcutil.Amount
SlashingPkScript []byte
SlashingRate sdkmath.LegacyDec
MinComissionRate sdkmath.LegacyDec
CovenantPks []*btcec.PublicKey
CovenantQuruomThreshold uint32
MinSlashingFee btcutil.Amount
MinUnbondingTime uint16
UnbondingFee btcutil.Amount
MinStakingTime uint16
MaxStakingTime uint16
MinStakingValue btcutil.Amount
MaxStakingValue btcutil.Amount
AllowListExpirationHeight uint64
}

type FinalityProviderInfo struct {
Expand Down Expand Up @@ -193,6 +194,7 @@ func (bc *BabylonController) Params() (*StakingParams, error) {
MaxStakingTime: stakingTrackerParams.MaxStakingTime,
MinStakingValue: stakingTrackerParams.MinStakingValue,
MaxStakingValue: stakingTrackerParams.MaxStakingValue,
AllowListExpirationHeight: stakingTrackerParams.AllowListExpirationHeight,
}, nil
}

Expand All @@ -217,6 +219,18 @@ func (bc *BabylonController) GetKeyAddress() sdk.AccAddress {
return addr
}

func (bc *BabylonController) GetLatestBlockHeight() (uint64, error) {
ctx, cancel := getQueryContext(bc.cfg.Timeout)
defer cancel()

status, err := bc.bbnClient.RPCClient.Status(ctx)
if err != nil {
return 0, err
}

return uint64(status.SyncInfo.LatestBlockHeight), nil
}

func (bc *BabylonController) getTxSigner() string {
signer := bc.GetKeyAddress()
prefix := bc.cfg.AccountPrefix
Expand Down Expand Up @@ -501,18 +515,19 @@ func (bc *BabylonController) QueryStakingTracker() (*StakingTrackerResponse, err
}

return &StakingTrackerResponse{
SlashingPkScript: response.Params.SlashingPkScript,
SlashingRate: response.Params.SlashingRate,
MinComissionRate: response.Params.MinCommissionRate,
CovenantPks: covenantPks,
MinSlashingFee: btcutil.Amount(response.Params.MinSlashingTxFeeSat),
CovenantQuruomThreshold: response.Params.CovenantQuorum,
MinUnbondingTime: uint16(minUnbondingTimeBlocksU32),
UnbondingFee: btcutil.Amount(response.Params.UnbondingFeeSat),
MinStakingTime: uint16(minStakingTimeBlocksU32),
MaxStakingTime: uint16(maxStakingTimeBlocksU32),
MinStakingValue: btcutil.Amount(response.Params.MinStakingValueSat),
MaxStakingValue: btcutil.Amount(response.Params.MaxStakingValueSat),
SlashingPkScript: response.Params.SlashingPkScript,
SlashingRate: response.Params.SlashingRate,
MinComissionRate: response.Params.MinCommissionRate,
CovenantPks: covenantPks,
MinSlashingFee: btcutil.Amount(response.Params.MinSlashingTxFeeSat),
CovenantQuruomThreshold: response.Params.CovenantQuorum,
MinUnbondingTime: uint16(minUnbondingTimeBlocksU32),
UnbondingFee: btcutil.Amount(response.Params.UnbondingFeeSat),
MinStakingTime: uint16(minStakingTimeBlocksU32),
MaxStakingTime: uint16(maxStakingTimeBlocksU32),
MinStakingValue: btcutil.Amount(response.Params.MinStakingValueSat),
MaxStakingValue: btcutil.Amount(response.Params.MaxStakingValueSat),
AllowListExpirationHeight: response.Params.AllowListExpirationHeight,
}, nil
}

Expand Down
8 changes: 8 additions & 0 deletions babylonclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type StakingParams struct {

// Maximum staking value required by babylon
MaxStakingValue btcutil.Amount

// AllowList expiration height
AllowListExpirationHeight uint64
}

// SingleKeyCosmosKeyring represents a keyring that supports only one pritvate/public key pair
Expand All @@ -72,6 +75,7 @@ type BabylonClient interface {
QueryHeaderDepth(headerHash *chainhash.Hash) (uint32, error)
IsTxAlreadyPartOfDelegation(stakingTxHash *chainhash.Hash) (bool, error)
QueryDelegationInfo(stakingTxHash *chainhash.Hash) (*DelegationInfo, error)
GetLatestBlockHeight() (uint64, error)
}

type MockBabylonClient struct {
Expand Down Expand Up @@ -159,6 +163,10 @@ func (m *MockBabylonClient) Undelegate(
return &pv.RelayerTxResponse{Code: 0}, nil
}

func (m *MockBabylonClient) GetLatestBlockHeight() (uint64, error) {
return 0, nil
}

func GetMockClient() *MockBabylonClient {
covenantPk, err := btcec.NewPrivateKey()
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions staker/stakerapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,21 @@ func (app *App) StakeFunds(
return nil, err
}

// Allow list is enabled, check if we are past the expiration height and we can
// create new delegations
if params.AllowListExpirationHeight > 0 {
latestBlockHeight, err := app.babylonClient.GetLatestBlockHeight()

if err != nil {
return nil, fmt.Errorf("failed to get latest block height: %w", err)
}
// we add +1 to account for comet bft lazy execution
if latestBlockHeight <= params.AllowListExpirationHeight+1 {
return nil, fmt.Errorf("allow is enabled, cannot create new delegations. Latest block height %d is before allow list expiration height %d",
latestBlockHeight, params.AllowListExpirationHeight)
}
}

slashingFee := app.getSlashingFee(params.MinSlashingTxFeeSat)

if stakingAmount <= slashingFee {
Expand Down

0 comments on commit 4569d9b

Please sign in to comment.