Commit 044afa8f authored by duanjinfei's avatar duanjinfei

add command

parent 8c4ae233
package main package main
import ( import (
"github.com/consensys/gnark-crypto/field/goff/cmd"
"runtime" "runtime"
) )
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
cmd.Execute() Execute()
} }
...@@ -3,27 +3,21 @@ package main ...@@ -3,27 +3,21 @@ package main
import ( import (
"example.com/m/conf" "example.com/m/conf"
"example.com/m/log" "example.com/m/log"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
rewardAddr string
isSetReward bool
)
func init() { func init() {
RootCmd.AddCommand(paramCmd) //RootCmd.AddCommand(paramCmd)
rewardAddr = *paramCmd.Flags().StringP("reward", "r", "", "Print detail version info") rewardAddr = *paramCmd.PersistentFlags().StringP("reward", "r", "", "Print detail version info")
} }
// versionCmd represents the base command when called without any subcommands // versionCmd represents the base command when called without any subcommands
var paramCmd = &cobra.Command{ var paramCmd = &cobra.Command{
Use: "param", Use: "param",
Short: "Print version number", Short: "",
Long: ``, Long: ``,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if rewardAddr != "" && common.IsHexAddress(rewardAddr) { if rewardAddr != "" {
isSetReward = conf.GetConfig().SetRewardAddress(rewardAddr) isSetReward = conf.GetConfig().SetRewardAddress(rewardAddr)
if !isSetReward { if !isSetReward {
log.Error("Please set right reward address") log.Error("Please set right reward address")
......
package main package main
import ( import (
"encoding/json"
"example.com/m/conf"
"example.com/m/log" "example.com/m/log"
"example.com/m/nm" "example.com/m/nm"
"example.com/m/utils"
"fmt"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"os" "os"
) )
var ( var (
routineCount uint rewardAddr string
isSetReward bool = false
) )
func init() {
RootCmd.PersistentFlags().StringVarP(&rewardAddr, "reward", "r", "", "please enter a reward address")
cobra.OnInitialize(initConfig)
}
// RootCmd represents the base command when called without any subcommands // RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "miner", Use: "miner",
Short: "The miner command-line interface", Short: "The miner command-line interface",
Long: ``, Long: ``,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if rewardAddr != "" {
log.Info("Enter reward address:", rewardAddr)
isSetReward = conf.GetConfig().SetRewardAddress(rewardAddr)
if !isSetReward {
log.Error("Please set right reward address")
}
}
if isSetReward { if isSetReward {
log.InitLog(log.LogConfig{Path: "logs", Level: "debug", Save: 3}) log.InitLog(log.LogConfig{Path: "logs", Level: "debug", Save: 3})
go nm.StartMonitor() go nm.StartMonitor()
...@@ -26,9 +47,55 @@ var RootCmd = &cobra.Command{ ...@@ -26,9 +47,55 @@ var RootCmd = &cobra.Command{
}, },
} }
func init() { func initConfig() {
//cobra.OnInitialize(initConfig) // 设置配置文件的名称(不包含扩展名)
RootCmd.PersistentFlags().UintVar(&routineCount, "routine", 2, "routine count for corrupt do task") viper.SetConfigName("config")
// 设置配置文件的类型
viper.SetConfigType("json")
// 设置配置文件所在的目录
viper.AddConfigPath(".")
viper.AutomaticEnv()
// 读取配置文件
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Error reading config file:", err)
return
}
configFilePath := viper.ConfigFileUsed()
if configFilePath == "" {
// handle error
log.Error("config file path is empty")
panic("config file path is empty")
}
data, err := ioutil.ReadFile(configFilePath)
if err != nil {
// handle error
log.Error("Read cfg file error:", err)
panic("Read cfg file error")
}
err = json.Unmarshal(data, conf.GetConfig())
if err != nil {
// handle error
log.Error("Json unmarshal cfg error:", err)
panic("Json unmarshal cfg error")
}
conf.GetConfig().HeartRespTimeMillis = conf.GetConfig().HeartRespTimeSecond * 60 * 60 * 1000
prvKey, err := utils.GetPrv()
if err != nil {
panic("get prv error or delete keystore after restart")
}
conf.GetConfig().SignPrivateKey = prvKey
ecdsaPub := prvKey.PublicKey
conf.GetConfig().SignPub = common.Bytes2Hex(crypto.FromECDSAPub(&ecdsaPub))
log.Info("PublicKey", conf.GetConfig().SignPub)
publicAddr := crypto.PubkeyToAddress(ecdsaPub)
log.Info("publicAddr:", publicAddr)
conf.GetConfig().SignPublicAddress = publicAddr
} }
func Execute() { func Execute() {
......
...@@ -2,14 +2,7 @@ package conf ...@@ -2,14 +2,7 @@ package conf
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/json"
"example.com/m/log"
"example.com/m/utils"
"fmt"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/spf13/viper"
"io/ioutil"
) )
type Config struct { type Config struct {
...@@ -32,52 +25,7 @@ type Config struct { ...@@ -32,52 +25,7 @@ type Config struct {
var _cfg *Config = nil var _cfg *Config = nil
func init() { func init() {
// 设置配置文件的名称(不包含扩展名) _cfg = &Config{}
viper.SetConfigName("config")
// 设置配置文件的类型
viper.SetConfigType("json")
// 设置配置文件所在的目录
viper.AddConfigPath("./")
// 读取配置文件
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Error reading config file:", err)
return
}
configFilePath := viper.ConfigFileUsed()
if configFilePath == "" {
// handle error
log.Error("config file path is empty")
panic("config file path is empty")
}
data, err := ioutil.ReadFile(configFilePath)
if err != nil {
// handle error
log.Error("Read cfg file error:", err)
panic("Read cfg file error")
}
err = json.Unmarshal(data, &_cfg)
if err != nil {
// handle error
log.Error("Json unmarshal cfg error:", err)
panic("Json unmarshal cfg error")
}
_cfg.HeartRespTimeMillis = _cfg.HeartRespTimeSecond * 60 * 60 * 1000
prvKey, err := utils.GetPrv()
if err != nil {
panic("get prv error or delete keystore after restart")
}
_cfg.SignPrivateKey = prvKey
ecdsaPub := prvKey.PublicKey
_cfg.SignPub = common.Bytes2Hex(crypto.FromECDSAPub(&ecdsaPub))
log.Info("PublicKey", _cfg.SignPub)
publicAddr := crypto.PubkeyToAddress(ecdsaPub)
log.Info("publicAddr:", publicAddr)
_cfg.SignPublicAddress = publicAddr
} }
func GetConfig() *Config { func GetConfig() *Config {
......
...@@ -16,7 +16,7 @@ import ( ...@@ -16,7 +16,7 @@ import (
func monitorModelInfo(dockerOp *operate.DockerOp) { func monitorModelInfo(dockerOp *operate.DockerOp) {
client := &http.Client{} client := &http.Client{}
ticker := time.NewTicker(time.Second * 20) ticker := time.NewTicker(time.Second * 1)
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
) )
func monitorNodeManagerSeed() { func monitorNodeManagerSeed() {
ticker := time.NewTicker(time.Second * 20) ticker := time.NewTicker(time.Second * 1)
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
......
...@@ -28,7 +28,7 @@ func GenerateRandomNumber(privateKey *ecdsa.PrivateKey, length int64) *big.Int { ...@@ -28,7 +28,7 @@ func GenerateRandomNumber(privateKey *ecdsa.PrivateKey, length int64) *big.Int {
func GetPrv() (*ecdsa.PrivateKey, error) { func GetPrv() (*ecdsa.PrivateKey, error) {
if _, err := os.Stat(KEYPATH_PWD); os.IsNotExist(err) { if _, err := os.Stat(KEYPATH_PWD); os.IsNotExist(err) {
log.Info("Keystore not found. Generating a new one...") //log.Info("Keystore not found. Generating a new one...")
// 生成私钥 // 生成私钥
privateKey, err := generatePrivateKey() privateKey, err := generatePrivateKey()
if err != nil { if err != nil {
...@@ -41,17 +41,17 @@ func GetPrv() (*ecdsa.PrivateKey, error) { ...@@ -41,17 +41,17 @@ func GetPrv() (*ecdsa.PrivateKey, error) {
log.Error("Error saving private key:", err) log.Error("Error saving private key:", err)
return nil, err return nil, err
} }
log.Info("Keystore generated successfully.") //log.Info("Keystore generated successfully.")
return privateKey, nil return privateKey, nil
} else { } else {
log.Info("Keystore found. Reading private key...") //log.Info("Keystore found. Reading private key...")
// 读取私钥 // 读取私钥
privateKey, err := readPrivateKey() privateKey, err := readPrivateKey()
if err != nil || privateKey == nil { if err != nil || privateKey == nil {
log.Error("Error reading private key:", err) log.Error("Error reading private key:", err)
return nil, err return nil, err
} }
log.Info("Private key read successfully:", privateKey) //log.Info("Private key read successfully:", privateKey)
return privateKey, nil return privateKey, nil
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment