github.com/aergoio/aergo@v1.3.1/consensus/impl/raftv2/p2p.go (about)

     1  package raftv2
     2  
     3  import (
     4  	"errors"
     5  	"github.com/aergoio/aergo/message"
     6  	"github.com/aergoio/aergo/pkg/component"
     7  	"github.com/aergoio/aergo/types"
     8  	"time"
     9  )
    10  
    11  var (
    12  	MaxTimeOutCluter = time.Second * 10
    13  	MaxTryGetCluster = 3
    14  
    15  	ErrGetClusterReplyC  = errors.New("reply channel of getcluster request is closed")
    16  	ErrGetClusterTimeout = errors.New("timeout for getcluster")
    17  	ErrGetClusterEmpty   = errors.New("getcluster reply is empty")
    18  	ErrGetClusterFail    = errors.New("failed to get cluster info")
    19  )
    20  
    21  // GetBestBlock returns the current best block from chainservice
    22  func GetClusterInfo(hs *component.ComponentHub, bestHash []byte) (*Cluster, *types.HardStateInfo, error) {
    23  	logger.Info().Msg("try getclusterinfo to p2p")
    24  
    25  	replyC := make(chan *message.GetClusterRsp)
    26  	hs.Tell(message.P2PSvc, &message.GetCluster{BestBlockHash: bestHash, ReplyC: replyC})
    27  
    28  	var (
    29  		rsp   *message.GetClusterRsp
    30  		ok    bool
    31  		err   error
    32  		newCl *Cluster
    33  	)
    34  
    35  	select {
    36  	case rsp, ok = <-replyC:
    37  		if !ok {
    38  			return nil, nil, ErrGetClusterReplyC
    39  		}
    40  
    41  		if rsp.Err != nil {
    42  			return nil, nil, rsp.Err
    43  		}
    44  
    45  		if len(rsp.Members) == 0 {
    46  			return nil, nil, ErrGetClusterEmpty
    47  		}
    48  
    49  	case <-time.After(MaxTimeOutCluter):
    50  		return nil, nil, ErrGetClusterTimeout
    51  	}
    52  
    53  	if newCl, err = NewClusterFromMemberAttrs(rsp.ClusterID, rsp.ChainID, rsp.Members); err != nil {
    54  		return nil, nil, err
    55  	}
    56  
    57  	//logger.Debug().Str("info", newCl.toString()).Msg("get remote cluster info")
    58  	return newCl, rsp.HardStateInfo, nil
    59  }