[#446] innerring: Add alphabet index getter in global state

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-03-23 17:58:25 +03:00 committed by Alex Vanin
parent cd21641dfc
commit 1332db883e
3 changed files with 49 additions and 0 deletions

View file

@ -18,6 +18,7 @@ type innerRingIndexer struct {
timeout time.Duration timeout time.Duration
innerRingIndex, innerRingSize int32 innerRingIndex, innerRingSize int32
alphabetIndex int32
lastAccess time.Time lastAccess time.Time
} }
@ -52,6 +53,11 @@ func (s *innerRingIndexer) update() (err error) {
return err return err
} }
s.alphabetIndex, err = invoke.AlphabetIndex(s.cli, s.key)
if err != nil {
return err
}
s.lastAccess = time.Now() s.lastAccess = time.Now()
return nil return nil
@ -78,3 +84,14 @@ func (s *innerRingIndexer) InnerRingSize() (int32, error) {
return s.innerRingSize, nil return s.innerRingSize, nil
} }
func (s *innerRingIndexer) AlphabetIndex() (int32, error) {
if err := s.update(); err != nil {
return 0, errors.Wrap(err, "can't update index state")
}
s.RLock()
defer s.RUnlock()
return s.alphabetIndex, nil
}

View file

@ -25,6 +25,21 @@ func InnerRingIndex(cli *client.Client, key *ecdsa.PublicKey) (int32, int32, err
return keyPosition(key, innerRing), int32(len(innerRing)), nil return keyPosition(key, innerRing), int32(len(innerRing)), nil
} }
// AlphabetIndex returns index of the `key` in the alphabet key list from sidechain
// If key is not in the inner ring list, then returns `-1` as index.
func AlphabetIndex(cli *client.Client, key *ecdsa.PublicKey) (int32, error) {
if cli == nil {
return 0, client.ErrNilClient
}
alphabet, err := cli.Committee()
if err != nil {
return 0, err
}
return keyPosition(key, alphabet), nil
}
// keyPosition returns "-1" if key is not found in the list, otherwise returns // keyPosition returns "-1" if key is not found in the list, otherwise returns
// index of the key. // index of the key.
func keyPosition(key *ecdsa.PublicKey, list keys.PublicKeys) (result int32) { func keyPosition(key *ecdsa.PublicKey, list keys.PublicKeys) (result int32) {

View file

@ -24,6 +24,11 @@ func (s *Server) IsActive() bool {
return s.InnerRingIndex() >= 0 return s.InnerRingIndex() >= 0
} }
// IsAlphabet is a getter for a global alphabet flag state.
func (s *Server) IsAlphabet() bool {
return s.AlphabetIndex() >= 0
}
// InnerRingIndex is a getter for a global index of node in inner ring list. Negative // InnerRingIndex 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. // index means that node is not in the inner ring list.
func (s *Server) InnerRingIndex() int { func (s *Server) InnerRingIndex() int {
@ -48,6 +53,18 @@ func (s *Server) InnerRingSize() int {
return int(size) return int(size)
} }
// 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)
}
func (s *Server) voteForSidechainValidator(validators []keys.PublicKey) error { func (s *Server) voteForSidechainValidator(validators []keys.PublicKey) error {
index := s.InnerRingIndex() index := s.InnerRingIndex()
if s.contracts.alphabet.indexOutOfRange(index) { if s.contracts.alphabet.indexOutOfRange(index) {