neo-go/pkg/core/native_policy_test.go

228 lines
7.7 KiB
Go
Raw Normal View History

package core
import (
"testing"
"github.com/nspcc-dev/neo-go/internal/random"
2021-01-21 12:05:15 +00:00
"github.com/nspcc-dev/neo-go/internal/testchain"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
2020-11-27 10:55:48 +00:00
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
)
2021-01-21 12:05:15 +00:00
func transferFundsToCommittee(t *testing.T, chain *Blockchain) {
transferTokenFromMultisigAccount(t, chain, testchain.CommitteeScriptHash(),
2021-01-22 12:12:09 +00:00
chain.contracts.GAS.Hash, 1000_00000000)
2021-01-21 12:05:15 +00:00
}
2020-12-16 13:11:12 +00:00
func testGetSet(t *testing.T, chain *Blockchain, hash util.Uint160, name string, defaultValue, minValue, maxValue int64) {
getName := "get" + name
setName := "set" + name
2021-01-21 12:05:15 +00:00
transferFundsToCommittee(t, chain)
t.Run("set, not signed by committee", func(t *testing.T) {
signer, err := wallet.NewAccount()
require.NoError(t, err)
2020-12-16 13:11:12 +00:00
invokeRes, err := invokeContractMethodBy(t, chain, signer, hash, setName, minValue+1)
require.NoError(t, err)
checkFAULTState(t, invokeRes)
})
2020-12-16 13:11:12 +00:00
t.Run("get, defult value", func(t *testing.T) {
res, err := invokeContractMethod(chain, 100000000, hash, getName)
require.NoError(t, err)
checkResult(t, res, stackitem.Make(defaultValue))
require.NoError(t, chain.persist())
})
2020-12-16 13:11:12 +00:00
t.Run("set, too small value", func(t *testing.T) {
2021-01-21 12:05:15 +00:00
res, err := invokeContractMethodGeneric(chain, 100000000, hash, setName, true, minValue-1)
require.NoError(t, err)
checkFAULTState(t, res)
})
2020-11-27 10:55:48 +00:00
if maxValue != 0 {
2020-12-16 13:11:12 +00:00
t.Run("set, too large value", func(t *testing.T) {
2021-01-21 12:05:15 +00:00
res, err := invokeContractMethodGeneric(chain, 100000000, hash, setName, true, maxValue+1)
require.NoError(t, err)
checkFAULTState(t, res)
})
}
t.Run("set, success", func(t *testing.T) {
// Set and get in the same block.
2021-01-21 12:05:15 +00:00
txSet, err := prepareContractMethodInvokeGeneric(chain, 100000000, hash, setName, true, defaultValue+1)
2020-11-27 10:55:48 +00:00
require.NoError(t, err)
2020-12-16 13:11:12 +00:00
txGet1, err := prepareContractMethodInvoke(chain, 100000000, hash, getName)
require.NoError(t, err)
aers, err := persistBlock(chain, txSet, txGet1)
require.NoError(t, err)
checkResult(t, aers[0], stackitem.Null{})
if name != "GasPerBlock" { // GasPerBlock is set on the next block
checkResult(t, aers[1], stackitem.Make(defaultValue+1))
}
require.NoError(t, chain.persist())
// Get in the next block.
2020-12-16 13:11:12 +00:00
res, err := invokeContractMethod(chain, 100000000, hash, getName)
require.NoError(t, err)
checkResult(t, res, stackitem.Make(defaultValue+1))
require.NoError(t, chain.persist())
2020-11-27 10:55:48 +00:00
})
}
func TestMaxTransactionsPerBlock(t *testing.T) {
chain := newTestChain(t)
defer chain.Close()
t.Run("get, internal method", func(t *testing.T) {
n := chain.contracts.Policy.GetMaxTransactionsPerBlockInternal(chain.dao)
require.Equal(t, 512, int(n))
})
2020-12-16 13:11:12 +00:00
testGetSet(t, chain, chain.contracts.Policy.Hash, "MaxTransactionsPerBlock", 512, 0, block.MaxTransactionsPerBlock)
}
func TestMaxBlockSize(t *testing.T) {
chain := newTestChain(t)
defer chain.Close()
t.Run("get, internal method", func(t *testing.T) {
n := chain.contracts.Policy.GetMaxBlockSizeInternal(chain.dao)
require.Equal(t, 1024*256, int(n))
})
2020-11-27 10:55:48 +00:00
2020-12-16 13:11:12 +00:00
testGetSet(t, chain, chain.contracts.Policy.Hash, "MaxBlockSize", 1024*256, 0, payload.MaxSize)
}
func TestFeePerByte(t *testing.T) {
chain := newTestChain(t)
defer chain.Close()
t.Run("get, internal method", func(t *testing.T) {
n := chain.contracts.Policy.GetFeePerByteInternal(chain.dao)
require.Equal(t, 1000, int(n))
})
2020-12-16 13:11:12 +00:00
testGetSet(t, chain, chain.contracts.Policy.Hash, "FeePerByte", 1000, 0, 100_000_000)
}
func TestExecFeeFactor(t *testing.T) {
chain := newTestChain(t)
defer chain.Close()
t.Run("get, internal method", func(t *testing.T) {
n := chain.contracts.Policy.GetExecFeeFactorInternal(chain.dao)
require.EqualValues(t, interop.DefaultBaseExecFee, n)
})
2020-12-16 13:11:12 +00:00
testGetSet(t, chain, chain.contracts.Policy.Hash, "ExecFeeFactor", interop.DefaultBaseExecFee, 1, 1000)
}
func TestBlockSystemFee(t *testing.T) {
chain := newTestChain(t)
defer chain.Close()
t.Run("get, internal method", func(t *testing.T) {
n := chain.contracts.Policy.GetMaxBlockSystemFeeInternal(chain.dao)
require.Equal(t, 9000*native.GASFactor, int(n))
})
2020-12-16 13:11:12 +00:00
testGetSet(t, chain, chain.contracts.Policy.Hash, "MaxBlockSystemFee", 9000*native.GASFactor, 4007600, 0)
}
func TestStoragePrice(t *testing.T) {
chain := newTestChain(t)
defer chain.Close()
t.Run("get, internal method", func(t *testing.T) {
n := chain.contracts.Policy.GetStoragePriceInternal(chain.dao)
require.Equal(t, int64(native.StoragePrice), n)
})
2020-12-16 13:11:12 +00:00
testGetSet(t, chain, chain.contracts.Policy.Hash, "StoragePrice", native.StoragePrice, 1, 10000000)
}
func TestBlockedAccounts(t *testing.T) {
chain := newTestChain(t)
defer chain.Close()
account := util.Uint160{1, 2, 3}
2020-11-19 10:00:46 +00:00
policyHash := chain.contracts.Policy.Metadata().Hash
2021-01-21 12:05:15 +00:00
transferTokenFromMultisigAccount(t, chain, testchain.CommitteeScriptHash(),
chain.contracts.GAS.Hash, 100_00000000)
2020-10-21 12:51:59 +00:00
t.Run("isBlocked, internal method", func(t *testing.T) {
isBlocked := chain.contracts.Policy.IsBlockedInternal(chain.dao, random.Uint160())
require.Equal(t, false, isBlocked)
})
2020-10-21 12:51:59 +00:00
t.Run("isBlocked, contract method", func(t *testing.T) {
2020-11-19 10:00:46 +00:00
res, err := invokeContractMethod(chain, 100000000, policyHash, "isBlocked", random.Uint160())
require.NoError(t, err)
2020-10-21 12:51:59 +00:00
checkResult(t, res, stackitem.NewBool(false))
require.NoError(t, chain.persist())
})
t.Run("block-unblock account", func(t *testing.T) {
2021-01-21 12:05:15 +00:00
res, err := invokeContractMethodGeneric(chain, 100000000, policyHash, "blockAccount", true, account.BytesBE())
require.NoError(t, err)
checkResult(t, res, stackitem.NewBool(true))
2020-10-21 12:51:59 +00:00
isBlocked := chain.contracts.Policy.IsBlockedInternal(chain.dao, account)
require.Equal(t, isBlocked, true)
require.NoError(t, chain.persist())
2021-01-21 12:05:15 +00:00
res, err = invokeContractMethodGeneric(chain, 100000000, policyHash, "unblockAccount", true, account.BytesBE())
require.NoError(t, err)
checkResult(t, res, stackitem.NewBool(true))
2020-10-21 12:51:59 +00:00
isBlocked = chain.contracts.Policy.IsBlockedInternal(chain.dao, account)
require.Equal(t, false, isBlocked)
require.NoError(t, chain.persist())
})
t.Run("double-block", func(t *testing.T) {
// block
2021-01-21 12:05:15 +00:00
res, err := invokeContractMethodGeneric(chain, 100000000, policyHash, "blockAccount", true, account.BytesBE())
require.NoError(t, err)
checkResult(t, res, stackitem.NewBool(true))
require.NoError(t, chain.persist())
// double-block should fail
2021-01-21 12:05:15 +00:00
res, err = invokeContractMethodGeneric(chain, 100000000, policyHash, "blockAccount", true, account.BytesBE())
require.NoError(t, err)
checkResult(t, res, stackitem.NewBool(false))
require.NoError(t, chain.persist())
// unblock
2021-01-21 12:05:15 +00:00
res, err = invokeContractMethodGeneric(chain, 100000000, policyHash, "unblockAccount", true, account.BytesBE())
require.NoError(t, err)
checkResult(t, res, stackitem.NewBool(true))
require.NoError(t, chain.persist())
// unblock the same account should fail as we don't have it blocked
2021-01-21 12:05:15 +00:00
res, err = invokeContractMethodGeneric(chain, 100000000, policyHash, "unblockAccount", true, account.BytesBE())
require.NoError(t, err)
checkResult(t, res, stackitem.NewBool(false))
require.NoError(t, chain.persist())
})
2020-11-27 10:55:48 +00:00
t.Run("not signed by committee", func(t *testing.T) {
signer, err := wallet.NewAccount()
require.NoError(t, err)
invokeRes, err := invokeContractMethodBy(t, chain, signer, policyHash, "blockAccount", account.BytesBE())
require.NoError(t, err)
checkFAULTState(t, invokeRes)
invokeRes, err = invokeContractMethodBy(t, chain, signer, policyHash, "unblockAccount", account.BytesBE())
require.NoError(t, err)
checkFAULTState(t, invokeRes)
2020-11-27 10:55:48 +00:00
})
}