From e3d95d29edc34e266a9c19dfd5137ee5f9150d6c Mon Sep 17 00:00:00 2001 From: hoangdz_97 Date: Mon, 6 Sep 2021 14:14:41 +0700 Subject: [PATCH 1/5] Update default validator image --- exporter/validator.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/exporter/validator.go b/exporter/validator.go index 872d59f..699a7ae 100644 --- a/exporter/validator.go +++ b/exporter/validator.go @@ -2,6 +2,7 @@ package exporter import ( "encoding/base64" + "fmt" "github.com/cosmos-gaminghub/explorer-backend/client" "github.com/cosmos-gaminghub/explorer-backend/conf" @@ -14,6 +15,10 @@ import ( "gopkg.in/mgo.v2/bson" ) +const ( + ImageDefault = "https://raw.githubusercontent.com/cosmostation/cosmostation_token_resource/master/moniker/cosmoshub/%s.png" +) + // getValidators parses validators information and wrap into Precommit schema struct func GetValidators(vals []types.Validator, validatorSets []types.ValidatorOfValidatorSet) (validators []*schema.Validator, err error) { validatorSetsFormat := client.FormatValidatorSetPubkeyToAddress(validatorSets) @@ -26,6 +31,7 @@ func GetValidators(vals []types.Validator, validatorSets []types.ValidatorOfVali _, decodeByte, _ := bech32.DecodeAndConvert(consensusAddress) str := base64.StdEncoding.EncodeToString(decodeByte) + validator.Description.ImageUrl = fmt.Sprintf(ImageDefault, validator.OperatorAddress) if validator.Description.ImageUrl == "" && validator.Description.Identity != "" { validator.Description.ImageUrl = client.GetImageUrl(validator.Description.Identity) } From 50087f25c351fc068f309f67f4d22dd4b1fccd41 Mon Sep 17 00:00:00 2001 From: hoangdz_97 Date: Mon, 13 Sep 2021 11:57:29 +0700 Subject: [PATCH 2/5] Feature missed block count --- client/client.go | 16 +++++++++++- exporter/validator.go | 36 ++++++++++++++++---------- lcd/types.go | 10 ++++++++ orm/document/validator.go | 29 +++++++++++---------- schema/validator.go | 54 ++++++++++++++++++++------------------- 5 files changed, 91 insertions(+), 54 deletions(-) diff --git a/client/client.go b/client/client.go index 14236c3..eed98a4 100644 --- a/client/client.go +++ b/client/client.go @@ -235,7 +235,6 @@ func GetProposalProposer(proposalId int) (types.ProposalProposerResult, error) { logger.Error("Get proposal proposer error", logger.String("err", err.Error())) } - fmt.Println(string(resBytes)) var result types.ProposalProposerResult if err := json.Unmarshal(resBytes, &result); err != nil { logger.Error("Unmarshal proposal proposer error", logger.String("err", err.Error())) @@ -267,3 +266,18 @@ func GetProposalVotes(proposalId int, offset int) ([]types.ProposalVote, error) return votes, nil } + +func GetValSigningInfo(consensusAddress string) (types.ValSigningInfo, error) { + url := fmt.Sprintf(lcd.UrlValSigningInfo, conf.Get().Hub.LcdUrl, consensusAddress) + resBytes, err := utils.Get(url) + if err != nil { + logger.Error("Get val signing info error address:"+consensusAddress, logger.String("err", err.Error())) + } + + var result types.ValSigningInfo + if err := json.Unmarshal(resBytes, &result); err != nil { + logger.Error(fmt.Sprintf("Unmarshal val signing info error address: %s", consensusAddress), logger.String("err", err.Error())) + } + + return result, nil +} diff --git a/exporter/validator.go b/exporter/validator.go index 699a7ae..cf5ad04 100644 --- a/exporter/validator.go +++ b/exporter/validator.go @@ -3,6 +3,7 @@ package exporter import ( "encoding/base64" "fmt" + "strconv" "github.com/cosmos-gaminghub/explorer-backend/client" "github.com/cosmos-gaminghub/explorer-backend/conf" @@ -36,20 +37,29 @@ func GetValidators(vals []types.Validator, validatorSets []types.ValidatorOfVali validator.Description.ImageUrl = client.GetImageUrl(validator.Description.Identity) } + var missedBlockCount int64 + if consensusAddress != "" { + valSigningInfo, err := client.GetValSigningInfo(consensusAddress) + if err == nil { + missedBlockCount, _ = strconv.ParseInt(valSigningInfo.Info.MissedBlocksCount, 10, 64) + } + } + val := &schema.Validator{ - OperatorAddr: validator.OperatorAddress, - ConsensusAddres: consensusAddress, - ConsensusPubkey: validator.ConsensusPubkey.Key, - AccountAddr: utils.Convert(conf.Get().Db.AddresPrefix, validator.OperatorAddress), - Jailed: validator.Jailed, - Status: validator.Status, - Tokens: tokens, - DelegatorShares: validator.DelegatorShares, - Description: validator.Description, - UnbondingHeight: validator.UnbondingHeight, - UnbondingTime: validator.UnbondingTime, - Commission: validator.Commission, - ProposerAddr: str, + OperatorAddr: validator.OperatorAddress, + ConsensusAddres: consensusAddress, + ConsensusPubkey: validator.ConsensusPubkey.Key, + AccountAddr: utils.Convert(conf.Get().Db.AddresPrefix, validator.OperatorAddress), + Jailed: validator.Jailed, + Status: validator.Status, + Tokens: tokens, + DelegatorShares: validator.DelegatorShares, + Description: validator.Description, + UnbondingHeight: validator.UnbondingHeight, + UnbondingTime: validator.UnbondingTime, + Commission: validator.Commission, + ProposerAddr: str, + TotalMissedBlock: missedBlockCount, } validators = append(validators, val) } diff --git a/lcd/types.go b/lcd/types.go index af7059f..44359fc 100644 --- a/lcd/types.go +++ b/lcd/types.go @@ -41,6 +41,7 @@ const ( UrlLookupIconsByKeySuffix = "https://keybase.io/_/api/1.0/user/lookup.json?fields=pictures&key_suffix=%s" UrlAssetTokens = "%s/asset/tokens" UrlAssetGateways = "%s/asset/gateways" + UrlValSigningInfo = "%s/cosmos/slashing/v1beta1/signing_infos/%s" UrlProposal = "%s/cosmos/gov/v1beta1/proposals" UrlProposalDeposit = "%s/cosmos/gov/v1beta1/proposals/%d/deposits" @@ -822,3 +823,12 @@ type ProposalProposerResult struct { Proposer string `json:"proposer"` } `json:"result"` } + +type ValSigningInfo struct { + Info struct { + Address string `json:"address"` + StartHeight string `json:"start_height"` + IndexOffset string `json:"index_offset"` + MissedBlocksCount string `json:"missed_blocks_counter"` + } `json:"val_signing_info"` +} diff --git a/orm/document/validator.go b/orm/document/validator.go index 6bdb0ec..83825cb 100644 --- a/orm/document/validator.go +++ b/orm/document/validator.go @@ -28,20 +28,21 @@ const ( ) type Validator struct { - OperatorAddr string `json:"operator_address"` - ConsensusPubkey string `json:"consensus_pubkey"` - ConsensusAddres string `json:"consensus_address"` - AccountAddr string `json:"account_address"` - Jailed bool `json:"jailed"` - Status string `json:"status"` - Tokens int64 `json:"tokens"` - DelegatorShares string `json:"delegator_shares"` - Description types.Description `json:"description"` - UnbondingHeight string `json:"unbonding_height"` - UnbondingTime time.Time `json:"unbonding_time"` - Commission types.Commission `json:"commission"` - ProposerAddr string `json:"proposer_addr"` - Icons string `json:"icons"` + OperatorAddr string `json:"operator_address"` + ConsensusPubkey string `json:"consensus_pubkey"` + ConsensusAddres string `json:"consensus_address"` + AccountAddr string `json:"account_address"` + Jailed bool `json:"jailed"` + Status string `json:"status"` + Tokens int64 `json:"tokens"` + DelegatorShares string `json:"delegator_shares"` + Description types.Description `json:"description"` + UnbondingHeight string `json:"unbonding_height"` + UnbondingTime time.Time `json:"unbonding_time"` + Commission types.Commission `json:"commission"` + ProposerAddr string `json:"proposer_addr"` + Icons string `json:"icons"` + TotalMissedBlock int64 `bson:"total_missed_block"` } func (v Validator) GetValidatorList() ([]Validator, error) { diff --git a/schema/validator.go b/schema/validator.go index ba4ffa9..bed1a5a 100644 --- a/schema/validator.go +++ b/schema/validator.go @@ -8,36 +8,38 @@ import ( // Validator defines the structure for validator information. type Validator struct { - OperatorAddr string `bson:"operator_address"` - ConsensusPubkey string `bson:"consensus_pubkey"` - ConsensusAddres string `bson:"consensus_address"` - AccountAddr string `bson:"account_address"` - Jailed bool `bson:"jailed"` - Status string `bson:"status"` - Tokens int64 `bson:"tokens" json:"tokens"` - DelegatorShares string `bson:"delegator_shares"` - Description types.Description `bson:"description" json:"description"` - UnbondingHeight string `bson:"unbonding_height"` - UnbondingTime time.Time `bson:"unbonding_time"` - Commission types.Commission `bson:"commission" json:"commission"` - ProposerAddr string `bson:"proposer_addr"` - Icons string `bson:"icons"` + OperatorAddr string `bson:"operator_address"` + ConsensusPubkey string `bson:"consensus_pubkey"` + ConsensusAddres string `bson:"consensus_address"` + AccountAddr string `bson:"account_address"` + Jailed bool `bson:"jailed"` + Status string `bson:"status"` + Tokens int64 `bson:"tokens" json:"tokens"` + DelegatorShares string `bson:"delegator_shares"` + Description types.Description `bson:"description" json:"description"` + UnbondingHeight string `bson:"unbonding_height"` + UnbondingTime time.Time `bson:"unbonding_time"` + Commission types.Commission `bson:"commission" json:"commission"` + ProposerAddr string `bson:"proposer_addr"` + Icons string `bson:"icons"` + TotalMissedBlock int64 `bson:"total_missed_block"` } // NewValidator returns a new Validator. func NewValidator(v Validator) *Validator { return &Validator{ - OperatorAddr: v.OperatorAddr, - ConsensusPubkey: v.ConsensusPubkey, - ConsensusAddres: v.ConsensusAddres, - AccountAddr: v.AccountAddr, - Jailed: v.Jailed, - Status: v.Status, - Tokens: v.Tokens, - DelegatorShares: v.DelegatorShares, - Description: v.Description, - UnbondingHeight: v.UnbondingHeight, - UnbondingTime: v.UnbondingTime, - Commission: v.Commission, + OperatorAddr: v.OperatorAddr, + ConsensusPubkey: v.ConsensusPubkey, + ConsensusAddres: v.ConsensusAddres, + AccountAddr: v.AccountAddr, + Jailed: v.Jailed, + Status: v.Status, + Tokens: v.Tokens, + DelegatorShares: v.DelegatorShares, + Description: v.Description, + UnbondingHeight: v.UnbondingHeight, + UnbondingTime: v.UnbondingTime, + Commission: v.Commission, + TotalMissedBlock: v.TotalMissedBlock, } } From df473a4788cffd7537dcbd50e4e1a72d67bbe2d7 Mon Sep 17 00:00:00 2001 From: hoangdz_97 Date: Mon, 13 Sep 2021 11:58:45 +0700 Subject: [PATCH 3/5] Delete default validator image --- exporter/validator.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/exporter/validator.go b/exporter/validator.go index cf5ad04..02e6958 100644 --- a/exporter/validator.go +++ b/exporter/validator.go @@ -2,7 +2,6 @@ package exporter import ( "encoding/base64" - "fmt" "strconv" "github.com/cosmos-gaminghub/explorer-backend/client" @@ -16,10 +15,6 @@ import ( "gopkg.in/mgo.v2/bson" ) -const ( - ImageDefault = "https://raw.githubusercontent.com/cosmostation/cosmostation_token_resource/master/moniker/cosmoshub/%s.png" -) - // getValidators parses validators information and wrap into Precommit schema struct func GetValidators(vals []types.Validator, validatorSets []types.ValidatorOfValidatorSet) (validators []*schema.Validator, err error) { validatorSetsFormat := client.FormatValidatorSetPubkeyToAddress(validatorSets) @@ -32,7 +27,6 @@ func GetValidators(vals []types.Validator, validatorSets []types.ValidatorOfVali _, decodeByte, _ := bech32.DecodeAndConvert(consensusAddress) str := base64.StdEncoding.EncodeToString(decodeByte) - validator.Description.ImageUrl = fmt.Sprintf(ImageDefault, validator.OperatorAddress) if validator.Description.ImageUrl == "" && validator.Description.Identity != "" { validator.Description.ImageUrl = client.GetImageUrl(validator.Description.Identity) } From a9b1d4b60e4df6c5e85624b05ade9e34406be86f Mon Sep 17 00:00:00 2001 From: hoangdz_97 Date: Tue, 14 Sep 2021 13:42:18 +0700 Subject: [PATCH 4/5] Update struct validator --- orm/document/validator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orm/document/validator.go b/orm/document/validator.go index 83825cb..55ee8a4 100644 --- a/orm/document/validator.go +++ b/orm/document/validator.go @@ -42,7 +42,7 @@ type Validator struct { Commission types.Commission `json:"commission"` ProposerAddr string `json:"proposer_addr"` Icons string `json:"icons"` - TotalMissedBlock int64 `bson:"total_missed_block"` + TotalMissedBlock int64 `json:"total_missed_block"` } func (v Validator) GetValidatorList() ([]Validator, error) { From 627bc6620c88d40e4a9caaf5bca3679c486397ec Mon Sep 17 00:00:00 2001 From: hoangdz_97 Date: Thu, 16 Sep 2021 10:11:45 +0700 Subject: [PATCH 5/5] Feature proposal tally --- client/client.go | 15 +++++++++++++++ exporter/proposal.go | 4 +++- lcd/types.go | 7 ++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/client/client.go b/client/client.go index eed98a4..0065a67 100644 --- a/client/client.go +++ b/client/client.go @@ -281,3 +281,18 @@ func GetValSigningInfo(consensusAddress string) (types.ValSigningInfo, error) { return result, nil } + +func GetProposalTally(id int) (types.PropsalTally, error) { + url := fmt.Sprintf(lcd.UrlProposalTally, conf.Get().Hub.LcdUrl, id) + resBytes, err := utils.Get(url) + if err != nil { + logger.Error(fmt.Sprintf("Get proposal tally error proposal: %d", id), logger.String("err", err.Error())) + } + + var result types.PropsalTally + if err := json.Unmarshal(resBytes, &result); err != nil { + logger.Error(fmt.Sprintf("Unmarshal proposal tally error proposal: %d", id), logger.String("err", err.Error())) + } + + return result, nil +} diff --git a/exporter/proposal.go b/exporter/proposal.go index 81fd255..c411f90 100644 --- a/exporter/proposal.go +++ b/exporter/proposal.go @@ -4,6 +4,7 @@ import ( "fmt" "strconv" + "github.com/cosmos-gaminghub/explorer-backend/client" types "github.com/cosmos-gaminghub/explorer-backend/lcd" "github.com/cosmos-gaminghub/explorer-backend/orm" "github.com/cosmos-gaminghub/explorer-backend/orm/document" @@ -27,12 +28,13 @@ func GetProposals(ps types.ProposalResult) (proposals []*schema.Proposal, err er fmt.Println(err.Error()) continue } + proposalTally, _ := client.GetProposalTally(proposalId) pro := &schema.Proposal{ ProposalId: proposalId, ProposalStatus: proposal.Status, Content: proposal.Content, SubmitTime: proposal.SubmitTime, - FinalTallyResult: proposal.FinalTallyResult, + FinalTallyResult: proposalTally.Tally, VotingEndTime: proposal.VotingEndTime, VotingStartTime: proposal.VotingStartTime, DepositEndTime: proposal.DepositEndTime, diff --git a/lcd/types.go b/lcd/types.go index 44359fc..0354f89 100644 --- a/lcd/types.go +++ b/lcd/types.go @@ -47,6 +47,7 @@ const ( UrlProposalDeposit = "%s/cosmos/gov/v1beta1/proposals/%d/deposits" UrlProposalProposer = "%s/gov/proposals/%d/proposer" UrlProposalVoters = "%s/cosmos/gov/v1beta1/proposals/%d/votes?pagination.offset=%d" + UrlProposalTally = "%s/cosmos/gov/v1beta1/proposals/%d/tally" DefaultValidatorSetLimit = 100 DefaultValidatorLimit = 200 @@ -783,7 +784,7 @@ type ProposalFinalTallyResult struct { Yes string `json:"yes"` Abstain string `json:"abstain"` No string `json:"no"` - NoWithVeto string `bson:"no_with_veto" json:"no_with_veto"` + NoWithVeto string `json:"no_with_veto"` } type ProposalDepositResult struct { @@ -832,3 +833,7 @@ type ValSigningInfo struct { MissedBlocksCount string `json:"missed_blocks_counter"` } `json:"val_signing_info"` } + +type PropsalTally struct { + Tally ProposalFinalTallyResult `json:"tally"` +}