Skip to content

Commit

Permalink
feat: fix all linters and add ci to check linters
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalangel committed Mar 24, 2024
1 parent 0d804d0 commit 96b823a
Show file tree
Hide file tree
Showing 36 changed files with 654 additions and 693 deletions.
Empty file added .github/workflows/go.yaml
Empty file.
37 changes: 28 additions & 9 deletions src/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ run:

linters-settings:
cyclop:
max-complexity: 30
package-average: 10.0
max-complexity: 35
package-average: 13.0

gosec:
excludes:
- G601
errcheck:
check-type-assertions: true

Expand Down Expand Up @@ -47,11 +50,19 @@ linters-settings:
- "^gopkg.in/yaml.v3.Node$"

funlen:
lines: 100
statements: 50
# should be decreased to 30
lines: 200
statements: 150
ignore-comments: true

# should be decreased to 30
gocognit:
min-complexity: 20
min-complexity: 110

gocyclo:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 31

gocritic:
settings:
Expand Down Expand Up @@ -135,14 +146,13 @@ linters:
- forbidigo
- funlen
- gocheckcompilerdirectives
- gochecknoinits
# - gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- goimports
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
Expand Down Expand Up @@ -175,7 +185,6 @@ linters:
- usestdlibvars
- wastedassign
- whitespace
- godox
- interfacebloat
- tagalign
- containedctx
Expand All @@ -184,13 +193,23 @@ linters:
issues:
max-same-issues: 50
exclude-rules:
- text: 'shadow: declaration of "(err|ctx|ok)" shadows declaration at'
linters: [ govet ]
- source: "(noinspection|TODO)"
linters: [ godot ]
- source: "//noinspection"
linters: [ gocritic ]
- path: "cmd/"
- path: "cmd"
linters:
- funlen
- forbidigo
- path: "ent"
linters:
- gomnd
- forbidigo
- path: "config"
linters:
- gomnd
- path: "_test\\.go"
linters:
- bodyclose
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tools:
check: errors imports fmt lint

errors:
errcheck -ignoretests -blank ./...
errcheck -ignoretests -ignoregenerated -blank ./...

lint:
$(CURDIR)/bin/golangci-lint run
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/spf13/cobra"
)

// brokerCmd represents the broker command
// brokerCmd represents the broker command.
var brokerCmd = &cobra.Command{
Use: "broker",
Short: "Run the Unchained client in broker mode",
Expand Down Expand Up @@ -47,5 +47,4 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// brokerCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
9 changes: 6 additions & 3 deletions src/cmd/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ import (
"github.com/spf13/cobra"
)

// consumerCmd represents the consumer command
// consumerCmd represents the consumer command.
var consumerCmd = &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.Config.BindPFlag("broker.uri", cmd.Flags().Lookup("broker"))
err := config.Config.BindPFlag("broker.uri", cmd.Flags().Lookup("broker"))
if err != nil {
panic(err)
}
},

Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -49,7 +52,7 @@ var consumerCmd = &cobra.Command{
logs.Setup()
client.StartClient()
consumers.StartConsumer()
client.ClientBlock()
client.Listen()
},
}

Expand Down
12 changes: 9 additions & 3 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var secretsPath string
var contextPath string
var printVersion bool

// rootCmd represents the base command when called without any subcommands
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "unchained",
Short: "Unchained is the universal data validation and processing protocol",
Expand Down Expand Up @@ -52,6 +52,12 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./conf.yaml", "Config file")
rootCmd.PersistentFlags().StringVarP(&secretsPath, "secrets", "s", "./secrets.yaml", "Secrets file")
rootCmd.PersistentFlags().StringVarP(&contextPath, "context", "x", "./context", "Context DB")
rootCmd.MarkFlagFilename("config", "yaml")
rootCmd.MarkFlagRequired("config")
err := rootCmd.MarkFlagFilename("config", "yaml")
if err != nil {
panic(err)
}
err = rootCmd.MarkFlagRequired("config")
if err != nil {
panic(err)
}
}
9 changes: 6 additions & 3 deletions src/cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ import (
"github.com/spf13/cobra"
)

// workerCmd represents the worker command
// workerCmd represents the worker command.
var workerCmd = &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.Config.BindPFlag("broker.uri", cmd.Flags().Lookup("broker"))
err := config.Config.BindPFlag("broker.uri", cmd.Flags().Lookup("broker"))
if err != nil {
panic(err)
}
},

Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -49,7 +52,7 @@ var workerCmd = &cobra.Command{
logs.Setup()
logs.Start()
persistence.Start(contextPath)
client.ClientBlock()
client.Listen()
},
}

Expand Down
17 changes: 10 additions & 7 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,28 @@ func init() {
Secrets = viper.New()
}

func LoadConfig(ConfigFileName string, SecretsFileName string) {
func LoadConfig(configFileName string, secretsFileName string) {
defaults()

Config.SetConfigFile(ConfigFileName)
Config.SetConfigFile(configFileName)
err := Config.ReadInConfig()

if err != nil {
if os.IsNotExist(err) {
Config.WriteConfig()
} else {
isNotExist := os.IsNotExist(err)
if !isNotExist {
panic(err)
}

err = Config.WriteConfig()
if err != nil {
panic(err)
}
}

Secrets.SetConfigFile(SecretsFileName)
Secrets.SetConfigFile(secretsFileName)
err = Secrets.MergeInConfig()

if err != nil && os.IsExist(err) {
panic(err)
}

}
18 changes: 18 additions & 0 deletions src/constants/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package constants

import "errors"

var (
ErrInvalidPacket = errors.New("packet.invalid")
ErrCantSendPacket = errors.New("socket.unreachable")
ErrInvalidKosk = errors.New("kosk.invalid")
ErrInvalidConfig = errors.New("conf.invalid")
ErrKosk = errors.New("kosk.error")
ErrMissingHello = errors.New("hello.missing")
ErrMissingKosk = errors.New("kosk.missing")
ErrInternalError = errors.New("internal_error")
ErrCantVerifyBls = errors.New("cant_verify_bls")
ErrInvalidSignature = errors.New("signature.invalid")
ErrNotSupportedDataset = errors.New("dataset not supported")
ErrNotSupportedInstruction = errors.New("instruction not supported")
)
27 changes: 15 additions & 12 deletions src/constants/opcodes/opcodes.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package opcodes

// TODO: Should we have a Data opcode instead of PriceReport & EventLog?

type OpCode byte

const (
Hello = iota
KoskChallenge
KoskResult
Hello OpCode = iota
KoskChallenge OpCode = 1
KoskResult OpCode = 2

RegisterConsumer
RegisterConsumer OpCode = 3

Feedback
Error
Feedback OpCode = 4
Error OpCode = 5

PriceReport
PriceReportBroadcast
PriceReport OpCode = 6
PriceReportBroadcast OpCode = 7

EventLog
EventLogBroadcast
EventLog OpCode = 8
EventLogBroadcast OpCode = 9

CorrectnessReport
CorrectnessReportBroadcast
CorrectnessReport OpCode = 10
CorrectnessReportBroadcast OpCode = 11
)
7 changes: 4 additions & 3 deletions src/consumers/consumers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
func ConsumePriceReport(message []byte) {
var packet datasets.BroadcastPricePacket
err := msgpack.Unmarshal(message, &packet)

if err != nil {
log.Logger.
With("Error", err).
Expand Down Expand Up @@ -166,10 +165,12 @@ func ConsumeCorrectnessReport(message []byte) {
hash,
packet.Info,
true,
false,
)
}

func StartConsumer() {
shared.Client.WriteMessage(websocket.BinaryMessage, []byte{opcodes.RegisterConsumer})
err := shared.Client.WriteMessage(websocket.BinaryMessage, []byte{byte(opcodes.RegisterConsumer)})
if err != nil {
panic(err)
}
}
15 changes: 9 additions & 6 deletions src/crypto/bls/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func GetShortPublicKey(sk *big.Int) *bls12381.G1Affine {
return new(bls12381.G1Affine).ScalarMultiplication(&g1Aff, sk)
}

// generate BLS private and public key pair
// generate BLS private and public key pair.
func GenerateKeyPair() (*big.Int, *bls12381.G2Affine, error) {
// generate a random point in G2
g2Order := bls12381_fr.Modulus()
Expand All @@ -57,10 +57,12 @@ func Verify(
signature bls12381.G1Affine,
hashedMessage bls12381.G1Affine,
publicKey bls12381.G2Affine) (bool, error) {

pairingSigG2, _ := bls12381.Pair(
pairingSigG2, err := bls12381.Pair(
[]bls12381.G1Affine{signature},
[]bls12381.G2Affine{g2Aff})
if err != nil {
return false, err
}

pairingHmPk, pairingError := bls12381.Pair(
[]bls12381.G1Affine{hashedMessage},
Expand All @@ -76,7 +78,6 @@ func FastVerify(
g2Gen bls12381.G2Affine,
invertedHashedMessage bls12381.G1Affine,
publicKey bls12381.G2Affine) (bool, error) {

ok, pairingError := bls12381.PairingCheck(
[]bls12381.G1Affine{signature, invertedHashedMessage},
[]bls12381.G2Affine{g2Gen, publicKey})
Expand All @@ -90,7 +91,10 @@ func Hash(message []byte) (bls12381.G1Affine, error) {
}

func Sign(secretKey big.Int, message []byte) (bls12381.G1Affine, bls12381.G1Affine) {
hashedMessage, _ := Hash(message)
hashedMessage, err := Hash(message)
if err != nil {
panic(err)
}
signature := new(bls12381.G1Affine).ScalarMultiplication(&hashedMessage, &secretKey)

return *signature, hashedMessage
Expand All @@ -109,7 +113,6 @@ func RecoverPublicKey(bytes [96]byte) (bls12381.G2Affine, error) {
}

func AggregateSignatures(signatures []bls12381.G1Affine) (bls12381.G1Affine, error) {

aggregated := new(bls12381.G1Jac).FromAffine(&signatures[0])

for _, sig := range signatures[1:] {
Expand Down
2 changes: 0 additions & 2 deletions src/crypto/bls/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ func InitClientIdentity() {
var pkBytes [96]byte

if config.Secrets.IsSet("secretKey") {

decoded := base58.Decode(config.Secrets.GetString("secretKey"))

ClientSecretKey = new(big.Int)
ClientSecretKey.SetBytes(decoded)

ClientPublicKey = GetPublicKey(ClientSecretKey)
pkBytes = ClientPublicKey.Bytes()

} else {
ClientSecretKey, ClientPublicKey, err = GenerateKeyPair()

Expand Down
6 changes: 3 additions & 3 deletions src/datasets/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

type EventLogArg struct {
Name string
Type string
Value any
Name string `json:"name"`
Type string `json:"type"`
Value any `json:"value"`
}

var _ msgpack.CustomEncoder = (*EventLogArg)(nil)
Expand Down
Loading

0 comments on commit 96b823a

Please sign in to comment.