ceremonyclient/node/consensus/master/master_clock_consensus_engine.go

391 lines
9.6 KiB
Go
Raw Normal View History

2023-09-03 23:47:09 +00:00
package master
import (
2024-03-01 07:12:31 +00:00
"context"
2023-09-03 23:47:09 +00:00
"encoding/hex"
2024-03-04 03:20:24 +00:00
"math/big"
2023-09-03 23:47:09 +00:00
"sync"
"time"
"github.com/pkg/errors"
"go.uber.org/zap"
2024-03-01 07:12:31 +00:00
"google.golang.org/grpc"
2023-09-03 23:47:09 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/config"
"source.quilibrium.com/quilibrium/monorepo/node/consensus"
2024-02-13 07:04:56 +00:00
qtime "source.quilibrium.com/quilibrium/monorepo/node/consensus/time"
"source.quilibrium.com/quilibrium/monorepo/node/crypto"
2023-09-03 23:47:09 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/execution"
"source.quilibrium.com/quilibrium/monorepo/node/keys"
"source.quilibrium.com/quilibrium/monorepo/node/p2p"
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
2023-09-09 23:45:47 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/store"
2023-09-03 23:47:09 +00:00
)
type SyncStatusType int
const (
SyncStatusNotSyncing = iota
SyncStatusAwaitingResponse
SyncStatusSynchronizing
)
type MasterClockConsensusEngine struct {
2024-03-01 07:12:31 +00:00
*protobufs.UnimplementedValidationServiceServer
2023-09-03 23:47:09 +00:00
difficulty uint32
logger *zap.Logger
state consensus.EngineState
pubSub p2p.PubSub
keyManager keys.KeyManager
2024-02-13 07:04:56 +00:00
frameProver crypto.FrameProver
2023-09-03 23:47:09 +00:00
lastFrameReceivedAt time.Time
2024-01-03 07:31:42 +00:00
frameChan chan *protobufs.ClockFrame
2023-09-03 23:47:09 +00:00
executionEngines map[string]execution.ExecutionEngine
filter []byte
input []byte
syncingStatus SyncStatusType
syncingTarget []byte
engineMx sync.Mutex
seenFramesMx sync.Mutex
historicFramesMx sync.Mutex
2023-09-09 23:45:47 +00:00
seenFrames []*protobufs.ClockFrame
historicFrames []*protobufs.ClockFrame
clockStore store.ClockStore
2024-02-13 07:04:56 +00:00
masterTimeReel *qtime.MasterTimeReel
2024-03-01 07:12:31 +00:00
report *protobufs.SelfTestReport
peerMapMx sync.Mutex
peerMap map[string]*protobufs.SelfTestReport
2023-09-03 23:47:09 +00:00
}
var _ consensus.ConsensusEngine = (*MasterClockConsensusEngine)(nil)
func NewMasterClockConsensusEngine(
engineConfig *config.EngineConfig,
logger *zap.Logger,
2023-09-09 23:45:47 +00:00
clockStore store.ClockStore,
2023-09-03 23:47:09 +00:00
keyManager keys.KeyManager,
pubSub p2p.PubSub,
2024-02-13 07:04:56 +00:00
frameProver crypto.FrameProver,
masterTimeReel *qtime.MasterTimeReel,
2024-03-01 07:12:31 +00:00
report *protobufs.SelfTestReport,
2023-09-03 23:47:09 +00:00
) *MasterClockConsensusEngine {
if logger == nil {
panic(errors.New("logger is nil"))
}
if engineConfig == nil {
panic(errors.New("engine config is nil"))
}
if keyManager == nil {
panic(errors.New("key manager is nil"))
}
if pubSub == nil {
panic(errors.New("pubsub is nil"))
}
2024-02-13 07:04:56 +00:00
if frameProver == nil {
panic(errors.New("frame prover is nil"))
}
if masterTimeReel == nil {
panic(errors.New("master time reel is nil"))
}
2023-09-03 23:47:09 +00:00
seed, err := hex.DecodeString(engineConfig.GenesisSeed)
if err != nil {
panic(errors.New("genesis seed is nil"))
}
e := &MasterClockConsensusEngine{
difficulty: 10000,
logger: logger,
state: consensus.EngineStateStopped,
keyManager: keyManager,
pubSub: pubSub,
executionEngines: map[string]execution.ExecutionEngine{},
2024-01-03 07:31:42 +00:00
frameChan: make(chan *protobufs.ClockFrame),
2023-09-03 23:47:09 +00:00
input: seed,
lastFrameReceivedAt: time.Time{},
syncingStatus: SyncStatusNotSyncing,
2023-09-09 23:45:47 +00:00
clockStore: clockStore,
2024-02-13 07:04:56 +00:00
frameProver: frameProver,
masterTimeReel: masterTimeReel,
2024-03-01 07:12:31 +00:00
report: report,
peerMap: map[string]*protobufs.SelfTestReport{},
2024-01-03 07:31:42 +00:00
}
2024-03-01 07:12:31 +00:00
e.peerMap[string(e.pubSub.GetPeerID())] = report
2023-09-03 23:47:09 +00:00
if e.filter, err = hex.DecodeString(engineConfig.Filter); err != nil {
panic(errors.Wrap(err, "could not parse filter value"))
}
logger.Info("constructing consensus engine")
return e
}
func (e *MasterClockConsensusEngine) Start() <-chan error {
2024-01-03 07:31:42 +00:00
e.logger.Info("starting master consensus engine")
2023-09-03 23:47:09 +00:00
e.state = consensus.EngineStateStarting
errChan := make(chan error)
e.state = consensus.EngineStateLoading
e.logger.Info("syncing last seen state")
2024-02-13 07:04:56 +00:00
err := e.masterTimeReel.Start()
if err != nil {
panic(err)
}
2023-09-09 23:45:47 +00:00
2024-02-13 07:04:56 +00:00
frame, err := e.masterTimeReel.Head()
if err != nil {
2023-09-09 23:45:47 +00:00
panic(err)
}
2024-02-13 07:04:56 +00:00
e.buildHistoricFrameCache(frame)
2023-09-03 23:47:09 +00:00
e.logger.Info("subscribing to pubsub messages")
e.pubSub.Subscribe(e.filter, e.handleMessage, true)
e.pubSub.Subscribe(e.pubSub.GetPeerID(), e.handleSync, true)
e.state = consensus.EngineStateCollecting
2024-03-01 07:12:31 +00:00
go func() {
server := grpc.NewServer(
grpc.MaxSendMsgSize(600*1024*1024),
grpc.MaxRecvMsgSize(600*1024*1024),
)
protobufs.RegisterValidationServiceServer(server, e)
if err := e.pubSub.StartDirectChannelListener(
e.pubSub.GetPeerID(),
"validation",
server,
); err != nil {
panic(err)
}
}()
2023-09-10 23:29:17 +00:00
go func() {
for {
e.logger.Info(
"peers in store",
zap.Int("peer_store_count", e.pubSub.GetPeerstoreCount()),
zap.Int("network_peer_count", e.pubSub.GetNetworkPeersCount()),
)
time.Sleep(10 * time.Second)
}
}()
2024-03-01 07:12:31 +00:00
go func() {
for {
time.Sleep(30 * time.Second)
e.logger.Info("broadcasting self-test info")
if err := e.publishMessage(e.filter, e.report); err != nil {
e.logger.Debug("error publishing message", zap.Error(err))
}
}
}()
2023-09-03 23:47:09 +00:00
go func() {
2024-02-13 07:04:56 +00:00
newFrameCh := e.masterTimeReel.NewFrameCh()
2023-09-03 23:47:09 +00:00
for e.state < consensus.EngineStateStopping {
var err error
2024-02-13 07:04:56 +00:00
select {
case frame := <-newFrameCh:
currentFrame := frame
latestFrame := frame
if latestFrame, err = e.collect(currentFrame); err != nil {
2023-09-03 23:47:09 +00:00
e.logger.Error("could not collect", zap.Error(err))
2024-01-03 07:31:42 +00:00
latestFrame = currentFrame
2024-02-13 07:04:56 +00:00
continue
2023-09-03 23:47:09 +00:00
}
if latestFrame, err = e.prove(latestFrame); err != nil {
e.logger.Error("could not prove", zap.Error(err))
2024-01-03 07:31:42 +00:00
latestFrame = currentFrame
2023-09-03 23:47:09 +00:00
}
if err = e.publishProof(latestFrame); err != nil {
e.logger.Error("could not publish", zap.Error(err))
}
2024-02-13 07:04:56 +00:00
case <-time.After(20 * time.Second):
frame, err := e.masterTimeReel.Head()
if err != nil {
panic(err)
}
if frame, err = e.prove(frame); err != nil {
e.logger.Error("could not prove", zap.Error(err))
continue
}
if err = e.publishProof(frame); err != nil {
e.logger.Error("could not publish", zap.Error(err))
}
2023-09-03 23:47:09 +00:00
}
}
}()
go func() {
errChan <- nil
}()
return errChan
}
2024-03-01 07:12:31 +00:00
func (e *MasterClockConsensusEngine) PerformValidation(
ctx context.Context,
msg *protobufs.ValidationMessage,
) (*protobufs.ValidationMessage, error) {
return msg, nil
}
2023-09-03 23:47:09 +00:00
func (e *MasterClockConsensusEngine) Stop(force bool) <-chan error {
e.logger.Info("stopping consensus engine")
e.state = consensus.EngineStateStopping
errChan := make(chan error)
wg := sync.WaitGroup{}
wg.Add(len(e.executionEngines))
for name := range e.executionEngines {
name := name
go func(name string) {
2024-02-13 07:04:56 +00:00
frame, err := e.masterTimeReel.Head()
if err != nil {
errChan <- err
return
}
err = <-e.UnregisterExecutor(name, frame.FrameNumber, force)
2023-09-03 23:47:09 +00:00
if err != nil {
errChan <- err
}
wg.Done()
}(name)
}
e.logger.Info("waiting for execution engines to stop")
wg.Wait()
e.logger.Info("execution engines stopped")
2024-02-13 07:04:56 +00:00
e.masterTimeReel.Stop()
2023-09-03 23:47:09 +00:00
e.state = consensus.EngineStateStopped
go func() {
errChan <- nil
}()
return errChan
}
2024-03-04 03:20:24 +00:00
func (
e *MasterClockConsensusEngine,
) GetPeerManifests() *protobufs.PeerManifestsResponse {
response := &protobufs.PeerManifestsResponse{
PeerManifests: []*protobufs.PeerManifest{},
}
e.peerMapMx.Lock()
for peerId, peerManifest := range e.peerMap {
peerId := peerId
peerManifest := peerManifest
manifest := &protobufs.PeerManifest{
PeerId: []byte(peerId),
Difficulty: peerManifest.Difficulty,
DifficultyMetric: peerManifest.DifficultyMetric,
Commit_16Metric: peerManifest.Commit_16Metric,
Commit_128Metric: peerManifest.Commit_128Metric,
Commit_1024Metric: peerManifest.Commit_1024Metric,
Commit_65536Metric: peerManifest.Commit_65536Metric,
Proof_16Metric: peerManifest.Proof_16Metric,
Proof_128Metric: peerManifest.Proof_128Metric,
Proof_1024Metric: peerManifest.Proof_1024Metric,
Proof_65536Metric: peerManifest.Proof_65536Metric,
Cores: peerManifest.Cores,
Memory: new(big.Int).SetBytes(peerManifest.Memory).Bytes(),
Storage: new(big.Int).SetBytes(peerManifest.Storage).Bytes(),
}
for _, capability := range peerManifest.Capabilities {
metadata := make([]byte, len(capability.AdditionalMetadata))
copy(metadata[:], capability.AdditionalMetadata[:])
manifest.Capabilities = append(
manifest.Capabilities,
&protobufs.Capability{
ProtocolIdentifier: capability.ProtocolIdentifier,
AdditionalMetadata: metadata,
},
)
}
response.PeerManifests = append(
response.PeerManifests,
manifest,
)
}
e.peerMapMx.Unlock()
return response
}
2023-09-03 23:47:09 +00:00
func (e *MasterClockConsensusEngine) GetDifficulty() uint32 {
return e.difficulty
}
2024-01-03 07:31:42 +00:00
func (e *MasterClockConsensusEngine) GetFrame() *protobufs.ClockFrame {
2024-02-13 07:04:56 +00:00
frame, err := e.masterTimeReel.Head()
if err != nil {
panic(err)
}
return frame
2023-09-03 23:47:09 +00:00
}
func (e *MasterClockConsensusEngine) GetState() consensus.EngineState {
return e.state
}
2024-01-03 07:31:42 +00:00
func (
e *MasterClockConsensusEngine,
) GetFrameChannel() <-chan *protobufs.ClockFrame {
2023-09-03 23:47:09 +00:00
return e.frameChan
}
2024-01-03 07:31:42 +00:00
func (e *MasterClockConsensusEngine) buildHistoricFrameCache(
latestFrame *protobufs.ClockFrame,
) {
e.historicFrames = []*protobufs.ClockFrame{}
if latestFrame.FrameNumber != 0 {
min := uint64(0)
if latestFrame.FrameNumber-255 > min && latestFrame.FrameNumber > 255 {
min = latestFrame.FrameNumber - 255
}
iter, err := e.clockStore.RangeMasterClockFrames(
e.filter,
min,
latestFrame.FrameNumber-1,
)
if err != nil {
panic(err)
}
for iter.First(); iter.Valid(); iter.Next() {
frame, err := iter.Value()
if err != nil {
panic(err)
}
e.historicFrames = append(e.historicFrames, frame)
}
if err = iter.Close(); err != nil {
panic(err)
}
}
e.historicFrames = append(e.historicFrames, latestFrame)
}