core: rename (*Blockchain).GetValidators to ComputeNextBlockValidators

We have two similar blockchain APIs: GetNextBlockValidators and GetValidators.
It's hard to distinguish them, thus renaming it to match the meaning, so what
we have now is:

GetNextBlockValidators literally just returns the top of the committee that
was elected in the start of batch of CommitteeSize blocks batch. It doesn't
change its valie every block.

ComputeNextBlockValidators literally computes the list of validators based on
the most fresh committee members information got from the NeoToken's storage
and based on the latest register/unregister/vote events. The list returned by
this method may be updated every block.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-08-30 19:48:20 +03:00
parent 96449d803a
commit 07e1bc7cd7
4 changed files with 17 additions and 15 deletions

View file

@ -48,7 +48,7 @@ type Ledger interface {
GetNextBlockValidators() ([]*keys.PublicKey, error)
GetStateRoot(height uint32) (*state.MPTRoot, error)
GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error)
GetValidators() []*keys.PublicKey
ComputeNextBlockValidators() []*keys.PublicKey
PoolTx(t *transaction.Transaction, pools ...*mempool.Pool) error
SubscribeForBlocks(ch chan *coreb.Block)
UnsubscribeFromBlocks(ch chan *coreb.Block)
@ -679,7 +679,7 @@ func (s *service) getValidators(txes ...block.Transaction) []crypto.PublicKey {
if txes == nil {
pKeys, err = s.Chain.GetNextBlockValidators()
} else {
pKeys = s.Chain.GetValidators()
pKeys = s.Chain.ComputeNextBlockValidators()
}
if err != nil {
s.log.Error("error while trying to get validators", zap.Error(err))
@ -725,7 +725,7 @@ func (s *service) newBlockFromContext(ctx *dbft.Context) block.Block {
var err error
cfg := s.Chain.GetConfig().ProtocolConfiguration
if cfg.ShouldUpdateCommitteeAt(ctx.BlockIndex) {
validators = s.Chain.GetValidators()
validators = s.Chain.ComputeNextBlockValidators()
} else {
validators, err = s.Chain.GetNextBlockValidators()
}

View file

@ -2697,18 +2697,20 @@ func (bc *Blockchain) GetCommittee() (keys.PublicKeys, error) {
return pubs, nil
}
// GetValidators returns current validators. Validators list returned from this
// method may be updated every block (depending on register/unregister/vote
// calls to NeoToken contract), not only once per (committee size) number of
// blocks.
func (bc *Blockchain) GetValidators() []*keys.PublicKey {
// ComputeNextBlockValidators returns current validators. Validators list
// returned from this method may be updated every block (depending on
// register/unregister/vote calls to NeoToken contract), not only once per
// (committee size) number of blocks.
func (bc *Blockchain) ComputeNextBlockValidators() []*keys.PublicKey {
return bc.contracts.NEO.ComputeNextBlockValidators(bc.dao)
}
// GetNextBlockValidators returns next block validators. Validators list returned
// from this method is the sorted top NumOfCNs number of public keys from the
// current committee, thus, validators list returned from this method is being
// updated once per (committee size) number of blocks.
// committee of the current dBFT round (that was calculated once for the
// CommitteeSize number of blocks), thus, validators list returned from this
// method is being updated once per (committee size) number of blocks, but not
// every block.
func (bc *Blockchain) GetNextBlockValidators() ([]*keys.PublicKey, error) {
return bc.contracts.NEO.GetNextBlockValidatorsInternal(bc.dao), nil
}

View file

@ -261,7 +261,7 @@ func TestChainWithVolatileNumOfValidators(t *testing.T) {
priv0 := testchain.PrivateKeyByID(0)
vals := bc.GetValidators()
vals := bc.ComputeNextBlockValidators()
script, err := smartcontract.CreateDefaultMultiSigRedeemScript(vals)
require.NoError(t, err)
curWit := transaction.Witness{
@ -279,7 +279,7 @@ func TestChainWithVolatileNumOfValidators(t *testing.T) {
}
// Mimic consensus.
if bc.config.ShouldUpdateCommitteeAt(uint32(i)) {
vals = bc.GetValidators()
vals = bc.ComputeNextBlockValidators()
} else {
vals, err = bc.GetNextBlockValidators()
}

View file

@ -138,7 +138,7 @@ func TestNEO_Vote(t *testing.T) {
require.NoError(t, err)
standBySorted = standBySorted[:validatorsCount]
sort.Sort(standBySorted)
pubs := e.Chain.GetValidators()
pubs := e.Chain.ComputeNextBlockValidators()
require.Equal(t, standBySorted, keys.PublicKeys(pubs))
// voters vote for candidates. The aim of this test is to check if voting
@ -175,7 +175,7 @@ func TestNEO_Vote(t *testing.T) {
}
// We still haven't voted enough validators in.
pubs = e.Chain.GetValidators()
pubs = e.Chain.ComputeNextBlockValidators()
require.NoError(t, err)
require.Equal(t, standBySorted, keys.PublicKeys(pubs))
@ -266,7 +266,7 @@ func TestNEO_Vote(t *testing.T) {
advanceChain(t)
pubs = e.Chain.GetValidators()
pubs = e.Chain.ComputeNextBlockValidators()
for i := range pubs {
require.NotEqual(t, candidates[0], pubs[i])
require.NotEqual(t, candidates[len(candidates)-1], pubs[i])