diff --git a/api/service/manager.go b/api/service/manager.go index baaaba29b..2beb79fe5 100644 --- a/api/service/manager.go +++ b/api/service/manager.go @@ -82,6 +82,11 @@ func (m *Manager) GetServices() []Service { return m.services } +// GetService get the specified service +func (m *Manager) GetService(t Type) Service { + return m.serviceMap[t] +} + // StartServices run all registered services. If one of the starting service returns // an error, closing all started services. func (m *Manager) StartServices() (err error) { diff --git a/api/service/synchronize/service.go b/api/service/synchronize/service.go new file mode 100644 index 000000000..92aac3206 --- /dev/null +++ b/api/service/synchronize/service.go @@ -0,0 +1,37 @@ +package synchronize + +import ( + "github.com/ethereum/go-ethereum/rpc" + "github.com/harmony-one/harmony/core" + "github.com/harmony-one/harmony/hmy/downloader" + "github.com/harmony-one/harmony/p2p" +) + +// Service is simply a adapter of Downloaders, which support block synchronization +type Service struct { + Downloaders *downloader.Downloaders +} + +// NewService creates the a new downloader service +func NewService(host p2p.Host, bcs []*core.BlockChain, config downloader.Config) *Service { + return &Service{ + Downloaders: downloader.NewDownloaders(host, bcs, config), + } +} + +// Start start the service +func (s *Service) Start() error { + s.Downloaders.Start() + return nil +} + +// Stop stop the service +func (s *Service) Stop() error { + s.Downloaders.Close() + return nil +} + +// APIs return all APIs of the service +func (s *Service) APIs() []rpc.API { + return nil +} diff --git a/p2p/host.go b/p2p/host.go index ba8b1ad77..020f45969 100644 --- a/p2p/host.go +++ b/p2p/host.go @@ -10,6 +10,8 @@ import ( "strings" "sync" + "github.com/libp2p/go-libp2p-core/protocol" + "github.com/harmony-one/bls/ffi/go/bls" nodeconfig "github.com/harmony-one/harmony/internal/configs/node" "github.com/harmony-one/harmony/internal/utils" @@ -230,8 +232,9 @@ func (host *HostV2) C() (int, int, int) { // AddStreamProtocol adds the stream protocols to the host to be started and closed // when the host starts or close func (host *HostV2) AddStreamProtocol(protocols ...sttypes.Protocol) { - for _, protocol := range protocols { - host.streamProtos = append(host.streamProtos, protocol) + for _, proto := range protocols { + host.streamProtos = append(host.streamProtos, proto) + host.h.SetStreamHandlerMatch(protocol.ID(proto.ProtoID()), proto.Match, proto.HandleStream) } }