mirror of
https://source.quilibrium.com/quilibrium/ceremonyclient.git
synced 2024-11-10 18:25:17 +00:00
196 lines
4.8 KiB
Go
196 lines
4.8 KiB
Go
package master
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"go.uber.org/zap"
|
|
"source.quilibrium.com/quilibrium/monorepo/node/config"
|
|
"source.quilibrium.com/quilibrium/monorepo/node/consensus"
|
|
"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"
|
|
)
|
|
|
|
type SyncStatusType int
|
|
|
|
const (
|
|
SyncStatusNotSyncing = iota
|
|
SyncStatusAwaitingResponse
|
|
SyncStatusSynchronizing
|
|
)
|
|
|
|
type MasterClockConsensusEngine struct {
|
|
frame uint64
|
|
difficulty uint32
|
|
logger *zap.Logger
|
|
state consensus.EngineState
|
|
pubSub p2p.PubSub
|
|
keyManager keys.KeyManager
|
|
lastFrameReceivedAt time.Time
|
|
latestFrame *protobufs.ClockFrame
|
|
|
|
frameChan chan uint64
|
|
executionEngines map[string]execution.ExecutionEngine
|
|
filter []byte
|
|
input []byte
|
|
syncingStatus SyncStatusType
|
|
syncingTarget []byte
|
|
engineMx sync.Mutex
|
|
seenFramesMx sync.Mutex
|
|
historicFramesMx sync.Mutex
|
|
// for DHT testing, we're only using in memory stores
|
|
seenFrames []*protobufs.ClockFrame
|
|
historicFrames []*protobufs.ClockFrame
|
|
}
|
|
|
|
var _ consensus.ConsensusEngine = (*MasterClockConsensusEngine)(nil)
|
|
|
|
func NewMasterClockConsensusEngine(
|
|
engineConfig *config.EngineConfig,
|
|
logger *zap.Logger,
|
|
keyManager keys.KeyManager,
|
|
pubSub p2p.PubSub,
|
|
) *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"))
|
|
}
|
|
|
|
seed, err := hex.DecodeString(engineConfig.GenesisSeed)
|
|
if err != nil {
|
|
panic(errors.New("genesis seed is nil"))
|
|
}
|
|
|
|
e := &MasterClockConsensusEngine{
|
|
frame: 0,
|
|
difficulty: 10000,
|
|
logger: logger,
|
|
state: consensus.EngineStateStopped,
|
|
keyManager: keyManager,
|
|
pubSub: pubSub,
|
|
frameChan: make(chan uint64),
|
|
executionEngines: map[string]execution.ExecutionEngine{},
|
|
input: seed,
|
|
lastFrameReceivedAt: time.Time{},
|
|
syncingStatus: SyncStatusNotSyncing,
|
|
}
|
|
|
|
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 {
|
|
e.logger.Info("starting consensus engine")
|
|
e.state = consensus.EngineStateStarting
|
|
errChan := make(chan error)
|
|
|
|
e.state = consensus.EngineStateLoading
|
|
e.logger.Info("syncing last seen state")
|
|
|
|
latestFrame := e.createGenesisFrame()
|
|
e.historicFrames = []*protobufs.ClockFrame{latestFrame}
|
|
|
|
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
|
|
|
|
go func() {
|
|
for e.state < consensus.EngineStateStopping {
|
|
var err error
|
|
switch e.state {
|
|
case consensus.EngineStateCollecting:
|
|
if latestFrame, err = e.collect(latestFrame); err != nil {
|
|
e.logger.Error("could not collect", zap.Error(err))
|
|
errChan <- err
|
|
}
|
|
case consensus.EngineStateProving:
|
|
if latestFrame, err = e.prove(latestFrame); err != nil {
|
|
e.logger.Error("could not prove", zap.Error(err))
|
|
errChan <- err
|
|
}
|
|
case consensus.EngineStatePublishing:
|
|
if err = e.publishProof(latestFrame); err != nil {
|
|
e.logger.Error("could not publish", zap.Error(err))
|
|
errChan <- err
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
errChan <- nil
|
|
}()
|
|
|
|
return errChan
|
|
}
|
|
|
|
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) {
|
|
err := <-e.UnregisterExecutor(name, e.frame, force)
|
|
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")
|
|
|
|
e.state = consensus.EngineStateStopped
|
|
|
|
e.engineMx.Lock()
|
|
defer e.engineMx.Unlock()
|
|
go func() {
|
|
errChan <- nil
|
|
}()
|
|
return errChan
|
|
}
|
|
|
|
func (e *MasterClockConsensusEngine) GetDifficulty() uint32 {
|
|
return e.difficulty
|
|
}
|
|
|
|
func (e *MasterClockConsensusEngine) GetFrame() uint64 {
|
|
return e.frame
|
|
}
|
|
|
|
func (e *MasterClockConsensusEngine) GetState() consensus.EngineState {
|
|
return e.state
|
|
}
|
|
|
|
func (e *MasterClockConsensusEngine) GetFrameChannel() <-chan uint64 {
|
|
return e.frameChan
|
|
}
|