native: implement NEO.UnregisterCandidate method

This commit is contained in:
Evgenii Stratonikov 2020-08-06 14:57:10 +03:00
parent 38a92323c9
commit 8af3f05358
2 changed files with 36 additions and 0 deletions

View file

@ -96,6 +96,11 @@ func NewNEO() *NEO {
md = newMethodAndPrice(n.registerCandidate, 5000000, smartcontract.AllowModifyStates)
n.AddMethod(md, desc, false)
desc = newDescriptor("unregisterCandidate", smartcontract.BoolType,
manifest.NewParameter("pubkey", smartcontract.PublicKeyType))
md = newMethodAndPrice(n.unregisterCandidate, 5000000, smartcontract.AllowModifyStates)
n.AddMethod(md, desc, false)
desc = newDescriptor("vote", smartcontract.BoolType,
manifest.NewParameter("account", smartcontract.Hash160Type),
manifest.NewParameter("pubkey", smartcontract.PublicKeyType))
@ -237,6 +242,28 @@ func (n *NEO) RegisterCandidateInternal(ic *interop.Context, pub *keys.PublicKey
return ic.DAO.PutStorageItem(n.ContractID, key, si)
}
func (n *NEO) unregisterCandidate(ic *interop.Context, args []stackitem.Item) stackitem.Item {
err := n.UnregisterCandidateInternal(ic, toPublicKey(args[0]))
return stackitem.NewBool(err == nil)
}
// UnregisterCandidateInternal unregisters pub as a candidate.
func (n *NEO) UnregisterCandidateInternal(ic *interop.Context, pub *keys.PublicKey) error {
key := makeValidatorKey(pub)
si := ic.DAO.GetStorageItem(n.ContractID, key)
if si == nil {
return nil
}
n.validators.Store(keys.PublicKeys(nil))
c := new(candidate).FromBytes(si.Value)
if c.Votes.Sign() == 0 {
return ic.DAO.DeleteStorageItem(n.ContractID, key)
}
c.Registered = false
si.Value = c.Bytes()
return ic.DAO.PutStorageItem(n.ContractID, key, si)
}
func (n *NEO) vote(ic *interop.Context, args []stackitem.Item) stackitem.Item {
acc := toUint160(args[0])
var pub *keys.PublicKey

View file

@ -92,4 +92,13 @@ func TestNEO_Vote(t *testing.T) {
pubs, err = neo.GetValidatorsInternal(bc, ic.DAO)
require.NoError(t, err)
require.Equal(t, candidates, pubs)
require.NoError(t, neo.UnregisterCandidateInternal(ic, candidates[0]))
require.Error(t, neo.VoteInternal(ic, h, candidates[0]))
pubs, err = neo.GetValidatorsInternal(bc, ic.DAO)
require.NoError(t, err)
for i := range pubs {
require.NotEqual(t, candidates[0], pubs[i])
}
}