Skip to content

Commit

Permalink
core: rename (*Blockchain).GetValidators to ComputeNextBlockValidators
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
AnnaShaleva committed Aug 30, 2023
1 parent e650d19 commit 8103179
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions pkg/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2703,18 +2703,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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/blockchain_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/core/native/native_test/neo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit 8103179

Please sign in to comment.