native/policy: move MaxTransactionsPerBlock to config

This commit is contained in:
Evgeniy Stratonikov 2021-02-17 18:22:57 +03:00
parent 924de3e090
commit 7b8533b67c
8 changed files with 25 additions and 116 deletions

View file

@ -120,14 +120,12 @@ func TestNativeHelpersCompile(t *testing.T) {
{"getFeePerByte", nil},
{"getMaxBlockSize", nil},
{"getMaxBlockSystemFee", nil},
{"getMaxTransactionsPerBlock", nil},
{"getStoragePrice", nil},
{"isBlocked", []string{u160}},
{"setExecFeeFactor", []string{"42"}},
{"setFeePerByte", []string{"42"}},
{"setMaxBlockSize", []string{"42"}},
{"setMaxBlockSystemFee", []string{"42"}},
{"setMaxTransactionsPerBlock", []string{"42"}},
{"setStoragePrice", []string{"42"}},
{"unblockAccount", []string{u160}},
})

View file

@ -20,6 +20,8 @@ type (
RemoveUntraceableBlocks bool `yaml:"RemoveUntraceableBlocks"`
// MaxTraceableBlocks is the length of the chain accessible to smart contracts.
MaxTraceableBlocks uint32 `yaml:"MaxTraceableBlocks"`
// MaxTransactionsPerBlock is the maximum amount of transactions per block.
MaxTransactionsPerBlock uint16 `yaml:"MaxTransactionsPerBlock"`
// P2PSigExtensions enables additional signature-related logic.
P2PSigExtensions bool `yaml:"P2PSigExtensions"`
// ReservedAttributes allows to have reserved attributes range for experimental or private purposes.

View file

@ -46,7 +46,8 @@ const (
defaultMemPoolSize = 50000
defaultP2PNotaryRequestPayloadPoolSize = 1000
defaultMaxTraceableBlocks = 2102400 // 1 year of 15s blocks
defaultMaxTraceableBlocks = 2102400 // 1 year of 15s blocks
defaultMaxTransactionsPerBlock = 512
verificationGasLimit = 100000000 // 1 GAS
)
@ -168,6 +169,11 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L
cfg.MaxTraceableBlocks = defaultMaxTraceableBlocks
log.Info("MaxTraceableBlocks is not set or wrong, using default value", zap.Uint32("MaxTraceableBlocks", cfg.MaxTraceableBlocks))
}
if cfg.MaxTransactionsPerBlock == 0 {
cfg.MaxTransactionsPerBlock = defaultMaxTransactionsPerBlock
log.Info("MaxTransactionsPerBlock is not set or wrong, using default value",
zap.Uint16("MaxTransactionsPerBlock", cfg.MaxTransactionsPerBlock))
}
committee, err := committeeFromConfig(cfg)
if err != nil {
return nil, err
@ -1347,7 +1353,7 @@ func (bc *Blockchain) GetMemPool() *mempool.Pool {
// ApplyPolicyToTxSet applies configured policies to given transaction set. It
// expects slice to be ordered by fee and returns a subslice of it.
func (bc *Blockchain) ApplyPolicyToTxSet(txes []*transaction.Transaction) []*transaction.Transaction {
maxTx := bc.contracts.Policy.GetMaxTransactionsPerBlockInternal(bc.dao)
maxTx := bc.config.MaxTransactionsPerBlock
if maxTx != 0 && len(txes) > int(maxTx) {
txes = txes[:maxTx]
}

View file

@ -6,7 +6,6 @@ import (
"sort"
"sync"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"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/native/nativenames"
@ -23,12 +22,11 @@ import (
const (
policyContractID = -5
defaultMaxBlockSize = 1024 * 256
defaultMaxTransactionsPerBlock = 512
defaultExecFeeFactor = interop.DefaultBaseExecFee
defaultFeePerByte = 1000
defaultMaxVerificationGas = 50000000
defaultMaxBlockSystemFee = 9000 * GASFactor
defaultMaxBlockSize = 1024 * 256
defaultExecFeeFactor = interop.DefaultBaseExecFee
defaultFeePerByte = 1000
defaultMaxVerificationGas = 50000000
defaultMaxBlockSystemFee = 9000 * GASFactor
// DefaultStoragePrice is the price to pay for 1 byte of storage.
DefaultStoragePrice = 100000
@ -46,9 +44,6 @@ const (
)
var (
// maxTransactionsPerBlockKey is a key used to store the maximum number of
// transactions allowed in block.
maxTransactionsPerBlockKey = []byte{23}
// execFeeFactorKey is a key used to store execution fee factor.
execFeeFactorKey = []byte{18}
// feePerByteKey is a key used to store the minimum fee per byte for
@ -70,15 +65,14 @@ type Policy struct {
// isValid defies whether cached values were changed during the current
// consensus iteration. If false, these values will be updated after
// blockchain DAO persisting. If true, we can safely use cached values.
isValid bool
maxTransactionsPerBlock uint32
maxBlockSize uint32
execFeeFactor uint32
feePerByte int64
maxBlockSystemFee int64
maxVerificationGas int64
storagePrice uint32
blockedAccounts []util.Uint160
isValid bool
maxBlockSize uint32
execFeeFactor uint32
feePerByte int64
maxBlockSystemFee int64
maxVerificationGas int64
storagePrice uint32
blockedAccounts []util.Uint160
}
var _ interop.Contract = (*Policy)(nil)
@ -88,12 +82,8 @@ func newPolicy() *Policy {
p := &Policy{ContractMD: *interop.NewContractMD(nativenames.Policy, policyContractID)}
defer p.UpdateHash()
desc := newDescriptor("getMaxTransactionsPerBlock", smartcontract.IntegerType)
md := newMethodAndPrice(p.getMaxTransactionsPerBlock, 1000000, callflag.ReadStates)
p.AddMethod(md, desc)
desc = newDescriptor("getMaxBlockSize", smartcontract.IntegerType)
md = newMethodAndPrice(p.getMaxBlockSize, 1000000, callflag.ReadStates)
desc := newDescriptor("getMaxBlockSize", smartcontract.IntegerType)
md := newMethodAndPrice(p.getMaxBlockSize, 1000000, callflag.ReadStates)
p.AddMethod(md, desc)
desc = newDescriptor("getFeePerByte", smartcontract.IntegerType)
@ -132,11 +122,6 @@ func newPolicy() *Policy {
md = newMethodAndPrice(p.setMaxBlockSize, 3000000, callflag.States)
p.AddMethod(md, desc)
desc = newDescriptor("setMaxTransactionsPerBlock", smartcontract.VoidType,
manifest.NewParameter("value", smartcontract.IntegerType))
md = newMethodAndPrice(p.setMaxTransactionsPerBlock, 3000000, callflag.States)
p.AddMethod(md, desc)
desc = newDescriptor("setFeePerByte", smartcontract.VoidType,
manifest.NewParameter("value", smartcontract.IntegerType))
md = newMethodAndPrice(p.setFeePerByte, 3000000, callflag.States)
@ -167,9 +152,6 @@ func (p *Policy) Metadata() *interop.ContractMD {
// Initialize initializes Policy native contract and implements Contract interface.
func (p *Policy) Initialize(ic *interop.Context) error {
if err := setIntWithKey(p.ID, ic.DAO, maxTransactionsPerBlockKey, defaultMaxTransactionsPerBlock); err != nil {
return err
}
if err := setIntWithKey(p.ID, ic.DAO, feePerByteKey, defaultFeePerByte); err != nil {
return err
}
@ -187,7 +169,6 @@ func (p *Policy) Initialize(ic *interop.Context) error {
}
p.isValid = true
p.maxTransactionsPerBlock = defaultMaxTransactionsPerBlock
p.maxBlockSize = defaultMaxBlockSize
p.execFeeFactor = defaultExecFeeFactor
p.feePerByte = defaultFeePerByte
@ -212,7 +193,6 @@ func (p *Policy) PostPersist(ic *interop.Context) error {
return nil
}
p.maxTransactionsPerBlock = uint32(getIntWithKey(p.ID, ic.DAO, maxTransactionsPerBlockKey))
p.maxBlockSize = uint32(getIntWithKey(p.ID, ic.DAO, maxBlockSizeKey))
p.execFeeFactor = uint32(getIntWithKey(p.ID, ic.DAO, execFeeFactorKey))
p.feePerByte = getIntWithKey(p.ID, ic.DAO, feePerByteKey)
@ -240,23 +220,6 @@ func (p *Policy) PostPersist(ic *interop.Context) error {
return nil
}
// getMaxTransactionsPerBlock is Policy contract method and returns the upper
// limit of transactions per block.
func (p *Policy) getMaxTransactionsPerBlock(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
return stackitem.NewBigInteger(big.NewInt(int64(p.GetMaxTransactionsPerBlockInternal(ic.DAO))))
}
// GetMaxTransactionsPerBlockInternal returns the upper limit of transactions per
// block.
func (p *Policy) GetMaxTransactionsPerBlockInternal(dao dao.DAO) uint32 {
p.lock.RLock()
defer p.lock.RUnlock()
if p.isValid {
return p.maxTransactionsPerBlock
}
return uint32(getIntWithKey(p.ID, dao, maxTransactionsPerBlockKey))
}
// getMaxBlockSize is Policy contract method and returns maximum block size.
func (p *Policy) getMaxBlockSize(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
return stackitem.NewBigInteger(big.NewInt(int64(p.GetMaxBlockSizeInternal(ic.DAO))))
@ -400,26 +363,6 @@ func (p *Policy) setStoragePrice(ic *interop.Context, args []stackitem.Item) sta
return stackitem.Null{}
}
// setMaxTransactionsPerBlock is Policy contract method and sets the upper limit
// of transactions per block.
func (p *Policy) setMaxTransactionsPerBlock(ic *interop.Context, args []stackitem.Item) stackitem.Item {
value := uint32(toBigInt(args[0]).Int64())
if value > block.MaxTransactionsPerBlock {
panic(fmt.Errorf("MaxTransactionsPerBlock cannot exceed the maximum allowed transactions per block = %d", block.MaxTransactionsPerBlock))
}
if !p.NEO.checkCommittee(ic) {
panic("invalid committee signature")
}
p.lock.Lock()
defer p.lock.Unlock()
err := setIntWithKey(p.ID, ic.DAO, maxTransactionsPerBlockKey, int64(value))
if err != nil {
panic(err)
}
p.isValid = false
return stackitem.Null{}
}
// setMaxBlockSize is Policy contract method and sets maximum block size.
func (p *Policy) setMaxBlockSize(ic *interop.Context, args []stackitem.Item) stackitem.Item {
value := uint32(toBigInt(args[0]).Int64())

View file

@ -5,7 +5,6 @@ import (
"github.com/nspcc-dev/neo-go/internal/random"
"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"
@ -76,17 +75,6 @@ func testGetSet(t *testing.T, chain *Blockchain, hash util.Uint160, name string,
})
}
func TestMaxTransactionsPerBlock(t *testing.T) {
chain := newTestChain(t)
t.Run("get, internal method", func(t *testing.T) {
n := chain.contracts.Policy.GetMaxTransactionsPerBlockInternal(chain.dao)
require.Equal(t, 512, int(n))
})
testGetSet(t, chain, chain.contracts.Policy.Hash, "MaxTransactionsPerBlock", 512, 0, block.MaxTransactionsPerBlock)
}
func TestMaxBlockSize(t *testing.T) {
chain := newTestChain(t)

View file

@ -8,16 +8,6 @@ import (
// Hash represents Policy contract hash.
const Hash = "\x7b\xc6\x81\xc0\xa1\xf7\x1d\x54\x34\x57\xb6\x8b\xba\x8d\x5f\x9f\xdd\x4e\x5e\xcc"
// GetMaxTransactionsPerBlock represents `getMaxTransactionsPerBlock` method of Policy native contract.
func GetMaxTransactionsPerBlock() int {
return contract.Call(interop.Hash160(Hash), "getMaxTransactionsPerBlock", contract.ReadStates).(int)
}
// SetMaxTransactionsPerBlock represents `setMaxTransactionsPerBlock` method of Policy native contract.
func SetMaxTransactionsPerBlock(value int) {
contract.Call(interop.Hash160(Hash), "setMaxTransactionsPerBlock", contract.States, value)
}
// GetMaxBlockSize represents `getMaxBlockSize` method of Policy native contract.
func GetMaxBlockSize() int {
return contract.Call(interop.Hash160(Hash), "getMaxBlockSize", contract.ReadStates).(int)

View file

@ -9,12 +9,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// GetMaxTransactionsPerBlock invokes `getMaxTransactionsPerBlock` method on a
// native Policy contract.
func (c *Client) GetMaxTransactionsPerBlock() (int64, error) {
return c.invokeNativePolicyMethod("getMaxTransactionsPerBlock")
}
// GetMaxBlockSize invokes `getMaxBlockSize` method on a native Policy contract.
func (c *Client) GetMaxBlockSize() (int64, error) {
return c.invokeNativePolicyMethod("getMaxBlockSize")

View file

@ -431,18 +431,6 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
},
},
},
"getMaxTransacctionsPerBlock": {
{
name: "positive",
invoke: func(c *Client) (interface{}, error) {
return c.GetMaxTransactionsPerBlock()
},
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"EMAMGmdldE1heFRyYW5zYWN0aW9uc1BlckJsb2NrDBSaYaRu7Je4kwbXzoHxW0YgkdAJMkFifVtS","stack":[{"type":"Integer","value":"512"}],"tx":null}}`,
result: func(c *Client) interface{} {
return int64(512)
},
},
},
"getMaxBlockSize": {
{
name: "positive",