Skip to content

Commit

Permalink
feat: Add periodic status update to managed validators (ethereum-opti…
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry authored Sep 26, 2023
1 parent 8caa336 commit 93bf292
Show file tree
Hide file tree
Showing 10 changed files with 532 additions and 190 deletions.
18 changes: 18 additions & 0 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,25 @@ func (bc *BabylonController) QueryUnbondindBTCDelegations() ([]*btcstakingtypes.
return res.BtcDelegations, nil
}

func (bc *BabylonController) QueryValidator(btcPk *bbntypes.BIP340PubKey) (*btcstakingtypes.BTCValidator, error) {
ctx, cancel := getContextWithCancel(bc.timeout)
defer cancel()

clientCtx := sdkclient.Context{Client: bc.provider.RPCClient}

queryRequest := &btcstakingtypes.QueryBTCValidatorRequest{ValBtcPkHex: btcPk.MarshalHex()}

queryClient := btcstakingtypes.NewQueryClient(clientCtx)
res, err := queryClient.BTCValidator(ctx, queryRequest)
if err != nil {
return nil, fmt.Errorf("failed to query the validator %s: %v", btcPk.MarshalHex(), err)
}

return res.BtcValidator, nil
}

// QueryValidators queries BTC validators
// Currently this is only used for e2e tests, probably does not need to add this into the interface
func (bc *BabylonController) QueryValidators() ([]*btcstakingtypes.BTCValidator, error) {
var validators []*btcstakingtypes.BTCValidator
pagination := &sdkquery.PageRequest{
Expand Down
2 changes: 2 additions & 0 deletions clientcontroller/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ type ClientController interface {
QueryLatestFinalizedBlocks(count uint64) ([]*types.BlockInfo, error)
// QueryBlocks returns a list of blocks from startHeight to endHeight
QueryBlocks(startHeight, endHeight, limit uint64) ([]*types.BlockInfo, error)
// QueryValidator returns a BTC validator object
QueryValidator(btcPk *bbntypes.BIP340PubKey) (*btcstakingtypes.BTCValidator, error)
// QueryBlockFinalization queries whether the block has been finalized
QueryBlockFinalization(height uint64) (bool, error)

Expand Down
5 changes: 1 addition & 4 deletions itest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ func StartManager(t *testing.T, isJury bool) *TestManager {
cfg := defaultValidatorConfig(bh.GetNodeDataDir(), testDir, isJury)

bc, err := clientcontroller.NewBabylonController(bh.GetNodeDataDir(), cfg.BabylonConfig, logger)
// making sure the fee is sufficient
cfg.BabylonConfig.GasAdjustment = 1.5
cfg.BabylonConfig.GasPrices = "0.1ubbn"
require.NoError(t, err)

valApp, err := service.NewValidatorAppFromConfig(cfg, logger, bc)
Expand Down Expand Up @@ -534,7 +531,7 @@ func defaultValidatorConfig(keyringDir, testDir string, isJury bool) *valcfg.Con
// errors
cfg.BabylonConfig.Key = "test-spending-key"
// Big adjustment to make sure we have enough gas in our transactions
cfg.BabylonConfig.GasAdjustment = 5
cfg.BabylonConfig.GasAdjustment = 10
cfg.DatabaseConfig.Path = filepath.Join(testDir, "db")
cfg.JuryMode = isJury
cfg.JuryModeConfig.QueryInterval = 7 * time.Second
Expand Down
4 changes: 2 additions & 2 deletions service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ func (app *ValidatorApp) GetJuryPk() (*btcec.PublicKey, error) {
}

func (app *ValidatorApp) ListValidatorInstances() []*ValidatorInstance {
return app.validatorManager.listValidatorInstances()
return app.validatorManager.ListValidatorInstances()
}

// GetValidatorInstance returns the validator instance with the given Babylon public key
func (app *ValidatorApp) GetValidatorInstance(babylonPk *secp256k1.PubKey) (*ValidatorInstance, error) {
return app.validatorManager.getValidatorInstance(babylonPk)
return app.validatorManager.GetValidatorInstance(babylonPk)
}

func (app *ValidatorApp) RegisterValidator(keyName string) (*RegisterValidatorResponse, *secp256k1.PubKey, error) {
Expand Down
168 changes: 164 additions & 4 deletions service/types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package service

import (
"github.com/babylonchain/babylon/types"
"sync"

bbntypes "github.com/babylonchain/babylon/types"
btcstakingtypes "github.com/babylonchain/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdktypes "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/sirupsen/logrus"

"github.com/babylonchain/btc-validator/proto"
"github.com/babylonchain/btc-validator/val"
)

type createValidatorResponse struct {
Expand All @@ -23,7 +29,7 @@ type createValidatorRequest struct {

type registerValidatorRequest struct {
bbnPubKey *secp256k1.PubKey
btcPubKey *types.BIP340PubKey
btcPubKey *bbntypes.BIP340PubKey
// TODO we should have our own representation of PoP
pop *btcstakingtypes.ProofOfPossession
description *stakingtypes.Description
Expand Down Expand Up @@ -52,12 +58,166 @@ type CreateValidatorResult struct {
}

type unbondingTxSigData struct {
stakerPk *types.BIP340PubKey
stakerPk *bbntypes.BIP340PubKey
stakingTxHash string
signature *types.BIP340Signature
signature *bbntypes.BIP340Signature
}

type unbondingTxSigSendResult struct {
err error
stakingTxHash string
}

type valState struct {
mu sync.Mutex
v *proto.StoreValidator
s *val.ValidatorStore
}

func (vs *valState) getStoreValidator() *proto.StoreValidator {
vs.mu.Lock()
defer vs.mu.Unlock()
return vs.v
}

func (vs *valState) setStatus(s proto.ValidatorStatus) error {
vs.mu.Lock()
vs.v.Status = s
vs.mu.Unlock()
return vs.s.UpdateValidator(vs.v)
}

func (vs *valState) setLastVotedHeight(height uint64) error {
vs.mu.Lock()
vs.v.LastVotedHeight = height
vs.mu.Unlock()
return vs.s.UpdateValidator(vs.v)
}

func (vs *valState) setLastProcessedHeight(height uint64) error {
vs.mu.Lock()
vs.v.LastProcessedHeight = height
vs.mu.Unlock()
return vs.s.UpdateValidator(vs.v)
}

func (vs *valState) setLastCommittedHeight(height uint64) error {
vs.mu.Lock()
vs.v.LastCommittedHeight = height
vs.mu.Unlock()
return vs.s.UpdateValidator(vs.v)
}

func (vs *valState) setLastProcessedAndVotedHeight(height uint64) error {
vs.mu.Lock()
vs.v.LastVotedHeight = height
vs.v.LastProcessedHeight = height
vs.mu.Unlock()
return vs.s.UpdateValidator(vs.v)
}

func (v *ValidatorInstance) GetStoreValidator() *proto.StoreValidator {
return v.state.getStoreValidator()
}

func (v *ValidatorInstance) GetBabylonPk() *secp256k1.PubKey {
return v.state.getStoreValidator().GetBabylonPK()
}

func (v *ValidatorInstance) GetBabylonPkHex() string {
return v.state.getStoreValidator().GetBabylonPkHexString()
}

func (v *ValidatorInstance) GetBtcPkBIP340() *bbntypes.BIP340PubKey {
return v.state.getStoreValidator().MustGetBIP340BTCPK()
}

func (v *ValidatorInstance) MustGetBtcPk() *btcec.PublicKey {
return v.state.getStoreValidator().MustGetBTCPK()
}

func (v *ValidatorInstance) GetBtcPkHex() string {
return v.GetBtcPkBIP340().MarshalHex()
}

func (v *ValidatorInstance) GetStatus() proto.ValidatorStatus {
return v.state.getStoreValidator().Status
}

func (v *ValidatorInstance) GetLastVotedHeight() uint64 {
return v.state.getStoreValidator().LastVotedHeight
}

func (v *ValidatorInstance) GetLastProcessedHeight() uint64 {
return v.state.getStoreValidator().LastProcessedHeight
}

func (v *ValidatorInstance) GetLastCommittedHeight() uint64 {
return v.state.getStoreValidator().LastCommittedHeight
}

func (v *ValidatorInstance) SetStatus(s proto.ValidatorStatus) error {
return v.state.setStatus(s)
}

func (v *ValidatorInstance) MustSetStatus(s proto.ValidatorStatus) {
if err := v.SetStatus(s); err != nil {
v.logger.WithFields(logrus.Fields{
"err": err,
"btc_pk_hex": v.GetBtcPkHex(),
"status": s.String(),
}).Fatal("failed to set validator status")
}
}

func (v *ValidatorInstance) SetLastProcessedHeight(height uint64) error {
return v.state.setLastProcessedHeight(height)
}

func (v *ValidatorInstance) MustSetLastProcessedHeight(height uint64) {
if err := v.SetLastProcessedHeight(height); err != nil {
v.logger.WithFields(logrus.Fields{
"err": err,
"btc_pk_hex": v.GetBtcPkHex(),
"height": height,
}).Fatal("failed to set last processed height")
}
}

func (v *ValidatorInstance) SetLastCommittedHeight(height uint64) error {
return v.state.setLastCommittedHeight(height)
}

func (v *ValidatorInstance) MustSetLastCommittedHeight(height uint64) {
if err := v.SetLastCommittedHeight(height); err != nil {
v.logger.WithFields(logrus.Fields{
"err": err,
"btc_pk_hex": v.GetBtcPkHex(),
"height": height,
}).Fatal("failed to set last committed height")
}
}

func (v *ValidatorInstance) updateStateAfterFinalitySigSubmission(height uint64) error {
return v.state.setLastProcessedAndVotedHeight(height)
}

func (v *ValidatorInstance) MustUpdateStateAfterFinalitySigSubmission(height uint64) {
if err := v.updateStateAfterFinalitySigSubmission(height); err != nil {
v.logger.WithFields(logrus.Fields{
"err": err,
"btc_pk_hex": v.GetBtcPkHex(),
"height": height,
}).Fatal("failed to update state after finality sig submission")
}
}

// only used for testing purpose
func (v *ValidatorInstance) GetCommittedPubRandPairList() ([]*proto.SchnorrRandPair, error) {
return v.state.s.GetRandPairList(v.bbnPk.Key)
}

// only used for testing purposes
func (v *ValidatorInstance) BtcPrivKey() (*btcec.PrivateKey, error) {
return v.kc.GetBtcPrivKey()
}
Loading

0 comments on commit 93bf292

Please sign in to comment.