github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/raft/backend/api.go (about) 1 package backend 2 3 import ( 4 "github.com/bigzoro/my_simplechain/consensus/raft" 5 ) 6 7 type RaftNodeInfo struct { 8 ClusterSize int `json:"clusterSize"` 9 Role string `json:"role"` 10 Address *raft.Address `json:"address"` 11 PeerAddresses []*raft.Address `json:"peerAddresses"` 12 RemovedPeerIds []uint16 `json:"removedPeerIds"` 13 AppliedIndex uint64 `json:"appliedIndex"` 14 SnapshotIndex uint64 `json:"snapshotIndex"` 15 } 16 17 type PublicRaftAPI struct { 18 raftService *RaftService 19 } 20 21 func NewPublicRaftAPI(raftService *RaftService) *PublicRaftAPI { 22 return &PublicRaftAPI{raftService} 23 } 24 25 func (s *PublicRaftAPI) Role() string { 26 return s.raftService.RaftProtocolManager.NodeInfo().Role 27 } 28 29 //func (s *PublicRaftAPI) AddPeer(enodeId string) (uint16, error) { 30 // return s.raftService.RaftProtocolManager.ProposeNewPeer(enodeId) 31 //} 32 // 33 //func (s *PublicRaftAPI) RemovePeer(raftId uint16) { 34 // s.raftService.RaftProtocolManager.ProposePeerRemoval(raftId) 35 //} 36 37 func (s *PublicRaftAPI) Leader() (string, error) { 38 39 addr, err := s.raftService.RaftProtocolManager.LeaderAddress() 40 if nil != err { 41 return "", err 42 } 43 return addr.NodeId.String(), nil 44 } 45 46 func (s *PublicRaftAPI) Cluster() []*raft.Address { 47 nodeInfo := s.raftService.RaftProtocolManager.NodeInfo() 48 return append(nodeInfo.PeerAddresses, nodeInfo.Address) 49 } 50 51 func (s *PublicRaftAPI) GetRaftId(enodeId string) (uint16, error) { 52 return s.raftService.RaftProtocolManager.FetchRaftId(enodeId) 53 } 54 55 func (s *PublicRaftAPI) GetMaxRaftId() uint16 { 56 return s.raftService.RaftProtocolManager.MaxRaftId() + 1 57 }