2023-09-03 23:47:09 +00:00
|
|
|
package master
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/go-libp2p-blossomsub/pb"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/consensus"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/p2p"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e *MasterClockConsensusEngine) prove(
|
|
|
|
previousFrame *protobufs.ClockFrame,
|
|
|
|
) (*protobufs.ClockFrame, error) {
|
2024-02-13 07:04:56 +00:00
|
|
|
e.logger.Debug("proving new frame")
|
2023-09-03 23:47:09 +00:00
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
frame, err := e.frameProver.ProveMasterClockFrame(
|
|
|
|
previousFrame,
|
|
|
|
time.Now().UnixMilli(),
|
|
|
|
e.difficulty,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "prove")
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
e.state = consensus.EngineStatePublishing
|
|
|
|
e.logger.Debug("returning new proven frame")
|
|
|
|
return frame, nil
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MasterClockConsensusEngine) collect(
|
|
|
|
currentFramePublished *protobufs.ClockFrame,
|
|
|
|
) (*protobufs.ClockFrame, error) {
|
2024-02-13 07:04:56 +00:00
|
|
|
e.logger.Debug("collecting vdf proofs")
|
2023-09-03 23:47:09 +00:00
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
latest, err := e.masterTimeReel.Head()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-09-03 23:47:09 +00:00
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
if e.syncingStatus == SyncStatusNotSyncing {
|
|
|
|
peer, err := e.pubSub.GetRandomPeer(e.filter)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, p2p.ErrNoPeersAvailable) {
|
|
|
|
e.logger.Debug("no peers available, skipping sync")
|
2023-09-03 23:47:09 +00:00
|
|
|
} else {
|
2024-02-13 07:04:56 +00:00
|
|
|
e.logger.Error("error while fetching random peer", zap.Error(err))
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
} else {
|
|
|
|
e.syncingStatus = SyncStatusAwaitingResponse
|
|
|
|
e.logger.Debug("setting syncing target", zap.Binary("peer_id", peer))
|
|
|
|
e.syncingTarget = peer
|
2023-09-03 23:47:09 +00:00
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
channel := e.createPeerReceiveChannel(peer)
|
2023-09-28 07:59:27 +00:00
|
|
|
e.logger.Debug(
|
2024-02-13 07:04:56 +00:00
|
|
|
"listening on peer receive channel",
|
|
|
|
zap.Binary("channel", channel),
|
|
|
|
)
|
|
|
|
e.pubSub.Subscribe(channel, e.handleSync, true)
|
|
|
|
e.pubSub.Subscribe(
|
|
|
|
peer,
|
|
|
|
func(message *pb.Message) error { return nil },
|
|
|
|
true,
|
2023-09-03 23:47:09 +00:00
|
|
|
)
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
if err := e.publishMessage(peer, &protobufs.ClockFramesRequest{
|
|
|
|
Filter: e.filter,
|
|
|
|
FromFrameNumber: latest.FrameNumber + 1,
|
|
|
|
}); err != nil {
|
|
|
|
e.logger.Error(
|
|
|
|
"could not publish clock frame request",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
}()
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
waitDecay := time.Duration(2000)
|
|
|
|
for e.syncingStatus != SyncStatusNotSyncing {
|
2023-09-28 07:59:27 +00:00
|
|
|
e.logger.Debug(
|
2024-02-13 07:04:56 +00:00
|
|
|
"waiting for sync to complete...",
|
|
|
|
zap.Duration("wait_decay", waitDecay),
|
2023-09-03 23:47:09 +00:00
|
|
|
)
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
time.Sleep(waitDecay * time.Millisecond)
|
2023-09-09 23:45:47 +00:00
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
waitDecay = waitDecay * 2
|
|
|
|
if waitDecay >= (100 * (2 << 6)) {
|
|
|
|
if e.syncingStatus == SyncStatusAwaitingResponse {
|
|
|
|
e.logger.Debug("maximum wait for sync response, skipping sync")
|
|
|
|
e.syncingStatus = SyncStatusNotSyncing
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
waitDecay = 100 * (2 << 6)
|
|
|
|
}
|
2023-09-09 23:45:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
return latest, nil
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|