Skip to content

Commit

Permalink
fix: grpc query requests and extract vairables to be senior validator
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Jul 17, 2023
1 parent ee7abec commit 3a65650
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 34 deletions.
2 changes: 1 addition & 1 deletion internal/provider/alliance/alliance_protocols_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *allianceProtocolsInfo) GetProtocolsInfo(ctx context.Context) (*pkgtypes
// Iterate over all configured nodes in the config file,
// create a grpcConnection to each node and query the required data.
for _, grpcUrl := range p.config.GRPCUrls {
grpcConn, err := p.BaseGrpc.Connection(grpcUrl, nil)
grpcConn, err := p.BaseGrpc.Connection(ctx, grpcUrl)
if err != nil {
return nil, err
}
Expand Down
64 changes: 46 additions & 18 deletions internal/provider/alliance/alliance_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"os"
"strconv"
"strings"

"github.com/terra-money/oracle-feeder-go/config"
Expand All @@ -27,11 +28,13 @@ import (

type allianceValidatorsProvider struct {
internal.BaseGrpc
nodeGrpcUrl string
stationApiUrl string
allianceHubContractAddress string
config *config.AllianceConfig
providerManager *provider.ProviderManager
config *config.AllianceConfig
providerManager *provider.ProviderManager
nodeGrpcUrl string
stationApiUrl string
allianceHubContractAddress string
blocksToBeSeniorValidator int64
voteOnProposalsToBeSeniorValidator int64
}

func NewAllianceValidatorsProvider(config *config.AllianceConfig, providerManager *provider.ProviderManager) *allianceValidatorsProvider {
Expand All @@ -50,13 +53,37 @@ func NewAllianceValidatorsProvider(config *config.AllianceConfig, providerManage
panic("ALLIANCE_HUB_CONTRACT_ADDRESS env variable is not set!")
}

var blocksToBeSeniorValidator int64 = 100_000
if blocks := os.Getenv("BLOCKS_TO_BE_SENIOR_VALIDATOR"); len(blocks) != 0 {
blocks, err := strconv.ParseInt(blocks, 10, 64)
if err != nil {
panic("BLOCKS_TO_BE_SENIOR_VALIDATOR env variable is not a valid integer!")
}

blocksToBeSeniorValidator = blocks
}

var voteOnProposalsToBeSeniorValidator int64 = 3
if proposals := os.Getenv("VOTE_ON_PROPOSALS_TO_BE_SENIOR_VALIDATOR"); len(proposals) != 0 {
voteOnProposals, err := strconv.ParseInt(proposals, 10, 64)
if err != nil {
panic("VOTE_ON_PROPOSALS_TO_BE_SENIOR_VALIDATOR env variable is not a valid integer!")
}
if voteOnProposals > 3 {
panic("VOTE_ON_PROPOSALS_TO_BE_SENIOR_VALIDATOR env variable is greater than 3!")
}
voteOnProposalsToBeSeniorValidator = voteOnProposals
}

return &allianceValidatorsProvider{
BaseGrpc: *internal.NewBaseGrpc(),
config: config,
nodeGrpcUrl: nodeGrpcUrl,
stationApiUrl: stationApiUrl,
providerManager: providerManager,
allianceHubContractAddress: allianceHubContractAddress,
BaseGrpc: *internal.NewBaseGrpc(),
config: config,
nodeGrpcUrl: nodeGrpcUrl,
stationApiUrl: stationApiUrl,
providerManager: providerManager,
allianceHubContractAddress: allianceHubContractAddress,
blocksToBeSeniorValidator: blocksToBeSeniorValidator,
voteOnProposalsToBeSeniorValidator: voteOnProposalsToBeSeniorValidator,
}
}

Expand Down Expand Up @@ -100,7 +127,7 @@ func (p *allianceValidatorsProvider) GetAllianceRedelegateReq(ctx context.Contex
}

// (4) skip if have not voted in the last 3 proposals
if !atLeastThreeOccurrences(proposalsVotes, val.OperatorAddress) {
if !p.votedForLatestProposals(proposalsVotes, val.OperatorAddress) {
continue
}

Expand All @@ -117,6 +144,7 @@ func (p *allianceValidatorsProvider) GetAllianceRedelegateReq(ctx context.Contex
valsWithAllianceTokens, totalAllianceStakedTokens := FilterAllianceValsWithStake(allianceVals, smartContractRes.AllianceTokenDenom)
compliantValsWithAllianceTokens,
nonCompliantValsWithAllianceTokens := ParseAllianceValsByCompliance(compliantVals, valsWithAllianceTokens, smartContractRes.AllianceTokenDenom)
fmt.Printf("compliantVals %s", len(compliantVals))

Check failure on line 147 in internal/provider/alliance/alliance_validators.go

View workflow job for this annotation

GitHub Actions / golangci-lint

printf: fmt.Printf format %s has arg len(compliantVals) of wrong type int (govet)
avgTokensPerCompliantVal := totalAllianceStakedTokens.Quo(sdktypes.NewDec(int64(len(compliantVals))))

redelegations := RebalanceVals(
Expand Down Expand Up @@ -300,18 +328,18 @@ func FilterAllianceValsWithStake(allianceVals []alliancetypes.QueryAllianceValid
}

func (p *allianceValidatorsProvider) querySmartContractConfig(ctx context.Context) (*types.AllianceHubConfigData, error) {
grpcConn, err := p.BaseGrpc.Connection(p.nodeGrpcUrl, nil)
grpcConn, err := p.BaseGrpc.Connection(ctx, p.nodeGrpcUrl)
if err != nil {
fmt.Printf("grpcConn: %v \n", err)
return nil, err
}
defer grpcConn.Close()
client := wasmtypes.NewQueryClient(grpcConn)

res, err := client.SmartContractState(ctx, &wasmtypes.QuerySmartContractStateRequest{
Address: p.allianceHubContractAddress,
QueryData: []byte(`{ "config" : {}}`),
})
fmt.Printf("res: %v err: %v\n", res, err)
if err != nil {
return nil, err
}
Expand All @@ -332,7 +360,7 @@ func (p *allianceValidatorsProvider) queryValidatorsData(ctx context.Context) (
[]alliancetypes.QueryAllianceValidatorResponse,
error,
) {
grpcConn, err := p.BaseGrpc.Connection(p.nodeGrpcUrl, nil)
grpcConn, err := p.BaseGrpc.Connection(ctx, p.nodeGrpcUrl)
if err != nil {
fmt.Printf("grpcConn: %v \n", err)
return nil, nil, nil, nil, err
Expand Down Expand Up @@ -373,7 +401,7 @@ func (p *allianceValidatorsProvider) queryValidatorsData(ctx context.Context) (
}

seniorValidatorsRes, err := nodeClient.GetValidatorSetByHeight(ctx, &tmservice.GetValidatorSetByHeightRequest{
Height: latestHeightRes.SdkBlock.Header.Height - 100_000,
Height: latestHeightRes.SdkBlock.Header.Height - p.blocksToBeSeniorValidator,
})
if err != nil {
fmt.Printf("seniorValidatorsRes: %v \n", err)
Expand Down Expand Up @@ -440,12 +468,12 @@ func (p allianceValidatorsProvider) queryStation(propId uint64) (res *[]types.St
return res, nil
}

func atLeastThreeOccurrences(stationVotes []types.StationVote, val string) bool {
func (p allianceValidatorsProvider) votedForLatestProposals(stationVotes []types.StationVote, val string) bool {
count := 0
for _, v := range stationVotes {
if v.Voter == val {
count++
}
}
return count >= 3
return count >= int(p.voteOnProposalsToBeSeniorValidator)
}
14 changes: 5 additions & 9 deletions internal/provider/internal/base_grpc.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package internal

import (
"context"
"crypto/tls"
"strings"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/codec/types"
)

type BaseGrpc struct {
Expand All @@ -20,8 +18,8 @@ func NewBaseGrpc() *BaseGrpc {
}

func (p *BaseGrpc) Connection(
ctx context.Context,
nodeUrl string,
interfaceRegistry sdk.InterfaceRegistry,
) (*grpc.ClientConn, error) {
var authCredentials = grpc.WithTransportCredentials(insecure.NewCredentials())

Expand All @@ -30,11 +28,9 @@ func (p *BaseGrpc) Connection(
authCredentials = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))
}

return grpc.Dial(
return grpc.DialContext(
ctx,
nodeUrl,
authCredentials,
grpc.WithDefaultCallOptions(
grpc.ForceCodec(codec.NewProtoCodec(interfaceRegistry).GRPCCodec()),
grpc.MaxCallRecvMsgSize(1024*1024*124), // 124MB
))
)
}
2 changes: 1 addition & 1 deletion internal/provider/internal/carbon/carbon.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewCarbonProvider() *CarbonProvider {
}

func (p *CarbonProvider) GetAnnualProvisions(ctx context.Context) (*mintypes.QueryAnnualProvisionsResponse, error) {
grpcConn, err := p.BaseGrpc.Connection(p.grpc, nil)
grpcConn, err := p.BaseGrpc.Connection(ctx, p.grpc)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/lsd_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (p *LSDProvider) QueryLSTRebaseFactor(symbol string) (*sdk.Dec, error) {

func (p *LSDProvider) queryAmpLunaRebaseFactor() (*sdk.Dec, error) {
ctx := context.Background()
connection, err := p.BaseGrpc.Connection(p.phoenixNodeUrl, nil)
connection, err := p.BaseGrpc.Connection(ctx, p.phoenixNodeUrl)
if err != nil {
return nil, err
}
Expand All @@ -72,7 +72,7 @@ func (p *LSDProvider) queryAmpLunaRebaseFactor() (*sdk.Dec, error) {

func (p *LSDProvider) queryBoneLunaRebaseFactor() (*sdk.Dec, error) {
ctx := context.Background()
connection, err := p.BaseGrpc.Connection(p.phoenixNodeUrl, nil)
connection, err := p.BaseGrpc.Connection(ctx, p.phoenixNodeUrl)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions params.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
"price_server_port": "8532",
"oracle_address": "terra1jf3nndysevley5p3wnajkjvjxcql9d00gpj4en3xwp7yrkrdqess48rr27",
"alliance_hub_contract_address": "terra1majrm6e6n0eg760n9fs4g5jvwzh4ytp8e2d99mfgzv2e7mjmdwxse0ty73",
"feeder_retries": 4
"feeder_retries": 4,
"blocks_to_be_senior_validator": 100000,
"vote_on_proposals_to_be_senior_validator": 3
},
"develop": {
"price_server_port": "8532",
"oracle_address": "terra1jf3nndysevley5p3wnajkjvjxcql9d00gpj4en3xwp7yrkrdqess48rr27",
"alliance_hub_contract_address" : "terra1majrm6e6n0eg760n9fs4g5jvwzh4ytp8e2d99mfgzv2e7mjmdwxse0ty73",
"feeder_retries": 4
"alliance_hub_contract_address": "terra1majrm6e6n0eg760n9fs4g5jvwzh4ytp8e2d99mfgzv2e7mjmdwxse0ty73",
"feeder_retries": 4,
"blocks_to_be_senior_validator": 10000,
"vote_on_proposals_to_be_senior_validator": 0
}
}

0 comments on commit 3a65650

Please sign in to comment.