2023-09-03 23:47:09 +00:00
|
|
|
package master
|
|
|
|
|
|
|
|
import (
|
2024-10-12 18:55:17 +00:00
|
|
|
"bytes"
|
2023-09-03 23:47:09 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"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-10-12 18:55:17 +00:00
|
|
|
if bytes.Equal(e.pubSub.GetPeerID(), []byte(e.beacon)) {
|
2024-10-17 04:51:27 +00:00
|
|
|
e.logger.Debug("proving new frame")
|
2024-10-12 18:55:17 +00:00
|
|
|
e.collectedProverSlotsMx.Lock()
|
|
|
|
collectedProverSlots := e.collectedProverSlots
|
|
|
|
e.collectedProverSlots = []*protobufs.InclusionAggregateProof{}
|
|
|
|
e.collectedProverSlotsMx.Unlock()
|
2023-09-03 23:47:09 +00:00
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
frame, err := e.frameProver.ProveMasterClockFrame(
|
|
|
|
previousFrame,
|
|
|
|
time.Now().UnixMilli(),
|
|
|
|
e.difficulty,
|
|
|
|
collectedProverSlots,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "prove")
|
|
|
|
}
|
|
|
|
|
|
|
|
e.state = consensus.EngineStatePublishing
|
|
|
|
e.logger.Debug("returning new proven frame")
|
|
|
|
return frame, nil
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
return previousFrame, nil
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 05:05:04 +00:00
|
|
|
func (e *MasterClockConsensusEngine) GetMostAheadPeers() (
|
|
|
|
[][]byte,
|
|
|
|
error,
|
|
|
|
) {
|
|
|
|
frame, err := e.masterTimeReel.Head()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needs to be enough to make the sync worthwhile:
|
|
|
|
max := frame.FrameNumber + 10
|
|
|
|
|
|
|
|
var peers [][]byte = [][]byte{}
|
2024-03-21 07:14:45 +00:00
|
|
|
peerMap := e.peerInfoManager.GetPeerMap()
|
|
|
|
for peerId, v := range peerMap {
|
2024-03-08 05:05:04 +00:00
|
|
|
if v.MasterHeadFrame > max {
|
|
|
|
peers = append(peers, []byte(peerId))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(peers) >= 30 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(peers) == 0 {
|
|
|
|
return nil, p2p.ErrNoPeersAvailable
|
|
|
|
}
|
|
|
|
|
|
|
|
return peers, 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
|
|
|
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
|
|
|
return latest, nil
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|