2020-07-24 13:54:03 +00:00
|
|
|
package innerring
|
|
|
|
|
2020-11-13 11:45:43 +00:00
|
|
|
import (
|
2021-09-06 13:01:50 +00:00
|
|
|
"fmt"
|
2021-03-25 13:24:44 +00:00
|
|
|
"sort"
|
|
|
|
|
2020-11-13 11:45:43 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-02-21 06:53:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-12-22 11:45:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/audit"
|
2021-06-09 15:41:58 +00:00
|
|
|
control "github.com/nspcc-dev/neofs-node/pkg/services/control/ir"
|
2021-09-06 13:01:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/state"
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsActive is a getter for a global active flag state.
|
|
|
|
func (s *Server) IsActive() bool {
|
2021-03-23 14:14:28 +00:00
|
|
|
return s.InnerRingIndex() >= 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.
|
|
|
|
func (s *Server) IsAlphabet() bool {
|
|
|
|
return s.AlphabetIndex() >= 0
|
|
|
|
}
|
|
|
|
|
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.
|
2021-03-23 14:14:28 +00:00
|
|
|
func (s *Server) InnerRingIndex() int {
|
2021-03-23 12:44:09 +00:00
|
|
|
index, err := s.statusIndex.InnerRingIndex()
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("can't get inner ring index", zap.String("error", err.Error()))
|
|
|
|
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.
|
2021-02-21 06:28:53 +00:00
|
|
|
func (s *Server) InnerRingSize() int {
|
2021-03-23 12:44:09 +00:00
|
|
|
size, err := s.statusIndex.InnerRingSize()
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("can't get inner ring size", zap.String("error", err.Error()))
|
|
|
|
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.
|
|
|
|
func (s *Server) AlphabetIndex() int {
|
|
|
|
index, err := s.statusIndex.AlphabetIndex()
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("can't get alphabet index", zap.String("error", err.Error()))
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(index)
|
|
|
|
}
|
|
|
|
|
2021-03-25 07:34:27 +00:00
|
|
|
func (s *Server) voteForSidechainValidator(validators keys.PublicKeys) error {
|
2021-03-23 14:14:28 +00:00
|
|
|
index := s.InnerRingIndex()
|
2021-02-21 06:53:18 +00:00
|
|
|
if s.contracts.alphabet.indexOutOfRange(index) {
|
2020-11-13 11:45:43 +00:00
|
|
|
s.log.Info("ignore validator vote: node not in alphabet range")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(validators) == 0 {
|
|
|
|
s.log.Info("ignore validator vote: empty validators list")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:27:58 +00:00
|
|
|
epoch := s.EpochCounter()
|
2020-11-13 11:45:43 +00:00
|
|
|
|
2021-07-06 11:27:07 +00:00
|
|
|
s.contracts.alphabet.iterate(func(letter GlagoliticLetter, contract util.Uint160) {
|
2021-09-23 16:56:13 +00:00
|
|
|
// FIXME: do not use constant nonce for alphabet NR: #844
|
|
|
|
err := s.morphClient.NotaryInvoke(contract, s.feeConfig.SideChainFee(), 1, voteMethod, int64(epoch), validators)
|
2020-12-04 11:27:58 +00:00
|
|
|
if err != nil {
|
|
|
|
s.log.Warn("can't invoke vote method in alphabet contract",
|
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
|
|
|
|
|
|
|
// InitAndVoteForSidechainValidator is a public function to use outside of
|
|
|
|
// inner ring daemon execution. It initialize inner ring structure with data
|
2021-03-25 13:24:44 +00:00
|
|
|
// from blockchain and then calls vote method on alphabet contracts.
|
2021-03-25 07:34:27 +00:00
|
|
|
func (s *Server) InitAndVoteForSidechainValidator(validators keys.PublicKeys) error {
|
2020-11-13 12:19:33 +00:00
|
|
|
err := s.initConfigFromBlockchain()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:24:44 +00:00
|
|
|
return s.VoteForSidechainValidator(validators)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VoteForSidechainValidator calls vote method on alphabet contracts with
|
|
|
|
// provided list of keys.
|
|
|
|
func (s *Server) VoteForSidechainValidator(validators keys.PublicKeys) error {
|
|
|
|
sort.Sort(validators)
|
2020-11-13 12:19:33 +00:00
|
|
|
return s.voteForSidechainValidator(validators)
|
|
|
|
}
|
2020-12-22 11:45:33 +00:00
|
|
|
|
|
|
|
// WriteReport composes audit result structure from audit report
|
|
|
|
// and sends it to Audit contract.
|
|
|
|
func (s *Server) WriteReport(r *audit.Report) error {
|
|
|
|
res := r.Result()
|
|
|
|
res.SetPublicKey(s.pubKey)
|
|
|
|
|
|
|
|
return s.auditClient.PutAuditResult(res)
|
|
|
|
}
|
2021-01-29 07:42:40 +00:00
|
|
|
|
|
|
|
// ResetEpochTimer resets block timer that produces events to update epoch
|
|
|
|
// counter in netmap contract. Used to synchronize this even production
|
|
|
|
// based on block with notification of last epoch.
|
|
|
|
func (s *Server) ResetEpochTimer() error {
|
|
|
|
return s.epochTimer.Reset()
|
|
|
|
}
|
2021-06-09 15:41:58 +00:00
|
|
|
|
|
|
|
func (s *Server) setHealthStatus(hs control.HealthStatus) {
|
|
|
|
s.healthStatus.Store(hs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HealthStatus returns current health status of IR application.
|
|
|
|
func (s *Server) HealthStatus() control.HealthStatus {
|
|
|
|
return s.healthStatus.Load().(control.HealthStatus)
|
|
|
|
}
|
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
|
|
|
|
}
|