Skip to content

Commit 4fb3d38

Browse files
committed
go/p2p: Ensure only server providers advertise themselves
1 parent 2bb1b3b commit 4fb3d38

File tree

17 files changed

+67
-10
lines changed

17 files changed

+67
-10
lines changed

.changelog/6270.internal.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
go/p2p: Ensure only server providers advertise themselves
2+
3+
Previously, the host would advertise itself upon creation
4+
of p2p protocol client, even if the server was not running.
5+
6+
Advertisement is now independent and is only triggered
7+
when serving the P2P protocol.

go/oasis-node/cmd/debug/byzantine/node.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ func initializeAndRegisterByzantineNode(
155155
return nil, fmt.Errorf("initializing storage node failed: %w", err)
156156
}
157157
b.p2p.service.RegisterProtocolServer(storageP2P.NewServer(b.chainContext, b.runtimeID, storage))
158+
b.p2p.service.AdvertiseProtocol(storageP2P.ProtocolID(b.chainContext, b.runtimeID))
159+
158160
b.storage = storage
159161

160162
// Wait for activation epoch.

go/oasis-node/cmd/node/node.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ func NewNode() (node *Node, err error) { // nolint: gocyclo
549549

550550
// Register consensus light client P2P protocol server.
551551
node.P2P.RegisterProtocolServer(consensusLightP2P.NewServer(node.P2P, node.chainContext, node.Consensus.Core()))
552+
node.P2P.AdvertiseProtocol(consensusLightP2P.ProtocolID(node.chainContext))
552553

553554
// Register the consensus service with the peer registry.
554555
if mgr := node.P2P.PeerManager(); mgr != nil {

go/p2p/api/api.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,16 @@ type Service interface {
8383
RegisterProtocol(p core.ProtocolID, minPeers int, totalPeers int)
8484

8585
// RegisterProtocolServer registers a protocol server for the given protocol.
86+
//
87+
// Normally, registered protocol server should be also advertised (AdvertiseProtocol),
88+
// so that remote peers find the host node.
8689
RegisterProtocolServer(srv rpc.Server)
8790

91+
// AdvertiseProtocol starts advertising the host as available for serving the specified protocol.
92+
//
93+
// It should be called only after the protocol server is running and ready to serve.
94+
AdvertiseProtocol(p core.ProtocolID)
95+
8896
// GetMinRepublishInterval returns the minimum republish interval that needs to be respected by
8997
// the caller when publishing the same message. If Publish is called for the same message more
9098
// quickly, the message may be dropped and not published.

go/p2p/nop.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func (p *nopP2P) RegisterProtocol(core.ProtocolID, int, int) {
8585
func (p *nopP2P) RegisterProtocolServer(rpc.Server) {
8686
}
8787

88+
// Implements api.Service.
89+
func (p *nopP2P) AdvertiseProtocol(core.ProtocolID) {
90+
}
91+
8892
// Implements api.Service.
8993
func (p *nopP2P) GetMinRepublishInterval() time.Duration {
9094
return time.Hour

go/p2p/p2p.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ func (p *p2p) RegisterProtocolServer(srv rpc.Server) {
345345
)
346346
}
347347

348+
// Implements api.Service.
349+
func (p *p2p) AdvertiseProtocol(protocol core.ProtocolID) {
350+
p.peerMgr.AdvertiseProtocol(protocol)
351+
}
352+
348353
// Implements api.Service.
349354
func (p *p2p) GetMinRepublishInterval() time.Duration {
350355
return seenMessagesTTL + 5*time.Second

go/p2p/peermgmt/peermgr.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,21 @@ func (m *PeerManager) RegisterProtocol(p core.ProtocolID, minPeers int, totalPee
188188
}
189189

190190
m.protocols[p] = &watermark{minPeers, totalPeers}
191-
m.discovery.startAdvertising(string(p))
192-
193191
m.logger.Debug("protocol registered",
194192
"protocol", p,
195193
"min_peers", minPeers,
196194
"total_peers", totalPeers,
197195
)
198196
}
199197

198+
// AdvertiseProtocol starts advertising readiness to serve the specified protocol.
199+
func (m *PeerManager) AdvertiseProtocol(p core.ProtocolID) {
200+
m.discovery.startAdvertising(string(p))
201+
m.logger.Debug("triggered protocol advertisement",
202+
"protocol", p,
203+
)
204+
}
205+
200206
// RegisterTopic starts tracking and managing peers that support the given topic.
201207
// If the topic is already registered, its values are updated.
202208
func (m *PeerManager) RegisterTopic(topic string, minPeers int, totalPeers int) {

go/worker/common/committee/node.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ func NewNode(
956956

957957
// Register transaction sync service.
958958
p2pHost.RegisterProtocolServer(txsync.NewServer(chainContext, runtime.ID(), n.TxPool))
959+
p2pHost.AdvertiseProtocol(txsync.ProtocolID(chainContext, runtime.ID()))
959960

960961
return n, nil
961962
}

go/worker/common/p2p/txsync/protocol.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package txsync
33
import (
44
"github.com/libp2p/go-libp2p/core"
55

6+
"github.com/oasisprotocol/oasis-core/go/common"
67
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
78
"github.com/oasisprotocol/oasis-core/go/common/node"
89
"github.com/oasisprotocol/oasis-core/go/common/version"
@@ -16,6 +17,11 @@ const TxSyncProtocolID = "txsync"
1617
// TxSyncProtocolVersion is the supported version of the transaction sync protocol.
1718
var TxSyncProtocolVersion = version.Version{Major: 2, Minor: 0, Patch: 0}
1819

20+
// ProtocolID returns the runtime transaction sync protocol ID.
21+
func ProtocolID(chainContext string, runtimeID common.Namespace) core.ProtocolID {
22+
return protocol.NewRuntimeProtocolID(chainContext, runtimeID, TxSyncProtocolID, TxSyncProtocolVersion)
23+
}
24+
1925
// Constants related to the GetTxs method.
2026
const (
2127
MethodGetTxs = "GetTxs"

go/worker/common/p2p/txsync/server.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/oasisprotocol/oasis-core/go/common"
77
"github.com/oasisprotocol/oasis-core/go/common/cbor"
8-
"github.com/oasisprotocol/oasis-core/go/p2p/protocol"
98
"github.com/oasisprotocol/oasis-core/go/p2p/rpc"
109
"github.com/oasisprotocol/oasis-core/go/runtime/txpool"
1110
)
@@ -52,5 +51,5 @@ func (s *service) handleGetTxs(request *GetTxsRequest) (*GetTxsResponse, error)
5251

5352
// NewServer creates a new transaction sync protocol server.
5453
func NewServer(chainContext string, runtimeID common.Namespace, txPool txpool.TransactionPool) rpc.Server {
55-
return rpc.NewServer(protocol.NewRuntimeProtocolID(chainContext, runtimeID, TxSyncProtocolID, TxSyncProtocolVersion), &service{txPool})
54+
return rpc.NewServer(ProtocolID(chainContext, runtimeID), &service{txPool})
5655
}

0 commit comments

Comments
 (0)