github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/protocol/protocol.go (about)

     1  // Package protocol p2p protocol
     2  package protocol
     3  
     4  import (
     5  	"context"
     6  	"time"
     7  
     8  	"github.com/turingchain2020/turingchain/client"
     9  	dbm "github.com/turingchain2020/turingchain/common/db"
    10  	"github.com/turingchain2020/turingchain/p2p"
    11  	"github.com/turingchain2020/turingchain/queue"
    12  	"github.com/turingchain2020/turingchain/system/p2p/dht/extension"
    13  	types2 "github.com/turingchain2020/turingchain/system/p2p/dht/types"
    14  	"github.com/turingchain2020/turingchain/types"
    15  	"github.com/libp2p/go-libp2p-core/host"
    16  	"github.com/libp2p/go-libp2p-core/metrics"
    17  	"github.com/libp2p/go-libp2p-core/peer"
    18  	discovery "github.com/libp2p/go-libp2p-discovery"
    19  	kbt "github.com/libp2p/go-libp2p-kbucket"
    20  )
    21  
    22  // P2PEnv p2p全局公共变量
    23  type P2PEnv struct {
    24  	Ctx             context.Context
    25  	ChainCfg        *types.TuringchainConfig
    26  	SubConfig       *types2.P2PSubConfig
    27  	API             client.QueueProtocolAPI
    28  	QueueClient     queue.Client
    29  	Host            host.Host
    30  	P2PManager      *p2p.Manager
    31  	DB              dbm.DB
    32  	PeerInfoManager IPeerInfoManager
    33  	ConnManager     IConnManager
    34  	ConnBlackList   iLRU
    35  	Pubsub          *extension.PubSub
    36  	RoutingTable    *kbt.RoutingTable
    37  	*discovery.RoutingDiscovery
    38  }
    39  
    40  type iLRU interface {
    41  	Add(s string, t time.Duration)
    42  	Has(s string) bool
    43  }
    44  
    45  // IPeerInfoManager is interface of PeerInfoManager
    46  type IPeerInfoManager interface {
    47  	Refresh(info *types.Peer)
    48  	Fetch(pid peer.ID) *types.Peer
    49  	FetchAll() []*types.Peer
    50  	PeerHeight(pid peer.ID) int64
    51  	PeerMaxHeight() int64
    52  }
    53  
    54  // IConnManager is interface of ConnManager
    55  type IConnManager interface {
    56  	FetchConnPeers() []peer.ID
    57  	BoundSize() (in int, out int)
    58  	GetNetRate() metrics.Stats
    59  	BandTrackerByProtocol() *types.NetProtocolInfos
    60  	RateCalculate(ratebytes float64) string
    61  }
    62  
    63  // QueryModule sends message to other module and waits response
    64  func (p *P2PEnv) QueryModule(topic string, ty int64, data interface{}) (interface{}, error) {
    65  	msg := p.QueueClient.NewMessage(topic, ty, data)
    66  	err := p.QueueClient.Send(msg, true)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	resp, err := p.QueueClient.WaitTimeout(msg, time.Second*10)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return resp.Data, nil
    75  }