Generic events search in API
This commit is contained in:
parent
5fbb5ea7c8
commit
a5d142ed2a
|
@ -22,8 +22,8 @@ linters-settings:
|
||||||
- default
|
- default
|
||||||
- prefix(github.com/poanetwork/tokenbridge-monitor)
|
- prefix(github.com/poanetwork/tokenbridge-monitor)
|
||||||
funlen:
|
funlen:
|
||||||
lines: 130
|
lines: 60
|
||||||
statements: 60
|
statements: 40
|
||||||
varnamelen:
|
varnamelen:
|
||||||
ignore-names:
|
ignore-names:
|
||||||
- db
|
- db
|
||||||
|
@ -49,6 +49,10 @@ issues:
|
||||||
- append only allowed to cuddle with appended value
|
- append only allowed to cuddle with appended value
|
||||||
- for statement without condition should never be cuddled
|
- for statement without condition should never be cuddled
|
||||||
exclude-rules:
|
exclude-rules:
|
||||||
- linters:
|
- path: cmd
|
||||||
|
linters:
|
||||||
- cyclop
|
- cyclop
|
||||||
path: cmd
|
- funlen
|
||||||
|
- path: '(.+)_test\.go'
|
||||||
|
linters:
|
||||||
|
- funlen
|
||||||
|
|
|
@ -15,7 +15,7 @@ type AlertManager struct {
|
||||||
jobs map[string]*Job
|
jobs map[string]*Job
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:cyclop
|
//nolint:cyclop,funlen
|
||||||
func NewAlertManager(logger logging.Logger, db *db.DB, cfg *config.BridgeConfig) (*AlertManager, error) {
|
func NewAlertManager(logger logging.Logger, db *db.DB, cfg *config.BridgeConfig) (*AlertManager, error) {
|
||||||
provider := NewDBAlertsProvider(db)
|
provider := NewDBAlertsProvider(db)
|
||||||
jobs := make(map[string]*Job, len(cfg.Alerts))
|
jobs := make(map[string]*Job, len(cfg.Alerts))
|
||||||
|
|
|
@ -2,6 +2,7 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -18,11 +19,14 @@ type ctxKey int
|
||||||
const (
|
const (
|
||||||
bridgeCfgCtxKey ctxKey = iota
|
bridgeCfgCtxKey ctxKey = iota
|
||||||
chainCfgCtxKey
|
chainCfgCtxKey
|
||||||
blockNumberCtxKey
|
fromBlockNumberCtxKey
|
||||||
|
toBlockNumberCtxKey
|
||||||
txHashCtxKey
|
txHashCtxKey
|
||||||
filterCtxKey
|
filterCtxKey
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrInvalidBlockNumber = errors.New("invalid block number parameter")
|
||||||
|
|
||||||
type FilterContext struct {
|
type FilterContext struct {
|
||||||
ChainID *string
|
ChainID *string
|
||||||
FromBlock *uint
|
FromBlock *uint
|
||||||
|
@ -59,6 +63,14 @@ func GetChainConfigMiddleware(cfg *config.Config) func(http.Handler) http.Handle
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
chainID := chi.URLParam(r, "chainID")
|
chainID := chi.URLParam(r, "chainID")
|
||||||
|
|
||||||
|
if chainID == "" {
|
||||||
|
chainID = r.URL.Query().Get("chainId")
|
||||||
|
if chainID == "" {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var chainCfg *config.ChainConfig
|
var chainCfg *config.ChainConfig
|
||||||
for _, c := range cfg.Chains {
|
for _, c := range cfg.Chains {
|
||||||
if c.ChainID == chainID {
|
if c.ChainID == chainID {
|
||||||
|
@ -79,13 +91,49 @@ func GetChainConfigMiddleware(cfg *config.Config) func(http.Handler) http.Handle
|
||||||
|
|
||||||
func GetBlockNumberMiddleware(next http.Handler) http.Handler {
|
func GetBlockNumberMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
blockNumber, err := strconv.ParseUint(chi.URLParam(r, "blockNumber"), 10, 32)
|
blockNumberStr := chi.URLParam(r, "blockNumber")
|
||||||
|
query := r.URL.Query()
|
||||||
|
|
||||||
|
if blockNumberStr == "" {
|
||||||
|
blockNumberStr = query.Get("blockNumber")
|
||||||
|
}
|
||||||
|
|
||||||
|
var fromBlockStr, toBlockStr string
|
||||||
|
if blockNumberStr == "" {
|
||||||
|
fromBlockStr = query.Get("fromBlock")
|
||||||
|
toBlockStr = query.Get("toBlock")
|
||||||
|
if fromBlockStr == "" || toBlockStr == "" {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fromBlockStr = blockNumberStr
|
||||||
|
toBlockStr = blockNumberStr
|
||||||
|
}
|
||||||
|
|
||||||
|
fromBlock, err := strconv.ParseUint(fromBlockStr, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
render.Error(w, r, fmt.Errorf("failed to parse blockNumber: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
toBlock, err := strconv.ParseUint(toBlockStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
render.Error(w, r, fmt.Errorf("failed to parse blockNumber: %w", err))
|
render.Error(w, r, fmt.Errorf("failed to parse blockNumber: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.WithValue(r.Context(), blockNumberCtxKey, uint(blockNumber))
|
if fromBlock > toBlock {
|
||||||
|
render.Error(w, r, fmt.Errorf("fromBlock should be less than toBlock: %w", ErrInvalidBlockNumber))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if toBlock-fromBlock > 10000 {
|
||||||
|
render.Error(w, r, fmt.Errorf("cannot request more than 10000 blocks in range: %w", ErrInvalidBlockNumber))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := r.Context()
|
||||||
|
ctx = context.WithValue(ctx, fromBlockNumberCtxKey, uint(fromBlock))
|
||||||
|
ctx = context.WithValue(ctx, toBlockNumberCtxKey, uint(toBlock))
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -94,6 +142,14 @@ func GetTxHashMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
txHash := chi.URLParam(r, "txHash")
|
txHash := chi.URLParam(r, "txHash")
|
||||||
|
|
||||||
|
if txHash == "" {
|
||||||
|
txHash = r.URL.Query().Get("txHash")
|
||||||
|
if txHash == "" {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx := context.WithValue(r.Context(), txHashCtxKey, common.HexToHash(txHash))
|
ctx := context.WithValue(r.Context(), txHashCtxKey, common.HexToHash(txHash))
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
})
|
})
|
||||||
|
@ -108,8 +164,10 @@ func GetFilterMiddleware(next http.Handler) http.Handler {
|
||||||
if cfg, ok := ctx.Value(chainCfgCtxKey).(*config.ChainConfig); ok {
|
if cfg, ok := ctx.Value(chainCfgCtxKey).(*config.ChainConfig); ok {
|
||||||
filter.ChainID = &cfg.ChainID
|
filter.ChainID = &cfg.ChainID
|
||||||
}
|
}
|
||||||
if blockNumber, ok := ctx.Value(blockNumberCtxKey).(uint); ok {
|
if blockNumber, ok := ctx.Value(fromBlockNumberCtxKey).(uint); ok {
|
||||||
filter.FromBlock = &blockNumber
|
filter.FromBlock = &blockNumber
|
||||||
|
}
|
||||||
|
if blockNumber, ok := ctx.Value(toBlockNumberCtxKey).(uint); ok {
|
||||||
filter.ToBlock = &blockNumber
|
filter.ToBlock = &blockNumber
|
||||||
}
|
}
|
||||||
if txHash, ok := ctx.Value(txHashCtxKey).(common.Hash); ok {
|
if txHash, ok := ctx.Value(txHashCtxKey).(common.Hash); ok {
|
||||||
|
|
|
@ -76,6 +76,14 @@ func (p *Presenter) Serve(addr string) error {
|
||||||
r.Use(middleware.GetTxHashMiddleware)
|
r.Use(middleware.GetTxHashMiddleware)
|
||||||
r.Group(registerSearchRoutes)
|
r.Group(registerSearchRoutes)
|
||||||
})
|
})
|
||||||
|
p.root.Group(func(r chi.Router) {
|
||||||
|
r.Use(middleware.GetChainConfigMiddleware(p.cfg))
|
||||||
|
r.Use(middleware.GetBlockNumberMiddleware)
|
||||||
|
r.Use(middleware.GetTxHashMiddleware)
|
||||||
|
r.Use(middleware.GetFilterMiddleware)
|
||||||
|
r.Get("/logs", p.GetLogs)
|
||||||
|
r.Get("/messages", p.GetMessages)
|
||||||
|
})
|
||||||
return http.ListenAndServe(addr, p.root)
|
return http.ListenAndServe(addr, p.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue