mirror of
https://github.com/arnaucube/slowlorisdb.git
synced 2026-02-27 21:36:41 +01:00
add cmd, update config.go
This commit is contained in:
82
cmd/cmd.go
Normal file
82
cmd/cmd.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/arnaucube/slowlorisdb/config"
|
||||
"github.com/arnaucube/slowlorisdb/core"
|
||||
"github.com/arnaucube/slowlorisdb/db"
|
||||
"github.com/arnaucube/slowlorisdb/node"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var Commands = []cli.Command{
|
||||
{
|
||||
Name: "create",
|
||||
Aliases: []string{},
|
||||
Usage: "create the node",
|
||||
Action: cmdCreate,
|
||||
},
|
||||
{
|
||||
Name: "start",
|
||||
Aliases: []string{},
|
||||
Usage: "start the node",
|
||||
Action: cmdStart,
|
||||
},
|
||||
}
|
||||
|
||||
func cmdCreate(c *cli.Context) error {
|
||||
log.Info("creating new keys of the node")
|
||||
privK, err := core.NewKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(privK)
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdStart(c *cli.Context) error {
|
||||
conf, err := config.MustRead(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dir, err := ioutil.TempDir("", conf.DbPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := db.New(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// parse AuthNodes from the config file
|
||||
var authNodes []*ecdsa.PublicKey
|
||||
for _, authNode := range conf.AuthNodes {
|
||||
packedPubK, err := hex.DecodeString(authNode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pubK := core.UnpackPubK(packedPubK)
|
||||
authNodes = append(authNodes, pubK)
|
||||
}
|
||||
|
||||
bc := core.NewPoABlockchain(db, authNodes)
|
||||
|
||||
// TODO parse privK from path in the config file
|
||||
privK, err := core.NewKey()
|
||||
node, err := node.NewNode(privK, bc, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = node.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -8,13 +8,15 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port string
|
||||
Dest string
|
||||
DbPath string
|
||||
Port string
|
||||
Dest string
|
||||
AuthNodes []string // PubKs in hex format of the AuthNodes for the blockchain
|
||||
}
|
||||
|
||||
var C Config
|
||||
func MustRead(c *cli.Context) (*Config, error) {
|
||||
var config Config
|
||||
|
||||
func MustRead(c *cli.Context) error {
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName("config")
|
||||
viper.AddConfigPath(".") // adding home directory as first search path
|
||||
@@ -27,10 +29,10 @@ func MustRead(c *cli.Context) error {
|
||||
}
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if err := viper.Unmarshal(&C); err != nil {
|
||||
return err
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
return &config, nil
|
||||
}
|
||||
Reference in New Issue
Block a user