From 65a066c70b3c57d1ecd0ae632a29ca5540251b27 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Mon, 2 Dec 2019 16:13:55 -0800 Subject: [PATCH] =?UTF-8?q?Break=20import=20cycle=20at=20./internal/utils?= =?UTF-8?q?=20=E2=86=92=20./p2p?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/service/networkinfo/service.go | 9 +-- cmd/client/txgen/main.go | 9 +-- cmd/client/wallet/main.go | 10 +-- cmd/client/wallet_stress_test/main.go | 10 +-- cmd/harmony/main.go | 9 +-- internal/utils/flags_test.go | 28 -------- internal/utils/utils_test.go | 38 ----------- internal/{utils => wallet}/configfile.go | 12 ++-- internal/{utils => wallet}/configfile_test.go | 12 ++-- internal/{utils => wallet}/test.ini | 0 {internal => p2p}/utils/flags.go | 2 +- p2p/utils/flags_test.go | 67 +++++++++++++++++++ 12 files changed, 107 insertions(+), 99 deletions(-) delete mode 100644 internal/utils/flags_test.go rename internal/{utils => wallet}/configfile.go (88%) rename internal/{utils => wallet}/configfile_test.go (88%) rename internal/{utils => wallet}/test.ini (100%) rename {internal => p2p}/utils/flags.go (99%) create mode 100644 p2p/utils/flags_test.go diff --git a/api/service/networkinfo/service.go b/api/service/networkinfo/service.go index 4fab2891f..e09c34d0a 100644 --- a/api/service/networkinfo/service.go +++ b/api/service/networkinfo/service.go @@ -15,6 +15,7 @@ import ( nodeconfig "github.com/harmony-one/harmony/internal/configs/node" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/p2p" + p2putils "github.com/harmony-one/harmony/p2p/utils" badger "github.com/ipfs/go-ds-badger" coredis "github.com/libp2p/go-libp2p-core/discovery" libp2pdis "github.com/libp2p/go-libp2p-discovery" @@ -27,7 +28,7 @@ import ( type Service struct { Host p2p.Host Rendezvous nodeconfig.GroupID - bootnodes utils.AddrList + bootnodes p2putils.AddrList dht *libp2pdht.IpfsDHT cancel context.CancelFunc stopChan chan struct{} @@ -60,7 +61,7 @@ const ( // points to a persistent database directory to use. func New( h p2p.Host, rendezvous nodeconfig.GroupID, peerChan chan p2p.Peer, - bootnodes utils.AddrList, dataStorePath string, + bootnodes p2putils.AddrList, dataStorePath string, ) (*Service, error) { ctx, cancel := context.WithCancel(context.Background()) var dhtOpts []libp2pdhtopts.Option @@ -98,7 +99,7 @@ func New( // MustNew is a panic-on-error version of New. func MustNew( h p2p.Host, rendezvous nodeconfig.GroupID, peerChan chan p2p.Peer, - bootnodes utils.AddrList, dataStorePath string, + bootnodes p2putils.AddrList, dataStorePath string, ) *Service { service, err := New(h, rendezvous, peerChan, bootnodes, dataStorePath) if err != nil { @@ -134,7 +135,7 @@ func (s *Service) Init() error { var wg sync.WaitGroup if s.bootnodes == nil { // TODO: should've passed in bootnodes through constructor. - s.bootnodes = utils.BootNodes + s.bootnodes = p2putils.BootNodes } connected := false diff --git a/cmd/client/txgen/main.go b/cmd/client/txgen/main.go index 3bd00f460..ea30eac66 100644 --- a/cmd/client/txgen/main.go +++ b/cmd/client/txgen/main.go @@ -30,6 +30,7 @@ import ( "github.com/harmony-one/harmony/p2p" p2p_host "github.com/harmony-one/harmony/p2p/host" "github.com/harmony-one/harmony/p2p/p2pimpl" + p2putils "github.com/harmony-one/harmony/p2p/utils" "github.com/harmony-one/harmony/shard" ) @@ -129,7 +130,7 @@ func setUpTXGen() *node.Node { } func main() { - flag.Var(&utils.BootNodes, "bootnodes", "a list of bootnode multiaddress") + flag.Var(&p2putils.BootNodes, "bootnodes", "a list of bootnode multiaddress") flag.Parse() if *versionFlag { printVersion(os.Args[0]) @@ -137,12 +138,12 @@ func main() { // Logging setup utils.SetLogContext(*port, *ip) utils.SetLogVerbosity(log.Lvl(*verbosity)) - if len(utils.BootNodes) == 0 { - bootNodeAddrs, err := utils.StringsToAddrs(utils.DefaultBootNodeAddrStrings) + if len(p2putils.BootNodes) == 0 { + bootNodeAddrs, err := p2putils.StringsToAddrs(p2putils.DefaultBootNodeAddrStrings) if err != nil { panic(err) } - utils.BootNodes = bootNodeAddrs + p2putils.BootNodes = bootNodeAddrs } // Init with LibP2P enabled, FIXME: (leochen) right now we support only one shard setting := Settings{ diff --git a/cmd/client/wallet/main.go b/cmd/client/wallet/main.go index a639f64c8..4fe6170ec 100644 --- a/cmd/client/wallet/main.go +++ b/cmd/client/wallet/main.go @@ -33,10 +33,12 @@ import ( "github.com/harmony-one/harmony/internal/ctxerror" "github.com/harmony-one/harmony/internal/shardchain" "github.com/harmony-one/harmony/internal/utils" + "github.com/harmony-one/harmony/internal/wallet" "github.com/harmony-one/harmony/node" "github.com/harmony-one/harmony/p2p" p2p_host "github.com/harmony-one/harmony/p2p/host" "github.com/harmony-one/harmony/p2p/p2pimpl" + p2putils "github.com/harmony-one/harmony/p2p/utils" ) var ( @@ -124,7 +126,7 @@ var ( var ( networkType string - walletProfile *utils.WalletProfile + walletProfile *wallet.Profile ks *keystore.KeyStore ) @@ -278,7 +280,7 @@ func readProfile(profile string) { iniBytes = []byte(defaultWalletIni) } - walletProfile, err = utils.ReadWalletProfile(iniBytes, profile) + walletProfile, err = wallet.ReadProfile(iniBytes, profile) if err != nil { fmt.Printf("Read wallet profile error: %v\nExiting ...\n", err) os.Exit(2) @@ -287,11 +289,11 @@ func readProfile(profile string) { // createWalletNode creates wallet server node. func createWalletNode() *node.Node { - bootNodeAddrs, err := utils.StringsToAddrs(walletProfile.Bootnodes) + bootNodeAddrs, err := p2putils.StringsToAddrs(walletProfile.Bootnodes) if err != nil { panic(err) } - utils.BootNodes = bootNodeAddrs + p2putils.BootNodes = bootNodeAddrs shardID := 0 // dummy host for wallet // TODO: potentially, too many dummy IP may flush out good IP address from our bootnode DHT diff --git a/cmd/client/wallet_stress_test/main.go b/cmd/client/wallet_stress_test/main.go index 6b1574db8..bf7f7dd02 100644 --- a/cmd/client/wallet_stress_test/main.go +++ b/cmd/client/wallet_stress_test/main.go @@ -27,10 +27,12 @@ import ( "github.com/harmony-one/harmony/internal/ctxerror" "github.com/harmony-one/harmony/internal/shardchain" "github.com/harmony-one/harmony/internal/utils" + "github.com/harmony-one/harmony/internal/wallet" "github.com/harmony-one/harmony/node" "github.com/harmony-one/harmony/p2p" p2p_host "github.com/harmony-one/harmony/p2p/host" "github.com/harmony-one/harmony/p2p/p2pimpl" + p2putils "github.com/harmony-one/harmony/p2p/utils" ) var ( @@ -72,7 +74,7 @@ var ( ) var ( - walletProfile *utils.WalletProfile + walletProfile *wallet.Profile ks *keystore.KeyStore ) @@ -162,7 +164,7 @@ func readProfile(profile string) { iniBytes = []byte(defaultWalletIni) } - walletProfile, err = utils.ReadWalletProfile(iniBytes, profile) + walletProfile, err = wallet.ReadProfile(iniBytes, profile) if err != nil { fmt.Printf("Read wallet profile error: %v\nExiting ...\n", err) os.Exit(2) @@ -171,11 +173,11 @@ func readProfile(profile string) { // createWalletNode creates wallet server node. func createWalletNode() *node.Node { - bootNodeAddrs, err := utils.StringsToAddrs(walletProfile.Bootnodes) + bootNodeAddrs, err := p2putils.StringsToAddrs(walletProfile.Bootnodes) if err != nil { panic(err) } - utils.BootNodes = bootNodeAddrs + p2putils.BootNodes = bootNodeAddrs shardID := 0 // dummy host for wallet // TODO: potentially, too many dummy IP may flush out good IP address from our bootnode DHT diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index a90faf628..af73b6046 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -33,6 +33,7 @@ import ( "github.com/harmony-one/harmony/node" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p/p2pimpl" + p2putils "github.com/harmony-one/harmony/p2p/utils" "github.com/harmony-one/harmony/shard" ) @@ -154,12 +155,12 @@ func initSetup() { // Set up randomization seed. rand.Seed(int64(time.Now().Nanosecond())) - if len(utils.BootNodes) == 0 { - bootNodeAddrs, err := utils.StringsToAddrs(utils.DefaultBootNodeAddrStrings) + if len(p2putils.BootNodes) == 0 { + bootNodeAddrs, err := p2putils.StringsToAddrs(p2putils.DefaultBootNodeAddrStrings) if err != nil { panic(err) } - utils.BootNodes = bootNodeAddrs + p2putils.BootNodes = bootNodeAddrs } } @@ -398,7 +399,7 @@ func setupConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node { } func main() { - flag.Var(&utils.BootNodes, "bootnodes", "a list of bootnode multiaddress (delimited by ,)") + flag.Var(&p2putils.BootNodes, "bootnodes", "a list of bootnode multiaddress (delimited by ,)") flag.Parse() switch *nodeType { diff --git a/internal/utils/flags_test.go b/internal/utils/flags_test.go deleted file mode 100644 index 39343d93e..000000000 --- a/internal/utils/flags_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "fmt" - "testing" -) - -func TestStringsToAddrs(t *testing.T) { - bn, err := StringsToAddrs(DefaultBootNodeAddrStrings) - if err != nil { - t.Fatalf("unable to convert string to addresses: %v", err) - } - if len(bn) == 0 { - t.Fatalf("should have more than one multiaddress returned") - } -} - -func TestAddrListFunc(t *testing.T) { - addr := new(AddrList) - err := addr.Set("/ip4/127.0.0.1/tcp/9999/p2p/QmayB8NwxmfGE4Usb4H61M8uwbfc7LRbmXb3ChseJgbVuf,/ip4/127.0.0.1/tcp/9877/p2p/QmS374uzJ9yEEoWcEQ6JcbSUaVUj29SKakcmVvr3HVAjKP") - if err != nil || len(*addr) < 2 { - t.Fatalf("unable to set addr list") - } - s := fmt.Sprintf("addr: %s\n", addr) - if len(s) == 0 { - t.Fatalf("unable to print AddrList") - } -} diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index 0cb2dc76b..97c87fcda 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -3,10 +3,8 @@ package utils import ( "net" "os" - "reflect" "testing" - "github.com/harmony-one/harmony/p2p" crypto "github.com/libp2p/go-libp2p-crypto" "github.com/stretchr/testify/assert" ) @@ -168,39 +166,3 @@ func TestIsPrivateIP(t *testing.T) { } } } - -func TestStringsToPeers(t *testing.T) { - tests := []struct { - input string - expected []p2p.Peer - }{ - { - "127.0.0.1:9000,192.168.192.1:8888,54.32.12.3:9898", - []p2p.Peer{ - {IP: "127.0.0.1", Port: "9000"}, - {IP: "192.168.192.1", Port: "8888"}, - {IP: "54.32.12.3", Port: "9898"}, - }, - }, - { - "a:b,xx:XX,hello:world", - []p2p.Peer{ - {IP: "a", Port: "b"}, - {IP: "xx", Port: "XX"}, - {IP: "hello", Port: "world"}, - }, - }, - } - - for _, test := range tests { - peers := StringsToPeers(test.input) - if len(peers) != 3 { - t.Errorf("StringsToPeers failure") - } - for i, p := range peers { - if !reflect.DeepEqual(p, test.expected[i]) { - t.Errorf("StringToPeers: expected: %v, got: %v", test.expected[i], p) - } - } - } -} diff --git a/internal/utils/configfile.go b/internal/wallet/configfile.go similarity index 88% rename from internal/utils/configfile.go rename to internal/wallet/configfile.go index aa2123d1a..477c8bd83 100644 --- a/internal/utils/configfile.go +++ b/internal/wallet/configfile.go @@ -1,4 +1,4 @@ -package utils +package wallet // this module in utils handles the ini file read/write import ( @@ -12,8 +12,8 @@ import ( "github.com/harmony-one/harmony/p2p" ) -// WalletProfile contains a section and key value pair map -type WalletProfile struct { +// Profile contains a section and key value pair map +type Profile struct { Profile string ChainID string Bootnodes []string @@ -22,13 +22,13 @@ type WalletProfile struct { Network string } -// ReadWalletProfile reads an ini file and return WalletProfile -func ReadWalletProfile(iniBytes []byte, profile string) (*WalletProfile, error) { +// ReadProfile reads an ini file and return Profile +func ReadProfile(iniBytes []byte, profile string) (*Profile, error) { cfg, err := ini.ShadowLoad(iniBytes) if err != nil { return nil, err } - config := new(WalletProfile) + config := new(Profile) config.Profile = profile // get the profile section diff --git a/internal/utils/configfile_test.go b/internal/wallet/configfile_test.go similarity index 88% rename from internal/utils/configfile_test.go rename to internal/wallet/configfile_test.go index 681a18d28..4703e1d28 100644 --- a/internal/utils/configfile_test.go +++ b/internal/wallet/configfile_test.go @@ -1,4 +1,4 @@ -package utils +package wallet import ( "io/ioutil" @@ -10,7 +10,7 @@ import ( ) func TestReadWalletProfile(t *testing.T) { - config := []*WalletProfile{ + config := []*Profile{ { Profile: "default", ChainID: params.MainnetChainID.String(), @@ -106,16 +106,16 @@ func TestReadWalletProfile(t *testing.T) { t.Fatalf("Failed to read test.ini: %v", err) } - config1, err := ReadWalletProfile(testIniBytes, "default") + config1, err := ReadProfile(testIniBytes, "default") if err != nil { - t.Fatalf("ReadWalletProfile Error: %v", err) + t.Fatalf("ReadProfile Error: %v", err) } if !reflect.DeepEqual(config[0], config1) { t.Errorf("Got: %v\nExpect: %v\n", config1, config[0]) } - config2, err := ReadWalletProfile(testIniBytes, "testnet") + config2, err := ReadProfile(testIniBytes, "testnet") if err != nil { - t.Fatalf("ReadWalletProfile Error: %v", err) + t.Fatalf("ReadProfile Error: %v", err) } if !reflect.DeepEqual(config[1], config2) { t.Errorf("Got: %v\nExpect: %v\n", config2, config[1]) diff --git a/internal/utils/test.ini b/internal/wallet/test.ini similarity index 100% rename from internal/utils/test.ini rename to internal/wallet/test.ini diff --git a/internal/utils/flags.go b/p2p/utils/flags.go similarity index 99% rename from internal/utils/flags.go rename to p2p/utils/flags.go index 0f8d4ec9d..28ff73b28 100644 --- a/internal/utils/flags.go +++ b/p2p/utils/flags.go @@ -1,4 +1,4 @@ -package utils +package p2putils import ( "fmt" diff --git a/p2p/utils/flags_test.go b/p2p/utils/flags_test.go new file mode 100644 index 000000000..43b21cc39 --- /dev/null +++ b/p2p/utils/flags_test.go @@ -0,0 +1,67 @@ +package p2putils + +import ( + "fmt" + "reflect" + "testing" + + "github.com/harmony-one/harmony/p2p" +) + +func TestStringsToAddrs(t *testing.T) { + bn, err := StringsToAddrs(DefaultBootNodeAddrStrings) + if err != nil { + t.Fatalf("unable to convert string to addresses: %v", err) + } + if len(bn) == 0 { + t.Fatalf("should have more than one multiaddress returned") + } +} + +func TestAddrListFunc(t *testing.T) { + addr := new(AddrList) + err := addr.Set("/ip4/127.0.0.1/tcp/9999/p2p/QmayB8NwxmfGE4Usb4H61M8uwbfc7LRbmXb3ChseJgbVuf,/ip4/127.0.0.1/tcp/9877/p2p/QmS374uzJ9yEEoWcEQ6JcbSUaVUj29SKakcmVvr3HVAjKP") + if err != nil || len(*addr) < 2 { + t.Fatalf("unable to set addr list") + } + s := fmt.Sprintf("addr: %s\n", addr) + if len(s) == 0 { + t.Fatalf("unable to print AddrList") + } +} + +func TestStringsToPeers(t *testing.T) { + tests := []struct { + input string + expected []p2p.Peer + }{ + { + "127.0.0.1:9000,192.168.192.1:8888,54.32.12.3:9898", + []p2p.Peer{ + {IP: "127.0.0.1", Port: "9000"}, + {IP: "192.168.192.1", Port: "8888"}, + {IP: "54.32.12.3", Port: "9898"}, + }, + }, + { + "a:b,xx:XX,hello:world", + []p2p.Peer{ + {IP: "a", Port: "b"}, + {IP: "xx", Port: "XX"}, + {IP: "hello", Port: "world"}, + }, + }, + } + + for _, test := range tests { + peers := StringsToPeers(test.input) + if len(peers) != 3 { + t.Errorf("StringsToPeers failure") + } + for i, p := range peers { + if !reflect.DeepEqual(p, test.expected[i]) { + t.Errorf("StringToPeers: expected: %v, got: %v", test.expected[i], p) + } + } + } +}