From 07e1bc7cd7ae4e68f9f67f412e28408433c11b30 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 30 Aug 2023 19:48:20 +0300 Subject: [PATCH] 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 --- pkg/consensus/consensus.go | 6 +++--- pkg/core/blockchain.go | 16 +++++++++------- pkg/core/blockchain_core_test.go | 4 ++-- pkg/core/native/native_test/neo_test.go | 6 +++--- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index 8120e1d16..633cda9c8 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -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() } diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index a75fdad95..ced06626f 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -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 } diff --git a/pkg/core/blockchain_core_test.go b/pkg/core/blockchain_core_test.go index e17ee7df9..1d45c115b 100644 --- a/pkg/core/blockchain_core_test.go +++ b/pkg/core/blockchain_core_test.go @@ -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() } diff --git a/pkg/core/native/native_test/neo_test.go b/pkg/core/native/native_test/neo_test.go index dcb9ac415..e0a46f5d2 100644 --- a/pkg/core/native/native_test/neo_test.go +++ b/pkg/core/native/native_test/neo_test.go @@ -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])