github.com/aergoio/aergo@v1.3.1/p2p/versionmanager.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2p
     7  
     8  import (
     9  	"fmt"
    10  	"github.com/aergoio/aergo-lib/log"
    11  	"github.com/aergoio/aergo/chain"
    12  	"github.com/aergoio/aergo/p2p/p2pcommon"
    13  	v030 "github.com/aergoio/aergo/p2p/v030"
    14  	"github.com/aergoio/aergo/types"
    15  	"io"
    16  )
    17  
    18  type defaultVersionManager struct {
    19  	pm     p2pcommon.PeerManager
    20  	actor  p2pcommon.ActorService
    21  	ca     types.ChainAccessor
    22  	logger *log.Logger
    23  
    24  	// check if is it ad hoc
    25  	localChainID *types.ChainID
    26  }
    27  
    28  func newDefaultVersionManager(pm p2pcommon.PeerManager, actor p2pcommon.ActorService, ca types.ChainAccessor, logger *log.Logger, localChainID *types.ChainID) *defaultVersionManager {
    29  	return &defaultVersionManager{pm: pm, actor: actor, ca:ca, logger: logger, localChainID: localChainID}
    30  }
    31  
    32  func (vm *defaultVersionManager) FindBestP2PVersion(versions []p2pcommon.P2PVersion) p2pcommon.P2PVersion {
    33  	for _, supported := range AcceptedInboundVersions {
    34  		for _, reqVer := range versions {
    35  			if supported == reqVer {
    36  				return reqVer
    37  			}
    38  		}
    39  	}
    40  	return p2pcommon.P2PVersionUnknown
    41  }
    42  
    43  func (h *defaultVersionManager) GetVersionedHandshaker(version p2pcommon.P2PVersion, peerID types.PeerID, rwc io.ReadWriteCloser) (p2pcommon.VersionedHandshaker, error) {
    44  	switch version {
    45  	case p2pcommon.P2PVersion032:
    46  		vhs := v030.NewV032VersionedHS(h.pm, h.actor, h.logger, h.localChainID, peerID, rwc, chain.Genesis.Block().Hash)
    47  		return vhs, nil
    48  	case p2pcommon.P2PVersion031:
    49  		v030hs := v030.NewV030VersionedHS(h.pm, h.actor, h.logger, h.localChainID, peerID, rwc)
    50  		return v030hs, nil
    51  	case p2pcommon.P2PVersion030:
    52  		v030hs := v030.NewV030VersionedHS(h.pm, h.actor, h.logger, h.localChainID, peerID, rwc)
    53  		return v030hs, nil
    54  	default:
    55  		return nil, fmt.Errorf("not supported version")
    56  	}
    57  }