Skip to content

Commit 2c8db5d

Browse files
committed
WIP
1 parent e510b43 commit 2c8db5d

8 files changed

Lines changed: 85 additions & 49 deletions

File tree

.github/workflows/juno-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
fail-fast: false
1919
matrix:
2020
os: [ubuntu-latest, macos-latest, ubuntu-arm64-4-core]
21+
iteration: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2122
runs-on: ${{ matrix.os }}
2223
env:
2324
VM_DEBUG: true

consensus/p2p/buffered/buffered_test.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"maps"
7-
"sync"
87
"testing"
98
"time"
109

@@ -26,9 +25,10 @@ const (
2625
protocolID = "test-buffered-topic-subscription-protocol"
2726
topicName = "test-buffered-topic-subscription-topic"
2827
nodeCount = 20
29-
messageCount = 100
30-
logLevel = zapcore.ErrorLevel
28+
messageCount = 50
29+
logLevel = zapcore.InfoLevel
3130
retryInterval = 1 * time.Second
31+
maxWait = 5 * time.Second
3232
)
3333

3434
type TestMessage = consensus.ConsensusStreamId
@@ -57,22 +57,29 @@ func TestBufferedTopicSubscriptionAndProtoBroadcaster(t *testing.T) {
5757
}
5858
}
5959

60-
iterator := iter.Iterator[*pubsub.Topic]{MaxGoroutines: len(topics)}
61-
wg := sync.WaitGroup{}
62-
wg.Add(len(messages))
60+
iterator := iter.Iterator[*pubsub.Topic]{MaxGoroutines: nodeCount}
61+
finished := make(chan struct{}, nodeCount)
62+
liveness := make(chan struct{}, 1)
6363

6464
go func() {
6565
iterator.ForEachIdx(topics, func(i int, destination **pubsub.Topic) {
6666
logger := &utils.ZapLogger{SugaredLogger: logger.Named(fmt.Sprintf("destination-%d", i))}
6767
pending := maps.Clone(allMessages)
6868
subscription := buffered.NewTopicSubscription(logger, nodeCount*messageCount, func(ctx context.Context, msg *pubsub.Message) {
69-
if len(pending) == 0 {
69+
msgStr := string(msg.Message.Data)
70+
if _, ok := pending[msgStr]; !ok {
7071
return
7172
}
7273

73-
delete(pending, string(msg.Message.Data))
74+
select {
75+
case liveness <- struct{}{}:
76+
default:
77+
}
78+
79+
delete(pending, msgStr)
80+
7481
if len(pending) == 0 {
75-
wg.Done()
82+
finished <- struct{}{}
7683
logger.Info("all messages received")
7784
}
7885
logger.Debugw("received", "message", string(msg.Message.Data), "pending", len(pending))
@@ -83,7 +90,6 @@ func TestBufferedTopicSubscriptionAndProtoBroadcaster(t *testing.T) {
8390
}()
8491

8592
go func() {
86-
time.Sleep(1 * time.Second)
8793
iterator.ForEachIdx(topics, func(i int, source **pubsub.Topic) {
8894
logger := &utils.ZapLogger{SugaredLogger: logger.Named(fmt.Sprintf("source-%d", i))}
8995
var rebroadcastStrategy buffered.RebroadcastStrategy[*TestMessage]
@@ -101,7 +107,9 @@ func TestBufferedTopicSubscriptionAndProtoBroadcaster(t *testing.T) {
101107
})
102108
}()
103109

104-
wg.Wait()
110+
for range nodeCount {
111+
wait(t, liveness, finished)
112+
}
105113
})
106114

107115
t.Run("canceled context", func(t *testing.T) {
@@ -128,6 +136,21 @@ func TestBufferedTopicSubscriptionAndProtoBroadcaster(t *testing.T) {
128136
})
129137
}
130138

139+
func wait(t *testing.T, liveness, finished chan struct{}) {
140+
t.Helper()
141+
for {
142+
select {
143+
case <-finished:
144+
return
145+
case <-liveness:
146+
continue
147+
case <-time.After(maxWait):
148+
require.FailNow(t, "liveness check failed")
149+
return
150+
}
151+
}
152+
}
153+
131154
func getTestMessage(node, messageIndex int) *TestMessage {
132155
return &TestMessage{
133156
Nonce: uint64(node*messageCount + messageIndex),

consensus/p2p/buffered/proto_broadcaster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (b ProtoBroadcaster[M]) Broadcast(ctx context.Context, msg M) {
4242
func (b ProtoBroadcaster[M]) Loop(ctx context.Context, topic *pubsub.Topic) {
4343
readinessOpt := pubsub.WithReadiness(pubsub.MinTopicSize(1))
4444
var rebroadcasted rebroadcastMessages
45+
4546
for {
4647
select {
4748
case <-ctx.Done():

consensus/p2p/p2p.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ import (
2525
type topicName string
2626

2727
const (
28-
protocolPrefix = "starknet"
2928
chainID = "1" // TODO: Make this configurable
3029
consensusProtocolID = "consensus"
3130
proposalTopicName topicName = "consensus_proposals"
3231
voteTopicName topicName = "consensus_votes"
33-
gossipSubHistory = 60
3432
)
3533

3634
type P2P[V types.Hashable[H], H types.Hash, A types.Addr] interface {
@@ -122,14 +120,6 @@ func New(
122120
}
123121
}
124122

125-
func (p *p2p[V, H, A]) getGossipSubOptions() libp2p.Option {
126-
params := libp2p.DefaultGossipSubParams()
127-
params.HistoryLength = gossipSubHistory
128-
params.HistoryGossip = gossipSubHistory
129-
130-
return libp2p.WithGossipSubParams(params)
131-
}
132-
133123
func (p *p2p[V, H, A]) Run(ctx context.Context) error {
134124
gossipSub, err := pubsub.Run(
135125
ctx,
@@ -138,36 +128,42 @@ func (p *p2p[V, H, A]) Run(ctx context.Context) error {
138128
p.host,
139129
p.pubSubQueueSize,
140130
p.bootstrapPeersFn,
141-
p.getGossipSubOptions(),
142131
)
143132
if err != nil {
144133
return fmt.Errorf("unable to create gossipsub with error: %w", err)
145134
}
146135

147-
topics := make(map[topicName]*libp2p.Topic)
136+
topics := make([]*libp2p.Topic, 0, len(p.topicAttachment))
137+
relayCancels := make([]func(), 0, len(p.topicAttachment))
148138
defer func() {
139+
for _, cancel := range relayCancels {
140+
cancel()
141+
}
149142
for _, topic := range topics {
150143
topic.Close()
151144
}
152145
}()
153146

154147
wg := conc.NewWaitGroup()
148+
defer wg.Wait()
155149

156-
for topicName := range p.topicAttachment {
157-
if topics[topicName], err = gossipSub.Join(string(topicName)); err != nil {
150+
for topicName, services := range p.topicAttachment {
151+
topic, relayCancel, err := pubsub.JoinTopic(gossipSub, string(topicName))
152+
if err != nil {
158153
return fmt.Errorf("unable to join topic %s with error: %w", topicName, err)
159154
}
160-
}
161155

162-
for topicName, services := range p.topicAttachment {
156+
topics = append(topics, topic)
157+
relayCancels = append(relayCancels, relayCancel)
158+
163159
for _, service := range services {
164160
wg.Go(func() {
165-
service.Loop(ctx, topics[topicName])
161+
service.Loop(ctx, topic)
166162
})
167163
}
168164
}
169165

170-
wg.Wait()
166+
<-ctx.Done()
171167
return nil
172168
}
173169

mempool/p2p/p2p.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ func (p *P2P) Run(ctx context.Context) error {
6161
return fmt.Errorf("unable to create gossipsub with error: %w", err)
6262
}
6363

64-
topic, err := gossipSub.Join(transactionTopicName)
64+
topic, relayCancel, err := pubsub.JoinTopic(gossipSub, transactionTopicName)
6565
if err != nil {
6666
return fmt.Errorf("unable to join topic %s with error: %w", transactionTopicName, err)
6767
}
68+
defer relayCancel()
6869
defer topic.Close()
6970

7071
wg := conc.NewWaitGroup()

mempool/p2p/p2p_broadcasters_listeners_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
)
2121

2222
const (
23-
logLevel = zapcore.DebugLevel
24-
nodeCount = 3
25-
txCount = 300
23+
logLevel = zapcore.InfoLevel
24+
nodeCount = 20
25+
txCount = 50
2626
maxWait = 5 * time.Second
2727
)
2828

@@ -81,6 +81,7 @@ func TestMempoolBroadcastersAndListeners(t *testing.T) {
8181
delete(pending, transaction.Transaction.Hash().String())
8282
logger.Debugw("pending", "count", len(pending))
8383
if len(pending) == 0 {
84+
logger.Infow("all transactions received")
8485
return
8586
}
8687
case <-time.After(maxWait):
@@ -96,9 +97,7 @@ func TestMempoolBroadcastersAndListeners(t *testing.T) {
9697

9798
func getRandomTransactions(t *testing.T) []mempool.BroadcastedTransaction {
9899
transactions := make([]mempool.BroadcastedTransaction, txCount)
99-
transactions[0], _ = testutils.TransactionBuilder.GetTestDeclareTransaction(t)
100-
transactions[1], _ = testutils.TransactionBuilder.GetTestDeployAccountTransaction(t)
101-
for i := 2; i < txCount; i++ {
100+
for i := range txCount {
102101
transactions[i], _ = testutils.TransactionBuilder.GetTestInvokeTransaction(t)
103102
}
104103
return transactions

p2p/pubsub/pubsub.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
)
1717

1818
const (
19-
protocolPrefix = "starknet"
19+
protocolPrefix = "starknet"
20+
gossipSubHistory = 60
2021
)
2122

2223
func GetHost(hostPrivateKey crypto.PrivKey, hostAddress string) (host.Host, error) {
@@ -44,7 +45,6 @@ func Run(
4445
host host.Host,
4546
pubSubQueueSize int,
4647
bootstrapPeersFn func() []peer.AddrInfo,
47-
opts ...pubsub.Option,
4848
) (*pubsub.PubSub, error) {
4949
dht, err := dht.New(
5050
ctx,
@@ -59,17 +59,35 @@ func Run(
5959
return nil, fmt.Errorf("unable to create dht with error: %w", err)
6060
}
6161

62+
params := pubsub.DefaultGossipSubParams()
63+
params.HistoryLength = gossipSubHistory
64+
params.HistoryGossip = gossipSubHistory
65+
6266
return pubsub.NewGossipSub(
6367
ctx,
6468
host,
65-
append(opts,
66-
pubsub.WithPeerOutboundQueueSize(pubSubQueueSize),
67-
pubsub.WithValidateQueueSize(pubSubQueueSize),
68-
pubsub.WithDiscovery(routing.NewRoutingDiscovery(dht)),
69-
)...,
69+
pubsub.WithPeerOutboundQueueSize(pubSubQueueSize),
70+
pubsub.WithValidateQueueSize(pubSubQueueSize),
71+
pubsub.WithDiscovery(routing.NewRoutingDiscovery(dht)),
72+
pubsub.WithGossipSubParams(params),
7073
)
7174
}
7275

76+
func JoinTopic(pubSub *pubsub.PubSub, topicName string) (*pubsub.Topic, func(), error) {
77+
topic, err := pubSub.Join(topicName)
78+
if err != nil {
79+
return nil, nil, fmt.Errorf("unable to join topic %s with error: %w", topicName, err)
80+
}
81+
82+
// Make sure that the host starts connecting to other nodes
83+
relayCancel, err := topic.Relay()
84+
if err != nil {
85+
return nil, nil, fmt.Errorf("unable to relay topic %s with error: %w", topicName, err)
86+
}
87+
88+
return topic, relayCancel, nil
89+
}
90+
7391
func ExtractPeers(peers string) ([]peer.AddrInfo, error) {
7492
if peers == "" {
7593
return nil, nil

p2p/pubsub/testutils/pubsub.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,12 @@ func BuildNetworks(
6464
return nodes
6565
}
6666

67-
func (n Nodes) JoinTopic(t *testing.T, chainID, protocolID protocol.ID, topic string) []*libp2p.Topic {
67+
func (n Nodes) JoinTopic(t *testing.T, chainID, protocolID protocol.ID, topicName string) []*libp2p.Topic {
6868
return iter.Map(n, func(node *Node) *libp2p.Topic {
69-
pubsub, err := pubsub.Run(t.Context(), chainID, protocolID, node.Host, config.DefaultBufferSizes.PubSubQueueSize, node.GetBootstrapPeers)
69+
pubSub, err := pubsub.Run(t.Context(), chainID, protocolID, node.Host, config.DefaultBufferSizes.PubSubQueueSize, node.GetBootstrapPeers)
7070
require.NoError(t, err)
7171

72-
topic, err := pubsub.Join(topic)
73-
require.NoError(t, err)
74-
75-
relayCancel, err := topic.Relay()
72+
topic, relayCancel, err := pubsub.JoinTopic(pubSub, topicName)
7673
require.NoError(t, err)
7774
t.Cleanup(relayCancel)
7875

0 commit comments

Comments
 (0)