@@ -15,17 +15,23 @@ import (
1515 "github.com/NethermindEth/juno/core/address"
1616 "github.com/NethermindEth/juno/service"
1717 "github.com/NethermindEth/juno/utils"
18+ dht "github.com/libp2p/go-libp2p-kad-dht"
1819 pubsub "github.com/libp2p/go-libp2p-pubsub"
1920 "github.com/libp2p/go-libp2p/core/host"
21+ "github.com/libp2p/go-libp2p/core/peer"
22+ "github.com/libp2p/go-libp2p/p2p/discovery/routing"
2023 "github.com/sourcegraph/conc"
2124)
2225
2326type topicName string
2427
2528const (
26- proposalTopicName topicName = "consensus_proposals"
27- voteTopicName topicName = "consensus_votes"
28- gossipSubHistory = 60
29+ protocolPrefix = "starknet"
30+ chainID = "1" // TODO: Make this configurable
31+ consensusProtocolID = "consensus"
32+ proposalTopicName topicName = "consensus_proposals"
33+ voteTopicName topicName = "consensus_votes"
34+ gossipSubHistory = 60
2935)
3036
3137type P2P [V types.Hashable [H ], H types.Hash , A types.Addr ] interface {
@@ -36,12 +42,14 @@ type P2P[V types.Hashable[H], H types.Hash, A types.Addr] interface {
3642}
3743
3844type p2p [V types.Hashable [H ], H types.Hash , A types.Addr ] struct {
39- host host.Host
40- log utils.Logger
41- commitNotifier chan types.Height
42- broadcasters Broadcasters [V , H , A ]
43- listeners Listeners [V , H , A ]
44- topicAttachment map [topicName ][]attachedToTopic
45+ host host.Host
46+ log utils.Logger
47+ commitNotifier chan types.Height
48+ broadcasters Broadcasters [V , H , A ]
49+ listeners Listeners [V , H , A ]
50+ topicAttachment map [topicName ][]attachedToTopic
51+ pubSubQueueSize int
52+ bootstrapPeersFn func () []peer.AddrInfo
4553}
4654
4755type attachedToTopic interface {
@@ -55,6 +63,7 @@ func New(
5563 proposalStore * proposal.ProposalStore [starknet.Hash ],
5664 currentHeight types.Height ,
5765 bufferSizeConfig * config.BufferSizes ,
66+ bootstrapPeersFn func () []peer.AddrInfo ,
5867) P2P [starknet.Value , starknet.Hash , address.Address ] {
5968 commitNotifier := make (chan types.Height , bufferSizeConfig .ProposalCommitNotifier )
6069
@@ -104,12 +113,14 @@ func New(
104113 }
105114
106115 return & p2p [starknet.Value , starknet.Hash , address.Address ]{
107- host : host ,
108- log : log ,
109- commitNotifier : commitNotifier ,
110- broadcasters : broadcasters ,
111- listeners : listeners ,
112- topicAttachment : topicAttachment ,
116+ host : host ,
117+ log : log ,
118+ commitNotifier : commitNotifier ,
119+ broadcasters : broadcasters ,
120+ listeners : listeners ,
121+ topicAttachment : topicAttachment ,
122+ pubSubQueueSize : bufferSizeConfig .PubSubQueueSize ,
123+ bootstrapPeersFn : bootstrapPeersFn ,
113124 }
114125}
115126
@@ -122,7 +133,27 @@ func (p *p2p[V, H, A]) getGossipSubOptions() pubsub.Option {
122133}
123134
124135func (p * p2p [V , H , A ]) Run (ctx context.Context ) error {
125- gossipSub , err := pubsub .NewGossipSub (ctx , p .host , p .getGossipSubOptions ())
136+ dht , err := dht .New (
137+ ctx ,
138+ p .host ,
139+ dht .ProtocolPrefix ("/" + protocolPrefix ),
140+ dht .ProtocolExtension ("/" + chainID ),
141+ dht .ProtocolExtension ("/" + consensusProtocolID ),
142+ dht .BootstrapPeersFunc (p .bootstrapPeersFn ),
143+ dht .Mode (dht .ModeServer ),
144+ )
145+ if err != nil {
146+ return fmt .Errorf ("unable to create dht with error: %w" , err )
147+ }
148+
149+ gossipSub , err := pubsub .NewGossipSub (
150+ ctx ,
151+ p .host ,
152+ p .getGossipSubOptions (),
153+ pubsub .WithPeerOutboundQueueSize (p .pubSubQueueSize ),
154+ pubsub .WithValidateQueueSize (p .pubSubQueueSize ),
155+ pubsub .WithDiscovery (routing .NewRoutingDiscovery (dht )),
156+ )
126157 if err != nil {
127158 return fmt .Errorf ("unable to create gossipsub with error: %w" , err )
128159 }
0 commit comments