Skip to content

Commit

Permalink
add api urls rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
alrxy committed Aug 13, 2024
1 parent b0d4e76 commit 62d1783
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 45 deletions.
51 changes: 10 additions & 41 deletions x/symStaking/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package keeper

import (
"math/rand"
"os"
"strings"
"time"

gogotypes "github.com/cosmos/gogoproto/types"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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") != ""
Expand All @@ -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),
Expand Down Expand Up @@ -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))]
}
7 changes: 5 additions & 2 deletions x/symStaking/keeper/symbiotic_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions x/symStaking/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
57 changes: 57 additions & 0 deletions x/symStaking/types/api_urls.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 62d1783

Please sign in to comment.