Break import cycle at ./internal/utils → ./p2p

pull/1939/head
Eugene Kim 5 years ago
parent 229456eb41
commit 65a066c70b
  1. 9
      api/service/networkinfo/service.go
  2. 9
      cmd/client/txgen/main.go
  3. 10
      cmd/client/wallet/main.go
  4. 10
      cmd/client/wallet_stress_test/main.go
  5. 9
      cmd/harmony/main.go
  6. 28
      internal/utils/flags_test.go
  7. 38
      internal/utils/utils_test.go
  8. 12
      internal/wallet/configfile.go
  9. 12
      internal/wallet/configfile_test.go
  10. 0
      internal/wallet/test.ini
  11. 2
      p2p/utils/flags.go
  12. 67
      p2p/utils/flags_test.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

@ -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{

@ -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

@ -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

@ -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 {

@ -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")
}
}

@ -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)
}
}
}
}

@ -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

@ -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])

@ -1,4 +1,4 @@
package utils
package p2putils
import (
"fmt"

@ -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)
}
}
}
}
Loading…
Cancel
Save