ceremonyclient/node/consensus/ceremony/consensus_frames.go

303 lines
8.0 KiB
Go
Raw Normal View History

2023-09-25 02:43:35 +00:00
package ceremony
import (
"bytes"
"context"
2024-01-10 06:58:38 +00:00
"time"
2023-09-25 02:43:35 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/config"
2023-09-25 02:43:35 +00:00
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/grpc"
2023-09-25 02:43:35 +00:00
"google.golang.org/protobuf/proto"
"source.quilibrium.com/quilibrium/monorepo/node/consensus"
qcrypto "source.quilibrium.com/quilibrium/monorepo/node/crypto"
2024-02-13 07:04:56 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/ceremony/application"
2023-09-25 02:43:35 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/p2p"
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
)
func (e *CeremonyDataClockConsensusEngine) prove(
previousFrame *protobufs.ClockFrame,
) (*protobufs.ClockFrame, error) {
2024-02-13 07:04:56 +00:00
if !e.frameProverTrie.Contains(e.provingKeyAddress) {
2023-09-25 02:43:35 +00:00
e.stagedLobbyStateTransitionsMx.Lock()
e.stagedLobbyStateTransitions = &protobufs.CeremonyLobbyStateTransition{}
e.stagedLobbyStateTransitionsMx.Unlock()
2024-02-13 07:04:56 +00:00
e.state = consensus.EngineStateCollecting
return previousFrame, nil
2023-09-25 02:43:35 +00:00
}
2024-02-13 07:04:56 +00:00
e.stagedLobbyStateTransitionsMx.Lock()
executionOutput := &protobufs.IntrinsicExecutionOutput{}
app, err := application.MaterializeApplicationFromFrame(previousFrame)
2023-09-25 02:43:35 +00:00
if err != nil {
2024-02-13 07:04:56 +00:00
e.stagedLobbyStateTransitions = &protobufs.CeremonyLobbyStateTransition{}
e.stagedLobbyStateTransitionsMx.Unlock()
return nil, errors.Wrap(err, "prove")
2023-09-25 02:43:35 +00:00
}
2024-02-13 07:04:56 +00:00
if e.stagedLobbyStateTransitions == nil {
e.stagedLobbyStateTransitions = &protobufs.CeremonyLobbyStateTransition{}
2023-09-25 02:43:35 +00:00
}
2024-02-13 07:04:56 +00:00
e.logger.Info(
"proving new frame",
zap.Int("state_transitions", len(e.stagedLobbyStateTransitions.TypeUrls)),
2023-09-25 02:43:35 +00:00
)
2024-02-13 07:04:56 +00:00
var validLobbyTransitions *protobufs.CeremonyLobbyStateTransition
var skippedTransition *protobufs.CeremonyLobbyStateTransition
app, validLobbyTransitions, skippedTransition, err = app.ApplyTransition(
previousFrame.FrameNumber,
e.stagedLobbyStateTransitions,
true,
2023-09-25 02:43:35 +00:00
)
if err != nil {
2024-02-13 07:04:56 +00:00
e.stagedLobbyStateTransitions = &protobufs.CeremonyLobbyStateTransition{}
e.stagedLobbyStateTransitionsMx.Unlock()
return nil, errors.Wrap(err, "prove")
2023-09-25 02:43:35 +00:00
}
2024-02-13 07:04:56 +00:00
e.stagedLobbyStateTransitions = skippedTransition
defer e.stagedLobbyStateTransitionsMx.Unlock()
2023-09-25 02:43:35 +00:00
2024-02-13 07:04:56 +00:00
lobbyState, err := app.MaterializeLobbyStateFromApplication()
2024-01-03 07:31:42 +00:00
if err != nil {
2024-02-13 07:04:56 +00:00
return nil, errors.Wrap(err, "prove")
2024-01-03 07:31:42 +00:00
}
2024-02-13 07:04:56 +00:00
executionOutput.Address = application.CEREMONY_ADDRESS
executionOutput.Output, err = proto.Marshal(lobbyState)
2024-01-03 07:31:42 +00:00
if err != nil {
2024-02-13 07:04:56 +00:00
return nil, errors.Wrap(err, "prove")
2024-01-03 07:31:42 +00:00
}
2024-02-13 07:04:56 +00:00
executionOutput.Proof, err = proto.Marshal(validLobbyTransitions)
2023-09-25 02:43:35 +00:00
if err != nil {
2024-02-13 07:04:56 +00:00
return nil, errors.Wrap(err, "prove")
2023-09-25 02:43:35 +00:00
}
data, err := proto.Marshal(executionOutput)
if err != nil {
2024-02-13 07:04:56 +00:00
return nil, errors.Wrap(err, "prove")
2023-09-25 02:43:35 +00:00
}
2024-02-13 07:04:56 +00:00
e.logger.Debug("encoded execution output")
2023-09-25 02:43:35 +00:00
2024-02-13 07:04:56 +00:00
commitment, err := e.inclusionProver.Commit(
data,
protobufs.IntrinsicExecutionOutputType,
2023-09-25 02:43:35 +00:00
)
if err != nil {
2024-02-13 07:04:56 +00:00
return nil, errors.Wrap(err, "prove")
2023-09-25 02:43:35 +00:00
}
2024-02-13 07:04:56 +00:00
e.logger.Debug("creating kzg proof")
proof, err := e.inclusionProver.ProveAggregate(
[]*qcrypto.InclusionCommitment{commitment},
2023-09-25 02:43:35 +00:00
)
if err != nil {
2024-02-13 07:04:56 +00:00
return nil, errors.Wrap(err, "prove")
2023-09-25 02:43:35 +00:00
}
2024-02-13 07:04:56 +00:00
e.logger.Debug("finalizing execution proof")
2023-09-25 02:43:35 +00:00
2024-02-13 07:04:56 +00:00
frame, err := e.frameProver.ProveDataClockFrame(
previousFrame,
[][]byte{proof.AggregateCommitment},
[]*protobufs.InclusionAggregateProof{
2023-09-25 02:43:35 +00:00
{
Filter: e.filter,
2024-02-13 07:04:56 +00:00
FrameNumber: previousFrame.FrameNumber + 1,
InclusionCommitments: []*protobufs.InclusionCommitment{
{
Filter: e.filter,
FrameNumber: previousFrame.FrameNumber + 1,
TypeUrl: proof.InclusionCommitments[0].TypeUrl,
Commitment: proof.InclusionCommitments[0].Commitment,
Data: data,
Position: 0,
},
},
Proof: proof.Proof,
2023-09-25 02:43:35 +00:00
},
},
2024-02-13 07:04:56 +00:00
e.provingKey,
time.Now().UnixMilli(),
e.difficulty,
)
2023-09-25 02:43:35 +00:00
if err != nil {
2024-02-13 07:04:56 +00:00
return nil, errors.Wrap(err, "prove")
2023-09-25 02:43:35 +00:00
}
e.logger.Info(
2024-02-13 07:04:56 +00:00
"returning new proven frame",
zap.Uint64("frame_number", frame.FrameNumber),
zap.Int("proof_count", len(frame.AggregateProofs)),
zap.Int("commitment_count", len(frame.Input[516:])/74),
2023-09-25 02:43:35 +00:00
)
2024-02-13 07:04:56 +00:00
return frame, nil
2023-09-25 02:43:35 +00:00
}
2024-03-24 08:11:00 +00:00
func (e *CeremonyDataClockConsensusEngine) GetMostAheadPeer(
frameNumber uint64,
) (
2023-09-29 07:55:09 +00:00
[]byte,
uint64,
error,
) {
2024-02-20 07:59:03 +00:00
e.logger.Info(
"checking peer list",
zap.Int("peers", len(e.peerMap)),
zap.Int("uncooperative_peers", len(e.uncooperativePeersMap)),
2024-03-24 08:11:00 +00:00
zap.Uint64("current_head_frame", frameNumber),
2024-02-20 07:59:03 +00:00
)
2024-03-24 08:11:00 +00:00
max := frameNumber
2023-09-29 07:55:09 +00:00
var peer []byte = nil
e.peerMapMx.RLock()
2023-09-29 07:55:09 +00:00
for _, v := range e.peerMap {
e.logger.Debug(
"checking peer info",
zap.Binary("peer_id", v.peerId),
zap.Uint64("max_frame_number", v.maxFrame),
zap.Int64("timestamp", v.timestamp),
zap.Binary("version", v.version),
)
2024-01-10 06:58:38 +00:00
_, ok := e.uncooperativePeersMap[string(v.peerId)]
2024-01-15 03:33:25 +00:00
if v.maxFrame > max &&
v.timestamp > config.GetMinimumVersionCutoff().UnixMilli() &&
bytes.Compare(v.version, config.GetMinimumVersion()) >= 0 && !ok {
manifest := e.peerInfoManager.GetPeerInfo(v.peerId)
if manifest == nil || manifest.Bandwidth == 0 {
e.logger.Debug("peer manifest not found or bandwidth zero")
continue
}
2023-09-29 07:55:09 +00:00
peer = v.peerId
max = v.maxFrame
}
}
e.peerMapMx.RUnlock()
2023-09-29 07:55:09 +00:00
if peer == nil {
2024-01-10 06:58:38 +00:00
return nil, 0, p2p.ErrNoPeersAvailable
2023-09-29 07:55:09 +00:00
}
return peer, max, nil
}
2024-01-03 07:31:42 +00:00
func (e *CeremonyDataClockConsensusEngine) sync(
currentLatest *protobufs.ClockFrame,
maxFrame uint64,
peerId []byte,
2023-09-25 02:43:35 +00:00
) (*protobufs.ClockFrame, error) {
latest := currentLatest
2024-01-03 07:31:42 +00:00
e.logger.Info("polling peer for new frames", zap.Binary("peer_id", peerId))
2024-03-01 07:12:31 +00:00
cc, err := e.pubSub.GetDirectChannel(peerId, "")
if err != nil {
e.logger.Debug(
"could not establish direct channel",
zap.Error(err),
)
e.peerMapMx.Lock()
if _, ok := e.peerMap[string(peerId)]; ok {
e.uncooperativePeersMap[string(peerId)] = e.peerMap[string(peerId)]
2024-01-10 06:58:38 +00:00
e.uncooperativePeersMap[string(peerId)].timestamp = time.Now().UnixMilli()
delete(e.peerMap, string(peerId))
}
e.peerMapMx.Unlock()
2024-01-10 06:58:38 +00:00
return latest, errors.Wrap(err, "sync")
}
2023-09-25 02:43:35 +00:00
client := protobufs.NewCeremonyServiceClient(cc)
2023-09-25 02:43:35 +00:00
response, err := client.GetDataFrame(
context.TODO(),
&protobufs.GetDataFrameRequest{
FrameNumber: 0,
},
2024-01-03 07:31:42 +00:00
grpc.MaxCallRecvMsgSize(600*1024*1024),
)
2023-10-26 20:54:49 +00:00
if err != nil {
e.logger.Debug(
"could not get frame",
2023-10-26 20:54:49 +00:00
zap.Error(err),
)
e.peerMapMx.Lock()
if _, ok := e.peerMap[string(peerId)]; ok {
e.uncooperativePeersMap[string(peerId)] = e.peerMap[string(peerId)]
2024-01-10 06:58:38 +00:00
e.uncooperativePeersMap[string(peerId)].timestamp = time.Now().UnixMilli()
delete(e.peerMap, string(peerId))
}
2023-10-26 20:54:49 +00:00
e.peerMapMx.Unlock()
if err := cc.Close(); err != nil {
e.logger.Error("error while closing connection", zap.Error(err))
}
2024-02-28 09:00:20 +00:00
return latest, errors.Wrap(err, "sync")
}
if response == nil {
e.logger.Debug("received no response from peer")
2023-10-26 20:54:49 +00:00
if err := cc.Close(); err != nil {
e.logger.Error("error while closing connection", zap.Error(err))
}
return latest, nil
2024-01-03 07:31:42 +00:00
}
e.logger.Info(
"received new leading frame",
zap.Uint64("frame_number", response.ClockFrame.FrameNumber),
2024-01-03 07:31:42 +00:00
)
if err := cc.Close(); err != nil {
e.logger.Error("error while closing connection", zap.Error(err))
2023-10-26 20:54:49 +00:00
}
e.dataTimeReel.Insert(response.ClockFrame, false)
2024-03-17 21:14:37 +00:00
return response.ClockFrame, nil
2023-10-26 20:54:49 +00:00
}
func (e *CeremonyDataClockConsensusEngine) collect(
currentFramePublished *protobufs.ClockFrame,
) (*protobufs.ClockFrame, error) {
2024-02-13 07:04:56 +00:00
e.logger.Info("collecting vdf proofs")
2023-09-25 02:43:35 +00:00
2024-02-13 07:04:56 +00:00
latest := currentFramePublished
2023-09-25 02:43:35 +00:00
for {
2024-03-24 08:11:00 +00:00
peerId, maxFrame, err := e.GetMostAheadPeer(latest.FrameNumber)
if maxFrame > latest.FrameNumber {
e.syncingStatus = SyncStatusSynchronizing
if err != nil {
e.logger.Info("no peers available for sync, waiting")
time.Sleep(5 * time.Second)
} else if maxFrame-2 > latest.FrameNumber {
latest, err = e.sync(latest, maxFrame, peerId)
if err != nil {
time.Sleep(30 * time.Second)
} else {
break
}
2024-01-03 07:31:42 +00:00
}
} else {
break
2024-01-03 07:31:42 +00:00
}
2023-09-25 02:43:35 +00:00
}
e.syncingStatus = SyncStatusNotSyncing
2024-02-16 21:46:54 +00:00
if latest.FrameNumber < currentFramePublished.FrameNumber {
latest = currentFramePublished
}
2024-02-13 07:04:56 +00:00
e.logger.Info(
"returning leader frame",
zap.Uint64("frame_number", latest.FrameNumber),
)
return latest, nil
2023-09-25 02:43:35 +00:00
}