Skip to content

Commit

Permalink
native: remove getValidators method
Browse files Browse the repository at this point in the history
  • Loading branch information
fyrchik authored and AnnaShaleva committed Oct 1, 2020
1 parent fbaf2bb commit 42ef9e1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 6 additions & 19 deletions pkg/core/native/native_neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions pkg/core/native_neo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

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

0 comments on commit 42ef9e1

Please sign in to comment.