2020-07-24 13:54:03 +00:00
|
|
|
package innerring
|
|
|
|
|
2020-11-13 11:45:43 +00:00
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/invoke"
|
2020-12-22 11:45:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/audit"
|
2020-12-04 11:27:58 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-13 11:45:43 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2020-10-12 09:13:19 +00:00
|
|
|
return s.innerRingIndex.Load() >= 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index is a getter for a global index of node in inner ring list. Negative
|
|
|
|
// index means that node is not in the inner ring list.
|
|
|
|
func (s *Server) Index() int32 {
|
|
|
|
return s.innerRingIndex.Load()
|
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.
|
|
|
|
func (s *Server) InnerRingSize() int32 {
|
|
|
|
return s.innerRingSize.Load()
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:45:43 +00:00
|
|
|
func (s *Server) voteForSidechainValidator(validators []keys.PublicKey) error {
|
|
|
|
index := s.Index()
|
|
|
|
if index < 0 || index >= alphabetContractsN {
|
|
|
|
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
|
|
|
|
2020-12-04 11:27:58 +00:00
|
|
|
for i := range s.contracts.alphabet {
|
|
|
|
err := invoke.AlphabetVote(s.morphClient, s.contracts.alphabet[i], epoch, validators)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Warn("can't invoke vote method in alphabet contract",
|
|
|
|
zap.Int("alphabet_index", i),
|
|
|
|
zap.Uint64("epoch", epoch))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
// from blockchain and then calls vote method on corresponding alphabet contract.
|
|
|
|
func (s *Server) InitAndVoteForSidechainValidator(validators []keys.PublicKey) error {
|
|
|
|
err := s.initConfigFromBlockchain()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|