diff --git a/x/symStaking/keeper/keeper.go b/x/symStaking/keeper/keeper.go index 54bd7ccac..2b7b498e5 100644 --- a/x/symStaking/keeper/keeper.go +++ b/x/symStaking/keeper/keeper.go @@ -1,9 +1,7 @@ package keeper import ( - "math/rand" "os" - "strings" "time" gogotypes "github.com/cosmos/gogoproto/types" @@ -43,17 +41,15 @@ func HistoricalInfoCodec(cdc codec.BinaryCodec) collcodec.ValueCodec[types.Histo type Keeper struct { appmodule.Environment - cdc codec.BinaryCodec - authKeeper types.AccountKeeper - bankKeeper types.BankKeeper - hooks types.StakingHooks - authority string - validatorAddressCodec addresscodec.Codec - consensusAddressCodec addresscodec.Codec - cometInfoService comet.Service - - beaconApiUrls []string - ethApiUrls []string + cdc codec.BinaryCodec + authKeeper types.AccountKeeper + bankKeeper types.BankKeeper + hooks types.StakingHooks + authority string + validatorAddressCodec addresscodec.Codec + consensusAddressCodec addresscodec.Codec + cometInfoService comet.Service + apiUrls *types.ApiUrls networkMiddlewareAddress string debug bool @@ -102,24 +98,6 @@ func NewKeeper( panic("validator and/or consensus address codec are nil") } - // USE ONLY YOUR LOCAL BEACON CLIENT FOR SAFETY!!! - beaconApiUrls := strings.Split(os.Getenv("BEACON_API_URLS"), ",") - if len(beaconApiUrls) == 1 && beaconApiUrls[0] == "" { - beaconApiUrls[0] = "https://eth-holesky-beacon.public.blastapi.io" - beaconApiUrls = append(beaconApiUrls, "http://unstable.holesky.beacon-api.nimbus.team") - beaconApiUrls = append(beaconApiUrls, "https://ethereum-holesky-beacon-api.publicnode.com") - } - - ethApiUrls := strings.Split(os.Getenv("ETH_API_URLS"), ",") - - if len(ethApiUrls) == 1 && ethApiUrls[0] == "" { - ethApiUrls[0] = "https://rpc.ankr.com/eth_holesky" - ethApiUrls = append(ethApiUrls, "https://ethereum-holesky.blockpi.network/v1/rpc/public") - ethApiUrls = append(ethApiUrls, "https://eth-holesky.public.blastapi.io") - ethApiUrls = append(ethApiUrls, "https://ethereum-holesky.gateway.tatum.io") - ethApiUrls = append(ethApiUrls, "https://holesky.gateway.tenderly.co") - } - networkMiddlewareAddress := os.Getenv("MIDDLEWARE_ADDRESS") debug := os.Getenv("DEBUG") != "" @@ -134,8 +112,7 @@ func NewKeeper( validatorAddressCodec: validatorAddressCodec, consensusAddressCodec: consensusAddressCodec, cometInfoService: cometInfoService, - beaconApiUrls: beaconApiUrls, - ethApiUrls: ethApiUrls, + apiUrls: types.NewApiUrls(), networkMiddlewareAddress: networkMiddlewareAddress, debug: debug, LastTotalPower: collections.NewItem(sb, types.LastTotalPowerKey, "last_total_power", sdk.IntValue), @@ -209,11 +186,3 @@ func (k Keeper) ValidatorAddressCodec() addresscodec.Codec { func (k Keeper) ConsensusAddressCodec() addresscodec.Codec { return k.consensusAddressCodec } - -func (k Keeper) GetEthApiUrl() string { - return k.ethApiUrls[rand.Intn(len(k.ethApiUrls))] -} - -func (k Keeper) GetBeaconApiUrl() string { - return k.beaconApiUrls[rand.Intn(len(k.beaconApiUrls))] -} diff --git a/x/symStaking/keeper/symbiotic_state_change.go b/x/symStaking/keeper/symbiotic_state_change.go index ec76105c5..06bcc810b 100644 --- a/x/symStaking/keeper/symbiotic_state_change.go +++ b/x/symStaking/keeper/symbiotic_state_change.go @@ -126,11 +126,14 @@ func (k Keeper) SymbioticUpdateValidatorsPower(ctx context.Context) (string, err blockHash, err := k.getFinalizedBlockHash() if err != nil { + k.apiUrls.RotateBeaconUrl() + k.Logger.Info("kek", "updated", k.apiUrls.GetBeaconApiUrl()) return "", err } validators, err := k.GetSymbioticValidatorSet(ctx, blockHash) if err != nil { + k.apiUrls.RotateEthUrl() return "", err } @@ -151,7 +154,7 @@ func (k Keeper) SymbioticUpdateValidatorsPower(ctx context.Context) (string, err // Function to get the finality slot from the Beacon Chain API func (k Keeper) getFinalizedBlockHash() (string, error) { - url := k.GetBeaconApiUrl() + url := k.apiUrls.GetBeaconApiUrl() if k.debug { url += BLOCK_ATTESTED_PATH } else { @@ -182,7 +185,7 @@ func (k Keeper) getFinalizedBlockHash() (string, error) { } func (k Keeper) GetSymbioticValidatorSet(ctx context.Context, blockHash string) ([]Validator, error) { - client, err := ethclient.Dial(k.GetEthApiUrl()) + client, err := ethclient.Dial(k.apiUrls.GetEthApiUrl()) if err != nil { return nil, err } diff --git a/x/symStaking/keeper/val_state_change.go b/x/symStaking/keeper/val_state_change.go index 535db8ea6..70aa0eac3 100644 --- a/x/symStaking/keeper/val_state_change.go +++ b/x/symStaking/keeper/val_state_change.go @@ -24,13 +24,16 @@ func (k Keeper) BlockValidatorUpdates(ctx context.Context) ([]appmodule.Validato // Calculate validator set changes. var err error - for i := 0; i < 5; i++ { // retry 5 times with different random providers + for i := 0; i < 5; i++ { // retry 5 times with different providers + beaconUrl := k.apiUrls.GetBeaconApiUrl() + ethApiUrl := k.apiUrls.GetEthApiUrl() + _, err = k.SymbioticUpdateValidatorsPower(ctx) if err == nil { break } - k.Logger.Warn("Update validator power error; Trying ...", "error", err) + k.Logger.Warn("Update validator power error; Trying ...", "error", err, "beacon", beaconUrl, "eth", ethApiUrl) time.Sleep(time.Millisecond * 200) } diff --git a/x/symStaking/types/api_urls.go b/x/symStaking/types/api_urls.go new file mode 100644 index 000000000..5f16b3e7f --- /dev/null +++ b/x/symStaking/types/api_urls.go @@ -0,0 +1,57 @@ +package types + +import ( + "os" + "strings" +) + +type ApiUrls struct { + beaconApiUrls []string + ethApiUrls []string + currentBeaconId int + currentEthId int +} + +func NewApiUrls() *ApiUrls { + // USE ONLY YOUR LOCAL BEACON CLIENT FOR SAFETY!!! + beaconApiUrls := strings.Split(os.Getenv("BEACON_API_URLS"), ",") + if len(beaconApiUrls) == 1 && beaconApiUrls[0] == "" { + beaconApiUrls[0] = "https://eth-holesky-beacon.public.blastapi.io" + beaconApiUrls = append(beaconApiUrls, "http://unstable.holesky.beacon-api.nimbus.team") + beaconApiUrls = append(beaconApiUrls, "https://ethereum-holesky-beacon-api.publicnode.com") + } + + ethApiUrls := strings.Split(os.Getenv("ETH_API_URLS"), ",") + + if len(ethApiUrls) == 1 && ethApiUrls[0] == "" { + ethApiUrls[0] = "https://rpc.ankr.com/eth_holesky" + ethApiUrls = append(ethApiUrls, "https://ethereum-holesky.blockpi.network/v1/rpc/public") + ethApiUrls = append(ethApiUrls, "https://eth-holesky.public.blastapi.io") + ethApiUrls = append(ethApiUrls, "https://ethereum-holesky.gateway.tatum.io") + ethApiUrls = append(ethApiUrls, "https://holesky.gateway.tenderly.co") + } + + return &ApiUrls{beaconApiUrls: beaconApiUrls, ethApiUrls: ethApiUrls} +} + +func (au ApiUrls) GetEthApiUrl() string { + return au.ethApiUrls[au.currentEthId] +} + +func (au ApiUrls) GetBeaconApiUrl() string { + return au.beaconApiUrls[au.currentBeaconId] +} + +func (au *ApiUrls) RotateEthUrl() { + au.currentEthId++ + if au.currentEthId == len(au.ethApiUrls) { + au.currentEthId = 0 + } +} + +func (au *ApiUrls) RotateBeaconUrl() { + au.currentBeaconId++ + if au.currentBeaconId == len(au.beaconApiUrls) { + au.currentBeaconId = 0 + } +}