|
| 1 | +// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package p2p |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/binary" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/prometheus/client_golang/prometheus" |
| 13 | + |
| 14 | + "github.com/ava-labs/avalanchego/ids" |
| 15 | + "github.com/ava-labs/avalanchego/snow/engine/common" |
| 16 | + "github.com/ava-labs/avalanchego/snow/validators" |
| 17 | + "github.com/ava-labs/avalanchego/utils/logging" |
| 18 | + "github.com/ava-labs/avalanchego/utils/set" |
| 19 | + "github.com/ava-labs/avalanchego/version" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + _ validators.Connector = (*Network)(nil) |
| 24 | + _ common.AppHandler = (*Network)(nil) |
| 25 | + _ NodeSampler = (*peerSampler)(nil) |
| 26 | +) |
| 27 | + |
| 28 | +// ClientOption configures Client |
| 29 | +type ClientOption interface { |
| 30 | + apply(options *clientOptions) |
| 31 | +} |
| 32 | + |
| 33 | +type clientOptionFunc func(options *clientOptions) |
| 34 | + |
| 35 | +func (o clientOptionFunc) apply(options *clientOptions) { |
| 36 | + o(options) |
| 37 | +} |
| 38 | + |
| 39 | +// WithValidatorSampling configures Client.AppRequestAny to sample validators |
| 40 | +func WithValidatorSampling(validators *Validators) ClientOption { |
| 41 | + return clientOptionFunc(func(options *clientOptions) { |
| 42 | + options.nodeSampler = validators |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +// clientOptions holds client-configurable values |
| 47 | +type clientOptions struct { |
| 48 | + // nodeSampler is used to select nodes to route Client.AppRequestAny to |
| 49 | + nodeSampler NodeSampler |
| 50 | +} |
| 51 | + |
| 52 | +// NewNetwork returns an instance of Network |
| 53 | +func NewNetwork( |
| 54 | + log logging.Logger, |
| 55 | + sender common.AppSender, |
| 56 | + metrics prometheus.Registerer, |
| 57 | + namespace string, |
| 58 | +) *Network { |
| 59 | + return &Network{ |
| 60 | + Peers: &Peers{}, |
| 61 | + log: log, |
| 62 | + sender: sender, |
| 63 | + metrics: metrics, |
| 64 | + namespace: namespace, |
| 65 | + router: newRouter(log, sender, metrics, namespace), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Network exposes networking state and supports building p2p application |
| 70 | +// protocols |
| 71 | +type Network struct { |
| 72 | + Peers *Peers |
| 73 | + |
| 74 | + log logging.Logger |
| 75 | + sender common.AppSender |
| 76 | + metrics prometheus.Registerer |
| 77 | + namespace string |
| 78 | + |
| 79 | + router *router |
| 80 | +} |
| 81 | + |
| 82 | +func (n *Network) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte) error { |
| 83 | + return n.router.AppRequest(ctx, nodeID, requestID, deadline, request) |
| 84 | +} |
| 85 | + |
| 86 | +func (n *Network) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error { |
| 87 | + return n.router.AppResponse(ctx, nodeID, requestID, response) |
| 88 | +} |
| 89 | + |
| 90 | +func (n *Network) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32) error { |
| 91 | + return n.router.AppRequestFailed(ctx, nodeID, requestID) |
| 92 | +} |
| 93 | + |
| 94 | +func (n *Network) AppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error { |
| 95 | + return n.router.AppGossip(ctx, nodeID, msg) |
| 96 | +} |
| 97 | + |
| 98 | +func (n *Network) CrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, deadline time.Time, request []byte) error { |
| 99 | + return n.router.CrossChainAppRequest(ctx, chainID, requestID, deadline, request) |
| 100 | +} |
| 101 | + |
| 102 | +func (n *Network) CrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, response []byte) error { |
| 103 | + return n.router.CrossChainAppResponse(ctx, chainID, requestID, response) |
| 104 | +} |
| 105 | + |
| 106 | +func (n *Network) CrossChainAppRequestFailed(ctx context.Context, chainID ids.ID, requestID uint32) error { |
| 107 | + return n.router.CrossChainAppRequestFailed(ctx, chainID, requestID) |
| 108 | +} |
| 109 | + |
| 110 | +func (n *Network) Connected(_ context.Context, nodeID ids.NodeID, _ *version.Application) error { |
| 111 | + n.Peers.add(nodeID) |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (n *Network) Disconnected(_ context.Context, nodeID ids.NodeID) error { |
| 116 | + n.Peers.remove(nodeID) |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +// NewAppProtocol reserves an identifier for an application protocol handler and |
| 121 | +// returns a Client that can be used to send messages for the corresponding |
| 122 | +// protocol. |
| 123 | +func (n *Network) NewAppProtocol(handlerID uint64, handler Handler, options ...ClientOption) (*Client, error) { |
| 124 | + if err := n.router.addHandler(handlerID, handler); err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + client := &Client{ |
| 129 | + handlerID: handlerID, |
| 130 | + handlerPrefix: binary.AppendUvarint(nil, handlerID), |
| 131 | + sender: n.sender, |
| 132 | + router: n.router, |
| 133 | + options: &clientOptions{ |
| 134 | + nodeSampler: &peerSampler{ |
| 135 | + peers: n.Peers, |
| 136 | + }, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + for _, option := range options { |
| 141 | + option.apply(client.options) |
| 142 | + } |
| 143 | + |
| 144 | + return client, nil |
| 145 | +} |
| 146 | + |
| 147 | +// Peers contains metadata about the current set of connected peers |
| 148 | +type Peers struct { |
| 149 | + lock sync.RWMutex |
| 150 | + set set.SampleableSet[ids.NodeID] |
| 151 | +} |
| 152 | + |
| 153 | +func (p *Peers) add(nodeID ids.NodeID) { |
| 154 | + p.lock.Lock() |
| 155 | + defer p.lock.Unlock() |
| 156 | + |
| 157 | + p.set.Add(nodeID) |
| 158 | +} |
| 159 | + |
| 160 | +func (p *Peers) remove(nodeID ids.NodeID) { |
| 161 | + p.lock.Lock() |
| 162 | + defer p.lock.Unlock() |
| 163 | + |
| 164 | + p.set.Remove(nodeID) |
| 165 | +} |
| 166 | + |
| 167 | +func (p *Peers) has(nodeID ids.NodeID) bool { |
| 168 | + p.lock.RLock() |
| 169 | + defer p.lock.RUnlock() |
| 170 | + |
| 171 | + return p.set.Contains(nodeID) |
| 172 | +} |
| 173 | + |
| 174 | +// Sample returns a pseudo-random sample of up to limit Peers |
| 175 | +func (p *Peers) Sample(limit int) []ids.NodeID { |
| 176 | + p.lock.RLock() |
| 177 | + defer p.lock.RUnlock() |
| 178 | + |
| 179 | + return p.set.Sample(limit) |
| 180 | +} |
| 181 | + |
| 182 | +type peerSampler struct { |
| 183 | + peers *Peers |
| 184 | +} |
| 185 | + |
| 186 | +func (p peerSampler) Sample(_ context.Context, limit int) []ids.NodeID { |
| 187 | + return p.peers.Sample(limit) |
| 188 | +} |
0 commit comments