diff --git a/config/network.toml b/config/network.toml index f3034d8..ce63128 100644 --- a/config/network.toml +++ b/config/network.toml @@ -1,6 +1,18 @@ -addrs = [ - ["/ip4/127.0.0.1/tcp/4001/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL"], - ["/ip4/127.0.0.1/tcp/4002/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4"], - ["/ip4/127.0.0.1/tcp/4003/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS"], - ["/ip4/127.0.0.1/tcp/4004/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy"] -] \ No newline at end of file +id = 1 # self id +N = 4 # the number of cluster nodes + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4001/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS" +id = 1 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4002/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy" +id = 2 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4003/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL" +id = 3 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4004/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4" +id = 4 diff --git a/internal/repo/config_test.go b/internal/repo/config_test.go index 6bcb6d0..e7c31e1 100644 --- a/internal/repo/config_test.go +++ b/internal/repo/config_test.go @@ -20,15 +20,3 @@ func testReadConfig(t *testing.T) { assert.True(t, uint64(i+1) == node.ID) } } - -func TestReadNewConfig(t *testing.T) { - repo := "./testdata" - cfg, err := loadNetworkConfig(repo) - assert.Nil(t, err) - assert.True(t, 4 == cfg.ID) - assert.True(t, 4 == cfg.N) - assert.True(t, 4 == len(cfg.Nodes)) - assert.True(t, "/ip4/0.0.0.0/tcp/4001" == cfg.LocalAddr) - assert.True(t, "/ip4/127.0.0.1/tcp/4002/p2p/QmNRgD6djYJERNpDpHqRn3mxjJ9SYiiGWzExNSy4sEmSNL" == cfg.Nodes[0].Addr) - assert.True(t, 3 == len(cfg.OtherNodes)) -} diff --git a/internal/repo/network.go b/internal/repo/network.go index e1ad391..39fea7e 100644 --- a/internal/repo/network.go +++ b/internal/repo/network.go @@ -2,47 +2,28 @@ package repo import ( "fmt" - "io/ioutil" - "path/filepath" - "sort" - "strings" - "github.com/libp2p/go-libp2p-core/crypto" - "github.com/libp2p/go-libp2p-core/peer" crypto2 "github.com/meshplus/bitxhub-kit/crypto" "github.com/meshplus/bitxhub/pkg/cert" + "io/ioutil" + "path/filepath" + "strings" + + "github.com/libp2p/go-libp2p-core/peer" ma "github.com/multiformats/go-multiaddr" ) -const ( - networkConfigFile = "network.toml" - nodePrivFile = "certs/node.priv" -) - -// NetworkConfig . -// @param OtherNodes to fit original code -// @param OthersNodes for new network config type NetworkConfig struct { ID uint64 N uint64 LocalAddr string - Nodes []*NetworkNode + Nodes []*NetworkNodes OtherNodes map[uint64]*peer.AddrInfo } -// NetworkNode is the struct to describe network conf of a node -// @param ID is the id of the node, it is origined by sorting. -// @param Addrs is the address array of the node. -// @param Addr is the default used address the node. -type NetworkNode struct { - ID uint64 - Addr string // the optimal address of a node - Addrs []string -} - -// ReadinNetworkConfig is used for read in toml file -type ReadinNetworkConfig struct { - Addrs [][]string +type NetworkNodes struct { + ID uint64 + Addr string } // AddrToPeerInfo transfer addr to PeerInfo @@ -56,77 +37,16 @@ func AddrToPeerInfo(multiAddr string) (*peer.AddrInfo, error) { return peer.AddrInfoFromP2pAddr(maddr) } -// AddrsToPeerInfo transfer addrs to PeerInfo -func AddrsToPeerInfo(multiAddrs []string) ([]peer.AddrInfo, error) { - maddrs := []ma.Multiaddr{} - for _, multiAddr := range multiAddrs { - maddr, err := ma.NewMultiaddr(multiAddr) - if err != nil { - return nil, err - } - maddrs = append(maddrs, maddr) - } - - return peer.AddrInfosFromP2pAddrs(maddrs...) -} - -// loadNetworkConfig is compatible with old network.toml and support new network.toml config file func loadNetworkConfig(repoRoot string) (*NetworkConfig, error) { - - rdiNetworkConfig := &ReadinNetworkConfig{} - if err := ReadConfig(filepath.Join(repoRoot, networkConfigFile), "toml", rdiNetworkConfig); err != nil { - return nil, err - } - networkConfig := &NetworkConfig{} - for _, node := range rdiNetworkConfig.Addrs { - networkConfig.Nodes = append(networkConfig.Nodes, &NetworkNode{Addrs: node}) - } - - // whether new network format is new - if networkConfig.N == 0 { // judge whether new network format is new - networkConfig.N = uint64(len(networkConfig.Nodes)) + if err := ReadConfig(filepath.Join(repoRoot, "network.toml"), "toml", networkConfig); err != nil { + return nil, err } if uint64(len(networkConfig.Nodes)) != networkConfig.N { return nil, fmt.Errorf("wrong nodes number") } - // use the first address of node as its default addr - for _, node := range networkConfig.Nodes { - if node.Addr == "" { - node.Addr = node.Addrs[0] - } - } - // read private key to get PeerID - PeerID, err := GetPidFromPrivFile(filepath.Join(repoRoot, nodePrivFile)) - if err != nil { - return nil, err - } - // sort PeerId of nodes to produce IDs: - sort.Sort(networkConfig) - - findSelf := false - - for i, node := range networkConfig.Nodes { - // write ID into node struct: - node.ID = uint64(i + 1) - - pid, err := MultiaddrToPeerID(networkConfig.Nodes[i].Addrs[0]) - if err != nil { - return nil, err - } - if pid == PeerID { - // match PeerID to know node's self ID: - networkConfig.ID = node.ID - findSelf = true - } - } - - if findSelf == false { - return nil, fmt.Errorf("PeerID of this node was not matched to any of these nodes") - } - for _, node := range networkConfig.Nodes { if node.ID == networkConfig.ID { networkConfig.LocalAddr = node.Addr @@ -153,15 +73,10 @@ func loadNetworkConfig(repoRoot string) (*NetworkConfig, error) { m := make(map[uint64]*peer.AddrInfo) for _, node := range nodes { if node.ID != networkConfig.ID { - addrs, err := AddrsToPeerInfo(node.Addrs) + addr, err := AddrToPeerInfo(node.Addr) if err != nil { return nil, fmt.Errorf("wrong network addr: %w", err) } - if len(addrs) != 1 { - return nil, fmt.Errorf("different PeerIDs in the same node") - } - // overwrite addr if formatIsNew is true. - addr := &addrs[0] m[node.ID] = addr } } @@ -171,34 +86,6 @@ func loadNetworkConfig(repoRoot string) (*NetworkConfig, error) { return networkConfig, nil } -// Len returns length of the struct to be sorted -func (p NetworkConfig) Len() int { return len(p.Nodes) } - -// Less compares two iterms ascending(ASC) -func (p NetworkConfig) Less(i, j int) bool { - multiAddri := p.Nodes[i].Addrs[0] - multiAddrj := p.Nodes[j].Addrs[0] - maddri, err := ma.NewMultiaddr(multiAddri) - if err != nil { - panic(err) - } - maddrj, err := ma.NewMultiaddr(multiAddrj) - if err != nil { - panic(err) - } - _, idi := peer.SplitAddr(maddri) - if idi == "" { - panic(err) - } - _, idj := peer.SplitAddr(maddrj) - if idj == "" { - panic(err) - } - return idi < idj -} - -// Swap swaps iterms -func (p NetworkConfig) Swap(i, j int) { p.Nodes[i], p.Nodes[j] = p.Nodes[j], p.Nodes[i] } // GetPidFromPrivFile gets pid from libp2p node priv file func GetPidFromPrivFile(privPath string) (string, error) { diff --git a/internal/repo/testdata/certs/node-old.priv b/internal/repo/testdata/certs/node-old.priv deleted file mode 100644 index 0ded716..0000000 --- a/internal/repo/testdata/certs/node-old.priv +++ /dev/null @@ -1,5 +0,0 @@ ------BEGIN EC PRIVATE KEY----- -MHcCAQEEIKnfYqPIMB1JHPCEA8P5qX/9sJR92LKtiB8i3xAnnfz/oAoGCCqGSM49 -AwEHoUQDQgAET5MTfp/rudlnnVlTqFWaW9mSVkW/JY2SWTVkvgDIFJk4OWkAFNcW -qThuDBVHkbo09DplyDGN7MPq6KnBvA6MZg== ------END EC PRIVATE KEY----- diff --git a/internal/repo/testdata/certs/node.priv b/internal/repo/testdata/certs/node.priv index d208575..0ded716 100644 --- a/internal/repo/testdata/certs/node.priv +++ b/internal/repo/testdata/certs/node.priv @@ -1,5 +1,5 @@ -----BEGIN EC PRIVATE KEY----- -MHcCAQEEIFYN9+xvnRzlNX6Cob3trD3HlcvuYZUYRPrhYU+Yh2ADoAoGCCqGSM49 -AwEHoUQDQgAEIvG8oAjVXy4oFpY7xDgq/IEZaU6XyBBqD/hy/OAcCnEl9Z9IWQt6 -E37+3zDBsRO5VY2duguaueLyQJYnHpY2zw== +MHcCAQEEIKnfYqPIMB1JHPCEA8P5qX/9sJR92LKtiB8i3xAnnfz/oAoGCCqGSM49 +AwEHoUQDQgAET5MTfp/rudlnnVlTqFWaW9mSVkW/JY2SWTVkvgDIFJk4OWkAFNcW +qThuDBVHkbo09DplyDGN7MPq6KnBvA6MZg== -----END EC PRIVATE KEY----- diff --git a/internal/repo/testdata/network.toml b/internal/repo/testdata/network.toml index 0179b9f..efea830 100644 --- a/internal/repo/testdata/network.toml +++ b/internal/repo/testdata/network.toml @@ -1,6 +1,18 @@ -addrs = [ - ["/ip4/127.0.0.1/tcp/4001/p2p/QmZZFk1Tj6p25ecz98SpyHxb6joDPxR3wVPptDwuc8fue1"], - ["/ip4/127.0.0.1/tcp/4002/p2p/QmNRgD6djYJERNpDpHqRn3mxjJ9SYiiGWzExNSy4sEmSNL"], - ["/ip4/127.0.0.1/tcp/4003/p2p/QmXmyw2usKApP6UyK3cHEJ1XvxxSa8kM2M3Q1T6fhdifs5"], - ["/ip4/127.0.0.1/tcp/4004/p2p/QmY21wH1M694j1JFEvwegyJz8h2VpaSeeqcwt2vUpxsFPt"] -] \ No newline at end of file +id = 1 # self id +N = 4 # the number of cluster nodes + +[[nodes]] +id = 1 +addr = "/ip4/127.0.0.1/tcp/4001/p2p/QmYkQxwC3cj8HqxAnsgM1cSK5yPjkL8UySc21b1AhVF2Wf" + +[[nodes]] +id = 2 +addr = "/ip4/127.0.0.1/tcp/4002/p2p/QmfHtWy4uqu3JvUUDpAq7oHXkpq5qkuY1Ffb6zngqXtpMi" + +[[nodes]] +id = 3 +addr = "/ip4/127.0.0.1/tcp/4003/p2p/QmcpUhepA4tgkVvzW6WnxHCCk163CTLSiKatQeVSF8ro9z" + +[[nodes]] +id = 4 +addr = "/ip4/127.0.0.1/tcp/4004/p2p/QmT3ghgJFboqYYM9B6p6ehJFGHoEqpaRCJBByWCCYgHHZt" diff --git a/tester/test_data/config/node1/network.toml b/tester/test_data/config/node1/network.toml index 9d059c0..614e618 100644 --- a/tester/test_data/config/node1/network.toml +++ b/tester/test_data/config/node1/network.toml @@ -1,6 +1,18 @@ -addrs = [ - ["/ip4/127.0.0.1/tcp/4001/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL"], - ["/ip4/127.0.0.1/tcp/4002/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4"], - ["/ip4/127.0.0.1/tcp/4003/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS"], - ["/ip4/127.0.0.1/tcp/4004/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy"] -] +id = 1 # self id +N = 4 # the number of cluster nodes + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4001/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL" +id = 1 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4002/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4" +id = 2 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4003/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS" +id = 3 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4004/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy" +id = 4 diff --git a/tester/test_data/config/node2/network.toml b/tester/test_data/config/node2/network.toml index 9d059c0..6eaad81 100644 --- a/tester/test_data/config/node2/network.toml +++ b/tester/test_data/config/node2/network.toml @@ -1,6 +1,18 @@ -addrs = [ - ["/ip4/127.0.0.1/tcp/4001/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL"], - ["/ip4/127.0.0.1/tcp/4002/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4"], - ["/ip4/127.0.0.1/tcp/4003/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS"], - ["/ip4/127.0.0.1/tcp/4004/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy"] -] +id = 2 # self id +N = 4 # the number of cluster nodes + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4001/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL" +id = 1 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4002/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4" +id = 2 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4003/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS" +id = 3 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4004/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy" +id = 4 diff --git a/tester/test_data/config/node3/network.toml b/tester/test_data/config/node3/network.toml index 9d059c0..fc335d7 100644 --- a/tester/test_data/config/node3/network.toml +++ b/tester/test_data/config/node3/network.toml @@ -1,6 +1,18 @@ -addrs = [ - ["/ip4/127.0.0.1/tcp/4001/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL"], - ["/ip4/127.0.0.1/tcp/4002/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4"], - ["/ip4/127.0.0.1/tcp/4003/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS"], - ["/ip4/127.0.0.1/tcp/4004/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy"] -] +id = 3 # self id +N = 4 # the number of cluster nodes + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4001/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL" +id = 1 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4002/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4" +id = 2 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4003/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS" +id = 3 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4004/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy" +id = 4 diff --git a/tester/test_data/config/node4/network.toml b/tester/test_data/config/node4/network.toml index 9d059c0..1bec56b 100644 --- a/tester/test_data/config/node4/network.toml +++ b/tester/test_data/config/node4/network.toml @@ -1,6 +1,18 @@ -addrs = [ - ["/ip4/127.0.0.1/tcp/4001/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL"], - ["/ip4/127.0.0.1/tcp/4002/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4"], - ["/ip4/127.0.0.1/tcp/4003/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS"], - ["/ip4/127.0.0.1/tcp/4004/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy"] -] +id = 4 # self id +N = 4 # the number of cluster nodes + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4001/p2p/QmXi58fp9ZczF3Z5iz1yXAez3Hy5NYo1R8STHWKEM9XnTL" +id = 1 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4002/p2p/QmbmD1kzdsxRiawxu7bRrteDgW1ituXupR8GH6E2EUAHY4" +id = 2 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4003/p2p/QmQUcDYCtqbpn5Nhaw4FAGxQaSSNvdWfAFcpQT9SPiezbS" +id = 3 + +[[nodes]] +addr = "/ip4/127.0.0.1/tcp/4004/p2p/QmQW3bFn8XX1t4W14Pmn37bPJUpUVBrBjnPuBZwPog3Qdy" +id = 4