2020-03-25 10:00:11 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NEO represents NEO native contract.
|
|
|
|
type NEO struct {
|
|
|
|
nep5TokenNative
|
|
|
|
GAS *GAS
|
|
|
|
}
|
|
|
|
|
2020-04-25 21:23:30 +00:00
|
|
|
// keyWithVotes is a serialized key with votes balance. It's not deserialized
|
|
|
|
// because some uses of it imply serialized-only usage and converting to
|
|
|
|
// PublicKey is quite expensive.
|
|
|
|
type keyWithVotes struct {
|
|
|
|
Key string
|
|
|
|
Votes *big.Int
|
|
|
|
}
|
2020-03-25 10:00:11 +00:00
|
|
|
|
2020-04-25 21:23:30 +00:00
|
|
|
// pkeyWithVotes is a deserialized key with votes balance.
|
|
|
|
type pkeyWithVotes struct {
|
|
|
|
Key *keys.PublicKey
|
|
|
|
Votes *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
neoSyscallName = "Neo.Native.Tokens.NEO"
|
|
|
|
// NEOTotalSupply is the total amount of NEO in the system.
|
|
|
|
NEOTotalSupply = 100000000
|
|
|
|
// prefixValidator is a prefix used to store validator's data.
|
|
|
|
prefixValidator = 33
|
|
|
|
)
|
|
|
|
|
2020-04-26 07:15:59 +00:00
|
|
|
var (
|
|
|
|
// validatorsCountKey is a key used to store validators count
|
|
|
|
// used to determine the real number of validators.
|
|
|
|
validatorsCountKey = []byte{15}
|
|
|
|
// nextValidatorsKey is a key used to store validators for the
|
|
|
|
// next block.
|
|
|
|
nextValidatorsKey = []byte{14}
|
|
|
|
)
|
|
|
|
|
2020-04-25 21:23:30 +00:00
|
|
|
// makeValidatorKey creates a key from account script hash.
|
|
|
|
func makeValidatorKey(key *keys.PublicKey) []byte {
|
|
|
|
b := key.Bytes()
|
|
|
|
// Don't create a new buffer.
|
|
|
|
b = append(b, 0)
|
|
|
|
copy(b[1:], b[0:])
|
|
|
|
b[0] = prefixValidator
|
|
|
|
return b
|
|
|
|
}
|
2020-04-22 20:00:18 +00:00
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
// NewNEO returns NEO native contract.
|
|
|
|
func NewNEO() *NEO {
|
|
|
|
nep5 := newNEP5Native(neoSyscallName)
|
|
|
|
nep5.name = "NEO"
|
|
|
|
nep5.symbol = "neo"
|
|
|
|
nep5.decimals = 0
|
|
|
|
nep5.factor = 1
|
|
|
|
|
|
|
|
n := &NEO{nep5TokenNative: *nep5}
|
|
|
|
|
|
|
|
desc := newDescriptor("unclaimedGas", smartcontract.IntegerType,
|
|
|
|
manifest.NewParameter("account", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("end", smartcontract.IntegerType))
|
|
|
|
md := newMethodAndPrice(n.unclaimedGas, 1, smartcontract.NoneFlag)
|
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
|
|
|
desc = newDescriptor("registerValidator", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("pubkey", smartcontract.PublicKeyType))
|
|
|
|
md = newMethodAndPrice(n.registerValidator, 1, smartcontract.NoneFlag)
|
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
|
|
|
desc = newDescriptor("vote", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("account", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("pubkeys", smartcontract.ArrayType))
|
|
|
|
md = newMethodAndPrice(n.vote, 1, smartcontract.NoneFlag)
|
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
|
|
|
desc = newDescriptor("getRegisteredValidators", smartcontract.ArrayType)
|
2020-04-25 21:23:30 +00:00
|
|
|
md = newMethodAndPrice(n.getRegisteredValidatorsCall, 1, smartcontract.NoneFlag)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
|
|
|
desc = newDescriptor("getValidators", smartcontract.ArrayType)
|
|
|
|
md = newMethodAndPrice(n.getValidators, 1, smartcontract.NoneFlag)
|
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
|
|
|
desc = newDescriptor("getNextBlockValidators", smartcontract.ArrayType)
|
|
|
|
md = newMethodAndPrice(n.getNextBlockValidators, 1, smartcontract.NoneFlag)
|
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
|
|
|
n.onPersist = chainOnPersist(n.onPersist, n.OnPersist)
|
|
|
|
n.incBalance = n.increaseBalance
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initializes NEO contract.
|
|
|
|
func (n *NEO) Initialize(ic *interop.Context) error {
|
2020-04-26 07:15:59 +00:00
|
|
|
var si state.StorageItem
|
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
if err := n.nep5TokenNative.Initialize(ic); err != nil {
|
2020-03-25 10:00:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
if n.nep5TokenNative.getTotalSupply(ic).Sign() != 0 {
|
|
|
|
return errors.New("already initialized")
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 07:15:59 +00:00
|
|
|
vc := new(ValidatorsCount)
|
|
|
|
si.Value = vc.Bytes()
|
|
|
|
if err := ic.DAO.PutStorageItem(n.Hash, validatorsCountKey, &si); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-25 10:00:11 +00:00
|
|
|
h, vs, err := getStandbyValidatorsHash(ic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-22 20:00:18 +00:00
|
|
|
n.mint(ic, h, big.NewInt(NEOTotalSupply))
|
2020-03-25 10:00:11 +00:00
|
|
|
|
|
|
|
for i := range vs {
|
|
|
|
if err := n.registerValidatorInternal(ic, vs[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnPersist implements Contract interface.
|
|
|
|
func (n *NEO) OnPersist(ic *interop.Context) error {
|
|
|
|
pubs, err := n.GetValidatorsInternal(ic.Chain, ic.DAO)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ic.DAO.PutNextBlockValidators(pubs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-22 20:00:18 +00:00
|
|
|
return nil
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 18:28:37 +00:00
|
|
|
func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.StorageItem, amount *big.Int) error {
|
|
|
|
acc, err := state.NEOBalanceStateFromBytes(si.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-26 09:51:17 +00:00
|
|
|
if amount.Sign() == -1 && acc.Balance.Cmp(new(big.Int).Neg(amount)) == -1 {
|
2020-03-25 10:00:11 +00:00
|
|
|
return errors.New("insufficient funds")
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
if err := n.distributeGas(ic, h, acc); err != nil {
|
2020-03-25 10:00:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-26 09:51:17 +00:00
|
|
|
if amount.Sign() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
acc.Balance.Add(&acc.Balance, amount)
|
|
|
|
si.Value = acc.Bytes()
|
2020-03-25 10:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:28:37 +00:00
|
|
|
func (n *NEO) distributeGas(ic *interop.Context, h util.Uint160, acc *state.NEOBalanceState) error {
|
2020-04-23 13:25:30 +00:00
|
|
|
if ic.Block == nil || ic.Block.Index == 0 {
|
2020-03-25 10:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
sys, net, err := ic.Chain.CalculateClaimable(util.Fixed8(acc.Balance.Int64()), acc.BalanceHeight, ic.Block.Index)
|
2020-03-25 10:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
acc.BalanceHeight = ic.Block.Index
|
|
|
|
n.GAS.mint(ic, h, big.NewInt(int64(sys+net)))
|
2020-03-25 10:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) unclaimedGas(ic *interop.Context, args []vm.StackItem) vm.StackItem {
|
|
|
|
u := toUint160(args[0])
|
|
|
|
end := uint32(toBigInt(args[1]).Int64())
|
|
|
|
bs, err := ic.DAO.GetNEP5Balances(u)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tr := bs.Trackers[n.Hash]
|
|
|
|
|
|
|
|
sys, net, err := ic.Chain.CalculateClaimable(util.Fixed8(tr.Balance), tr.LastUpdatedBlock, end)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return vm.NewBigIntegerItem(big.NewInt(int64(sys.Add(net))))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) registerValidator(ic *interop.Context, args []vm.StackItem) vm.StackItem {
|
|
|
|
err := n.registerValidatorInternal(ic, toPublicKey(args[0]))
|
|
|
|
return vm.NewBoolItem(err == nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) registerValidatorInternal(ic *interop.Context, pub *keys.PublicKey) error {
|
2020-04-25 21:23:30 +00:00
|
|
|
key := makeValidatorKey(pub)
|
|
|
|
si := ic.DAO.GetStorageItem(n.Hash, key)
|
|
|
|
if si != nil {
|
|
|
|
return errors.New("already registered")
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-04-25 21:23:30 +00:00
|
|
|
si = new(state.StorageItem)
|
|
|
|
// It's the same simple counter, calling it `Votes` instead of `Balance`
|
|
|
|
// doesn't help a lot.
|
|
|
|
votes := state.NEP5BalanceState{}
|
|
|
|
si.Value = votes.Bytes()
|
|
|
|
return ic.DAO.PutStorageItem(n.Hash, key, si)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) vote(ic *interop.Context, args []vm.StackItem) vm.StackItem {
|
|
|
|
acc := toUint160(args[0])
|
|
|
|
arr := args[1].Value().([]vm.StackItem)
|
|
|
|
var pubs keys.PublicKeys
|
|
|
|
for i := range arr {
|
|
|
|
pub := new(keys.PublicKey)
|
|
|
|
bs, err := arr[i].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if err := pub.DecodeBytes(bs); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
pubs = append(pubs, pub)
|
|
|
|
}
|
|
|
|
err := n.VoteInternal(ic, acc, pubs)
|
|
|
|
return vm.NewBoolItem(err == nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VoteInternal votes from account h for validarors specified in pubs.
|
|
|
|
func (n *NEO) VoteInternal(ic *interop.Context, h util.Uint160, pubs keys.PublicKeys) error {
|
|
|
|
ok, err := runtime.CheckHashedWitness(ic, h)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
return errors.New("invalid signature")
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
key := makeAccountKey(h)
|
|
|
|
si := ic.DAO.GetStorageItem(n.Hash, key)
|
|
|
|
if si == nil {
|
|
|
|
return errors.New("invalid account")
|
|
|
|
}
|
|
|
|
acc, err := state.NEOBalanceStateFromBytes(si.Value)
|
2020-03-25 10:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
oldAcc, err := ic.DAO.GetAccountState(h)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-25 21:23:30 +00:00
|
|
|
if err := n.ModifyAccountVotes(oldAcc, ic.DAO, new(big.Int).Neg(&acc.Balance)); err != nil {
|
2020-03-25 10:00:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
pubs = pubs.Unique()
|
2020-04-25 21:23:30 +00:00
|
|
|
// Check validators registration.
|
2020-03-25 10:00:11 +00:00
|
|
|
var newPubs keys.PublicKeys
|
|
|
|
for _, pub := range pubs {
|
2020-04-25 21:23:30 +00:00
|
|
|
if ic.DAO.GetStorageItem(n.Hash, makeValidatorKey(pub)) == nil {
|
|
|
|
continue
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
newPubs = append(newPubs, pub)
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
if lp, lv := len(newPubs), len(oldAcc.Votes); lp != lv {
|
2020-04-26 07:15:59 +00:00
|
|
|
si := ic.DAO.GetStorageItem(n.Hash, validatorsCountKey)
|
|
|
|
if si == nil {
|
|
|
|
return errors.New("validators count uninitialized")
|
|
|
|
}
|
|
|
|
vc, err := ValidatorsCountFromBytes(si.Value)
|
2020-03-25 10:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if lv > 0 {
|
2020-04-26 07:15:59 +00:00
|
|
|
vc[lv-1].Sub(&vc[lv-1], &acc.Balance)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
if len(newPubs) > 0 {
|
2020-04-26 07:15:59 +00:00
|
|
|
vc[lp-1].Add(&vc[lp-1], &acc.Balance)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-04-26 07:15:59 +00:00
|
|
|
si.Value = vc.Bytes()
|
|
|
|
if err := ic.DAO.PutStorageItem(n.Hash, validatorsCountKey, si); err != nil {
|
2020-03-25 10:00:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
oldAcc.Votes = newPubs
|
2020-04-25 21:23:30 +00:00
|
|
|
if err := n.ModifyAccountVotes(oldAcc, ic.DAO, &acc.Balance); err != nil {
|
2020-04-23 18:28:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ic.DAO.PutAccountState(oldAcc)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ModifyAccountVotes modifies votes of the specified account by value (can be negative).
|
2020-04-25 21:23:30 +00:00
|
|
|
func (n *NEO) ModifyAccountVotes(acc *state.Account, d dao.DAO, value *big.Int) error {
|
2020-03-25 10:00:11 +00:00
|
|
|
for _, vote := range acc.Votes {
|
2020-04-25 21:23:30 +00:00
|
|
|
key := makeValidatorKey(vote)
|
|
|
|
si := d.GetStorageItem(n.Hash, key)
|
|
|
|
if si == nil {
|
|
|
|
return errors.New("invalid validator")
|
|
|
|
}
|
|
|
|
votes, err := state.NEP5BalanceStateFromBytes(si.Value)
|
2020-03-25 10:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-25 21:23:30 +00:00
|
|
|
votes.Balance.Add(&votes.Balance, value)
|
|
|
|
si.Value = votes.Bytes()
|
|
|
|
if err := d.PutStorageItem(n.Hash, key, si); err != nil {
|
|
|
|
return err
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-25 21:23:30 +00:00
|
|
|
func (n *NEO) getRegisteredValidators(d dao.DAO) ([]keyWithVotes, error) {
|
|
|
|
siMap, err := d.GetStorageItemsWithPrefix(n.Hash, []byte{prefixValidator})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arr := make([]keyWithVotes, 0, len(siMap))
|
|
|
|
for key, si := range siMap {
|
|
|
|
votes, err := state.NEP5BalanceStateFromBytes(si.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arr = append(arr, keyWithVotes{key, &votes.Balance})
|
|
|
|
}
|
|
|
|
return arr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) getRegisteredValidatorsCall(ic *interop.Context, _ []vm.StackItem) vm.StackItem {
|
|
|
|
validators, err := n.getRegisteredValidators(ic.DAO)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
arr := make([]vm.StackItem, len(validators))
|
|
|
|
for i := range validators {
|
2020-03-25 10:00:11 +00:00
|
|
|
arr[i] = vm.NewStructItem([]vm.StackItem{
|
2020-04-25 21:23:30 +00:00
|
|
|
vm.NewByteArrayItem([]byte(validators[i].Key)),
|
|
|
|
vm.NewBigIntegerItem(validators[i].Votes),
|
2020-03-25 10:00:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return vm.NewArrayItem(arr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidatorsInternal returns a list of current validators.
|
|
|
|
func (n *NEO) GetValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) ([]*keys.PublicKey, error) {
|
2020-04-26 07:15:59 +00:00
|
|
|
si := d.GetStorageItem(n.Hash, validatorsCountKey)
|
|
|
|
if si == nil {
|
|
|
|
return nil, errors.New("validators count uninitialized")
|
|
|
|
}
|
|
|
|
validatorsCount, err := ValidatorsCountFromBytes(si.Value)
|
2020-03-25 10:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-25 21:23:30 +00:00
|
|
|
validatorsBytes, err := n.getRegisteredValidators(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
validators := make([]pkeyWithVotes, len(validatorsBytes))
|
|
|
|
for i := range validatorsBytes {
|
|
|
|
validators[i].Key, err = keys.NewPublicKeyFromBytes([]byte(validatorsBytes[i].Key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-04-25 21:23:30 +00:00
|
|
|
validators[i].Votes = validatorsBytes[i].Votes
|
|
|
|
}
|
|
|
|
sort.Slice(validators, func(i, j int) bool {
|
2020-03-25 10:00:11 +00:00
|
|
|
// The most-voted validators should end up in the front of the list.
|
2020-04-25 21:23:30 +00:00
|
|
|
cmp := validators[i].Votes.Cmp(validators[j].Votes)
|
|
|
|
if cmp != 0 {
|
|
|
|
return cmp > 0
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
// Ties are broken with public keys.
|
2020-04-25 21:23:30 +00:00
|
|
|
return validators[i].Key.Cmp(validators[j].Key) == -1
|
2020-03-25 10:00:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
count := validatorsCount.GetWeightedAverage()
|
|
|
|
standByValidators, err := bc.GetStandByValidators()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if count < len(standByValidators) {
|
|
|
|
count = len(standByValidators)
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueSBValidators := standByValidators.Unique()
|
|
|
|
result := keys.PublicKeys{}
|
|
|
|
for _, validator := range validators {
|
2020-04-25 21:23:30 +00:00
|
|
|
if validator.Votes.Sign() > 0 || uniqueSBValidators.Contains(validator.Key) {
|
|
|
|
result = append(result, validator.Key)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Len() >= count {
|
|
|
|
result = result[:count]
|
|
|
|
} else {
|
|
|
|
for i := 0; i < uniqueSBValidators.Len() && result.Len() < count; i++ {
|
|
|
|
if !result.Contains(uniqueSBValidators[i]) {
|
|
|
|
result = append(result, uniqueSBValidators[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(result)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) getValidators(ic *interop.Context, _ []vm.StackItem) vm.StackItem {
|
|
|
|
result, err := n.GetValidatorsInternal(ic.Chain, ic.DAO)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pubsToArray(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) getNextBlockValidators(ic *interop.Context, _ []vm.StackItem) vm.StackItem {
|
|
|
|
result, err := n.GetNextBlockValidatorsInternal(ic.Chain, ic.DAO)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pubsToArray(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNextBlockValidatorsInternal returns next block validators.
|
|
|
|
func (n *NEO) GetNextBlockValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) ([]*keys.PublicKey, error) {
|
|
|
|
result, err := d.GetNextBlockValidators()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if result == nil {
|
|
|
|
return bc.GetStandByValidators()
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pubsToArray(pubs keys.PublicKeys) vm.StackItem {
|
|
|
|
arr := make([]vm.StackItem, len(pubs))
|
|
|
|
for i := range pubs {
|
|
|
|
arr[i] = vm.NewByteArrayItem(pubs[i].Bytes())
|
|
|
|
}
|
|
|
|
return vm.NewArrayItem(arr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toPublicKey(s vm.StackItem) *keys.PublicKey {
|
|
|
|
buf, err := s.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
pub := new(keys.PublicKey)
|
|
|
|
if err := pub.DecodeBytes(buf); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pub
|
|
|
|
}
|