github.com/aergoio/aergo@v1.3.1/types/raft.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/aergoio/etcd/raft/raftpb"
     7  	"strconv"
     8  )
     9  
    10  type ChangeClusterStatus struct {
    11  	State   string        `json:"status"`
    12  	Error   string        `json:"error"`
    13  	Members []*MemberAttr `json:"members"`
    14  }
    15  
    16  type EnterpriseTxStatus struct {
    17  	Status   string               `json:"status"`
    18  	Ret      string               `json:"ret"`
    19  	CCStatus *ChangeClusterStatus `json:"change_cluster,omitempty"`
    20  }
    21  
    22  func (ccProgress *ConfChangeProgress) ToString() string {
    23  	mbrsJson, err := json.Marshal(ccProgress.GetMembers())
    24  	if err != nil {
    25  		mbrsJson = []byte("")
    26  	}
    27  
    28  	return fmt.Sprintf("State=%s, Error=%s, cluster=%s", ConfChangeState_name[int32(ccProgress.State)], ccProgress.Err, string(mbrsJson))
    29  }
    30  
    31  func (ccProgress *ConfChangeProgress) ToPrintable() *ChangeClusterStatus {
    32  	return &ChangeClusterStatus{State: ConfChangeState_name[int32(ccProgress.State)], Error: ccProgress.Err, Members: ccProgress.Members}
    33  }
    34  
    35  func RaftConfChangeToString(cc *raftpb.ConfChange) string {
    36  	return fmt.Sprintf("requestID=%d, type=%s, nodeid=%d", cc.ID, raftpb.ConfChangeType_name[int32(cc.Type)], cc.NodeID)
    37  }
    38  
    39  func (mc *MembershipChange) ToString() string {
    40  	var buf string
    41  
    42  	buf = fmt.Sprintf("requestID=%d, type=%s,", mc.GetRequestID(), MembershipChangeType_name[int32(mc.Type)])
    43  
    44  	buf = buf + mc.Attr.ToString()
    45  	return buf
    46  }
    47  
    48  func (mattr *MemberAttr) MarshalJSON() ([]byte, error) {
    49  	return json.Marshal(&struct {
    50  		ID      string `json:"id,omitempty"`
    51  		Name    string `json:"name,omitempty"`
    52  		Address string `json:"address,omitempty"`
    53  		PeerID  string `json:"peerid,omitempty"`
    54  	}{
    55  		ID:      Uint64ToHexaString(mattr.ID),
    56  		Name:    mattr.Name,
    57  		Address: mattr.Address,
    58  		PeerID:  IDB58Encode(PeerID(mattr.PeerID)),
    59  	})
    60  }
    61  
    62  func (mattr *MemberAttr) UnmarshalJSON(data []byte) error {
    63  	var (
    64  		err    error
    65  		peerID PeerID
    66  	)
    67  
    68  	aux := &struct {
    69  		ID      string `json:"id,omitempty"`
    70  		Name    string `json:"name,omitempty"`
    71  		Address string `json:"address,omitempty"`
    72  		PeerID  string `json:"peerid,omitempty"`
    73  	}{}
    74  
    75  	if err = json.Unmarshal(data, aux); err != nil {
    76  		return err
    77  	}
    78  
    79  	if len(aux.ID) > 0 {
    80  		mattr.ID, err = strconv.ParseUint(aux.ID, 16, 64)
    81  		if err != nil {
    82  			return err
    83  		}
    84  	}
    85  
    86  	if len(aux.PeerID) > 0 {
    87  		peerID, err = IDB58Decode(aux.PeerID)
    88  		if err != nil {
    89  			return err
    90  		}
    91  
    92  		mattr.PeerID = []byte(peerID)
    93  	}
    94  	mattr.Name = aux.Name
    95  	mattr.Address = aux.Address
    96  
    97  	return nil
    98  }
    99  
   100  func (mattr *MemberAttr) ToString() string {
   101  	data, err := json.Marshal(mattr)
   102  	if err != nil {
   103  		return "{ \"name\": \"error to json\" }"
   104  	}
   105  
   106  	return string(data)
   107  }
   108  
   109  func (hs *HardStateInfo) ToString() string {
   110  	if hs == nil {
   111  		return fmt.Sprintf("hardstateinfo is nil")
   112  	}
   113  	return fmt.Sprintf("{ term=%d, commit=%d }", hs.Term, hs.Commit)
   114  }
   115  
   116  func RaftHardStateToString(hardstate raftpb.HardState) string {
   117  	return fmt.Sprintf("term=%d, vote=%x, commit=%d", hardstate.Term, hardstate.Vote, hardstate.Commit)
   118  }
   119  
   120  func RaftEntryToString(entry *raftpb.Entry) string {
   121  	return fmt.Sprintf("term=%d, index=%d, type=%s", entry.Term, entry.Index, raftpb.EntryType_name[int32(entry.Type)])
   122  }
   123  
   124  func Uint64ToHexaString(id uint64) string {
   125  	return fmt.Sprintf("%x", id)
   126  }