2020-07-24 13:54:03 +00:00
|
|
|
package innerring
|
|
|
|
|
2020-11-13 11:45:43 +00:00
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2021-09-06 13:01:50 +00:00
|
|
|
"fmt"
|
2021-03-25 13:24:44 +00:00
|
|
|
"sort"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/governance"
|
|
|
|
control "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir"
|
2023-12-05 14:05:34 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/sdnotify"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/state"
|
2021-02-21 06:53:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-09-06 13:01:50 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-12-04 11:27:58 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-13 11:45:43 +00:00
|
|
|
)
|
|
|
|
|
2021-05-21 07:45:15 +00:00
|
|
|
const voteMethod = "vote"
|
|
|
|
|
2021-09-06 13:01:50 +00:00
|
|
|
var (
|
|
|
|
persistateMainChainLastBlockKey = []byte("main_chain_last_processed_block")
|
|
|
|
persistateSideChainLastBlockKey = []byte("side_chain_last_processed_block")
|
|
|
|
)
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// EpochCounter is a getter for a global epoch counter.
|
|
|
|
func (s *Server) EpochCounter() uint64 {
|
|
|
|
return s.epochCounter.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEpochCounter is a setter for contract processors to update global
|
|
|
|
// epoch counter.
|
|
|
|
func (s *Server) SetEpochCounter(val uint64) {
|
|
|
|
s.epochCounter.Store(val)
|
2023-06-08 13:37:46 +00:00
|
|
|
if s.irMetrics != nil {
|
|
|
|
s.irMetrics.SetEpoch(val)
|
2021-12-27 12:24:05 +00:00
|
|
|
}
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 17:09:52 +00:00
|
|
|
// EpochDuration is a getter for a global epoch duration.
|
|
|
|
func (s *Server) EpochDuration() uint64 {
|
|
|
|
return s.epochDuration.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEpochDuration is a setter for the Netmap processor to update global
|
|
|
|
// epoch duration.
|
|
|
|
func (s *Server) SetEpochDuration(val uint64) {
|
|
|
|
s.epochDuration.Store(val)
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// IsActive is a getter for a global active flag state.
|
2024-10-21 09:21:01 +00:00
|
|
|
func (s *Server) IsActive(ctx context.Context) bool {
|
|
|
|
return s.InnerRingIndex(ctx) >= 0
|
2020-10-12 09:13:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 14:58:25 +00:00
|
|
|
// IsAlphabet is a getter for a global alphabet flag state.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (s *Server) IsAlphabet(ctx context.Context) bool {
|
|
|
|
return s.AlphabetIndex(ctx) >= 0
|
2021-03-23 14:58:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 14:14:28 +00:00
|
|
|
// InnerRingIndex is a getter for a global index of node in inner ring list. Negative
|
2020-10-12 09:13:19 +00:00
|
|
|
// index means that node is not in the inner ring list.
|
2024-10-21 09:21:01 +00:00
|
|
|
func (s *Server) InnerRingIndex(ctx context.Context) int {
|
2021-03-23 12:44:09 +00:00
|
|
|
index, err := s.statusIndex.InnerRingIndex()
|
|
|
|
if err != nil {
|
2024-10-21 09:21:01 +00:00
|
|
|
s.log.Error(ctx, logs.InnerringCantGetInnerRingIndex, zap.String("error", err.Error()))
|
2021-03-23 12:44:09 +00:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(index)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
2020-11-13 11:45:43 +00:00
|
|
|
|
2020-12-18 12:52:27 +00:00
|
|
|
// InnerRingSize is a getter for a global size of inner ring list. This value
|
|
|
|
// paired with inner ring index.
|
2024-10-21 09:21:01 +00:00
|
|
|
func (s *Server) InnerRingSize(ctx context.Context) int {
|
2021-03-23 12:44:09 +00:00
|
|
|
size, err := s.statusIndex.InnerRingSize()
|
|
|
|
if err != nil {
|
2024-10-21 09:21:01 +00:00
|
|
|
s.log.Error(ctx, logs.InnerringCantGetInnerRingSize, zap.String("error", err.Error()))
|
2021-03-23 12:44:09 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(size)
|
2020-12-18 12:52:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 14:58:25 +00:00
|
|
|
// AlphabetIndex is a getter for a global index of node in alphabet list.
|
|
|
|
// Negative index means that node is not in the alphabet list.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (s *Server) AlphabetIndex(ctx context.Context) int {
|
2021-03-23 14:58:25 +00:00
|
|
|
index, err := s.statusIndex.AlphabetIndex()
|
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
s.log.Error(ctx, logs.InnerringCantGetAlphabetIndex, zap.String("error", err.Error()))
|
2021-03-23 14:58:25 +00:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(index)
|
|
|
|
}
|
|
|
|
|
2024-10-21 09:21:01 +00:00
|
|
|
func (s *Server) voteForSidechainValidator(ctx context.Context, prm governance.VoteValidatorPrm) error {
|
2021-11-10 11:05:51 +00:00
|
|
|
validators := prm.Validators
|
|
|
|
|
2024-10-21 09:21:01 +00:00
|
|
|
index := s.InnerRingIndex(ctx)
|
2021-02-21 06:53:18 +00:00
|
|
|
if s.contracts.alphabet.indexOutOfRange(index) {
|
2024-10-21 09:21:01 +00:00
|
|
|
s.log.Info(ctx, logs.InnerringIgnoreValidatorVoteNodeNotInAlphabetRange)
|
2020-11-13 11:45:43 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(validators) == 0 {
|
2024-10-21 09:21:01 +00:00
|
|
|
s.log.Info(ctx, logs.InnerringIgnoreValidatorVoteEmptyValidatorsList)
|
2020-11-13 11:45:43 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:27:58 +00:00
|
|
|
epoch := s.EpochCounter()
|
2020-11-13 11:45:43 +00:00
|
|
|
|
2021-11-10 11:05:51 +00:00
|
|
|
var (
|
|
|
|
nonce uint32 = 1
|
|
|
|
vub uint32
|
2021-11-25 14:52:10 +00:00
|
|
|
vubP *uint32
|
2021-11-10 11:05:51 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if prm.Hash != nil {
|
2023-11-08 09:28:34 +00:00
|
|
|
nonce, vub, err = s.morphClient.CalculateNonceAndVUB(prm.Hash)
|
2021-11-10 11:05:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not calculate nonce and `validUntilBlock` values: %w", err)
|
|
|
|
}
|
2021-11-25 14:52:10 +00:00
|
|
|
vubP = &vub
|
2021-11-10 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 11:27:07 +00:00
|
|
|
s.contracts.alphabet.iterate(func(letter GlagoliticLetter, contract util.Uint160) {
|
2024-10-21 13:27:28 +00:00
|
|
|
_, err := s.morphClient.NotaryInvoke(ctx, contract, s.feeConfig.SideChainFee(), nonce, vubP, voteMethod, epoch, validators)
|
2020-12-04 11:27:58 +00:00
|
|
|
if err != nil {
|
2024-10-21 09:21:01 +00:00
|
|
|
s.log.Warn(ctx, logs.InnerringCantInvokeVoteMethodInAlphabetContract,
|
2021-02-21 06:53:18 +00:00
|
|
|
zap.Int8("alphabet_index", int8(letter)),
|
2021-09-29 16:54:49 +00:00
|
|
|
zap.Uint64("epoch", epoch),
|
|
|
|
zap.String("error", err.Error()))
|
2020-12-04 11:27:58 +00:00
|
|
|
}
|
2021-02-21 06:53:18 +00:00
|
|
|
})
|
2020-12-04 11:27:58 +00:00
|
|
|
|
|
|
|
return nil
|
2020-11-13 11:45:43 +00:00
|
|
|
}
|
2020-11-13 12:19:33 +00:00
|
|
|
|
2021-03-25 13:24:44 +00:00
|
|
|
// VoteForSidechainValidator calls vote method on alphabet contracts with
|
2022-04-21 11:28:05 +00:00
|
|
|
// the provided list of keys.
|
2024-10-21 09:21:01 +00:00
|
|
|
func (s *Server) VoteForSidechainValidator(ctx context.Context, prm governance.VoteValidatorPrm) error {
|
2021-11-10 11:05:51 +00:00
|
|
|
sort.Sort(prm.Validators)
|
2024-10-21 09:21:01 +00:00
|
|
|
return s.voteForSidechainValidator(ctx, prm)
|
2020-11-13 12:19:33 +00:00
|
|
|
}
|
2020-12-22 11:45:33 +00:00
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ResetEpochTimer resets the block timer that produces events to update epoch
|
|
|
|
// counter in the netmap contract. It is used to synchronize this even production
|
|
|
|
// based on the block with a notification of the last epoch.
|
2022-04-04 07:07:33 +00:00
|
|
|
func (s *Server) ResetEpochTimer(h uint32) error {
|
|
|
|
s.epochTimer.Tick(h)
|
2021-01-29 07:42:40 +00:00
|
|
|
return s.epochTimer.Reset()
|
|
|
|
}
|
2021-06-09 15:41:58 +00:00
|
|
|
|
2024-10-21 08:30:31 +00:00
|
|
|
func (s *Server) setHealthStatus(ctx context.Context, hs control.HealthStatus) {
|
2023-05-19 15:06:20 +00:00
|
|
|
s.healthStatus.Store(int32(hs))
|
2024-10-21 08:30:31 +00:00
|
|
|
s.notifySystemd(ctx, hs)
|
2023-06-08 13:37:46 +00:00
|
|
|
if s.irMetrics != nil {
|
|
|
|
s.irMetrics.SetHealth(int32(hs))
|
2023-03-13 06:21:28 +00:00
|
|
|
}
|
2021-06-09 15:41:58 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 08:30:31 +00:00
|
|
|
func (s *Server) CompareAndSwapHealthStatus(ctx context.Context, oldSt, newSt control.HealthStatus) (swapped bool) {
|
2024-08-14 10:47:50 +00:00
|
|
|
if swapped = s.healthStatus.CompareAndSwap(int32(oldSt), int32(newSt)); swapped {
|
2024-10-21 08:30:31 +00:00
|
|
|
s.notifySystemd(ctx, newSt)
|
2024-08-14 10:47:50 +00:00
|
|
|
if s.irMetrics != nil {
|
|
|
|
s.irMetrics.SetHealth(int32(newSt))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// HealthStatus returns the current health status of the IR application.
|
2021-06-09 15:41:58 +00:00
|
|
|
func (s *Server) HealthStatus() control.HealthStatus {
|
2023-05-19 15:06:20 +00:00
|
|
|
return control.HealthStatus(s.healthStatus.Load())
|
2021-06-09 15:41:58 +00:00
|
|
|
}
|
2021-09-06 13:01:50 +00:00
|
|
|
|
|
|
|
func initPersistentStateStorage(cfg *viper.Viper) (*state.PersistentStorage, error) {
|
|
|
|
persistPath := cfg.GetString("node.persistent_state.path")
|
|
|
|
persistStorage, err := state.NewPersistentStorage(persistPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("persistent state init error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return persistStorage, nil
|
|
|
|
}
|
2023-12-05 14:05:34 +00:00
|
|
|
|
2024-10-21 08:30:31 +00:00
|
|
|
func (s *Server) notifySystemd(ctx context.Context, st control.HealthStatus) {
|
2023-12-05 14:05:34 +00:00
|
|
|
if !s.sdNotify {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
switch st {
|
|
|
|
case control.HealthStatus_READY:
|
|
|
|
err = sdnotify.FlagAndStatus(sdnotify.ReadyEnabled)
|
|
|
|
case control.HealthStatus_SHUTTING_DOWN:
|
|
|
|
err = sdnotify.FlagAndStatus(sdnotify.StoppingEnabled)
|
2024-08-14 10:47:50 +00:00
|
|
|
case control.HealthStatus_RECONFIGURING:
|
|
|
|
err = sdnotify.FlagAndStatus(sdnotify.ReloadingEnabled)
|
2023-12-05 14:05:34 +00:00
|
|
|
default:
|
|
|
|
err = sdnotify.Status(fmt.Sprintf("%v", st))
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-10-21 08:30:31 +00:00
|
|
|
s.log.Error(ctx, logs.FailedToReportStatusToSystemd, zap.Error(err))
|
2023-12-05 14:05:34 +00:00
|
|
|
}
|
|
|
|
}
|