Skip to content

Commit

Permalink
feat: api added
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchon committed Mar 27, 2024
1 parent 7d198e8 commit cdd76ed
Show file tree
Hide file tree
Showing 13 changed files with 425 additions and 30 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Lint

on:
pull_request:
push:
branches:
- main
jobs:
golangci:
name: Run golangci-lint
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/setup-go@v5
with:
go-version: "1.22"
check-latest: true
- uses: actions/checkout@v4
- uses: technote-space/[email protected]
with:
PATTERNS: |
**/**.go
go.mod
go.sum
- uses: golangci/[email protected]
with:
version: latest
args: --timeout 10m
github-token: ${{ secrets.github_token }}
# Check only if there are differences in the source code
if: "env.GIT_DIFF"
49 changes: 49 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
run:
tests: true
timeout: 5m
concurrency: 4

linters:
enable:
- dogsled
- errcheck
- goconst
- gocritic
- gofumpt
- revive
- gosec
- gosimple
- govet
- ineffassign
# - lll TODO: enable
- misspell
- nakedret
- prealloc
- exportloopref
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- nolintlint
- asciicheck
- exportloopref
- gofumpt

linters-settings:
dogsled:
max-blank-identifiers: 3
golint:
min-confidence: 0
maligned:
suggest-new: true
misspell:
locale: US
nolintlint:
allow-unused: false
allow-leading-space: true
require-explanation: false
require-specific: false
gofumpt:
lang-version: "1.22"
87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ Config Example:
"cosmosapi": "localhost:1317",
"cosmosrpc": "localhost:26656",
"pruneoffset": 100,
"refreshduration": "5s"
"refreshduration": "5s",
"apimaxlimit": 100
}
```

- `Pruneoffset` is the value of your node pruned settings. The program will use the information from the latest height - pruneoffset to index the chain
- `Refreshduration` is the time the program will wait until pulling new blocks values
- `APIMaxLimit` is the max range allowed by the api, if the range if bigger than the one set, the query will overwrite the `end` value

## Run

- Clone the repo
- Create the `config.json` file in the root of the repo
- Run `make run`

## Database

Expand All @@ -36,3 +44,80 @@ make generate
```

_NOTE: you need `sqlc` to generate the files, run `make install-deps` if you need it_

## Queries:

### Request all the validators info by block height

- Request

```sh
curl "http://localhost:42069/api?start=7313145"
```

_NOTE: optional parameter `end` to set a rangeof blocks_

- Response

```json
{
"values": [
{
"ID": 3337,
"Height": 7313145,
"ValidatorID": 20,
"Missed": 0,
"ID_2": 20,
"OperatorAddress": "ethmvaloper1kdtjxywfvwq94jsst2uyshwkel6dwdv5vlf4l2",
"Pubkey": "TP8Ncm5KUUT3bQgN5SsQADVtPEwaW7O0MY5yX+ztJuE=",
"ValidatorAddress": "ethmvalcons1qwcmnlr2kwuunpmh0s4pshrgz5zqd9m7ljs8lu",
"Moniker": "evmOS",
"Indentity": ""
},
{
"ID": 3338,
"Height": 7313145,
"ValidatorID": 13,
"Missed": 2,
"ID_2": 13,
"OperatorAddress": "ethmvaloper1weh6nan3p8cpg7hsfke8teksjnf5pdl8ve97ny",
"Pubkey": "LxQKOI9n5enbjflu802ZL77lyYLHuejIu9o3FXQ3EOc=",
"ValidatorAddress": "ethmvalcons1qkxa6u5tdr820pv9kjc5rlf8p0r7qqr25fenyw",
"Moniker": "peersyst-node-3",
"Indentity": "14329789E9E20C43"
},
...
]
}
```

### Request all one validators info by block height

- Request

```sh
curl "http://localhost:42069/api?start=7313145&validator=ethmvaloper1weh6nan3p8cpg7hsfke8teksjnf5pdl8ve97ny"
```

_NOTE: optional parameter `end` to set a rangeof blocks_

- Response

```json
{
"values": [
{
"ID": 3338,
"Height": 7313145,
"ValidatorID": 13,
"Missed": 2,
"ID_2": 13,
"OperatorAddress": "ethmvaloper1weh6nan3p8cpg7hsfke8teksjnf5pdl8ve97ny",
"Pubkey": "LxQKOI9n5enbjflu802ZL77lyYLHuejIu9o3FXQ3EOc=",
"ValidatorAddress": "ethmvalcons1qkxa6u5tdr820pv9kjc5rlf8p0r7qqr25fenyw",
"Moniker": "peersyst-node-3",
"Indentity": "14329789E9E20C43"
}
]
}
```
4 changes: 2 additions & 2 deletions cmd/status/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main

import (
_ "github.com/mattn/go-sqlite3"

"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"

_ "github.com/mattn/go-sqlite3"

"github.com/evmos/validator-status/pkg/config"
"github.com/evmos/validator-status/pkg/database"
"github.com/evmos/validator-status/pkg/logger"
Expand Down
1 change: 1 addition & 0 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type Config struct {
CosmosAPI string `json:"cosmosapi"`
CosmosRPC string `json:"cosmosrpc"`
PruneOffset int `json:"pruneoffset"`
APIMaxLimit int `json:"apimaxlimit"`
}
6 changes: 6 additions & 0 deletions pkg/cosmos/missed_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ type MissedBlockResponse struct {
func (c *Cosmos) getMissedBlocksByHeight(height string) (MissedBlockResponse, error) {
// NOTE: assumes that we do not need pagination
reqGo, err := http.NewRequest("GET", c.apiURL+"/cosmos/slashing/v1beta1/signing_infos", nil)
if err != nil {
return MissedBlockResponse{}, fmt.Errorf("error creating the request %s", err.Error())
}
reqGo.Header.Set("x-cosmos-block-height", height)

resp, err := http.DefaultClient.Do(reqGo)
if err != nil {
return MissedBlockResponse{}, fmt.Errorf("error making the request %s", err.Error())
}
if resp.StatusCode != http.StatusOK {
return MissedBlockResponse{}, fmt.Errorf("status code %d", resp.StatusCode)
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/cosmos/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ type ValidatorsResponse struct {

func (c *Cosmos) getValidators(height string) (ValidatorsResponse, error) {
reqGo, err := http.NewRequest("GET", c.apiURL+"/cosmos/staking/v1beta1/validators", nil)
if err != nil {
return ValidatorsResponse{}, fmt.Errorf("error creating the request %s", err.Error())
}

reqGo.Header.Set("x-cosmos-block-height", height)

resp, err := http.DefaultClient.Do(reqGo)
if err != nil {
return ValidatorsResponse{}, fmt.Errorf("error making the request %s", err.Error())
}
if resp.StatusCode != http.StatusOK {
return ValidatorsResponse{}, fmt.Errorf("status code %d", resp.StatusCode)
}
Expand Down
113 changes: 113 additions & 0 deletions pkg/database/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cdd76ed

Please sign in to comment.