Skip to content

Commit

Permalink
Merge pull request #168 from ethpandaops/feat/cannon
Browse files Browse the repository at this point in the history
feat: Cannon
  • Loading branch information
Savid authored Sep 8, 2023
2 parents 20268d7 + 6e5727c commit 2e3b2a9
Show file tree
Hide file tree
Showing 52 changed files with 9,088 additions and 2,065 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sentry.yaml
server.yaml
discovery.yaml
mimicry.yaml
cannon.yaml
dist
GeoLite2-ASN.mmdb
GeoLite2-City.mmdb
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"mode": "debug",
"program": "${workspaceRoot}",
"args": [
"sentry"
"cannon"
],
}
]
Expand Down
86 changes: 86 additions & 0 deletions cmd/cannon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//nolint:dupl // disable duplicate code warning for cmds
package cmd

import (
"os"

"github.com/creasty/defaults"
"github.com/ethpandaops/xatu/pkg/cannon"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)

var (
cannonCfgFile string
)

// cannonCmd represents the cannon command
var cannonCmd = &cobra.Command{
Use: "cannon",
Short: "Runs Xatu in cannon mode.",
Long: `Runs Xatu in cannon mode, which means it will connect to xatu
server and process jobs like deriving events from beacon blocks..`,
Run: func(cmd *cobra.Command, args []string) {
initCommon()

log.WithField("location", cannonCfgFile).Info("Loading config")

config, err := loadcannonConfigFromFile(cannonCfgFile)
if err != nil {
log.Fatal(err)
}

log.Info("Config loaded")

logLevel, err := logrus.ParseLevel(config.LoggingLevel)
if err != nil {
log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level")
}

log.SetLevel(logLevel)

cannon, err := cannon.New(cmd.Context(), log, config)
if err != nil {
log.Fatal(err)
}

if err := cannon.Start(cmd.Context()); err != nil {
log.Fatal(err)
}

log.Info("Xatu cannon exited - cya!")
},
}

func init() {
rootCmd.AddCommand(cannonCmd)

cannonCmd.Flags().StringVar(&cannonCfgFile, "config", "cannon.yaml", "config file (default is cannon.yaml)")
}

func loadcannonConfigFromFile(file string) (*cannon.Config, error) {
if file == "" {
file = "cannon.yaml"
}

config := &cannon.Config{}

if err := defaults.Set(config); err != nil {
return nil, err
}

yamlFile, err := os.ReadFile(file)

if err != nil {
return nil, err
}

type plain cannon.Config

if err := yaml.Unmarshal(yamlFile, (*plain)(config)); err != nil {
return nil, err
}

return config, nil
}
9 changes: 9 additions & 0 deletions migrations/postgres/005_cannon.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE cannon_location (
location_id SERIAL PRIMARY KEY,
create_time TIMESTAMPTZ NOT NULL DEFAULT now(),
update_time TIMESTAMPTZ NOT NULL DEFAULT now(),
network_id VARCHAR(256),
type VARCHAR(256),
value TEXT,
CONSTRAINT cannon_location_unique UNIQUE (network_id, type)
);
1 change: 1 addition & 0 deletions migrations/postgres/005_cannon_down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS cannon_location;
Loading

0 comments on commit 2e3b2a9

Please sign in to comment.