ceremonyclient/node/consensus/master/peer_messaging.go

109 lines
2.3 KiB
Go
Raw Permalink Normal View History

2023-09-03 23:47:09 +00:00
package master
import (
2024-03-08 05:05:04 +00:00
"time"
2023-09-03 23:47:09 +00:00
"github.com/pkg/errors"
"go.uber.org/zap"
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
)
2024-03-08 05:05:04 +00:00
func (e *MasterClockConsensusEngine) Sync(
request *protobufs.SyncRequest,
server protobufs.ValidationService_SyncServer,
2023-09-03 23:47:09 +00:00
) error {
e.currentReceivingSyncPeersMx.Lock()
if e.currentReceivingSyncPeers > 4 {
e.currentReceivingSyncPeersMx.Unlock()
e.logger.Debug("currently processing maximum sync requests, returning")
return nil
}
e.currentReceivingSyncPeers++
e.currentReceivingSyncPeersMx.Unlock()
defer func() {
e.currentReceivingSyncPeersMx.Lock()
e.currentReceivingSyncPeers--
e.currentReceivingSyncPeersMx.Unlock()
}()
2024-03-08 05:05:04 +00:00
from := request.FramesRequest.FromFrameNumber
2023-09-03 23:47:09 +00:00
2024-03-08 05:05:04 +00:00
masterFrame, err := e.masterTimeReel.Head()
if err != nil {
panic(err)
2023-09-03 23:47:09 +00:00
}
2024-03-08 05:05:04 +00:00
if masterFrame.FrameNumber < from || len(e.historicFrames) == 0 {
2023-09-28 07:59:27 +00:00
e.logger.Debug(
2024-03-08 05:05:04 +00:00
"peer asked for undiscovered frame",
zap.Uint64("frame_number", request.FramesRequest.FromFrameNumber),
2023-09-03 23:47:09 +00:00
)
return nil
}
2024-03-08 05:05:04 +00:00
to := request.FramesRequest.ToFrameNumber
if to == 0 || to-request.FramesRequest.FromFrameNumber > 16 {
to = request.FramesRequest.FromFrameNumber + 15
2023-09-03 23:47:09 +00:00
}
2024-03-08 05:05:04 +00:00
for {
if int(to) > int(masterFrame.FrameNumber) {
to = masterFrame.FrameNumber
}
2024-02-13 07:04:56 +00:00
2023-09-28 07:59:27 +00:00
e.logger.Debug(
2024-03-08 05:05:04 +00:00
"sending response",
zap.Uint64("from", from),
zap.Uint64("to", to),
zap.Uint64("total_frames", uint64(to-from+1)),
2023-09-03 23:47:09 +00:00
)
2024-03-08 05:05:04 +00:00
iter, err := e.clockStore.RangeMasterClockFrames(
e.filter,
from,
to,
)
if err != nil {
return errors.Wrap(err, "sync")
2023-09-03 23:47:09 +00:00
}
2024-03-08 05:05:04 +00:00
response := []*protobufs.ClockFrame{}
2023-09-03 23:47:09 +00:00
2024-03-08 05:05:04 +00:00
for iter.First(); iter.Valid(); iter.Next() {
frame, err := iter.Value()
if err != nil {
return errors.Wrap(err, "sync")
}
2023-09-03 23:47:09 +00:00
2024-03-08 05:05:04 +00:00
response = append(response, frame)
}
2023-09-09 23:45:47 +00:00
2024-03-08 05:05:04 +00:00
if err = iter.Close(); err != nil {
return errors.Wrap(err, "sync")
}
2023-09-09 23:45:47 +00:00
2024-03-08 05:05:04 +00:00
if len(response) == 0 {
return nil
2023-09-09 23:45:47 +00:00
}
2024-03-08 05:05:04 +00:00
if err := server.Send(&protobufs.SyncResponse{
FramesResponse: &protobufs.ClockFramesResponse{
Filter: e.filter,
FromFrameNumber: from,
ToFrameNumber: to,
ClockFrames: response,
},
}); err != nil {
return errors.Wrap(err, "sync")
}
2023-09-09 23:45:47 +00:00
2024-03-08 05:05:04 +00:00
from = response[len(response)-1].FrameNumber + 1
to = from + 15
2023-09-09 23:45:47 +00:00
2024-03-08 05:05:04 +00:00
time.Sleep(1 * time.Second)
2023-09-03 23:47:09 +00:00
}
}