2018-04-10 06:44:09 +00:00
|
|
|
package peers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
"github.com/status-im/status-go/common"
|
2018-07-04 10:51:47 +00:00
|
|
|
"github.com/status-im/status-go/discovery"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2018-04-10 06:44:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Register manages register topic queries
|
|
|
|
type Register struct {
|
2018-07-04 10:51:47 +00:00
|
|
|
discovery discovery.Discovery
|
2018-07-03 11:27:04 +00:00
|
|
|
topics []discv5.Topic
|
2018-04-10 06:44:09 +00:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegister creates instance of topic register
|
2018-07-04 10:51:47 +00:00
|
|
|
func NewRegister(discovery discovery.Discovery, topics ...discv5.Topic) *Register {
|
2018-07-03 11:27:04 +00:00
|
|
|
return &Register{discovery: discovery, topics: topics}
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start topic register query for every topic
|
2018-07-03 11:27:04 +00:00
|
|
|
func (r *Register) Start() error {
|
|
|
|
if !r.discovery.Running() {
|
2018-04-10 06:44:09 +00:00
|
|
|
return ErrDiscv5NotRunning
|
|
|
|
}
|
|
|
|
r.quit = make(chan struct{})
|
|
|
|
for _, topic := range r.topics {
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func(t discv5.Topic) {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Debug("v5 register topic", zap.String("topic", string(t)))
|
2018-07-03 11:27:04 +00:00
|
|
|
if err := r.discovery.Register(string(t), r.quit); err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("error registering topic", zap.String("topic", string(t)), zap.Error(err))
|
2018-07-03 11:27:04 +00:00
|
|
|
}
|
2018-04-10 06:44:09 +00:00
|
|
|
r.wg.Done()
|
|
|
|
}(topic)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop all register topic queries and waits for them to exit
|
|
|
|
func (r *Register) Stop() {
|
|
|
|
if r.quit == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-r.quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(r.quit)
|
|
|
|
}
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Debug("waiting for register queries to exit")
|
2018-04-10 06:44:09 +00:00
|
|
|
r.wg.Wait()
|
|
|
|
}
|