From 81a11c629a5ec073f9021dc7659719fcce98c85f Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 22 Sep 2020 12:53:44 +0300 Subject: [PATCH] native: remove `getValidators` method Follow https://github.com/neo-project/neo/pull/1920 . --- pkg/core/blockchain.go | 2 +- pkg/core/native/native_neo.go | 25 ++++++------------------- pkg/core/native_neo_test.go | 10 ++++------ 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 660381f27..ff0505e00 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1380,7 +1380,7 @@ func (bc *Blockchain) GetCommittee() (keys.PublicKeys, error) { // GetValidators returns current validators. func (bc *Blockchain) GetValidators() ([]*keys.PublicKey, error) { - return bc.contracts.NEO.GetValidatorsInternal(bc, bc.dao) + return bc.contracts.NEO.ComputeNextBlockValidators(bc, bc.dao) } // GetNextBlockValidators returns next block validators. diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index 88e908985..fd9053782 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -132,10 +132,6 @@ func NewNEO() *NEO { md = newMethodAndPrice(n.getCommittee, 100000000, smartcontract.AllowStates) n.AddMethod(md, desc, true) - desc = newDescriptor("getValidators", smartcontract.ArrayType) - md = newMethodAndPrice(n.getValidators, 100000000, smartcontract.AllowStates) - n.AddMethod(md, desc, true) - desc = newDescriptor("getNextBlockValidators", smartcontract.ArrayType) md = newMethodAndPrice(n.getNextBlockValidators, 100000000, smartcontract.AllowStates) n.AddMethod(md, desc, true) @@ -585,30 +581,21 @@ func (n *NEO) getCandidatesCall(ic *interop.Context, _ []stackitem.Item) stackit return stackitem.NewArray(arr) } -// GetValidatorsInternal returns a list of current validators. -func (n *NEO) GetValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) { +// ComputeNextBlockValidators returns an actual list of current validators. +func (n *NEO) ComputeNextBlockValidators(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) { if vals := n.validators.Load().(keys.PublicKeys); vals != nil { return vals.Copy(), nil } - result := n.GetCommitteeMembers() - count := bc.GetConfig().ValidatorsCount - if len(result) < count { - count = len(result) + result, err := n.ComputeCommitteeMembers(bc, d) + if err != nil { + return nil, err } - result = result[:count] + result = result[:bc.GetConfig().ValidatorsCount] sort.Sort(result) n.validators.Store(result) return result, nil } -func (n *NEO) getValidators(ic *interop.Context, _ []stackitem.Item) stackitem.Item { - result, err := n.GetValidatorsInternal(ic.Chain, ic.DAO) - if err != nil { - panic(err) - } - return pubsToArray(result) -} - func (n *NEO) getCommittee(ic *interop.Context, _ []stackitem.Item) stackitem.Item { pubs := n.GetCommitteeMembers() sort.Sort(pubs) diff --git a/pkg/core/native_neo_test.go b/pkg/core/native_neo_test.go index d4d049370..531119ff5 100644 --- a/pkg/core/native_neo_test.go +++ b/pkg/core/native_neo_test.go @@ -37,7 +37,7 @@ func TestNEO_Vote(t *testing.T) { standBySorted := bc.GetStandByValidators() sort.Sort(standBySorted) - pubs, err := neo.GetValidatorsInternal(bc, ic.DAO) + pubs, err := neo.ComputeNextBlockValidators(bc, ic.DAO) require.NoError(t, err) require.Equal(t, standBySorted, pubs) @@ -68,10 +68,8 @@ func TestNEO_Vote(t *testing.T) { require.NoError(t, neo.VoteInternal(ic, h, candidates[i])) } - require.NoError(t, neo.OnPersist(ic)) - // We still haven't voted enough validators in. - pubs, err = neo.GetValidatorsInternal(bc, ic.DAO) + pubs, err = neo.ComputeNextBlockValidators(bc, ic.DAO) require.NoError(t, err) require.Equal(t, standBySorted, pubs) @@ -93,7 +91,7 @@ func TestNEO_Vote(t *testing.T) { } require.NoError(t, neo.OnPersist(ic)) - pubs, err = neo.GetValidatorsInternal(bc, ic.DAO) + pubs, err = neo.ComputeNextBlockValidators(bc, ic.DAO) require.NoError(t, err) sortedCandidates := candidates.Copy() sort.Sort(sortedCandidates) @@ -106,7 +104,7 @@ func TestNEO_Vote(t *testing.T) { require.Error(t, neo.VoteInternal(ic, h, candidates[0])) require.NoError(t, neo.OnPersist(ic)) - pubs, err = neo.GetValidatorsInternal(bc, ic.DAO) + pubs, err = neo.ComputeNextBlockValidators(bc, ic.DAO) require.NoError(t, err) for i := range pubs { require.NotEqual(t, candidates[0], pubs[i])