Skip to content

Commit

Permalink
feat: re-add sei (which involved porting oracle to sdk50)
Browse files Browse the repository at this point in the history
  • Loading branch information
PFC-developer committed Dec 22, 2023
1 parent 5284dd3 commit ebb2375
Show file tree
Hide file tree
Showing 38 changed files with 11,596 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- run: go mod download
- run: go build ./cmd/cosmos-exporter
- run: go build ./cmd/kuji-cosmos-exporter
# - run: go build ./cmd/sei-cosmos-exporter
- run: go build ./cmd/sei-cosmos-exporter
- run: go build ./cmd/inj-cosmos-exporter
go-vet:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ linters-settings:
- prefix(github.com/cosmos/cosmos-sdk)
- prefix(github.com/cometbft/cometbft)
- prefix(github.com/cosmos/ibc-go)
- prefix(github.com/solarlabsteam/cosmos-exporter)
- prefix(github.com/pfc-developer/cosmos-exporter)
custom-order: true
revive:
enable-all-rules: true
Expand Down
18 changes: 9 additions & 9 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ builds:
- linux
- windows
- darwin
# - main: ./cmd/sei-cosmos-exporter
# id: "sei-cosmos-exporter"
# binary: "sei-cosmos-exporter"
# env:
# - CGO_ENABLED=0
# goos:
# - linux
# - windows
# - darwin
- main: ./cmd/sei-cosmos-exporter
id: "sei-cosmos-exporter"
binary: "sei-cosmos-exporter"
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
- main: ./cmd/inj-cosmos-exporter
id: "inj-cosmos-exporter"
binary: "inj-cosmos-exporter"
Expand Down
3 changes: 2 additions & 1 deletion cmd/cosmos-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"net/http"
"os"

"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

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

"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion cmd/inj-cosmos-exporter/Injective.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

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

"github.com/solarlabsteam/cosmos-exporter/pkg/exporter"
"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
)

/*
Expand Down
2 changes: 1 addition & 1 deletion cmd/inj-cosmos-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

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

"github.com/solarlabsteam/cosmos-exporter/pkg/exporter"
"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion cmd/inj-cosmos-exporter/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

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

"github.com/solarlabsteam/cosmos-exporter/pkg/exporter"
"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
)

func InjSingleHandler(w http.ResponseWriter, r *http.Request, s *exporter.Service) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/kuji-cosmos-exporter/kuji.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (

oracletypes "github.com/Team-Kujira/core/x/oracle/types"
"github.com/google/uuid"
"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"

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

"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
)

/*
Expand Down
3 changes: 2 additions & 1 deletion cmd/kuji-cosmos-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"net/http"
"os"

"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

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

"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
)

var (
Expand Down
3 changes: 2 additions & 1 deletion cmd/kuji-cosmos-exporter/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"time"

"github.com/google/uuid"
"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"

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

"github.com/pfc-developer/cosmos-exporter/pkg/exporter"
)

func KujiSingleHandler(w http.ResponseWriter, r *http.Request, s *exporter.Service) {
Expand Down
184 changes: 184 additions & 0 deletions cmd/sei-cosmos-exporter/events.go.X
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package main

import (
"context"
"fmt"
"main/pkg/exporter"
"net/http"
"regexp"
"strconv"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
tmrpcclient "github.com/tendermint/tendermint/rpc/client"
//tmrpceventstream "github.com/tendermint/tendermint/rpc/client/eventstream"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
//"github.com/tendermint/tendermint/rpc/coretypes"
tmjsonclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
tmtypes "github.com/tendermint/tendermint/types"
)

var (
queryEventBankTransfer = fmt.Sprintf(
"%s='%s' AND %s='%s'",
tmtypes.EventTypeKey,
tmtypes.EventTxValue,
"message.action",
"/cosmos.bank.v1beta1.MsgSend",
)
)

type EventCollector struct {
rpcClient tmrpcclient.Client
logger zerolog.Logger
gauge *prometheus.GaugeVec
BankTransferThreshold float64
}

func NewEventCollector(tmRPC string, logger zerolog.Logger, bankTransferThreshold float64, config *exporter.ServiceConfig) (*EventCollector, error) {
httpClient, err := tmjsonclient.DefaultHTTPClient(tmRPC)
if err != nil {
return nil, err
}
// no timeout because we will continue fetching events continuously
rpcClient, err := rpchttp.NewWithClient(tmRPC, "/", httpClient)
if err != nil {
return nil, err
}
transfersValueGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_bank_transfer_amount",
Help: "Number of tokens transferred in a transfer message",
ConstLabels: config.ConstLabels,
},
[]string{"denom", "sender", "recipient"},
)
return &EventCollector{
rpcClient: rpcClient,
logger: logger,
gauge: transfersValueGauge,
BankTransferThreshold: bankTransferThreshold,
}, nil
}

func (s EventCollector) Start(
ctx context.Context,
) error {
if err := s.rpcClient.Start( /*ctx*/ ); err != nil {
return err
}
s.logger.Info().Msg("Starting StreamCollector")
go s.RunBankTransferEventStream(ctx)

return nil
}

func (s EventCollector) RunBankTransferEventStream(ctx context.Context) {
eventStream := tmrpceventstream.New(s.rpcClient, queryEventBankTransfer, &tmrpceventstream.StreamOptions{
WaitTime: 300 * time.Millisecond,
})
streamEventErr := make(chan error, 1)
go func() {
streamEventErr <- eventStream.Run(ctx, s.HandleBankTransferEvent)
}()
for {
// try recovering forever if only missed items errors
err := <-streamEventErr
if _, ok := err.(*tmrpceventstream.MissedItemsError); ok {
//fallen behind, restart
s.logger.Err(err).Msg("Error in eventstream")
// reset + run again
eventStream.Reset()
go func() {
streamEventErr <- eventStream.Run(ctx, s.HandleBankTransferEvent)
}()
} else {
panic(err) // panic so we trigger `up` alerting
}
}
}

func (s EventCollector) HandleBankTransferEvent(eventItem *coretypes.EventItem) error {
eventData, err := tmtypes.TryUnmarshalEventData(eventItem.Data)
if err != nil {
s.logger.Err(err).Msg("Failed to unmarshal event data")
return nil
}
eventDataTx, ok := eventData.(tmtypes.EventDataTx)
if !ok {
s.logger.Err(err).Msg("Failed to parse event from eventDataNewBlockHeader")
return nil
} else {
events := eventDataTx.Result.Events
for _, event := range events {
if event.Type == banktypes.EventTypeTransfer {
// check the balance
var amount float64
var denom string
var sender string
var recipient string
for _, attr := range event.Attributes {
attrKey := string(attr.Key)
switch attrKey {
case sdk.AttributeKeyAmount:
amountStr := string(attr.Value)
// separate the denom
re := regexp.MustCompile(`\d+|\D+`)
res := re.FindAllString(amountStr, -1)

denom = res[1]
amount, err = strconv.ParseFloat(res[0], 64)
if err != nil {
s.logger.Err(err).Msg("")
}
case banktypes.AttributeKeyRecipient:
recipient = string(attr.Value)
case banktypes.AttributeKeySender:
sender = string(attr.Value)
}
}
if amount > s.BankTransferThreshold {
s.gauge.With(prometheus.Labels{
"denom": denom,
"sender": sender,
"recipient": recipient,
}).Set(amount)
// Expire the metrics after 5 minutes
go func() {
time.Sleep(5 * time.Minute)
s.gauge.Delete(prometheus.Labels{
"denom": denom,
"sender": sender,
"recipient": recipient,
})
}()
}
}
}
}
return nil
}

func (s EventCollector) StreamHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()

sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()

registry := prometheus.NewRegistry()
registry.MustRegister(s.gauge)

h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
sublogger.Info().
Str("method", "GET").
Str("endpoint", "/metrics/event").
Float64("request-time", time.Since(requestStart).Seconds()).
Msg("Request processed")
}
Loading

0 comments on commit ebb2375

Please sign in to comment.