Skip to content

Commit

Permalink
Merge pull request #1 from node-a-team/websocket
Browse files Browse the repository at this point in the history
Use websocket
  • Loading branch information
wlsaud619 authored Oct 8, 2019
2 parents 9017b9a + 85e13e0 commit 1da43ea
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 93 deletions.
2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ address = "localhost:26657"
address = "localhost:1317"

[validator_info]
operatorAddress = ""
operatorAddress = "cosmosvaloper14l0fp639yudfl46zauvv8rkzjgd4u0zk2aseys"

[option]
exporterListenPort = "26661"
Expand Down
16 changes: 10 additions & 6 deletions function/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ func BlockStatus() t.BlockStatus {

var blockStatus t.BlockStatus

cmd := "curl -s " + t.RpcServer + "/block"
cmd := "curl -s " + t.RestServer + "/blocks/latest"
out, _ := exec.Command("/bin/bash", "-c", cmd).Output()
json.Unmarshal(out, &blockStatus)

currentBlockHeight, _ := strconv.Atoi(blockStatus.Result.Block.Header.Height)
currentBlockHeight, _ := strconv.Atoi(blockStatus.Block.Header.Height)


// 현재 precommit 정보와 현재 blockHeight를 맞추기 위해 이전 블록 정보로 표시
blockStatus = previousBlockStatus(currentBlockHeight - 1)


return blockStatus

}
Expand All @@ -33,10 +35,11 @@ func previousBlockStatus(blockHeight int) t.BlockStatus {

var blockStatus t.BlockStatus

cmd := "curl -s " + t.RpcServer + "/block?height=" + fmt.Sprint(blockHeight)
cmd := "curl -s " + t.RestServer + "/blocks/" + fmt.Sprint(blockHeight)
out, _ := exec.Command("/bin/bash", "-c", cmd).Output()
json.Unmarshal(out, &blockStatus)


return blockStatus

}
Expand All @@ -45,11 +48,12 @@ func CalcBlockTime(currentBlockStatus t.BlockStatus) float64 {

var blockTime float64

currentBlockHeight, _ := strconv.Atoi(currentBlockStatus.Result.Block.Header.Height)

currentBlockHeight, _ := strconv.Atoi(currentBlockStatus.Block.Header.Height)
previousBlockHeight := currentBlockHeight - 1

currentBlockTime := currentBlockStatus.Result.Block.Header.Time
previousBlockTime := previousBlockStatus(previousBlockHeight).Result.Block.Header.Time
currentBlockTime := currentBlockStatus.Block.Header.Time
previousBlockTime := previousBlockStatus(previousBlockHeight).Block.Header.Time

if previousBlockTime.IsZero() {
blockTime = 0.0
Expand Down
16 changes: 9 additions & 7 deletions function/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
t "github.com/node-a-team/cosmos-validator_exporter/types"

"encoding/json"
"fmt"
"os/exec"
)

func CommitStatus(blockHeight int) t.CommitStatus {

var commitStatus t.CommitStatus

cmd := "curl -s -XGET " + t.RpcServer + "/commit?height=" + fmt.Sprint(blockHeight) + " -H \"accept:application/json\""
out, _ := exec.Command("/bin/bash", "-c", cmd).Output()
json.Unmarshal(out, &commitStatus)
var blockHeightInt64 int64 = int64(blockHeight)

info, _ := t.Client.Commit(&blockHeightInt64)
infoMarshal, _ := json.Marshal(info)
json.Unmarshal(infoMarshal, &commitStatus)

return commitStatus

Expand All @@ -25,7 +25,8 @@ func ValidatorPrecommitStatus(commitStatus t.CommitStatus, consHexAddr string) i
// validatorCommitStatus(0: false, 1 : true)
validatorCommitStatus := 0

precommitData := commitStatus.Result.Signed_header.Commit.Precommits
precommitData := commitStatus.Signed_header.Commit.Precommits
// precommitData := commitStatus.Result.Signed_header.Commit.Precommits

for _, value := range precommitData {

Expand All @@ -42,7 +43,8 @@ func PrecommitRate(commitStatus t.CommitStatus) float64 {
// validatorCommitStatus(0: false, 1 : true)
precommitRate := 0.0

precommitData := commitStatus.Result.Signed_header.Commit.Precommits
precommitData := commitStatus.Signed_header.Commit.Precommits
// precommitData := commitStatus.Result.Signed_header.Commit.Precommits
totalCount, precommitCount := len(precommitData), len(precommitData)

for _, value := range precommitData {
Expand Down
42 changes: 40 additions & 2 deletions function/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"time"
"os"
"os/exec"
"log"

"github.com/BurntSushi/toml"
Expand All @@ -17,17 +18,19 @@ func Init() {

var config = readConfig()

fmt.Printf("\033[7m##### %s #####\033[0m\n", config.Title)
fmt.Printf("\033[7m##### Exporter Start #####\033[0m\n\n")

t.RpcServer = config.Rpc.Address
t.RestServer = config.Rest_server.Address

serverChecker()

t.OperatorAddr = config.Validator_info.OperatorAddress
t.ExporterListenPort = config.Option.ExporterListenPort
t.OutputPrint = config.Option.OutputPrint
t.Bech32MainPrefix = config.Network

fmt.Println("\n[ Your Info ]")
fmt.Println("\n[ Your Info]")
fmt.Println("- Network:", config.Network)
fmt.Println("- RPC Server Address:", config.Rpc.Address)
fmt.Println("- Rest Server Address:", config.Rest_server.Address)
Expand Down Expand Up @@ -63,3 +66,38 @@ func readConfig() t.Config {
return config

}

func serverChecker() {

fmt.Printf("RPC-Server Check..")
cmd := "curl -s -XGET " + t.RpcServer + "/abci_info" +" -H \"accept:application/json\""
cmdRun := exec.Command("/bin/bash", "-c", cmd)
timer(cmdRun)


fmt.Printf("Rest-Server Check..")
cmd = "curl -s -XGET " + t.RestServer + "/node_info" +" -H \"accept:application/json\""
cmdRun = exec.Command("/bin/bash", "-c", cmd)
timer(cmdRun)
}

func timer(cmdRun *exec.Cmd) {

if err := cmdRun.Start(); err != nil {
log.Fatal(err)
}

timer := time.AfterFunc(5 * time.Second, func() {
cmdRun.Process.Kill()
})

if err := cmdRun.Wait(); err != nil {

fmt.Println("Failure!")
panic("Error !!!")
} else {
fmt.Println("Success!")
}

timer.Stop()
}
20 changes: 0 additions & 20 deletions function/consensus/consensus.go

This file was deleted.

29 changes: 11 additions & 18 deletions function/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

block "github.com/node-a-team/cosmos-validator_exporter/function/block"
commit "github.com/node-a-team/cosmos-validator_exporter/function/commit"
consensus "github.com/node-a-team/cosmos-validator_exporter/function/consensus"
minting "github.com/node-a-team/cosmos-validator_exporter/function/minting"
keyutil "github.com/node-a-team/cosmos-validator_exporter/function/keyutil"
staking "github.com/node-a-team/cosmos-validator_exporter/function/staking"
Expand Down Expand Up @@ -79,19 +78,21 @@ func Exporter() {
for {

blockStatus := block.BlockStatus()
currentBlockHeight, _ := strconv.Atoi(blockStatus.Result.Block.Header.Height)
consensusStatus := consensus.ConsensusStatus()
currentBlockHeight, _ := strconv.Atoi(blockStatus.Block.Header.Height)
// consensusStatus := consensus.ConsensusStatus()
commitStatus := commit.CommitStatus(currentBlockHeight)
mintingParamsStatus := minting.MintingParamsStatus()



// validators
validatorCountOrigin, validatorsetsStatus := validators.ValidatorsetsStatus()
validatorsStatus := validators.ValidatorsStatus()

// block
chainId := blockStatus.Result.Block.Header.Chain_id
blockTime := blockStatus.Result.Block.Header.Time.Format("060102 15:04:05")
blockHeight := utils.StringToFloat64(blockStatus.Result.Block.Header.Height)
chainId := blockStatus.Block.Header.Chain_id
blockTime := blockStatus.Block.Header.Time.Format("060102 15:04:05")
blockHeight := utils.StringToFloat64(blockStatus.Block.Header.Height)
currentBlockTime := block.CalcBlockTime(blockStatus)

// minting
Expand All @@ -107,10 +108,11 @@ func Exporter() {
// commit
precommitRate := commit.PrecommitRate(commitStatus) * 100
validatorCount := float64(validatorCountOrigin)
proposerConsHexAddress := blockStatus.Result.Block.Header.Proposer_address
proposerMoniker := block.ProposerMoniker(blockStatus.Result.Block.Header.Proposer_address, validatorsetsStatus, validatorsStatus)
proposerConsHexAddress := blockStatus.Block.Header.Proposer_address
proposerMoniker := block.ProposerMoniker(blockStatus.Block.Header.Proposer_address, validatorsetsStatus, validatorsStatus)
proposerWalletAccountNumber := float64(0.0)


// staking
notBondedTokensOrigin, bondedTokensOrigin := staking.GetStakingPool()
totalBondedTokensOrigin := notBondedTokensOrigin + bondedTokensOrigin
Expand All @@ -120,11 +122,6 @@ func Exporter() {
totalBondedTokens := totalBondedTokensOrigin / math.Pow10(6)
bondedRate := bondedTokensOrigin / totalBondedTokensOrigin

// consensus
latestRound := len(consensusStatus.Result.Round_state.Height_vote_set) - 1
heightRoundStep := consensusStatus.Result.Round_state.Status
prevotes := consensusStatus.Result.Round_state.Height_vote_set[latestRound].Prevotes_bit_array
precommit := consensusStatus.Result.Round_state.Height_vote_set[latestRound].Precommits_bit_array

// csv file export(validatorsAccountNumber)
if fileExportChecker == 0 || int(blockHeight)%baseBlockForFileExport == 0 {
Expand Down Expand Up @@ -195,7 +192,7 @@ func Exporter() {
consHexAddress := keyutil.RunFromBech32(consBech32Address)

// etc
proposingStatus := float64(utils.GetPoposingCheck(blockStatus.Result.Block.Header.Proposer_address, consHexAddress))
proposingStatus := float64(utils.GetPoposingCheck(blockStatus.Block.Header.Proposer_address, consHexAddress))
delegatorCount, selfDelegationAmountOrigin := validators.ValidatorDelegatorNumber(operatorAddress, accountAddress)
delegationRatio := delegatorShares / bondedTokens
selfDelegationAmount := selfDelegationAmountOrigin / math.Pow10(6)
Expand All @@ -214,10 +211,6 @@ func Exporter() {
fmt.Printf(" - Proposer: %s(%s)\n", proposerMoniker, proposerConsHexAddress)
fmt.Printf(" - PrecommitRate: %f\n", precommitRate)

fmt.Println(" - height/round/step:", heightRoundStep)
fmt.Println(" - prevotes:", prevotes)
fmt.Println(" - precommit: ", precommit)

fmt.Println("\n - notBondedTokens: ", notBondedTokens)
fmt.Println(" - bondedTokens: ", bondedTokens)
fmt.Println(" - totalTokens: ", totalBondedTokens)
Expand Down
28 changes: 28 additions & 0 deletions function/websocket/websocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package websocket

import (
t "github.com/node-a-team/cosmos-validator_exporter/types"
tmclient "github.com/tendermint/tendermint/rpc/client"
// "encoding/json"
"fmt"
// "os/exec"
// "strconv"

)


func OpenSocket() {
t.Client = tmclient.NewHTTP("tcp://"+t.RpcServer, "/websocket")

err := t.Client.Start()
if err != nil {
// handle error
fmt.Println("######## RPC/websockets Connection Error ########")
}
defer t.Client.Stop()

fmt.Println("####### RPC/websockets Connect -> tcp://"+t.RpcServer +" ########")
}



2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ go 1.12

require (
github.com/BurntSushi/toml v0.3.1
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/prometheus/client_golang v1.1.0
github.com/tendermint/tendermint v0.32.3
)
Loading

0 comments on commit 1da43ea

Please sign in to comment.