prysm-pulse/beacon-chain/p2p/broadcaster.go
Preston Van Loon 0b8cbd06b6
Add flag for testing new p2p (#3243)
* refactor a bit to select p2p

* lint

* fix build

* fix build

* fix build

* fix build

* fix build
2019-08-19 17:20:56 -04:00

33 lines
841 B
Go

package p2p
import (
"bytes"
"context"
"reflect"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
)
// ErrMessageNotMapped occurs on a Broadcast attempt when a message has not been defined in the
// GossipTypeMapping.
var ErrMessageNotMapped = errors.New("message type is not mapped to a PubSub topic")
// Broadcast a message to the p2p network.
func (s *Service) Broadcast(ctx context.Context, msg proto.Message) error {
topic, ok := GossipTypeMapping[reflect.TypeOf(msg)]
if !ok {
return ErrMessageNotMapped
}
buf := new(bytes.Buffer)
if _, err := s.Encoding().Encode(buf, msg); err != nil {
return errors.Wrap(err, "could not encode message")
}
if err := s.pubsub.Publish(topic+s.Encoding().ProtocolSuffix(), buf.Bytes()); err != nil {
return errors.Wrap(err, "could not publish message")
}
return nil
}