2020-03-25 10:00:11 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2020-07-13 09:59:41 +00:00
|
|
|
"crypto/elliptic"
|
2020-08-06 14:44:08 +00:00
|
|
|
"errors"
|
2020-03-25 10:00:11 +00:00
|
|
|
"math/big"
|
|
|
|
"sort"
|
2020-05-29 08:02:26 +00:00
|
|
|
"strings"
|
2020-06-24 10:20:59 +00:00
|
|
|
"sync/atomic"
|
2020-03-25 10:00:11 +00:00
|
|
|
|
|
|
|
"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"
|
2020-08-26 10:06:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-03-25 10:00:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-06-23 15:30:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-03-25 10:00:11 +00:00
|
|
|
"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"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-25 10:00:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NEO represents NEO native contract.
|
|
|
|
type NEO struct {
|
|
|
|
nep5TokenNative
|
|
|
|
GAS *GAS
|
2020-06-24 10:20:59 +00:00
|
|
|
|
2020-09-28 07:51:25 +00:00
|
|
|
// gasPerBlock represents current value of generated gas per block.
|
|
|
|
// It is append-only and doesn't need to be copied when used.
|
|
|
|
gasPerBlock atomic.Value
|
|
|
|
gasPerBlockChanged atomic.Value
|
|
|
|
|
2020-08-20 15:49:01 +00:00
|
|
|
votesChanged atomic.Value
|
|
|
|
nextValidators atomic.Value
|
|
|
|
validators atomic.Value
|
2020-08-28 07:24:54 +00:00
|
|
|
// committee contains cached committee members and
|
2020-09-22 10:03:34 +00:00
|
|
|
// is updated once in a while depending on committee size
|
|
|
|
// (every 28 blocks for mainnet). It's value
|
2020-08-28 07:24:54 +00:00
|
|
|
// is always equal to value stored by `prefixCommittee`.
|
|
|
|
committee atomic.Value
|
2020-09-24 12:36:14 +00:00
|
|
|
// committeeHash contains script hash of the committee.
|
|
|
|
committeeHash atomic.Value
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
const (
|
2020-07-22 16:03:05 +00:00
|
|
|
neoName = "NEO"
|
|
|
|
neoContractID = -1
|
2020-04-25 21:23:30 +00:00
|
|
|
// NEOTotalSupply is the total amount of NEO in the system.
|
|
|
|
NEOTotalSupply = 100000000
|
2020-08-03 08:43:51 +00:00
|
|
|
// prefixCandidate is a prefix used to store validator's data.
|
|
|
|
prefixCandidate = 33
|
2020-08-03 12:00:27 +00:00
|
|
|
// prefixVotersCount is a prefix for storing total amount of NEO of voters.
|
|
|
|
prefixVotersCount = 1
|
2020-08-26 09:07:30 +00:00
|
|
|
// prefixGasPerBlock is a prefix for storing amount of GAS generated per block.
|
|
|
|
prefixGASPerBlock = 29
|
2020-08-03 12:00:27 +00:00
|
|
|
// effectiveVoterTurnout represents minimal ratio of total supply to total amount voted value
|
|
|
|
// which is require to use non-standby validators.
|
|
|
|
effectiveVoterTurnout = 5
|
2020-08-26 09:07:30 +00:00
|
|
|
// neoHolderRewardRatio is a percent of generated GAS that is distributed to NEO holders.
|
|
|
|
neoHolderRewardRatio = 10
|
|
|
|
// neoHolderRewardRatio is a percent of generated GAS that is distributed to committee.
|
|
|
|
committeeRewardRatio = 5
|
|
|
|
// neoHolderRewardRatio is a percent of generated GAS that is distributed to voters.
|
|
|
|
voterRewardRatio = 85
|
2020-04-25 21:23:30 +00:00
|
|
|
)
|
|
|
|
|
2020-04-26 07:15:59 +00:00
|
|
|
var (
|
2020-08-28 07:24:54 +00:00
|
|
|
// prefixCommittee is a key used to store committee.
|
|
|
|
prefixCommittee = []byte{14}
|
2020-04-26 07:15:59 +00:00
|
|
|
)
|
|
|
|
|
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:])
|
2020-08-03 08:43:51 +00:00
|
|
|
b[0] = prefixCandidate
|
2020-04-25 21:23:30 +00:00
|
|
|
return b
|
|
|
|
}
|
2020-04-22 20:00:18 +00:00
|
|
|
|
2020-10-02 10:50:56 +00:00
|
|
|
// newNEO returns NEO native contract.
|
|
|
|
func newNEO() *NEO {
|
2020-05-14 18:01:56 +00:00
|
|
|
n := &NEO{}
|
2020-07-22 16:03:05 +00:00
|
|
|
nep5 := newNEP5Native(neoName)
|
2020-03-25 10:00:11 +00:00
|
|
|
nep5.symbol = "neo"
|
|
|
|
nep5.decimals = 0
|
|
|
|
nep5.factor = 1
|
2020-06-17 10:57:54 +00:00
|
|
|
nep5.onPersist = chainOnPersist(nep5.OnPersist, n.OnPersist)
|
2020-09-23 08:48:31 +00:00
|
|
|
nep5.postPersist = chainOnPersist(nep5.postPersist, n.PostPersist)
|
2020-05-14 18:01:56 +00:00
|
|
|
nep5.incBalance = n.increaseBalance
|
2020-06-10 11:23:25 +00:00
|
|
|
nep5.ContractID = neoContractID
|
2020-03-25 10:00:11 +00:00
|
|
|
|
2020-05-14 18:01:56 +00:00
|
|
|
n.nep5TokenNative = *nep5
|
2020-08-20 15:49:01 +00:00
|
|
|
n.votesChanged.Store(true)
|
|
|
|
n.nextValidators.Store(keys.PublicKeys(nil))
|
2020-06-29 07:36:44 +00:00
|
|
|
n.validators.Store(keys.PublicKeys(nil))
|
2020-08-28 07:24:54 +00:00
|
|
|
n.committee.Store(keys.PublicKeys(nil))
|
2020-09-24 12:36:14 +00:00
|
|
|
n.committeeHash.Store(util.Uint160{})
|
2020-03-25 10:00:11 +00:00
|
|
|
|
2020-06-17 10:57:54 +00:00
|
|
|
onp := n.Methods["onPersist"]
|
|
|
|
onp.Func = getOnPersistWrapper(n.onPersist)
|
|
|
|
n.Methods["onPersist"] = onp
|
|
|
|
|
2020-09-23 08:48:31 +00:00
|
|
|
pp := n.Methods["postPersist"]
|
|
|
|
pp.Func = getOnPersistWrapper(n.postPersist)
|
|
|
|
n.Methods["postPersist"] = pp
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
desc := newDescriptor("unclaimedGas", smartcontract.IntegerType,
|
|
|
|
manifest.NewParameter("account", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("end", smartcontract.IntegerType))
|
2020-06-17 13:50:13 +00:00
|
|
|
md := newMethodAndPrice(n.unclaimedGas, 3000000, smartcontract.AllowStates)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
2020-08-03 08:43:51 +00:00
|
|
|
desc = newDescriptor("registerCandidate", smartcontract.BoolType,
|
2020-03-25 10:00:11 +00:00
|
|
|
manifest.NewParameter("pubkey", smartcontract.PublicKeyType))
|
2020-08-03 08:43:51 +00:00
|
|
|
md = newMethodAndPrice(n.registerCandidate, 5000000, smartcontract.AllowModifyStates)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
2020-08-06 11:57:10 +00:00
|
|
|
desc = newDescriptor("unregisterCandidate", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("pubkey", smartcontract.PublicKeyType))
|
|
|
|
md = newMethodAndPrice(n.unregisterCandidate, 5000000, smartcontract.AllowModifyStates)
|
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
desc = newDescriptor("vote", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("account", smartcontract.Hash160Type),
|
2020-08-03 12:00:27 +00:00
|
|
|
manifest.NewParameter("pubkey", smartcontract.PublicKeyType))
|
2020-06-17 13:50:13 +00:00
|
|
|
md = newMethodAndPrice(n.vote, 500000000, smartcontract.AllowModifyStates)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
2020-08-03 08:43:51 +00:00
|
|
|
desc = newDescriptor("getCandidates", smartcontract.ArrayType)
|
|
|
|
md = newMethodAndPrice(n.getCandidatesCall, 100000000, smartcontract.AllowStates)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
2020-08-03 12:00:27 +00:00
|
|
|
desc = newDescriptor("getСommittee", smartcontract.ArrayType)
|
|
|
|
md = newMethodAndPrice(n.getCommittee, 100000000, smartcontract.AllowStates)
|
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
desc = newDescriptor("getNextBlockValidators", smartcontract.ArrayType)
|
2020-06-17 13:50:13 +00:00
|
|
|
md = newMethodAndPrice(n.getNextBlockValidators, 100000000, smartcontract.AllowStates)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
2020-08-26 10:06:19 +00:00
|
|
|
desc = newDescriptor("getGasPerBlock", smartcontract.IntegerType)
|
|
|
|
md = newMethodAndPrice(n.getGASPerBlock, 100_0000, smartcontract.AllowStates)
|
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
|
|
|
desc = newDescriptor("setGasPerBlock", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("gasPerBlock", smartcontract.IntegerType))
|
|
|
|
md = newMethodAndPrice(n.setGASPerBlock, 500_0000, smartcontract.AllowModifyStates)
|
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initializes NEO contract.
|
|
|
|
func (n *NEO) Initialize(ic *interop.Context) error {
|
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-08-03 11:31:42 +00:00
|
|
|
if n.nep5TokenNative.getTotalSupply(ic.DAO).Sign() != 0 {
|
2020-04-22 20:00:18 +00:00
|
|
|
return errors.New("already initialized")
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 07:24:54 +00:00
|
|
|
committee := ic.Chain.GetStandByCommittee()
|
2020-09-28 12:29:43 +00:00
|
|
|
err := n.updateCache(committee, ic.Chain)
|
2020-09-24 12:36:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-28 07:24:54 +00:00
|
|
|
|
2020-09-24 12:36:14 +00:00
|
|
|
err = ic.DAO.PutStorageItem(n.ContractID, prefixCommittee, &state.StorageItem{Value: committee.Bytes()})
|
2020-08-28 07:24:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:51:46 +00:00
|
|
|
h, err := getStandbyValidatorsHash(ic)
|
2020-03-25 10:00:11 +00:00
|
|
|
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
|
|
|
|
2020-08-26 09:07:30 +00:00
|
|
|
gr := &state.GASRecord{{Index: 0, GASPerBlock: *big.NewInt(5 * GASFactor)}}
|
2020-09-28 07:51:25 +00:00
|
|
|
n.gasPerBlock.Store(*gr)
|
|
|
|
n.gasPerBlockChanged.Store(false)
|
2020-08-26 09:07:30 +00:00
|
|
|
err = ic.DAO.PutStorageItem(n.ContractID, []byte{prefixGASPerBlock}, &state.StorageItem{Value: gr.Bytes()})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-10 14:07:14 +00:00
|
|
|
err = ic.DAO.PutStorageItem(n.ContractID, []byte{prefixVotersCount}, &state.StorageItem{Value: []byte{}})
|
2020-08-03 12:00:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:29:43 +00:00
|
|
|
func (n *NEO) updateCache(committee keys.PublicKeys, bc blockchainer.Blockchainer) error {
|
|
|
|
n.committee.Store(committee)
|
|
|
|
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(committee.Copy())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.committeeHash.Store(hash.Hash160(script))
|
|
|
|
|
2020-08-28 07:24:54 +00:00
|
|
|
nextVals := committee[:bc.GetConfig().ValidatorsCount].Copy()
|
|
|
|
sort.Sort(nextVals)
|
|
|
|
n.nextValidators.Store(nextVals)
|
2020-09-28 12:29:43 +00:00
|
|
|
return nil
|
2020-08-28 07:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) updateCommittee(ic *interop.Context) error {
|
|
|
|
votesChanged := n.votesChanged.Load().(bool)
|
|
|
|
if !votesChanged {
|
|
|
|
// We need to put in storage anyway, as it affects dumps
|
|
|
|
committee := n.committee.Load().(keys.PublicKeys)
|
|
|
|
si := &state.StorageItem{Value: committee.Bytes()}
|
|
|
|
return ic.DAO.PutStorageItem(n.ContractID, prefixCommittee, si)
|
|
|
|
}
|
|
|
|
|
|
|
|
committee, err := n.ComputeCommitteeMembers(ic.Chain, ic.DAO)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-28 12:29:43 +00:00
|
|
|
if err := n.updateCache(committee, ic.Chain); err != nil {
|
2020-09-24 12:36:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-28 07:24:54 +00:00
|
|
|
n.votesChanged.Store(false)
|
|
|
|
si := &state.StorageItem{Value: committee.Bytes()}
|
|
|
|
return ic.DAO.PutStorageItem(n.ContractID, prefixCommittee, si)
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:03:34 +00:00
|
|
|
func shouldUpdateCommittee(h uint32, bc blockchainer.Blockchainer) bool {
|
|
|
|
cfg := bc.GetConfig()
|
|
|
|
r := cfg.ValidatorsCount + len(cfg.StandbyCommittee)
|
|
|
|
return h%uint32(r) == 0
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
// OnPersist implements Contract interface.
|
|
|
|
func (n *NEO) OnPersist(ic *interop.Context) error {
|
2020-09-28 12:29:43 +00:00
|
|
|
gpb := n.gasPerBlockChanged.Load()
|
|
|
|
if gpb == nil {
|
|
|
|
committee := keys.PublicKeys{}
|
|
|
|
si := ic.DAO.GetStorageItem(n.ContractID, prefixCommittee)
|
|
|
|
if err := committee.DecodeBytes(si.Value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := n.updateCache(committee, ic.Chain); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var gr state.GASRecord
|
|
|
|
si = ic.DAO.GetStorageItem(n.ContractID, []byte{prefixGASPerBlock})
|
|
|
|
if err := gr.FromBytes(si.Value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.gasPerBlock.Store(gr)
|
|
|
|
n.gasPerBlockChanged.Store(false)
|
|
|
|
}
|
2020-09-22 10:03:34 +00:00
|
|
|
if shouldUpdateCommittee(ic.Block.Index, ic.Chain) {
|
|
|
|
if err := n.updateCommittee(ic); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-26 13:16:57 +00:00
|
|
|
}
|
2020-09-23 08:48:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-28 07:24:54 +00:00
|
|
|
|
2020-09-23 08:48:31 +00:00
|
|
|
// PostPersist implements Contract interface.
|
|
|
|
func (n *NEO) PostPersist(ic *interop.Context) error {
|
2020-09-28 07:51:25 +00:00
|
|
|
gas := n.GetGASPerBlock(ic.DAO, ic.Block.Index)
|
2020-08-28 07:24:54 +00:00
|
|
|
pubs := n.GetCommitteeMembers()
|
2020-08-26 13:16:57 +00:00
|
|
|
index := int(ic.Block.Index) % len(ic.Chain.GetConfig().StandbyCommittee)
|
|
|
|
gas.Mul(gas, big.NewInt(committeeRewardRatio))
|
|
|
|
n.GAS.mint(ic, pubs[index].GetScriptHash(), gas.Div(gas, big.NewInt(100)))
|
2020-09-28 07:51:25 +00:00
|
|
|
n.OnPersistEnd(ic.DAO)
|
2020-08-28 07:24:54 +00:00
|
|
|
return nil
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 07:51:25 +00:00
|
|
|
// OnPersistEnd updates cached values if they've been changed.
|
|
|
|
func (n *NEO) OnPersistEnd(d dao.DAO) {
|
|
|
|
if n.gasPerBlockChanged.Load().(bool) {
|
|
|
|
gr := n.getGASRecordFromDAO(d)
|
|
|
|
n.gasPerBlock.Store(gr)
|
|
|
|
n.gasPerBlockChanged.Store(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-06-23 19:31:57 +00:00
|
|
|
si.Value = acc.Bytes()
|
2020-04-26 09:51:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-29 19:38:38 +00:00
|
|
|
if err := n.ModifyAccountVotes(acc, ic.DAO, amount, false); err != nil {
|
2020-08-03 12:00:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if acc.VoteTo != nil {
|
|
|
|
if err := n.modifyVoterTurnout(ic.DAO, amount); err != nil {
|
2020-04-26 10:42:05 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-26 10:00:17 +00:00
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
acc.Balance.Add(&acc.Balance, amount)
|
2020-06-23 18:57:05 +00:00
|
|
|
if acc.Balance.Sign() != 0 {
|
|
|
|
si.Value = acc.Bytes()
|
|
|
|
} else {
|
|
|
|
si.Value = nil
|
|
|
|
}
|
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-08-26 09:07:30 +00:00
|
|
|
gen, err := n.CalculateBonus(ic, &acc.Balance, acc.BalanceHeight, ic.Block.Index)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
acc.BalanceHeight = ic.Block.Index
|
2020-07-09 09:57:24 +00:00
|
|
|
n.GAS.mint(ic, h, gen)
|
2020-03-25 10:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func (n *NEO) unclaimedGas(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-03-25 10:00:11 +00:00
|
|
|
u := toUint160(args[0])
|
|
|
|
end := uint32(toBigInt(args[1]).Int64())
|
|
|
|
bs, err := ic.DAO.GetNEP5Balances(u)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-07-28 09:23:58 +00:00
|
|
|
tr := bs.Trackers[n.ContractID]
|
2020-03-25 10:00:11 +00:00
|
|
|
|
2020-08-26 09:07:30 +00:00
|
|
|
gen, err := n.CalculateBonus(ic, &tr.Balance, tr.LastUpdatedBlock, end)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-07-09 09:57:24 +00:00
|
|
|
return stackitem.NewBigInteger(gen)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 10:06:19 +00:00
|
|
|
func (n *NEO) getGASPerBlock(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2020-09-28 07:51:25 +00:00
|
|
|
gas := n.GetGASPerBlock(ic.DAO, ic.Block.Index)
|
2020-08-26 10:06:19 +00:00
|
|
|
return stackitem.NewBigInteger(gas)
|
|
|
|
}
|
|
|
|
|
2020-09-28 07:51:25 +00:00
|
|
|
func (n *NEO) getGASRecordFromDAO(d dao.DAO) state.GASRecord {
|
|
|
|
var gr state.GASRecord
|
|
|
|
si := d.GetStorageItem(n.ContractID, []byte{prefixGASPerBlock})
|
|
|
|
_ = gr.FromBytes(si.Value)
|
|
|
|
return gr
|
|
|
|
}
|
|
|
|
|
2020-08-26 10:06:19 +00:00
|
|
|
// GetGASPerBlock returns gas generated for block with provided index.
|
2020-09-28 07:51:25 +00:00
|
|
|
func (n *NEO) GetGASPerBlock(d dao.DAO, index uint32) *big.Int {
|
2020-08-26 10:06:19 +00:00
|
|
|
var gr state.GASRecord
|
2020-09-28 07:51:25 +00:00
|
|
|
if n.gasPerBlockChanged.Load().(bool) {
|
|
|
|
gr = n.getGASRecordFromDAO(d)
|
|
|
|
} else {
|
|
|
|
gr = n.gasPerBlock.Load().(state.GASRecord)
|
2020-08-26 10:06:19 +00:00
|
|
|
}
|
|
|
|
for i := len(gr) - 1; i >= 0; i-- {
|
|
|
|
if gr[i].Index <= index {
|
2020-09-28 07:51:25 +00:00
|
|
|
g := gr[i].GASPerBlock
|
|
|
|
return &g
|
2020-08-26 10:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 07:51:25 +00:00
|
|
|
panic("contract not initialized")
|
2020-08-26 10:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommitteeAddress returns address of the committee.
|
2020-09-24 12:36:14 +00:00
|
|
|
func (n *NEO) GetCommitteeAddress() util.Uint160 {
|
|
|
|
return n.committeeHash.Load().(util.Uint160)
|
2020-08-26 10:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) setGASPerBlock(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
gas := toBigInt(args[0])
|
|
|
|
ok, err := n.SetGASPerBlock(ic, ic.Block.Index+1, gas)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.NewBool(ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGASPerBlock sets gas generated for blocks after index.
|
|
|
|
func (n *NEO) SetGASPerBlock(ic *interop.Context, index uint32, gas *big.Int) (bool, error) {
|
|
|
|
if gas.Sign() == -1 || gas.Cmp(big.NewInt(10*GASFactor)) == 1 {
|
|
|
|
return false, errors.New("invalid value for GASPerBlock")
|
|
|
|
}
|
2020-09-24 12:36:14 +00:00
|
|
|
h := n.GetCommitteeAddress()
|
2020-08-26 10:06:19 +00:00
|
|
|
ok, err := runtime.CheckHashedWitness(ic, h)
|
|
|
|
if err != nil || !ok {
|
|
|
|
return ok, err
|
|
|
|
}
|
|
|
|
si := ic.DAO.GetStorageItem(n.ContractID, []byte{prefixGASPerBlock})
|
|
|
|
var gr state.GASRecord
|
|
|
|
if err := gr.FromBytes(si.Value); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(gr) > 0 && gr[len(gr)-1].Index == index {
|
|
|
|
gr[len(gr)-1].GASPerBlock = *gas
|
|
|
|
} else {
|
|
|
|
gr = append(gr, state.GASIndexPair{
|
|
|
|
Index: index,
|
|
|
|
GASPerBlock: *gas,
|
|
|
|
})
|
|
|
|
}
|
2020-09-28 07:51:25 +00:00
|
|
|
n.gasPerBlockChanged.Store(true)
|
2020-08-26 10:06:19 +00:00
|
|
|
return true, ic.DAO.PutStorageItem(n.ContractID, []byte{prefixGASPerBlock}, &state.StorageItem{Value: gr.Bytes()})
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:07:30 +00:00
|
|
|
// CalculateBonus calculates amount of gas generated for holding `value` NEO from start to end block.
|
|
|
|
func (n *NEO) CalculateBonus(ic *interop.Context, value *big.Int, start, end uint32) (*big.Int, error) {
|
|
|
|
if value.Sign() == 0 || start >= end {
|
|
|
|
return big.NewInt(0), nil
|
|
|
|
} else if value.Sign() < 0 {
|
|
|
|
return nil, errors.New("negative value")
|
|
|
|
}
|
|
|
|
si := ic.DAO.GetStorageItem(n.ContractID, []byte{prefixGASPerBlock})
|
|
|
|
var gr state.GASRecord
|
|
|
|
if err := gr.FromBytes(si.Value); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var sum, tmp big.Int
|
|
|
|
for i := len(gr) - 1; i >= 0; i-- {
|
|
|
|
if gr[i].Index >= end {
|
|
|
|
continue
|
|
|
|
} else if gr[i].Index <= start {
|
|
|
|
tmp.SetInt64(int64(end - start))
|
|
|
|
tmp.Mul(&tmp, &gr[i].GASPerBlock)
|
|
|
|
sum.Add(&sum, &tmp)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tmp.SetInt64(int64(end - gr[i].Index))
|
|
|
|
tmp.Mul(&tmp, &gr[i].GASPerBlock)
|
|
|
|
sum.Add(&sum, &tmp)
|
|
|
|
end = gr[i].Index
|
|
|
|
}
|
|
|
|
res := new(big.Int).Mul(value, &sum)
|
|
|
|
res.Mul(res, tmp.SetInt64(neoHolderRewardRatio))
|
|
|
|
res.Div(res, tmp.SetInt64(100*NEOTotalSupply))
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 08:43:51 +00:00
|
|
|
func (n *NEO) registerCandidate(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-08-06 12:13:21 +00:00
|
|
|
pub := toPublicKey(args[0])
|
|
|
|
ok, err := runtime.CheckKeyedWitness(ic, pub)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
|
|
|
err = n.RegisterCandidateInternal(ic, pub)
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewBool(err == nil)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 13:24:22 +00:00
|
|
|
// RegisterCandidateInternal registers pub as a new candidate.
|
|
|
|
func (n *NEO) RegisterCandidateInternal(ic *interop.Context, pub *keys.PublicKey) error {
|
2020-04-25 21:23:30 +00:00
|
|
|
key := makeValidatorKey(pub)
|
2020-06-18 10:50:30 +00:00
|
|
|
si := ic.DAO.GetStorageItem(n.ContractID, key)
|
2020-08-03 12:00:27 +00:00
|
|
|
if si == nil {
|
2020-08-06 11:49:30 +00:00
|
|
|
c := &candidate{Registered: true}
|
|
|
|
si = &state.StorageItem{Value: c.Bytes()}
|
|
|
|
} else {
|
|
|
|
c := new(candidate).FromBytes(si.Value)
|
|
|
|
c.Registered = true
|
|
|
|
si.Value = c.Bytes()
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-06-18 10:50:30 +00:00
|
|
|
return ic.DAO.PutStorageItem(n.ContractID, key, si)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 11:57:10 +00:00
|
|
|
func (n *NEO) unregisterCandidate(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-08-06 12:13:21 +00:00
|
|
|
pub := toPublicKey(args[0])
|
|
|
|
ok, err := runtime.CheckKeyedWitness(ic, pub)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
|
|
|
err = n.UnregisterCandidateInternal(ic, pub)
|
2020-08-06 11:57:10 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func (n *NEO) vote(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-03-25 10:00:11 +00:00
|
|
|
acc := toUint160(args[0])
|
2020-08-03 12:00:27 +00:00
|
|
|
var pub *keys.PublicKey
|
|
|
|
if _, ok := args[1].(stackitem.Null); !ok {
|
|
|
|
pub = toPublicKey(args[1])
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-08-03 12:00:27 +00:00
|
|
|
err := n.VoteInternal(ic, acc, pub)
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewBool(err == nil)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VoteInternal votes from account h for validarors specified in pubs.
|
2020-08-03 12:00:27 +00:00
|
|
|
func (n *NEO) VoteInternal(ic *interop.Context, h util.Uint160, pub *keys.PublicKey) error {
|
2020-07-15 19:43:30 +00:00
|
|
|
ok, err := runtime.CheckHashedWitness(ic, h)
|
2020-03-25 10:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
return errors.New("invalid signature")
|
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
key := makeAccountKey(h)
|
2020-06-18 10:50:30 +00:00
|
|
|
si := ic.DAO.GetStorageItem(n.ContractID, key)
|
2020-04-23 18:28:37 +00:00
|
|
|
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-08-03 12:00:27 +00:00
|
|
|
if (acc.VoteTo == nil) != (pub == nil) {
|
|
|
|
val := &acc.Balance
|
|
|
|
if pub == nil {
|
|
|
|
val = new(big.Int).Neg(val)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-08-03 12:00:27 +00:00
|
|
|
if err := n.modifyVoterTurnout(ic.DAO, val); err != nil {
|
2020-03-25 10:00:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 19:38:38 +00:00
|
|
|
if err := n.ModifyAccountVotes(acc, ic.DAO, new(big.Int).Neg(&acc.Balance), false); err != nil {
|
2020-08-03 12:00:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
acc.VoteTo = pub
|
2020-09-29 19:38:38 +00:00
|
|
|
if err := n.ModifyAccountVotes(acc, ic.DAO, &acc.Balance, true); err != nil {
|
2020-04-23 18:28:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-26 11:00:17 +00:00
|
|
|
si.Value = acc.Bytes()
|
2020-06-18 10:50:30 +00:00
|
|
|
return ic.DAO.PutStorageItem(n.ContractID, key, si)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ModifyAccountVotes modifies votes of the specified account by value (can be negative).
|
2020-08-14 09:16:24 +00:00
|
|
|
// typ specifies if this modify is occurring during transfer or vote (with old or new validator).
|
2020-09-29 19:38:38 +00:00
|
|
|
func (n *NEO) ModifyAccountVotes(acc *state.NEOBalanceState, d dao.DAO, value *big.Int, isNewVote bool) error {
|
2020-08-20 15:49:01 +00:00
|
|
|
n.votesChanged.Store(true)
|
2020-08-03 12:00:27 +00:00
|
|
|
if acc.VoteTo != nil {
|
|
|
|
key := makeValidatorKey(acc.VoteTo)
|
2020-06-18 10:50:30 +00:00
|
|
|
si := d.GetStorageItem(n.ContractID, key)
|
2020-04-25 21:23:30 +00:00
|
|
|
if si == nil {
|
|
|
|
return errors.New("invalid validator")
|
|
|
|
}
|
2020-08-03 12:00:27 +00:00
|
|
|
cd := new(candidate).FromBytes(si.Value)
|
|
|
|
cd.Votes.Add(&cd.Votes, value)
|
2020-09-29 19:38:38 +00:00
|
|
|
if !isNewVote {
|
2020-08-03 12:00:27 +00:00
|
|
|
if !cd.Registered && cd.Votes.Sign() == 0 {
|
|
|
|
return d.DeleteStorageItem(n.ContractID, key)
|
|
|
|
}
|
2020-09-29 19:38:38 +00:00
|
|
|
} else if !cd.Registered {
|
|
|
|
return errors.New("validator must be registered")
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-08-03 12:00:27 +00:00
|
|
|
n.validators.Store(keys.PublicKeys(nil))
|
|
|
|
si.Value = cd.Bytes()
|
|
|
|
return d.PutStorageItem(n.ContractID, key, si)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 08:43:51 +00:00
|
|
|
func (n *NEO) getCandidates(d dao.DAO) ([]keyWithVotes, error) {
|
|
|
|
siMap, err := d.GetStorageItemsWithPrefix(n.ContractID, []byte{prefixCandidate})
|
2020-04-25 21:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arr := make([]keyWithVotes, 0, len(siMap))
|
|
|
|
for key, si := range siMap {
|
2020-08-03 12:00:27 +00:00
|
|
|
c := new(candidate).FromBytes(si.Value)
|
|
|
|
if c.Registered {
|
|
|
|
arr = append(arr, keyWithVotes{key, &c.Votes})
|
|
|
|
}
|
2020-04-25 21:23:30 +00:00
|
|
|
}
|
2020-05-29 08:02:26 +00:00
|
|
|
sort.Slice(arr, func(i, j int) bool { return strings.Compare(arr[i].Key, arr[j].Key) == -1 })
|
2020-04-25 21:23:30 +00:00
|
|
|
return arr, nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 08:43:51 +00:00
|
|
|
// GetCandidates returns current registered validators list with keys
|
2020-04-26 17:04:16 +00:00
|
|
|
// and votes.
|
2020-08-03 08:43:51 +00:00
|
|
|
func (n *NEO) GetCandidates(d dao.DAO) ([]state.Validator, error) {
|
|
|
|
kvs, err := n.getCandidates(d)
|
2020-04-26 17:04:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arr := make([]state.Validator, len(kvs))
|
|
|
|
for i := range kvs {
|
2020-07-13 09:59:41 +00:00
|
|
|
arr[i].Key, err = keys.NewPublicKeyFromBytes([]byte(kvs[i].Key), elliptic.P256())
|
2020-04-26 17:04:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arr[i].Votes = kvs[i].Votes
|
|
|
|
}
|
|
|
|
return arr, nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 08:43:51 +00:00
|
|
|
func (n *NEO) getCandidatesCall(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
validators, err := n.getCandidates(ic.DAO)
|
2020-04-25 21:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := make([]stackitem.Item, len(validators))
|
2020-04-25 21:23:30 +00:00
|
|
|
for i := range validators {
|
2020-06-03 12:55:06 +00:00
|
|
|
arr[i] = stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray([]byte(validators[i].Key)),
|
|
|
|
stackitem.NewBigInteger(validators[i].Votes),
|
2020-03-25 10:00:11 +00:00
|
|
|
})
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewArray(arr)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 09:53:44 +00:00
|
|
|
// ComputeNextBlockValidators returns an actual list of current validators.
|
|
|
|
func (n *NEO) ComputeNextBlockValidators(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) {
|
2020-06-29 07:36:44 +00:00
|
|
|
if vals := n.validators.Load().(keys.PublicKeys); vals != nil {
|
2020-07-11 11:07:48 +00:00
|
|
|
return vals.Copy(), nil
|
2020-06-24 10:20:59 +00:00
|
|
|
}
|
2020-09-22 09:53:44 +00:00
|
|
|
result, err := n.ComputeCommitteeMembers(bc, d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-25 21:23:30 +00:00
|
|
|
}
|
2020-09-22 09:53:44 +00:00
|
|
|
result = result[:bc.GetConfig().ValidatorsCount]
|
2020-08-10 16:16:54 +00:00
|
|
|
sort.Sort(result)
|
2020-06-24 10:20:59 +00:00
|
|
|
n.validators.Store(result)
|
2020-03-25 10:00:11 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:00:27 +00:00
|
|
|
func (n *NEO) getCommittee(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2020-08-28 07:24:54 +00:00
|
|
|
pubs := n.GetCommitteeMembers()
|
2020-08-03 12:00:27 +00:00
|
|
|
sort.Sort(pubs)
|
|
|
|
return pubsToArray(pubs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NEO) modifyVoterTurnout(d dao.DAO, amount *big.Int) error {
|
|
|
|
key := []byte{prefixVotersCount}
|
|
|
|
si := d.GetStorageItem(n.ContractID, key)
|
|
|
|
if si == nil {
|
|
|
|
return errors.New("voters count not found")
|
|
|
|
}
|
|
|
|
votersCount := bigint.FromBytes(si.Value)
|
|
|
|
votersCount.Add(votersCount, amount)
|
|
|
|
si.Value = bigint.ToBytes(votersCount)
|
|
|
|
return d.PutStorageItem(n.ContractID, key, si)
|
|
|
|
}
|
|
|
|
|
2020-08-28 07:24:54 +00:00
|
|
|
// GetCommitteeMembers returns public keys of nodes in committee using cached value.
|
|
|
|
func (n *NEO) GetCommitteeMembers() keys.PublicKeys {
|
|
|
|
return n.committee.Load().(keys.PublicKeys).Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComputeCommitteeMembers returns public keys of nodes in committee.
|
|
|
|
func (n *NEO) ComputeCommitteeMembers(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) {
|
2020-08-03 12:00:27 +00:00
|
|
|
key := []byte{prefixVotersCount}
|
|
|
|
si := d.GetStorageItem(n.ContractID, key)
|
|
|
|
if si == nil {
|
|
|
|
return nil, errors.New("voters count not found")
|
|
|
|
}
|
|
|
|
votersCount := bigint.FromBytes(si.Value)
|
|
|
|
// votersCount / totalSupply must be >= 0.2
|
|
|
|
votersCount.Mul(votersCount, big.NewInt(effectiveVoterTurnout))
|
|
|
|
voterTurnout := votersCount.Div(votersCount, n.getTotalSupply(d))
|
|
|
|
if voterTurnout.Sign() != 1 {
|
2020-08-28 07:24:54 +00:00
|
|
|
pubs := bc.GetStandByCommittee()
|
|
|
|
return pubs, nil
|
2020-08-03 12:00:27 +00:00
|
|
|
}
|
|
|
|
cs, err := n.getCandidates(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-27 15:05:26 +00:00
|
|
|
sbVals := bc.GetStandByCommittee()
|
2020-08-03 12:00:27 +00:00
|
|
|
count := len(sbVals)
|
|
|
|
if len(cs) < count {
|
|
|
|
return sbVals, nil
|
|
|
|
}
|
|
|
|
sort.Slice(cs, func(i, j int) bool {
|
|
|
|
// The most-voted validators should end up in the front of the list.
|
|
|
|
cmp := cs[i].Votes.Cmp(cs[j].Votes)
|
|
|
|
if cmp != 0 {
|
|
|
|
return cmp > 0
|
|
|
|
}
|
|
|
|
// Ties are broken with public keys.
|
|
|
|
return strings.Compare(cs[i].Key, cs[j].Key) == -1
|
|
|
|
})
|
|
|
|
pubs := make(keys.PublicKeys, count)
|
|
|
|
for i := range pubs {
|
|
|
|
pubs[i], err = keys.NewPublicKeyFromBytes([]byte(cs[i].Key), elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pubs, nil
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func (n *NEO) getNextBlockValidators(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2020-08-28 07:24:54 +00:00
|
|
|
result := n.GetNextBlockValidatorsInternal()
|
2020-03-25 10:00:11 +00:00
|
|
|
return pubsToArray(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNextBlockValidatorsInternal returns next block validators.
|
2020-08-28 07:24:54 +00:00
|
|
|
func (n *NEO) GetNextBlockValidatorsInternal() keys.PublicKeys {
|
|
|
|
return n.nextValidators.Load().(keys.PublicKeys).Copy()
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func pubsToArray(pubs keys.PublicKeys) stackitem.Item {
|
|
|
|
arr := make([]stackitem.Item, len(pubs))
|
2020-03-25 10:00:11 +00:00
|
|
|
for i := range pubs {
|
2020-06-03 12:55:06 +00:00
|
|
|
arr[i] = stackitem.NewByteArray(pubs[i].Bytes())
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewArray(arr)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func toPublicKey(s stackitem.Item) *keys.PublicKey {
|
2020-03-25 10:00:11 +00:00
|
|
|
buf, err := s.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
pub := new(keys.PublicKey)
|
|
|
|
if err := pub.DecodeBytes(buf); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pub
|
|
|
|
}
|