erigon-pulse/cmd/headers/download/broadcast.go

66 lines
1.8 KiB
Go
Raw Normal View History

package download
import (
"context"
"math/big"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/protocols/eth"
proto_sentry "github.com/ledgerwatch/erigon/gointerfaces/sentry"
"github.com/ledgerwatch/erigon/log"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/stages/headerdownload"
"google.golang.org/grpc"
)
2021-04-07 08:54:25 +00:00
// Methods of sentry called by Core
func (cs *ControlServerImpl) PropagateNewBlockHashes(ctx context.Context, announces []headerdownload.Announce) {
cs.lock.RLock()
defer cs.lock.RUnlock()
typedRequest := make(eth.NewBlockHashesPacket, len(announces))
for i := range announces {
typedRequest[i].Hash = announces[i].Hash
typedRequest[i].Number = announces[i].Number
}
data, err := rlp.EncodeToBytes(&typedRequest)
if err != nil {
log.Error("propagateNewBlockHashes", "error", err)
return
}
req := &proto_sentry.OutboundMessageData{
Id: proto_sentry.MessageId_NewBlockHashes,
Data: data,
}
2021-04-07 08:54:25 +00:00
for _, sentry := range cs.sentries {
_, err = sentry.SendMessageToAll(ctx, req, &grpc.EmptyCallOption{})
if err != nil {
log.Error("propagateNewBlockHashes", "error", err)
}
}
}
func (cs *ControlServerImpl) BroadcastNewBlock(ctx context.Context, block *types.Block, td *big.Int) {
cs.lock.RLock()
defer cs.lock.RUnlock()
data, err := rlp.EncodeToBytes(&eth.NewBlockPacket{
Block: block,
TD: td,
})
if err != nil {
log.Error("broadcastNewBlock", "error", err)
}
req := proto_sentry.SendMessageToRandomPeersRequest{
MaxPeers: 1024,
Data: &proto_sentry.OutboundMessageData{
Id: proto_sentry.MessageId_NewBlock,
Data: data,
},
}
2021-04-07 08:54:25 +00:00
for _, sentry := range cs.sentries {
if _, err = sentry.SendMessageToRandomPeers(ctx, &req, &grpc.EmptyCallOption{}); err != nil {
log.Error("broadcastNewBlock", "error", err)
}
}
}