Skip to content

Commit

Permalink
Merge pull request #122 from KenshiTech/fix-cobra-98
Browse files Browse the repository at this point in the history
Fix cobra 98
  • Loading branch information
pouya-eghbali authored Apr 14, 2024
2 parents 9532b86 + 9992f51 commit ab4ddd5
Show file tree
Hide file tree
Showing 54 changed files with 696 additions and 1,059 deletions.
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
6 changes: 3 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ linters-settings:
tenv:
all: true


linters:
disable-all: true
enable:
Expand All @@ -146,7 +145,7 @@ linters:
- forbidigo
- funlen
- gocheckcompilerdirectives
# - gochecknoinits
- gochecknoinits
- gocognit
- goconst
- gocritic
Expand All @@ -173,7 +172,7 @@ linters:
- predeclared
- promlinter
- reassign
# - revive
- revive
- rowserrcheck
- sqlclosecheck
- stylecheck
Expand Down Expand Up @@ -203,6 +202,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(_ *cobra.Command, _ []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, _ []string) {
config.App.Network.BrokerURI = cmd.Flags().Lookup("broker").Value.String()
config.App.Network.Bind = cmd.Flags().Lookup("graphql").Value.String()
},

Run: func(_ *cobra.Command, _ []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, _ []string) {
config.App.Network.BrokerURI = cmd.Flags().Lookup("broker").Value.String()
},

Run: func(_ *cobra.Command, _ []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(_ *cobra.Command, _ []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

0 comments on commit ab4ddd5

Please sign in to comment.