d044e30e12
Index provider integration uses a gossipsub topic to announce changes to the advertised content. The topic name was fixed to the default topic which is `/indexer/ingest/mainnet`. In the case of lotus, the gossipsub validators enforce a list of topics the instance is permitted to join by setting subscription filter option when `PubSub` instance is constructed via DI. Having the fixed topic name meant that any SP starting up a node on a network other than `mainnet` would have to override the default config to avoid the node crashing when index provider is enabled. Instead of a fixed default, the changes here infer the allowed indexer topic name from network name automatically if the topic configuration is left empty. Fixes #8510
98 lines
2.9 KiB
Go
98 lines
2.9 KiB
Go
package modules_test
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
provider "github.com/filecoin-project/index-provider"
|
|
"github.com/ipfs/go-datastore"
|
|
"github.com/libp2p/go-libp2p"
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/fx"
|
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
|
"github.com/filecoin-project/lotus/node/modules"
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
)
|
|
|
|
func Test_IndexProviderTopic(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
givenAllowedTopics []string
|
|
givenConfiguredTopic string
|
|
givenNetworkName dtypes.NetworkName
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "Joins configured topic when allowed",
|
|
givenAllowedTopics: []string{"fish"},
|
|
givenConfiguredTopic: "fish",
|
|
},
|
|
{
|
|
name: "Joins topic inferred from network name when allowed",
|
|
givenAllowedTopics: []string{"/indexer/ingest/fish"},
|
|
givenNetworkName: "fish",
|
|
},
|
|
{
|
|
name: "Fails to join configured topic when disallowed",
|
|
givenAllowedTopics: []string{"/indexer/ingest/fish"},
|
|
givenConfiguredTopic: "lobster",
|
|
wantErr: "joining indexer topic lobster: topic is not allowed by the subscription filter",
|
|
},
|
|
{
|
|
name: "Fails to join topic inferred from network name when disallowed",
|
|
givenAllowedTopics: []string{"/indexer/ingest/fish"},
|
|
givenNetworkName: "lobster",
|
|
wantErr: "joining indexer topic /indexer/ingest/lobster: topic is not allowed by the subscription filter",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
test := test
|
|
t.Run(test.name, func(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
h, err := libp2p.New()
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
require.NoError(t, h.Close())
|
|
}()
|
|
|
|
filter := pubsub.WithSubscriptionFilter(pubsub.NewAllowlistSubscriptionFilter(test.givenAllowedTopics...))
|
|
ps, err := pubsub.NewGossipSub(ctx, h, filter)
|
|
require.NoError(t, err)
|
|
|
|
app := fx.New(
|
|
fx.Provide(
|
|
func() host.Host { return h },
|
|
func() dtypes.NetworkName { return test.givenNetworkName },
|
|
func() dtypes.MinerAddress { return dtypes.MinerAddress(address.TestAddress) },
|
|
func() dtypes.ProviderDataTransfer { return nil },
|
|
func() *pubsub.PubSub { return ps },
|
|
func() dtypes.MetadataDS { return datastore.NewMapDatastore() },
|
|
modules.IndexProvider(config.IndexProviderConfig{
|
|
Enable: true,
|
|
TopicName: test.givenConfiguredTopic,
|
|
}),
|
|
),
|
|
fx.Invoke(func(p provider.Interface) {}),
|
|
)
|
|
err = app.Start(ctx)
|
|
|
|
if test.wantErr == "" {
|
|
require.NoError(t, err)
|
|
err = app.Stop(ctx)
|
|
require.NoError(t, err)
|
|
} else {
|
|
require.True(t, strings.HasSuffix(err.Error(), test.wantErr))
|
|
}
|
|
})
|
|
}
|
|
}
|