2023-07-07 06:07:10 +00:00
|
|
|
package blossomsub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaultBlossomSubFeatures(t *testing.T) {
|
2024-10-12 18:55:17 +00:00
|
|
|
if !BlossomSubDefaultFeatures(BlossomSubFeatureMesh, BlossomSubID_v2) {
|
|
|
|
t.Fatal("BlossomSub-v2.0 should support Mesh")
|
2023-07-07 06:07:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
if !BlossomSubDefaultFeatures(BlossomSubFeaturePX, BlossomSubID_v2) {
|
|
|
|
t.Fatal("BlossomSub-v2.0 should support PX")
|
2023-07-07 06:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlossomSubCustomProtocols(t *testing.T) {
|
|
|
|
customsub := protocol.ID("customsub/1.0.0")
|
2024-10-12 18:55:17 +00:00
|
|
|
protos := []protocol.ID{customsub}
|
2023-07-07 06:07:10 +00:00
|
|
|
features := func(feat BlossomSubFeature, proto protocol.ID) bool {
|
|
|
|
return proto == customsub
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2024-10-12 18:55:17 +00:00
|
|
|
hosts := getDefaultHosts(t, 3)
|
2023-07-07 06:07:10 +00:00
|
|
|
|
2023-08-21 03:50:38 +00:00
|
|
|
bsubs := getBlossomSubs(ctx, hosts[:2], WithBlossomSubProtocols(protos, features))
|
2023-07-07 06:07:10 +00:00
|
|
|
|
|
|
|
connectAll(t, hosts)
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
bitmask := []byte{0x00, 0x80, 0x00, 0x00}
|
|
|
|
var bitmasks []*Bitmask
|
2023-07-07 06:07:10 +00:00
|
|
|
var subs []*Subscription
|
2024-10-12 18:55:17 +00:00
|
|
|
for _, ps := range bsubs {
|
|
|
|
b, err := ps.Join(bitmask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-07-07 06:07:10 +00:00
|
|
|
subch, err := ps.Subscribe(bitmask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
subs = append(subs, subch...)
|
|
|
|
bitmasks = append(bitmasks, b...)
|
2023-07-07 06:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for heartbeats to build mesh
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
2023-08-21 03:50:38 +00:00
|
|
|
// check the meshes of the bsubs, the BlossomSub meshes should include each other but not the
|
2023-07-07 06:07:10 +00:00
|
|
|
// floddsub peer
|
2023-08-21 03:50:38 +00:00
|
|
|
bsubs[0].eval <- func() {
|
|
|
|
bs := bsubs[0].rt.(*BlossomSubRouter)
|
2023-07-07 06:07:10 +00:00
|
|
|
|
2023-08-21 03:50:38 +00:00
|
|
|
_, ok := bs.mesh[string(bitmask)][hosts[1].ID()]
|
2023-07-07 06:07:10 +00:00
|
|
|
if !ok {
|
2023-08-21 03:50:38 +00:00
|
|
|
t.Fatal("expected bs0 to have bs1 in its mesh")
|
2023-07-07 06:07:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 03:50:38 +00:00
|
|
|
_, ok = bs.mesh[string(bitmask)][hosts[2].ID()]
|
2023-07-07 06:07:10 +00:00
|
|
|
if ok {
|
2023-08-21 03:50:38 +00:00
|
|
|
t.Fatal("expected bs0 to not have fs in its mesh")
|
2023-07-07 06:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 03:50:38 +00:00
|
|
|
bsubs[1].eval <- func() {
|
|
|
|
bs := bsubs[1].rt.(*BlossomSubRouter)
|
2023-07-07 06:07:10 +00:00
|
|
|
|
2023-08-21 03:50:38 +00:00
|
|
|
_, ok := bs.mesh[string(bitmask)][hosts[0].ID()]
|
2023-07-07 06:07:10 +00:00
|
|
|
if !ok {
|
2023-08-21 03:50:38 +00:00
|
|
|
t.Fatal("expected bs1 to have bs0 in its mesh")
|
2023-07-07 06:07:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 03:50:38 +00:00
|
|
|
_, ok = bs.mesh[string(bitmask)][hosts[2].ID()]
|
2023-07-07 06:07:10 +00:00
|
|
|
if ok {
|
2023-08-21 03:50:38 +00:00
|
|
|
t.Fatal("expected bs1 to not have fs in its mesh")
|
2023-07-07 06:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send some messages
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
msg := []byte(fmt.Sprintf("%d it's not quite a floooooood %d", i, i))
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
owner := rand.Intn(len(bsubs))
|
|
|
|
bitmasks[owner].Publish(ctx, bitmasks[owner].bitmask, msg)
|
2023-07-07 06:07:10 +00:00
|
|
|
|
|
|
|
for _, sub := range subs {
|
|
|
|
got, err := sub.Next(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(sub.err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(msg, got.Data) {
|
|
|
|
t.Fatal("got wrong message!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|