github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/stateroot/validators.go (about)

     1  package stateroot
     2  
     3  import (
     4  	"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
     5  	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
     6  	"github.com/nspcc-dev/neo-go/pkg/smartcontract"
     7  )
     8  
     9  // UpdateStateValidators updates list of state validator keys.
    10  func (s *Module) UpdateStateValidators(height uint32, pubs keys.PublicKeys) {
    11  	script, _ := smartcontract.CreateDefaultMultiSigRedeemScript(pubs)
    12  	h := hash.Hash160(script)
    13  
    14  	s.mtx.Lock()
    15  	if s.updateValidatorsCb != nil {
    16  		s.updateValidatorsCb(height, pubs)
    17  	}
    18  	kc := s.getKeyCacheForHeight(height)
    19  	if kc.validatorsHash != h {
    20  		s.keys = append(s.keys, keyCache{
    21  			height:           height,
    22  			validatorsKeys:   pubs,
    23  			validatorsHash:   h,
    24  			validatorsScript: script,
    25  		})
    26  	}
    27  	s.mtx.Unlock()
    28  }
    29  
    30  func (s *Module) getKeyCacheForHeight(h uint32) keyCache {
    31  	for i := len(s.keys) - 1; i >= 0; i-- {
    32  		if s.keys[i].height <= h && (i+1 == len(s.keys) || s.keys[i+1].height < h) {
    33  			return s.keys[i]
    34  		}
    35  	}
    36  	return keyCache{}
    37  }
    38  
    39  // GetStateValidators returns current state validators.
    40  func (s *Module) GetStateValidators(height uint32) keys.PublicKeys {
    41  	s.mtx.RLock()
    42  	defer s.mtx.RUnlock()
    43  	return s.getKeyCacheForHeight(height).validatorsKeys.Copy()
    44  }