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

update run #80

Merged
merged 1 commit into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
90 changes: 42 additions & 48 deletions cmd/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package cmd
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -27,7 +26,9 @@ import (
"github.com/CESSProject/cess-go-sdk/core/pattern"
sutils "github.com/CESSProject/cess-go-sdk/core/utils"
"github.com/CESSProject/p2p-go/config"
"github.com/CESSProject/p2p-go/out"
"github.com/howeyc/gopass"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -47,13 +48,13 @@ func cmd_run_func(cmd *cobra.Command, args []string) {
// Building Profile Instances
n.Confile, err = buildConfigFile(cmd)
if err != nil {
log.Println(err)
out.Err(err.Error())
os.Exit(1)
}

signKey, err := sutils.CalcMD5(n.Confile.GetMnemonic())
if err != nil {
log.Println(err)
out.Err(err.Error())
os.Exit(1)
}

Expand All @@ -62,19 +63,19 @@ func cmd_run_func(cmd *cobra.Command, args []string) {
boots := n.GetBootNodes()
for _, v := range boots {
if strings.Contains(v, "testnet") {
log.Println("Test network")
out.Tip("Test network")
protocolPrefix = config.TestnetProtocolPrefix
break
} else if strings.Contains(v, "mainnet") {
log.Println("Main network")
out.Tip("Main network")
protocolPrefix = config.MainnetProtocolPrefix
break
} else if strings.Contains(v, "devnet") {
log.Println("Dev network")
out.Tip("Dev network")
protocolPrefix = config.DevnetProtocolPrefix
break
} else {
log.Println("Unknown network")
out.Tip("Unknown network")
}
}

Expand All @@ -91,21 +92,21 @@ func cmd_run_func(cmd *cobra.Command, args []string) {
sdkgo.ProtocolPrefix(protocolPrefix),
)
if err != nil {
log.Println(err)
out.Err(err.Error())
os.Exit(1)
}

for {
syncSt, err = n.SyncState()
if err != nil {
log.Println(err.Error())
out.Err(err.Error())
os.Exit(1)
}
if syncSt.CurrentBlock == syncSt.HighestBlock {
log.Println(fmt.Sprintf("Synchronization main chain completed: %d", syncSt.CurrentBlock))
out.Tip(fmt.Sprintf("Synchronization main chain completed: %d", syncSt.CurrentBlock))
break
}
log.Println(fmt.Sprintf("In the synchronization main chain: %d ...", syncSt.CurrentBlock))
out.Tip(fmt.Sprintf("In the synchronization main chain: %d ...", syncSt.CurrentBlock))
time.Sleep(time.Second * time.Duration(utils.Ternary(int64(syncSt.HighestBlock-syncSt.CurrentBlock)*6, 30)))
}

Expand All @@ -114,14 +115,14 @@ func cmd_run_func(cmd *cobra.Command, args []string) {
if err.Error() == pattern.ERR_Empty {
registerFlag = true
} else {
log.Println("Weak network signal or rpc service failure")
out.Err("Weak network signal or rpc service failure")
os.Exit(1)
}
}

_, _, err = n.Register(n.GetRoleName(), n.GetPeerPublickey(), "", 0)
if err != nil {
log.Println("Register or update err: ", err)
out.Err(fmt.Sprintf("Register or update err: %v", err))
os.Exit(1)
}

Expand All @@ -131,24 +132,26 @@ func cmd_run_func(cmd *cobra.Command, args []string) {

logDir, dbDir, n.TrackDir, err = buildDir(n.Workspace())
if err != nil {
log.Println("buildDir err: ", err)
out.Err(err.Error())
os.Exit(1)
}

// Build cache
n.Cache, err = buildCache(dbDir)
if err != nil {
log.Println(err)
out.Err(err.Error())
os.Exit(1)
}

// Build Log
n.Logger, err = buildLogs(logDir)
if err != nil {
log.Println(err)
out.Err(err.Error())
os.Exit(1)
}

out.Tip(n.GetProtocolPrefix())

// run
n.Run()
}
Expand All @@ -160,15 +163,13 @@ func buildConfigFile(cmd *cobra.Command) (confile.Confile, error) {
if configpath1 != "" {
_, err := os.Stat(configpath1)
if err != nil {
log.Println(err)
os.Exit(1)
return nil, errors.Wrapf(err, "[Stat %s]", configpath1)
}
conFilePath = configpath1
} else if configpath2 != "" {
_, err := os.Stat(configpath2)
if err != nil {
log.Println(err)
os.Exit(1)
return nil, errors.Wrapf(err, "[Stat %s]", configpath2)
}
conFilePath = configpath2
} else {
Expand All @@ -178,27 +179,25 @@ func buildConfigFile(cmd *cobra.Command) (confile.Confile, error) {
cfg := confile.NewConfigfile()
err := cfg.Parse(conFilePath)
if err == nil {
return cfg, err
return cfg, nil
}

rpc, err := cmd.Flags().GetStringSlice("rpc")
if err != nil {
return cfg, err
return cfg, errors.Wrapf(err, "[cmd.Flags().GetStringSlice(\"rpc\")]")
}

if len(rpc) == 0 {
log.Println("Please specify the rpc address with --rpc")
os.Exit(1)
return cfg, errors.New("Please specify the rpc address with --rpc")
}
cfg.SetRpcAddr(rpc)

boot, err := cmd.Flags().GetStringSlice("boot")
if err != nil {
return cfg, err
return cfg, errors.Wrapf(err, "[cmd.Flags().GetStringSlice(\"boot\")]")
}
if len(boot) == 0 {
log.Println("Please specify the boot node address with --boot")
os.Exit(1)
return cfg, errors.New("Please specify the boot node address with --boot")
}
cfg.SetBootNodes(boot)

Expand All @@ -207,67 +206,62 @@ func buildConfigFile(cmd *cobra.Command) (confile.Confile, error) {
return cfg, err
}
if workspace == "" {
log.Println("Please specify the sorkspace with --ws")
os.Exit(1)
return cfg, errors.New("Please specify the sorkspace with --ws")
}
err = cfg.SetWorkspace(workspace)
if err != nil {
log.Println(err)
os.Exit(1)
return cfg, errors.Wrapf(err, "[SetWorkspace %s]", workspace)
}

http_port, err := cmd.Flags().GetInt("http_port")
if err != nil {
return cfg, err
return cfg, errors.Wrapf(err, "[cmd.Flags().GetInt(\"http_port\")]")
}

p2p_port, err := cmd.Flags().GetInt("p2p_port")
if err != nil {
return cfg, err
return cfg, errors.Wrapf(err, "[cmd.Flags().GetInt(\"p2p_port\")]")
}

err = cfg.SetHttpPort(http_port)
if err != nil {
log.Println(err)
os.Exit(1)
return cfg, errors.Wrapf(err, "[SetHttpPort %d]", http_port)
}
err = cfg.SetP2pPort(p2p_port)
if err != nil {
log.Println(err)
os.Exit(1)
return cfg, errors.Wrapf(err, "[SetP2pPort %d]", p2p_port)
}

mnemonic, err := cmd.Flags().GetString("mnemonic")
if err != nil {
return cfg, err
return cfg, errors.Wrapf(err, "[cmd.Flags().GetString(\"mnemonic\")]")
}
if mnemonic == "" {
log.Println("Please enter the mnemonic of the staking account:")
out.Input("Please enter the mnemonic of the staking account:")
for {
pwd, err := gopass.GetPasswdMasked()
if err != nil {
if err.Error() == "interrupted" || err.Error() == "interrupt" || err.Error() == "killed" {
os.Exit(0)
}
log.Println("Invalid mnemonic, please check and re-enter:")
out.Input("Invalid mnemonic, please check and re-enter:")
continue
}
if len(pwd) == 0 {
log.Println("The mnemonic you entered is empty, please re-enter:")
out.Input("The mnemonic you entered is empty, please re-enter:")
continue
}
err = cfg.SetMnemonic(string(pwd))
if err != nil {
log.Println("Invalid mnemonic, please check and re-enter:")
out.Input("Invalid mnemonic, please check and re-enter:")
continue
}
break
}
} else {
err = cfg.SetMnemonic(mnemonic)
if err != nil {
log.Println("invalid mnemonic")
os.Exit(1)
return cfg, errors.Wrapf(err, "[SetMnemonic] [%s]", mnemonic)
}
}
return cfg, nil
Expand Down Expand Up @@ -297,23 +291,23 @@ func buildAuthenticationConfig(cmd *cobra.Command) (confile.Confile, error) {
}
cfg.SetRpcAddr(rpc)

log.Println("Please enter the mnemonic of the staking account:")
out.Input("Please enter the mnemonic of the staking account:")
for {
pwd, err := gopass.GetPasswdMasked()
if err != nil {
if err.Error() == "interrupted" || err.Error() == "interrupt" || err.Error() == "killed" {
os.Exit(0)
}
log.Println("Invalid mnemonic, please check and re-enter:")
out.Input("Invalid mnemonic, please check and re-enter:")
continue
}
if len(pwd) == 0 {
log.Println("The mnemonic you entered is empty, please re-enter:")
out.Input("The mnemonic you entered is empty, please re-enter:")
continue
}
err = cfg.SetMnemonic(string(pwd))
if err != nil {
log.Println("Invalid mnemonic, please check and re-enter:")
out.Input("Invalid mnemonic, please check and re-enter:")
continue
}
break
Expand Down
2 changes: 1 addition & 1 deletion node/discoverMgt.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (n *Node) discoverMgt(ch chan<- bool) {
}
}
n.Discover("info", "RouteTableFindPeers")
_, err := n.RouteTableFindPeers(len(n.peers) + 10)
_, err := n.RouteTableFindPeers(len(n.peers) + 20)
if err != nil {
n.Discover("err", err.Error())
}
Expand Down
3 changes: 2 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/CESSProject/DeOSS/pkg/logger"
"github.com/CESSProject/cess-go-sdk/core/pattern"
"github.com/CESSProject/cess-go-sdk/core/sdk"
"github.com/CESSProject/p2p-go/out"
"github.com/bytedance/sonic"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (n *Node) Run() {
n.addRoute()
// Task management
go n.TaskMgt()
log.Println("Listening on port:", n.GetHttpPort())
out.Tip(fmt.Sprintf("Listening on port: %d", n.GetHttpPort()))
// Run
err := n.Engine.Run(fmt.Sprintf(":%d", n.GetHttpPort()))
if err != nil {
Expand Down