2020-08-03 13:24:22 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
2020-08-10 16:16:54 +00:00
|
|
|
"sort"
|
2020-08-03 13:24:22 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/testchain"
|
2020-08-10 06:20:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-08-03 13:24:22 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-08-10 06:20:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-08-03 13:24:22 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setSigner(tx *transaction.Transaction, h util.Uint160) {
|
|
|
|
tx.Signers = []transaction.Signer{{
|
|
|
|
Account: h,
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNEO_Vote(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
neo := bc.contracts.NEO
|
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte{}, 0)
|
|
|
|
ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx)
|
2020-08-10 06:20:21 +00:00
|
|
|
ic.VM = vm.New()
|
2020-08-03 13:24:22 +00:00
|
|
|
|
2020-08-10 16:16:54 +00:00
|
|
|
standBySorted := bc.GetStandByValidators()
|
|
|
|
sort.Sort(standBySorted)
|
2020-08-03 13:24:22 +00:00
|
|
|
pubs, err := neo.GetValidatorsInternal(bc, ic.DAO)
|
|
|
|
require.NoError(t, err)
|
2020-08-10 16:16:54 +00:00
|
|
|
require.Equal(t, standBySorted, pubs)
|
2020-08-03 13:24:22 +00:00
|
|
|
|
|
|
|
sz := testchain.Size()
|
|
|
|
|
|
|
|
candidates := make(keys.PublicKeys, sz)
|
|
|
|
for i := 0; i < sz; i++ {
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
candidates[i] = priv.PublicKey()
|
|
|
|
if i > 0 {
|
|
|
|
require.NoError(t, neo.RegisterCandidateInternal(ic, candidates[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < sz; i++ {
|
|
|
|
to := testchain.PrivateKeyByID(i).GetScriptHash()
|
2020-08-10 06:20:21 +00:00
|
|
|
ic.VM.Load(testchain.MultisigVerificationScript())
|
|
|
|
ic.VM.LoadScriptWithHash(testchain.MultisigVerificationScript(), testchain.MultisigScriptHash(), smartcontract.All)
|
2020-08-03 13:24:22 +00:00
|
|
|
require.NoError(t, neo.TransferInternal(ic, testchain.MultisigScriptHash(), to, big.NewInt(int64(sz-i)*10000000)))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < sz; i++ {
|
2020-08-10 06:20:21 +00:00
|
|
|
priv := testchain.PrivateKeyByID(i)
|
|
|
|
h := priv.GetScriptHash()
|
2020-08-03 13:24:22 +00:00
|
|
|
setSigner(tx, h)
|
2020-08-10 06:20:21 +00:00
|
|
|
ic.VM.Load(priv.PublicKey().GetVerificationScript())
|
2020-08-03 13:24:22 +00:00
|
|
|
require.NoError(t, neo.VoteInternal(ic, h, candidates[i]))
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:51:46 +00:00
|
|
|
// We still haven't voted enough validators in.
|
2020-08-03 13:24:22 +00:00
|
|
|
pubs, err = neo.GetValidatorsInternal(bc, ic.DAO)
|
|
|
|
require.NoError(t, err)
|
2020-08-10 16:16:54 +00:00
|
|
|
require.Equal(t, standBySorted, pubs)
|
2020-08-03 13:24:22 +00:00
|
|
|
|
2020-08-20 15:49:01 +00:00
|
|
|
require.NoError(t, neo.OnPersist(ic))
|
|
|
|
pubs, err = neo.GetNextBlockValidatorsInternal(bc, ic.DAO)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, standBySorted, pubs)
|
|
|
|
|
2020-08-03 13:24:22 +00:00
|
|
|
// Register and give some value to the last validator.
|
|
|
|
require.NoError(t, neo.RegisterCandidateInternal(ic, candidates[0]))
|
2020-08-10 06:20:21 +00:00
|
|
|
priv := testchain.PrivateKeyByID(0)
|
|
|
|
h := priv.GetScriptHash()
|
2020-08-03 13:24:22 +00:00
|
|
|
setSigner(tx, h)
|
2020-08-10 06:20:21 +00:00
|
|
|
ic.VM.Load(priv.PublicKey().GetVerificationScript())
|
2020-08-03 13:24:22 +00:00
|
|
|
require.NoError(t, neo.VoteInternal(ic, h, candidates[0]))
|
|
|
|
|
2020-08-27 15:05:26 +00:00
|
|
|
for i := testchain.ValidatorsCount; i < testchain.CommitteeSize(); i++ {
|
|
|
|
priv := testchain.PrivateKey(i)
|
|
|
|
require.NoError(t, neo.RegisterCandidateInternal(ic, priv.PublicKey()))
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:24:22 +00:00
|
|
|
pubs, err = neo.GetValidatorsInternal(bc, ic.DAO)
|
|
|
|
require.NoError(t, err)
|
2020-08-10 16:16:54 +00:00
|
|
|
sortedCandidates := candidates.Copy()
|
|
|
|
sort.Sort(sortedCandidates)
|
|
|
|
require.EqualValues(t, sortedCandidates, pubs)
|
2020-08-06 11:57:10 +00:00
|
|
|
|
2020-08-20 15:49:01 +00:00
|
|
|
require.NoError(t, neo.OnPersist(ic))
|
|
|
|
pubs, err = neo.GetNextBlockValidatorsInternal(bc, ic.DAO)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, sortedCandidates, pubs)
|
|
|
|
|
2020-08-06 11:57:10 +00:00
|
|
|
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])
|
|
|
|
}
|
2020-08-03 13:24:22 +00:00
|
|
|
}
|