diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index b85e0ecbbc..6c055f7680 100755 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -628,7 +628,11 @@ web3._extend({ new web3._extend.Property({ name: 'leader', getter: 'raft_leader' - }) + }), + new web3._extend.Property({ + name: 'cluster', + getter: 'raft_cluster' + }), ] }) ` diff --git a/raft/api.go b/raft/api.go index 4fec8e17dc..1a699bb6c5 100755 --- a/raft/api.go +++ b/raft/api.go @@ -35,5 +35,10 @@ func (s *PublicRaftAPI) Leader() (string, error) { if nil != err { return "", err } - return addr.nodeId.String(), nil + return addr.NodeId.String(), nil +} + +func (s *PublicRaftAPI) Cluster() []*Address { + nodeInfo := s.raftService.raftProtocolManager.NodeInfo() + return append(nodeInfo.PeerAddresses, nodeInfo.Address) } diff --git a/raft/handler.go b/raft/handler.go index 91d4e823a5..b02f6e18c4 100755 --- a/raft/handler.go +++ b/raft/handler.go @@ -624,17 +624,17 @@ func (pm *ProtocolManager) entriesToApply(allEntries []raftpb.Entry) (entriesToA } func raftUrl(address *Address) string { - return fmt.Sprintf("http://%s:%d", address.ip, address.raftPort) + return fmt.Sprintf("http://%s:%d", address.Ip, address.RaftPort) } func (pm *ProtocolManager) addPeer(address *Address) { pm.mu.Lock() defer pm.mu.Unlock() - raftId := address.raftId + raftId := address.RaftId // Add P2P connection: - p2pNode := discover.NewNode(address.nodeId, address.ip, 0, uint16(address.p2pPort)) + p2pNode := discover.NewNode(address.NodeId, address.Ip, 0, uint16(address.P2pPort)) pm.p2pServer.AddPeer(p2pNode) // Add raft transport connection: diff --git a/raft/peer.go b/raft/peer.go index 66682f8f5c..84aac45218 100644 --- a/raft/peer.go +++ b/raft/peer.go @@ -5,28 +5,29 @@ import ( "net" "fmt" + "log" + "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/rlp" - "log" ) // Serializable information about a Peer. Sufficient to build `etcdRaft.Peer` // or `discover.Node`. type Address struct { - raftId uint16 - nodeId discover.NodeID - ip net.IP - p2pPort uint16 - raftPort uint16 + RaftId uint16 `json:"raftId"` + NodeId discover.NodeID `json:"nodeId"` + Ip net.IP `json:"ip"` + P2pPort uint16 `json:"p2pPort"` + RaftPort uint16 `json:"raftPort"` } func newAddress(raftId uint16, raftPort uint16, node *discover.Node) *Address { return &Address{ - raftId: raftId, - nodeId: node.ID, - ip: node.IP, - p2pPort: node.TCP, - raftPort: raftPort, + RaftId: raftId, + NodeId: node.ID, + Ip: node.IP, + P2pPort: node.TCP, + RaftPort: raftPort, } } @@ -37,7 +38,7 @@ type Peer struct { } func (addr *Address) EncodeRLP(w io.Writer) error { - return rlp.Encode(w, []interface{}{addr.raftId, addr.nodeId, addr.ip, addr.p2pPort, addr.raftPort}) + return rlp.Encode(w, []interface{}{addr.RaftId, addr.NodeId, addr.Ip, addr.P2pPort, addr.RaftPort}) } func (addr *Address) DecodeRLP(s *rlp.Stream) error { @@ -53,7 +54,7 @@ func (addr *Address) DecodeRLP(s *rlp.Stream) error { if err := s.Decode(&temp); err != nil { return err } else { - addr.raftId, addr.nodeId, addr.ip, addr.p2pPort, addr.raftPort = temp.RaftId, temp.NodeId, temp.Ip, temp.P2pPort, temp.RaftPort + addr.RaftId, addr.NodeId, addr.Ip, addr.P2pPort, addr.RaftPort = temp.RaftId, temp.NodeId, temp.Ip, temp.P2pPort, temp.RaftPort return nil } } diff --git a/raft/snapshot.go b/raft/snapshot.go index 59e2b83b05..4e3fa41e6e 100644 --- a/raft/snapshot.go +++ b/raft/snapshot.go @@ -30,7 +30,7 @@ type ByRaftId []Address func (a ByRaftId) Len() int { return len(a) } func (a ByRaftId) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ByRaftId) Less(i, j int) bool { return a[i].raftId < a[j].raftId } +func (a ByRaftId) Less(i, j int) bool { return a[i].RaftId < a[j].RaftId } func (pm *ProtocolManager) buildSnapshot() *Snapshot { pm.mu.RLock() @@ -140,17 +140,17 @@ func (pm *ProtocolManager) updateClusterMembership(newConfState raftpb.ConfState for _, tempAddress := range addresses { address := tempAddress // Allocate separately on the heap for each iteration. - if address.raftId == pm.raftId { + if address.RaftId == pm.raftId { // If we're a newcomer to an existing cluster, this is where we learn // our own Address. pm.setLocalAddress(&address) } else { pm.mu.RLock() - existingPeer := pm.peers[address.raftId] + existingPeer := pm.peers[address.RaftId] pm.mu.RUnlock() if existingPeer == nil { - log.Info("adding new raft peer", "raft id", address.raftId) + log.Info("adding new raft peer", "raft id", address.RaftId) pm.addPeer(&address) } }