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"
|
2020-08-26 10:06:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
2020-08-03 13:24:22 +00:00
|
|
|
"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-26 13:16:57 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
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
|
2020-08-26 13:16:57 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0)
|
2020-08-03 13:24:22 +00:00
|
|
|
ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx)
|
2020-08-10 06:20:21 +00:00
|
|
|
ic.VM = vm.New()
|
2020-08-26 13:16:57 +00:00
|
|
|
ic.Block = bc.newBlock(tx)
|
2020-08-03 13:24:22 +00:00
|
|
|
|
2020-09-22 10:03:34 +00:00
|
|
|
freq := testchain.ValidatorsCount + testchain.CommitteeSize()
|
|
|
|
advanceChain := func(t *testing.T) {
|
|
|
|
for i := 0; i < freq; i++ {
|
|
|
|
require.NoError(t, neo.OnPersist(ic))
|
|
|
|
ic.Block.Index++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:16:54 +00:00
|
|
|
standBySorted := bc.GetStandByValidators()
|
|
|
|
sort.Sort(standBySorted)
|
2020-09-22 09:53:44 +00:00
|
|
|
pubs, err := neo.ComputeNextBlockValidators(bc, ic.DAO)
|
2020-08-03 13:24:22 +00:00
|
|
|
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-09-22 09:53:44 +00:00
|
|
|
pubs, err = neo.ComputeNextBlockValidators(bc, ic.DAO)
|
2020-08-03 13:24:22 +00:00
|
|
|
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-09-22 10:03:34 +00:00
|
|
|
advanceChain(t)
|
2020-08-28 07:24:54 +00:00
|
|
|
pubs = neo.GetNextBlockValidatorsInternal()
|
2020-08-20 15:49:01 +00:00
|
|
|
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-09-22 10:03:34 +00:00
|
|
|
advanceChain(t)
|
2020-09-22 09:53:44 +00:00
|
|
|
pubs, err = neo.ComputeNextBlockValidators(bc, ic.DAO)
|
2020-08-03 13:24:22 +00:00
|
|
|
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-28 07:24:54 +00:00
|
|
|
pubs = neo.GetNextBlockValidatorsInternal()
|
2020-08-20 15:49:01 +00:00
|
|
|
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]))
|
2020-09-22 10:03:34 +00:00
|
|
|
advanceChain(t)
|
2020-08-06 11:57:10 +00:00
|
|
|
|
2020-09-22 09:53:44 +00:00
|
|
|
pubs, err = neo.ComputeNextBlockValidators(bc, ic.DAO)
|
2020-08-06 11:57:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
for i := range pubs {
|
|
|
|
require.NotEqual(t, candidates[0], pubs[i])
|
|
|
|
}
|
2020-08-03 13:24:22 +00:00
|
|
|
}
|
2020-08-26 09:07:30 +00:00
|
|
|
|
2020-08-26 10:06:19 +00:00
|
|
|
func TestNEO_SetGasPerBlock(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)
|
|
|
|
ic.VM = vm.New()
|
2020-09-29 06:56:19 +00:00
|
|
|
ic.VM.LoadScript([]byte{byte(opcode.RET)})
|
2020-08-26 10:06:19 +00:00
|
|
|
|
2020-09-24 12:36:14 +00:00
|
|
|
h := neo.GetCommitteeAddress()
|
2020-08-26 10:06:19 +00:00
|
|
|
t.Run("Default", func(t *testing.T) {
|
2020-09-28 07:51:25 +00:00
|
|
|
g := neo.GetGASPerBlock(ic.DAO, 0)
|
2020-08-26 10:06:19 +00:00
|
|
|
require.EqualValues(t, 5*native.GASFactor, g.Int64())
|
|
|
|
})
|
|
|
|
t.Run("Invalid", func(t *testing.T) {
|
|
|
|
t.Run("InvalidSignature", func(t *testing.T) {
|
|
|
|
setSigner(tx, util.Uint160{})
|
|
|
|
ok, err := neo.SetGASPerBlock(ic, 10, big.NewInt(native.GASFactor))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, ok)
|
|
|
|
})
|
|
|
|
t.Run("TooBigValue", func(t *testing.T) {
|
|
|
|
setSigner(tx, h)
|
|
|
|
_, err := neo.SetGASPerBlock(ic, 10, big.NewInt(10*native.GASFactor+1))
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("Valid", func(t *testing.T) {
|
|
|
|
setSigner(tx, h)
|
2020-09-28 07:51:25 +00:00
|
|
|
ok, err := neo.SetGASPerBlock(ic, 10, big.NewInt(native.GASFactor*2))
|
2020-08-26 10:06:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ok)
|
2020-09-28 07:51:25 +00:00
|
|
|
neo.OnPersistEnd(ic.DAO)
|
|
|
|
_, err = ic.DAO.Persist()
|
|
|
|
require.NoError(t, err)
|
2020-08-26 10:06:19 +00:00
|
|
|
|
|
|
|
t.Run("Again", func(t *testing.T) {
|
|
|
|
setSigner(tx, h)
|
|
|
|
ok, err := neo.SetGASPerBlock(ic, 10, big.NewInt(native.GASFactor))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ok)
|
2020-09-28 07:51:25 +00:00
|
|
|
|
|
|
|
t.Run("NotPersisted", func(t *testing.T) {
|
|
|
|
g := neo.GetGASPerBlock(bc.dao, 10)
|
|
|
|
// Old value should be returned.
|
|
|
|
require.EqualValues(t, 2*native.GASFactor, g.Int64())
|
|
|
|
})
|
2020-08-26 10:06:19 +00:00
|
|
|
})
|
|
|
|
|
2020-09-28 07:51:25 +00:00
|
|
|
neo.OnPersistEnd(ic.DAO)
|
|
|
|
g := neo.GetGASPerBlock(ic.DAO, 9)
|
2020-08-26 10:06:19 +00:00
|
|
|
require.EqualValues(t, 5*native.GASFactor, g.Int64())
|
|
|
|
|
2020-09-28 07:51:25 +00:00
|
|
|
g = neo.GetGASPerBlock(ic.DAO, 10)
|
2020-08-26 10:06:19 +00:00
|
|
|
require.EqualValues(t, native.GASFactor, g.Int64())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:07:30 +00:00
|
|
|
func TestNEO_CalculateBonus(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
neo := bc.contracts.NEO
|
2020-08-26 10:06:19 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte{}, 0)
|
|
|
|
ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx)
|
2020-09-29 06:56:19 +00:00
|
|
|
ic.SpawnVM()
|
|
|
|
ic.VM.LoadScript([]byte{byte(opcode.RET)})
|
2020-08-26 09:07:30 +00:00
|
|
|
t.Run("Invalid", func(t *testing.T) {
|
|
|
|
_, err := neo.CalculateBonus(ic, new(big.Int).SetInt64(-1), 0, 1)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("Zero", func(t *testing.T) {
|
|
|
|
res, err := neo.CalculateBonus(ic, big.NewInt(0), 0, 100)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 0, res.Int64())
|
|
|
|
})
|
2020-08-26 10:06:19 +00:00
|
|
|
t.Run("ManyBlocks", func(t *testing.T) {
|
2020-09-24 12:36:14 +00:00
|
|
|
setSigner(tx, neo.GetCommitteeAddress())
|
2020-08-26 10:06:19 +00:00
|
|
|
ok, err := neo.SetGASPerBlock(ic, 10, big.NewInt(1*native.GASFactor))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
res, err := neo.CalculateBonus(ic, big.NewInt(100), 5, 15)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, (100*5*5/10)+(100*5*1/10), res.Int64())
|
|
|
|
|
|
|
|
})
|
2020-08-26 09:07:30 +00:00
|
|
|
}
|
2020-08-26 13:16:57 +00:00
|
|
|
|
|
|
|
func TestNEO_CommitteeBountyOnPersist(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
hs := make([]util.Uint160, testchain.CommitteeSize())
|
|
|
|
for i := range hs {
|
|
|
|
hs[i] = testchain.PrivateKeyByID(i).GetScriptHash()
|
|
|
|
}
|
|
|
|
|
2020-09-23 08:48:31 +00:00
|
|
|
const singleBounty = 25000000
|
|
|
|
bs := map[int]int64{0: singleBounty}
|
2020-08-26 13:16:57 +00:00
|
|
|
checkBalances := func() {
|
|
|
|
for i := 0; i < testchain.CommitteeSize(); i++ {
|
2020-09-23 08:48:31 +00:00
|
|
|
require.EqualValues(t, bs[i], bc.GetUtilityTokenBalance(hs[i]).Int64(), i)
|
2020-08-26 13:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < testchain.CommitteeSize()*2; i++ {
|
|
|
|
require.NoError(t, bc.AddBlock(bc.newBlock()))
|
2020-09-23 08:48:31 +00:00
|
|
|
bs[(i+1)%testchain.CommitteeSize()] += singleBounty
|
2020-08-26 13:16:57 +00:00
|
|
|
checkBalances()
|
|
|
|
}
|
|
|
|
}
|