feat: implement report node

This commit is contained in:
Gaze
2024-04-21 02:04:48 +07:00
parent 83557ce1cf
commit e9cde98a9b
4 changed files with 51 additions and 6 deletions

View File

@@ -13,6 +13,9 @@ network: mainnet
reporting:
disabled: false
base_url: "https://indexer.api.gaze.network" # defaults to "https://indexer.api.gaze.network" if empty
name: "local-dev" # name of this instance to show on the dashboard
website_url: "" # public website URL to show on the dashboard. Can be left empty.
indexer_api_url: "" # public url to access this api. Can be left empty.
้http_server:
port: 8080

View File

@@ -71,6 +71,11 @@ func (p *Processor) VerifyStates(ctx context.Context) error {
return errors.Wrap(err, "error during ensureGenesisRune")
}
}
if p.reportingClient != nil {
if err := p.reportingClient.SubmitNodeReport(ctx, "runes", p.network); err != nil {
return errors.Wrap(err, "failed to submit node report")
}
}
return nil
}

View File

@@ -11,7 +11,6 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/cockroachdb/errors"
"github.com/gaze-network/indexer-network/common"
"github.com/gaze-network/indexer-network/common/errs"
"github.com/gaze-network/indexer-network/core/types"
"github.com/gaze-network/indexer-network/modules/runes/datagateway"
@@ -812,7 +811,7 @@ func (p *Processor) flushBlock(ctx context.Context, blockHeader types.BlockHeade
// submit event to reporting system
if p.reportingClient != nil {
if err := p.reportingClient.SubmitBlockReport(ctx, reportingclient.SubmitBlockReportPayload{
Type: common.ModuleRunes,
Type: "runes",
ClientVersion: Version,
DBVersion: DBVersion,
EventHashVersion: EventHashVersion,

View File

@@ -14,15 +14,19 @@ import (
)
type Config struct {
Disabled bool `mapstructure:"disabled"`
BaseURL string `mapstructure:"base_url"`
Disabled bool `mapstructure:"disabled"`
BaseURL string `mapstructure:"base_url"`
Name string `mapstructure:"name"`
WebsiteURL string `mapstructure:"website_url"`
IndexerAPIURL string `mapstructure:"indexer_api_url"`
}
type ReportingClient struct {
httpClient *httpclient.Client
config Config
}
const defaultBaseURL = "https://indexer-dev.api.gaze.network"
const defaultBaseURL = "https://indexer.api.gaze.network"
func New(config Config) (*ReportingClient, error) {
baseURL := utils.Default(config.BaseURL, defaultBaseURL)
@@ -32,11 +36,12 @@ func New(config Config) (*ReportingClient, error) {
}
return &ReportingClient{
httpClient: httpClient,
config: config,
}, nil
}
type SubmitBlockReportPayload struct {
Type common.Module `json:"type"`
Type string `json:"type"`
ClientVersion string `json:"clientVersion"`
DBVersion int `json:"dbVersion"`
EventHashVersion int `json:"eventHashVersion"`
@@ -64,3 +69,36 @@ func (r *ReportingClient) SubmitBlockReport(ctx context.Context, payload SubmitB
logger.DebugContext(ctx, "block report submitted", slog.Any("payload", payload))
return nil
}
type SubmitNodeReportPayload struct {
Name string `json:"name"`
Type string `json:"type"`
Network common.Network `json:"network"`
WebsiteURL string `json:"websiteURL,omitempty"`
IndexerAPIURL string `json:"indexerAPIURL,omitempty"`
}
func (r *ReportingClient) SubmitNodeReport(ctx context.Context, module string, network common.Network) error {
payload := SubmitNodeReportPayload{
Name: r.config.Name,
Type: module,
Network: network,
WebsiteURL: r.config.WebsiteURL,
IndexerAPIURL: r.config.IndexerAPIURL,
}
body, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "can't marshal payload")
}
resp, err := r.httpClient.Post(ctx, "/v1/report/node", httpclient.RequestOptions{
Body: body,
})
if err != nil {
return errors.Wrap(err, "can't send request")
}
if resp.StatusCode() >= 400 {
logger.WarnContext(ctx, "failed to submit node report", slog.Any("payload", payload), slog.Any("responseBody", resp.Body()))
}
logger.InfoContext(ctx, "node report submitted", slog.Any("payload", payload))
return nil
}