forked from anilcse/cosmos-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14628f4
commit e30c4f3
Showing
5 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package src | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/vitwit/cosmos-utils/validator-inactive-alerter/config" | ||
"github.com/vitwit/cosmos-utils/validator-inactive-alerter/types" | ||
) | ||
|
||
func GetBlockSigns(cfg *config.Config) error { | ||
var latestBlock types.LatestBlock | ||
|
||
ops := HTTPOptions{ | ||
Endpoint: cfg.LCDEndpoint + "/blocks/latest", | ||
Method: http.MethodGet, | ||
} | ||
|
||
resp, err := HitHTTPTarget(ops) | ||
if err != nil { | ||
log.Printf("Error while getting latest block info: %v", err) | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(resp.Body, &latestBlock) | ||
if err != nil { | ||
log.Printf("Error while unmarshelling Block response: %v", err) | ||
return err | ||
} | ||
|
||
previousBlock, err := strconv.Atoi(latestBlock.Block.Header.Height) | ||
if err != nil { | ||
log.Printf("Error while converting height to int") | ||
} | ||
|
||
previousBlockHeight := strconv.Itoa(previousBlock - 1) | ||
ops = HTTPOptions{ | ||
Endpoint: "http://" + cfg.RPCEndpoint + "/block?height=" + previousBlockHeight, | ||
Method: http.MethodGet, | ||
} | ||
|
||
res, err := HitHTTPTarget(ops) | ||
if err != nil { | ||
log.Printf("Error while getting previous block details: %v", err) | ||
return err | ||
} | ||
|
||
var pBlock types.Block | ||
err = json.Unmarshal(res.Body, &pBlock) | ||
if err != nil { | ||
log.Printf("Error while unmarshelling previous Block response: %v", err) | ||
return err | ||
} | ||
|
||
log.Printf("Len of latest block sign : %v and previous block : %v", len(latestBlock.Block.LastCommit.Signatures), len(pBlock.Result.Block.LastCommit.Signatures)) | ||
|
||
count := 0 | ||
for _, v1 := range latestBlock.Block.LastCommit.Signatures { | ||
count = 0 | ||
val := v1.ValidatorAddress | ||
for _, v2 := range pBlock.Result.Block.LastCommit.Signatures { | ||
if val == v2.ValidatorAddress { | ||
count++ | ||
break | ||
} | ||
} | ||
if count == 0 { | ||
log.Printf("This validator has missed : %v", val) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package src | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
client "github.com/influxdata/influxdb1-client/v2" | ||
|
||
"github.com/vitwit/cosmos-utils/validator-inactive-alerter/config" | ||
) | ||
|
||
type targetRunner struct{} | ||
|
||
// NewRunner returns targetRunner | ||
func NewRunner() *targetRunner { | ||
return &targetRunner{} | ||
} | ||
|
||
// Run to run the request | ||
func (m targetRunner) Run(function func(ops HTTPOptions, cfg *config.Config, c client.Client), ops HTTPOptions, cfg *config.Config, c client.Client) { | ||
function(ops, cfg, c) | ||
} | ||
|
||
func addQueryParameters(req *http.Request, queryParams QueryParams) { | ||
params := url.Values{} | ||
for key, value := range queryParams { | ||
params.Add(key, value) | ||
} | ||
req.URL.RawQuery = params.Encode() | ||
} | ||
|
||
// newHTTPRequest to make a new http request | ||
func newHTTPRequest(ops HTTPOptions) (*http.Request, error) { | ||
// make new request | ||
req, err := http.NewRequest(ops.Method, ops.Endpoint, bytes.NewBuffer(ops.Body)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Add any query parameters to the URL. | ||
if len(ops.QueryParams) != 0 { | ||
addQueryParameters(req, ops.QueryParams) | ||
} | ||
|
||
return req, nil | ||
} | ||
|
||
func makeResponse(res *http.Response) (*PingResp, error) { | ||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return &PingResp{}, err | ||
} | ||
|
||
response := &PingResp{ | ||
StatusCode: res.StatusCode, | ||
Body: body, | ||
} | ||
_ = res.Body.Close() | ||
return response, nil | ||
} | ||
|
||
// HitHTTPTarget to hit the target and get response | ||
func HitHTTPTarget(ops HTTPOptions) (*PingResp, error) { | ||
req, err := newHTTPRequest(ops) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
httpcli := http.Client{Timeout: time.Duration(30 * time.Second)} | ||
resp, err := httpcli.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := makeResponse(resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package src | ||
|
||
type ( | ||
// QueryParams to map the query params of an url | ||
QueryParams map[string]string | ||
|
||
// HTTPOptions of a target | ||
HTTPOptions struct { | ||
Endpoint string | ||
QueryParams QueryParams | ||
Body []byte | ||
Method string | ||
} | ||
|
||
// PingResp struct | ||
PingResp struct { | ||
StatusCode int | ||
Body []byte | ||
} | ||
|
||
// AccountBalance struct which holds the parameters of an account amount | ||
AccountBalance struct { | ||
Balances []struct { | ||
Denom string `json:"denom"` | ||
Amount string `json:"amount"` | ||
} `json:"balances"` | ||
Pagination interface{} `json:"pagination"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package types | ||
|
||
import ( | ||
"time" | ||
|
||
"gopkg.in/mgo.v2/bson" | ||
) | ||
|
||
type ( | ||
ValidatorSet struct { | ||
ID bson.ObjectId `json:"_id" bson:"_id,omitempty"` | ||
ValidatorAddress string `json:"validator_address" bson:"validator_address"` | ||
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"` | ||
HexAddress string `json:"hex_address" bson:"hex_address"` | ||
Status string `json:"status" bson:"status"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package types | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type ( | ||
LatestBlock struct { | ||
BlockID interface{} `json:"block_id"` | ||
Block struct { | ||
Header struct { | ||
Height string `json:"height"` | ||
} `json:"header"` | ||
Data struct { | ||
Txs []interface{} `json:"txs"` | ||
} `json:"data"` | ||
Evidence struct { | ||
Evidence []interface{} `json:"evidence"` | ||
} `json:"evidence"` | ||
LastCommit struct { | ||
Height string `json:"height"` | ||
Round int `json:"round"` | ||
BlockID struct { | ||
Hash string `json:"hash"` | ||
Parts struct { | ||
Total int `json:"total"` | ||
Hash string `json:"hash"` | ||
} `json:"parts"` | ||
} `json:"block_id"` | ||
Signatures []struct { | ||
BlockIDFlag int `json:"block_id_flag"` | ||
ValidatorAddress string `json:"validator_address"` | ||
Timestamp time.Time `json:"timestamp"` | ||
Signature string `json:"signature"` | ||
} `json:"signatures"` | ||
} `json:"last_commit"` | ||
} `json:"block"` | ||
} | ||
|
||
Block struct { | ||
Jsonrpc string `json:"jsonrpc"` | ||
ID int `json:"id"` | ||
Result struct { | ||
BlockID interface{} `json:"block_id"` | ||
Block struct { | ||
Header interface{} `json:"header"` | ||
Data struct { | ||
Txs []string `json:"txs"` | ||
} `json:"data"` | ||
Evidence struct { | ||
Evidence []interface{} `json:"evidence"` | ||
} `json:"evidence"` | ||
LastCommit struct { | ||
Height string `json:"height"` | ||
Round int `json:"round"` | ||
BlockID struct { | ||
Hash string `json:"hash"` | ||
Parts struct { | ||
Total int `json:"total"` | ||
Hash string `json:"hash"` | ||
} `json:"parts"` | ||
} `json:"block_id"` | ||
Signatures []struct { | ||
BlockIDFlag int `json:"block_id_flag"` | ||
ValidatorAddress string `json:"validator_address"` | ||
Timestamp time.Time `json:"timestamp"` | ||
Signature string `json:"signature"` | ||
} `json:"signatures"` | ||
} `json:"last_commit"` | ||
} `json:"block"` | ||
} `json:"result"` | ||
} | ||
) |