Skip to content

Commit

Permalink
feat: change init logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hunjixin committed May 4, 2023
1 parent ad3d0c6 commit 648202b
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 195 deletions.
67 changes: 32 additions & 35 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -80,7 +81,7 @@ var daemonCmd = &cmds.Command{
return fmt.Errorf("fetching proof parameters: %w", err)
}

exist, err := repo.Exists(repoDir)
exist, err := repo.Exists(repoDir) //The configuration file and devgen are required for the program to start
if err != nil {
return err
}
Expand All @@ -98,7 +99,16 @@ var daemonCmd = &cmds.Command{
if err := re.Emit(repoDir); err != nil {
return err
}
if err := repo.InitFSRepo(repoDir, repo.LatestVersion, config.NewDefaultConfig()); err != nil {

cfg, err := repo.LoadConfig(repoDir) //use exit config, allow user prepare config before
if err != nil {
if errors.Is(err, os.ErrNotExist) {
cfg = config.NewDefaultConfig()
} else {
return err
}
}
if err := repo.InitFSRepo(repoDir, repo.LatestVersion, cfg); err != nil {
return err
}

Expand All @@ -112,7 +122,12 @@ var daemonCmd = &cmds.Command{
}

func initRun(req *cmds.Request) error {
rep, err := getRepo(req)
repoDir, _ := req.Options[OptionRepoDir].(string)
err := repo.WriteVersion(repoDir, repo.LatestVersion)
if err != nil {
return err
}
rep, err := getRepo(repoDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -166,12 +181,22 @@ func initRun(req *cmds.Request) error {
return err
}

// import snapshot argument only work when init
importPath, _ := req.Options[ImportSnapshot].(string)
if len(importPath) != 0 {
err := Import(req.Context, rep, importPath)
if err != nil {
log.Errorf("failed to import snapshot, import path: %s, error: %s", importPath, err.Error())
return err
}
}

return nil
}

func daemonRun(req *cmds.Request, re cmds.ResponseEmitter) error {
// third precedence is config file.
rep, err := getRepo(req)
repoDir, _ := req.Options[OptionRepoDir].(string)
rep, err := getRepo(repoDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -219,7 +244,7 @@ func daemonRun(req *cmds.Request, re cmds.ResponseEmitter) error {
}

if bootPeers, ok := req.Options[BootstrapPeers].([]string); ok && len(bootPeers) > 0 {
config.Bootstrap.Addresses = MergePeers(config.Bootstrap.Addresses, bootPeers)
config.Bootstrap.AddPeers(bootPeers...)
}

opts, err := node.OptionsFromRepo(rep)
Expand All @@ -234,14 +259,6 @@ func daemonRun(req *cmds.Request, re cmds.ResponseEmitter) error {
if isRelay, ok := req.Options[IsRelay].(bool); ok && isRelay {
opts = append(opts, node.IsRelay())
}
importPath, _ := req.Options[ImportSnapshot].(string)
if len(importPath) != 0 {
err := Import(req.Context, rep, importPath)
if err != nil {
log.Errorf("failed to import snapshot, import path: %s, error: %s", importPath, err.Error())
return err
}
}

if password, _ := req.Options[Password].(string); len(password) > 0 {
opts = append(opts, node.SetWalletPassword([]byte(password)))
Expand Down Expand Up @@ -296,8 +313,7 @@ func daemonRun(req *cmds.Request, re cmds.ResponseEmitter) error {
return fcn.RunRPCAndWait(req.Context, RootCmdDaemon, ready)
}

func getRepo(req *cmds.Request) (repo.Repo, error) {
repoDir, _ := req.Options[OptionRepoDir].(string)
func getRepo(repoDir string) (repo.Repo, error) {
repoDir, err := paths.GetRepoPath(repoDir)
if err != nil {
return nil, err
Expand All @@ -307,22 +323,3 @@ func getRepo(req *cmds.Request) (repo.Repo, error) {
}
return repo.OpenFSRepo(repoDir, repo.LatestVersion)
}

func MergePeers(peerSet1 []string, peerSet2 []string) []string {

filter := map[string]struct{}{}
for _, peer := range peerSet1 {
filter[peer] = struct{}{}
}

notInclude := []string{}
for _, peer := range peerSet2 {
_, has := filter[peer]
if has {
continue
}
filter[peer] = struct{}{}
notInclude = append(notInclude, peer)
}
return append(peerSet1, notInclude...)
}
22 changes: 0 additions & 22 deletions cmd/daemon_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"strconv"
"testing"

"github.com/filecoin-project/venus/cmd"

manet "github.com/multiformats/go-multiaddr/net"

th "github.com/filecoin-project/venus/pkg/testhelpers"
Expand Down Expand Up @@ -166,23 +164,3 @@ func TestDaemonOverHttp(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusNotFound, res.StatusCode)
}

func Test_MergePeers(t *testing.T) {
t.Run("empty", func(t *testing.T) {
peerSet1 := []string{"a", "b"}
peerSet2 := []string{}
assert.Equal(t, peerSet1, cmd.MergePeers(peerSet1, peerSet2))
})

t.Run("join", func(t *testing.T) {
peerSet1 := []string{"a", "b"}
peerSet2 := []string{"c", "d"}
assert.Equal(t, append(peerSet1, peerSet2...), cmd.MergePeers(peerSet1, peerSet2))
})

t.Run("distinct", func(t *testing.T) {
peerSet1 := []string{"a", "b"}
peerSet2 := []string{"a", "b", "c", "a", "b", "e"}
assert.Equal(t, append(peerSet1, "c", "e"), cmd.MergePeers(peerSet1, peerSet2))
})
}
7 changes: 6 additions & 1 deletion fixtures/networks/network_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

"github.com/filecoin-project/venus/pkg/util"

"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/filecoin-project/venus/venus-shared/utils"
Expand All @@ -28,8 +30,11 @@ func SetConfigFromOptions(cfg *config.Config, networkName string) error {
if err != nil {
return err
}
cfg.Bootstrap = &netcfg.Bootstrap
cfg.NetworkParams = &netcfg.Network
//merge with config and option
peers := util.MergePeers(cfg.Bootstrap.Addresses, netcfg.Bootstrap.Addresses)
cfg.Bootstrap = &netcfg.Bootstrap
cfg.Bootstrap.AddPeers(peers...)
return nil
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ type BootstrapConfig struct {
Period string `json:"period,omitempty"`
}

func (bsc *BootstrapConfig) AddPeers(peers ...string) {
filter := map[string]struct{}{}
for _, peer := range bsc.Addresses {
filter[peer] = struct{}{}
}

notInclude := []string{}
for _, peer := range peers {
_, has := filter[peer]
if has {
continue
}
filter[peer] = struct{}{}
notInclude = append(notInclude, peer)
}
bsc.Addresses = append(bsc.Addresses, notInclude...)
}

// TODO: provide bootstrap node addresses
func newDefaultBootstrapConfig() *BootstrapConfig {
return &BootstrapConfig{
Expand Down
18 changes: 18 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ func createConfigFile(t *testing.T, content string) (string, error) {
}

func TestDuration(t *testing.T) {
tf.UnitTest(t)

d, err := time.ParseDuration("1h5m")
require.NoError(t, err)

Expand All @@ -277,3 +279,19 @@ func TestDuration(t *testing.T) {
require.NoError(t, json.Unmarshal(data, &res))
require.Equal(t, dd, res)
}

func TestAddBootPeers(t *testing.T) {
tf.UnitTest(t)

boot := &BootstrapConfig{}
boot.AddPeers("a")
assert.Equal(t, []string{"a"}, boot.Addresses)

boot.AddPeers("a")
assert.Equal(t, []string{"a"}, boot.Addresses)

boot.Addresses = []string{"a", "b"}
boot.AddPeers("a", "c")
assert.Equal(t, []string{"a", "b", "c"}, boot.Addresses)

}
Loading

0 comments on commit 648202b

Please sign in to comment.