Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cobra 98 #122

Merged
merged 12 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ docker/pgadmin
node_trace.*
internal/bin
bin
conf.*.private
**/conf.*.private
.idea
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ linters:
- forbidigo
- funlen
- gocheckcompilerdirectives
# - gochecknoinits
- gochecknoinits
- gocognit
- goconst
- gocritic
Expand Down Expand Up @@ -203,6 +203,7 @@ issues:
linters:
- funlen
- forbidigo
- gochecknoinits
- path: "ent"
linters:
- gomnd
Expand Down
24 changes: 12 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
prod:
GOOS=windows GOARCH=amd64 go build -o bin/unchained.windows.amd64.exe main.go
GOOS=darwin GOARCH=amd64 go build -o bin/unchained.darwin.amd64 main.go
GOOS=linux GOARCH=amd64 go build -o bin/unchained.linux.amd64 main.go
GOOS=windows GOARCH=amd64 go build -o bin/unchained.windows.amd64.exe ./cmd/main.go
GOOS=darwin GOARCH=amd64 go build -o bin/unchained.darwin.amd64 ./cmd/main.go
GOOS=linux GOARCH=amd64 go build -o bin/unchained.linux.amd64 ./cmd/main.go

GOOS=windows GOARCH=arm64 go build -o bin/unchained.windows.arm64.exe main.go
GOOS=darwin GOARCH=arm64 go build -o bin/unchained.darwin.arm64 main.go
GOOS=linux GOARCH=arm64 go build -o bin/unchained.linux.arm64 main.go
GOOS=windows GOARCH=arm64 go build -o bin/unchained.windows.arm64.exe ./cmd/main.go
GOOS=darwin GOARCH=arm64 go build -o bin/unchained.darwin.arm64 ./cmd/main.go
GOOS=linux GOARCH=arm64 go build -o bin/unchained.linux.arm64 ./cmd/main.go

find bin -type f -exec chmod u+x {} \;

strip:
GOOS=windows GOARCH=amd64 go build -ldflags "-w -s" -o bin/unchained.windows.amd64.exe main.go
GOOS=darwin GOARCH=amd64 go build -ldflags "-w -s" -o bin/unchained.darwin.amd64 main.go
GOOS=linux GOARCH=amd64 go build -ldflags "-w -s" -o bin/unchained.linux.amd64 main.go
GOOS=windows GOARCH=amd64 go build -ldflags "-w -s" -o bin/unchained.windows.amd64.exe ./cmd/main.go
GOOS=darwin GOARCH=amd64 go build -ldflags "-w -s" -o bin/unchained.darwin.amd64 ./cmd/main.go
GOOS=linux GOARCH=amd64 go build -ldflags "-w -s" -o bin/unchained.linux.amd64 ./cmd/main.go

GOOS=windows GOARCH=arm64 go build -ldflags "-w -s" -o bin/unchained.windows.arm64.exe main.go
GOOS=darwin GOARCH=arm64 go build -ldflags "-w -s" -o bin/unchained.darwin.arm64 main.go
GOOS=linux GOARCH=arm64 go build -ldflags "-w -s" -o bin/unchained.linux.arm64 main.go
GOOS=windows GOARCH=arm64 go build -ldflags "-w -s" -o bin/unchained.windows.arm64.exe ./cmd/main.go
GOOS=darwin GOARCH=arm64 go build -ldflags "-w -s" -o bin/unchained.darwin.arm64 ./cmd/main.go
GOOS=linux GOARCH=arm64 go build -ldflags "-w -s" -o bin/unchained.linux.arm64 ./cmd/main.go

find bin -type f -exec chmod u+x {} \;

Expand Down
38 changes: 38 additions & 0 deletions cmd/handler/broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package handler

import (
"github.com/KenshiTech/unchained/internal/app"
"github.com/KenshiTech/unchained/internal/config"
"github.com/KenshiTech/unchained/internal/log"
"github.com/spf13/cobra"
)

// broker represents the broker command.
var broker = &cobra.Command{
Use: "broker",
Short: "Run the Unchained client in broker mode",
Long: `Run the Unchained client in broker mode`,
Run: func(cmd *cobra.Command, args []string) {
err := config.Load(config.App.System.ConfigPath, config.App.System.SecretsPath)
if err != nil {
panic(err)
}

log.Start(config.App.System.Log)
app.Broker()
},
}

// WithBrokerCmd appends the broker command to the root command.
func WithBrokerCmd(cmd *cobra.Command) {
cmd.AddCommand(broker)
}

func init() {
broker.Flags().StringP(
"broker",
"b",
"wss://shinobi.brokers.kenshi.io",
"Unchained broker to connect to",
)
}
51 changes: 51 additions & 0 deletions cmd/handler/consumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package handler

import (
"github.com/KenshiTech/unchained/internal/app"
"github.com/KenshiTech/unchained/internal/config"
"github.com/KenshiTech/unchained/internal/log"
"github.com/spf13/cobra"
)

// consumer represents the consumer command.
var consumer = &cobra.Command{
Use: "consumer",
Short: "Run the Unchained client in consumer mode",
Long: `Run the Unchained client in consumer mode`,

PreRun: func(cmd *cobra.Command, args []string) {
config.App.Network.BrokerURI = cmd.Flags().Lookup("broker").Value.String()
config.App.Network.Bind = cmd.Flags().Lookup("graphql").Value.String()
},

Run: func(cmd *cobra.Command, args []string) {
err := config.Load(config.App.System.ConfigPath, config.App.System.SecretsPath)
if err != nil {
panic(err)
}

log.Start(config.App.System.Log)
app.Consumer()
},
}

// WithConsumerCmd append command of consumer to the root command.
func WithConsumerCmd(cmd *cobra.Command) {
cmd.AddCommand(consumer)
}

func init() {
consumer.Flags().StringP(
"broker",
"b",
"wss://shinobi.brokers.kenshi.io",
"Unchained broker to connect to",
)

consumer.Flags().StringP(
"graphql",
"g",
"127.0.0.1:8080",
"The graphql server path to bind",
)
}
43 changes: 43 additions & 0 deletions cmd/handler/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package handler

import (
"github.com/KenshiTech/unchained/internal/app"
"github.com/KenshiTech/unchained/internal/config"
"github.com/KenshiTech/unchained/internal/log"
"github.com/spf13/cobra"
)

// worker represents the worker command.
var worker = &cobra.Command{
Use: "worker",
Short: "Run the Unchained client in worker mode",
Long: `Run the Unchained client in worker mode`,

PreRun: func(cmd *cobra.Command, args []string) {
config.App.Network.BrokerURI = cmd.Flags().Lookup("broker").Value.String()
},

Run: func(cmd *cobra.Command, args []string) {
err := config.Load(config.App.System.ConfigPath, config.App.System.SecretsPath)
if err != nil {
panic(err)
}

log.Start(config.App.System.Log)
app.Worker()
},
}

// WithWorkerCmd appends the worker command to the root command.
func WithWorkerCmd(cmd *cobra.Command) {
cmd.AddCommand(worker)
}

func init() {
worker.Flags().StringP(
"broker",
"b",
"wss://shinobi.brokers.kenshi.io",
"Unchained broker to connect to",
)
}
53 changes: 53 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"os"

"github.com/KenshiTech/unchained/cmd/handler"
"github.com/KenshiTech/unchained/internal/config"
"github.com/KenshiTech/unchained/internal/constants"
"github.com/spf13/cobra"
)

// root represents the root command of application.
var root = &cobra.Command{
Use: "unchained",
Short: "Unchained is the universal data validation and processing protocol",
Long: `Unchained is the universal data validation and processing protocol`,
Run: func(cmd *cobra.Command, args []string) {
if config.App.System.PrintVersion {
fmt.Println(constants.Version)
} else {
os.Exit(1)
}
},
}

func main() {
handler.WithBrokerCmd(root)
handler.WithConsumerCmd(root)
handler.WithWorkerCmd(root)

err := root.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
root.Flags().BoolVarP(&config.App.System.PrintVersion, "version", "v", false, "Print the Unchained version number and die")
root.PersistentFlags().StringVarP(&config.App.System.ConfigPath, "config", "c", "./conf.yaml", "Config file")
root.PersistentFlags().StringVarP(&config.App.System.SecretsPath, "secrets", "s", "./secrets.yaml", "Secrets file")
root.PersistentFlags().StringVarP(&config.App.System.ContextPath, "context", "x", "./context", "Context DB")

err := root.MarkPersistentFlagRequired("config")
if err != nil {
panic(err)
}

err = root.MarkPersistentFlagFilename("config", "yaml")
if err != nil {
panic(err)
}
}
32 changes: 32 additions & 0 deletions internal/app/broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package app

import (
"github.com/KenshiTech/unchained/internal/constants"
"github.com/KenshiTech/unchained/internal/crypto"
"github.com/KenshiTech/unchained/internal/crypto/ethereum"
"github.com/KenshiTech/unchained/internal/log"
"github.com/KenshiTech/unchained/internal/pos"
"github.com/KenshiTech/unchained/internal/transport/server"
"github.com/KenshiTech/unchained/internal/transport/server/websocket"
)

// Broker starts the Unchained broker and contains its DI.
func Broker() {
log.Logger.
With("Mode", "Broker").
With("Version", constants.Version).
With("Protocol", constants.ProtocolVersion).
Info("Running Unchained")

crypto.InitMachineIdentity(
crypto.WithBlsIdentity(),
crypto.WithEvmSigner(),
)

ethRPC := ethereum.New()
pos.New(ethRPC)

server.New(
websocket.WithWebsocket(),
)
}
49 changes: 49 additions & 0 deletions internal/app/consumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package app

import (
"github.com/KenshiTech/unchained/internal/constants"
"github.com/KenshiTech/unchained/internal/crypto"
"github.com/KenshiTech/unchained/internal/crypto/ethereum"
"github.com/KenshiTech/unchained/internal/db"
"github.com/KenshiTech/unchained/internal/log"
"github.com/KenshiTech/unchained/internal/pos"
correctnessService "github.com/KenshiTech/unchained/internal/service/correctness"
evmlogService "github.com/KenshiTech/unchained/internal/service/evmlog"
uniswapService "github.com/KenshiTech/unchained/internal/service/uniswap"
"github.com/KenshiTech/unchained/internal/transport/client"
"github.com/KenshiTech/unchained/internal/transport/client/conn"
"github.com/KenshiTech/unchained/internal/transport/client/handler"
"github.com/KenshiTech/unchained/internal/transport/server"
"github.com/KenshiTech/unchained/internal/transport/server/gql"
)

// Consumer starts the Unchained consumer and contains its DI.
func Consumer() {
log.Logger.
With("Mode", "Consumer").
With("Version", constants.Version).
With("Protocol", constants.ProtocolVersion).
Info("Running Unchained")

crypto.InitMachineIdentity(
crypto.WithEvmSigner(),
crypto.WithBlsIdentity(),
)

ethRPC := ethereum.New()
pos := pos.New(ethRPC)
db.Start()

correctnessService := correctnessService.New(ethRPC)
evmLogService := evmlogService.New(ethRPC, pos)
uniswapService := uniswapService.New(ethRPC, pos)

conn.Start()

handler := handler.NewConsumerHandler(correctnessService, uniswapService, evmLogService)
client.NewRPC(handler)

server.New(
gql.WithGraphQL(),
)
}
50 changes: 50 additions & 0 deletions internal/app/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package app

import (
"github.com/KenshiTech/unchained/internal/config"
"github.com/KenshiTech/unchained/internal/constants"
"github.com/KenshiTech/unchained/internal/crypto"
"github.com/KenshiTech/unchained/internal/crypto/ethereum"
"github.com/KenshiTech/unchained/internal/log"
"github.com/KenshiTech/unchained/internal/persistence"
"github.com/KenshiTech/unchained/internal/pos"
"github.com/KenshiTech/unchained/internal/scheduler"
evmlogService "github.com/KenshiTech/unchained/internal/service/evmlog"
uniswapService "github.com/KenshiTech/unchained/internal/service/uniswap"
"github.com/KenshiTech/unchained/internal/transport/client"
"github.com/KenshiTech/unchained/internal/transport/client/conn"
"github.com/KenshiTech/unchained/internal/transport/client/handler"
)

// Worker starts the Unchained worker and contains its DI.
func Worker() {
log.Logger.
With("Mode", "Worker").
With("Version", constants.Version).
With("Protocol", constants.ProtocolVersion).
Info("Running Unchained")

crypto.InitMachineIdentity(
crypto.WithEvmSigner(),
crypto.WithBlsIdentity(),
)

ethRPC := ethereum.New()
pos := pos.New(ethRPC)
badger := persistence.New(config.App.System.ContextPath)

evmLogService := evmlogService.New(ethRPC, pos)
uniswapService := uniswapService.New(ethRPC, pos)

scheduler := scheduler.New(
scheduler.WithEthLogs(evmLogService, ethRPC, badger),
scheduler.WithUniswapEvents(uniswapService, ethRPC),
)

conn.Start()

handler := handler.NewWorkerHandler()
client.NewRPC(handler)

scheduler.Start()
}
Loading
Loading