2023-09-03 23:47:09 +00:00
|
|
|
package master
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/execution"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e *MasterClockConsensusEngine) RegisterExecutor(
|
|
|
|
exec execution.ExecutionEngine,
|
|
|
|
frame uint64,
|
|
|
|
) <-chan error {
|
|
|
|
logger := e.logger.With(zap.String("execution_engine_name", exec.GetName()))
|
|
|
|
logger.Info("registering execution engine")
|
|
|
|
errChan := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
logger.Info(
|
|
|
|
"starting execution engine at frame",
|
2024-01-03 07:31:42 +00:00
|
|
|
zap.Uint64("current_frame", e.frame.FrameNumber),
|
2023-09-03 23:47:09 +00:00
|
|
|
)
|
|
|
|
err := <-exec.Start()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("could not start execution engine", zap.Error(err))
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
logger.Info(
|
|
|
|
"awaiting frame",
|
2024-01-03 07:31:42 +00:00
|
|
|
zap.Uint64("current_frame", e.frame.FrameNumber),
|
2023-09-03 23:47:09 +00:00
|
|
|
zap.Uint64("target_frame", frame),
|
|
|
|
)
|
|
|
|
|
2024-01-03 07:31:42 +00:00
|
|
|
newFrame := e.frame.FrameNumber
|
2023-09-03 23:47:09 +00:00
|
|
|
if newFrame >= frame {
|
|
|
|
logger.Info(
|
|
|
|
"injecting execution engine at frame",
|
|
|
|
zap.Uint64("current_frame", newFrame),
|
|
|
|
)
|
|
|
|
|
|
|
|
e.engineMx.Lock()
|
|
|
|
e.executionEngines[exec.GetName()] = exec
|
|
|
|
e.engineMx.Unlock()
|
|
|
|
|
|
|
|
errChan <- nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return errChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MasterClockConsensusEngine) UnregisterExecutor(
|
|
|
|
name string,
|
|
|
|
frame uint64,
|
|
|
|
force bool,
|
|
|
|
) <-chan error {
|
|
|
|
logger := e.logger.With(zap.String("execution_engine_name", name))
|
|
|
|
logger.Info("unregistering execution engine")
|
|
|
|
errChan := make(chan error)
|
|
|
|
|
|
|
|
exec, ok := e.executionEngines[name]
|
|
|
|
if !ok {
|
|
|
|
logger.Error(
|
|
|
|
"could not unregister execution engine",
|
|
|
|
zap.Error(errors.New("execution engine not registered")),
|
|
|
|
)
|
|
|
|
errChan <- errors.New("execution engine not registered")
|
|
|
|
return errChan
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
logger.Info(
|
|
|
|
"awaiting frame",
|
2024-01-03 07:31:42 +00:00
|
|
|
zap.Uint64("current_frame", e.frame.FrameNumber),
|
2023-09-03 23:47:09 +00:00
|
|
|
zap.Uint64("target_frame", frame),
|
|
|
|
)
|
|
|
|
|
2024-01-03 07:31:42 +00:00
|
|
|
newFrame := e.frame.FrameNumber
|
2023-09-03 23:47:09 +00:00
|
|
|
if newFrame >= frame {
|
|
|
|
logger.Info(
|
|
|
|
"removing execution engine at frame",
|
|
|
|
zap.Uint64("current_frame", newFrame),
|
|
|
|
)
|
|
|
|
e.engineMx.Lock()
|
|
|
|
delete(e.executionEngines, name)
|
|
|
|
e.engineMx.Unlock()
|
|
|
|
|
|
|
|
logger.Info(
|
|
|
|
"stopping execution engine at frame",
|
|
|
|
zap.Uint64("current_frame", newFrame),
|
|
|
|
)
|
|
|
|
err := <-exec.Stop(force)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("could not stop execution engine", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
errChan <- err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return errChan
|
|
|
|
}
|