2020-06-15 18:13:32 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2020-08-06 15:34:44 +00:00
|
|
|
"fmt"
|
2020-06-15 18:13:32 +00:00
|
|
|
"math/big"
|
|
|
|
"sort"
|
2020-06-18 18:36:20 +00:00
|
|
|
"sync"
|
2020-06-15 18:13:32 +00:00
|
|
|
|
2020-10-02 15:15:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-06-15 18:13:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-12-13 18:25:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2020-06-15 18:13:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-10-06 09:00:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-06-15 18:13:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-22 16:03:05 +00:00
|
|
|
policyContractID = -3
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
defaultMaxBlockSize = 1024 * 256
|
|
|
|
defaultMaxTransactionsPerBlock = 512
|
2020-12-14 09:18:59 +00:00
|
|
|
defaultExecFeeFactor = interop.DefaultBaseExecFee
|
2020-06-15 18:13:32 +00:00
|
|
|
defaultFeePerByte = 1000
|
2020-07-25 11:32:04 +00:00
|
|
|
defaultMaxVerificationGas = 50000000
|
2020-08-03 13:35:05 +00:00
|
|
|
defaultMaxBlockSystemFee = 9000 * GASFactor
|
|
|
|
// minBlockSystemFee is the minimum allowed system fee per block.
|
|
|
|
minBlockSystemFee = 4007600
|
2020-12-14 09:18:59 +00:00
|
|
|
// maxExecFeeFactor is the maximum allowed execution fee factor.
|
|
|
|
maxExecFeeFactor = 1000
|
2020-10-02 15:15:16 +00:00
|
|
|
// maxFeePerByte is the maximum allowed fee per byte value.
|
|
|
|
maxFeePerByte = 100_000_000
|
2020-12-14 09:41:23 +00:00
|
|
|
// maxStoragePrice is the maximum allowed price for a byte of storage.
|
|
|
|
maxStoragePrice = 10000000
|
2020-10-21 12:51:59 +00:00
|
|
|
|
|
|
|
// blockedAccountPrefix is a prefix used to store blocked account.
|
|
|
|
blockedAccountPrefix = 15
|
2020-06-15 18:13:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// maxTransactionsPerBlockKey is a key used to store the maximum number of
|
|
|
|
// transactions allowed in block.
|
|
|
|
maxTransactionsPerBlockKey = []byte{23}
|
2020-12-14 09:18:59 +00:00
|
|
|
// execFeeFactorKey is a key used to store execution fee factor.
|
|
|
|
execFeeFactorKey = []byte{18}
|
2020-06-15 18:13:32 +00:00
|
|
|
// feePerByteKey is a key used to store the minimum fee per byte for
|
|
|
|
// transaction.
|
|
|
|
feePerByteKey = []byte{10}
|
|
|
|
// maxBlockSizeKey is a key used to store the maximum block size value.
|
2020-08-03 13:00:15 +00:00
|
|
|
maxBlockSizeKey = []byte{12}
|
2020-08-03 13:35:05 +00:00
|
|
|
// maxBlockSystemFeeKey is a key used to store the maximum block system fee value.
|
|
|
|
maxBlockSystemFeeKey = []byte{17}
|
2020-12-14 09:41:23 +00:00
|
|
|
// storagePriceKey is a key used to store storage price.
|
|
|
|
storagePriceKey = []byte{19}
|
2020-06-15 18:13:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Policy represents Policy native contract.
|
|
|
|
type Policy struct {
|
|
|
|
interop.ContractMD
|
2020-06-18 18:36:20 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
// 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
|
2020-12-14 09:18:59 +00:00
|
|
|
execFeeFactor uint32
|
2020-06-18 18:36:20 +00:00
|
|
|
feePerByte int64
|
2020-08-03 13:35:05 +00:00
|
|
|
maxBlockSystemFee int64
|
2020-07-25 11:32:04 +00:00
|
|
|
maxVerificationGas int64
|
2020-12-14 09:41:23 +00:00
|
|
|
storagePrice uint32
|
2020-10-21 12:51:59 +00:00
|
|
|
blockedAccounts []util.Uint160
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ interop.Contract = (*Policy)(nil)
|
|
|
|
|
|
|
|
// newPolicy returns Policy native contract.
|
|
|
|
func newPolicy() *Policy {
|
2020-12-13 18:25:04 +00:00
|
|
|
p := &Policy{ContractMD: *interop.NewContractMD(nativenames.Policy)}
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
p.ContractID = policyContractID
|
|
|
|
|
|
|
|
desc := newDescriptor("getMaxTransactionsPerBlock", smartcontract.IntegerType)
|
2020-12-11 07:34:01 +00:00
|
|
|
md := newMethodAndPrice(p.getMaxTransactionsPerBlock, 1000000, smartcontract.ReadStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
desc = newDescriptor("getMaxBlockSize", smartcontract.IntegerType)
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.getMaxBlockSize, 1000000, smartcontract.ReadStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
desc = newDescriptor("getFeePerByte", smartcontract.IntegerType)
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.getFeePerByte, 1000000, smartcontract.ReadStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
2020-10-21 12:51:59 +00:00
|
|
|
desc = newDescriptor("isBlocked", smartcontract.BoolType,
|
2020-12-22 14:30:45 +00:00
|
|
|
manifest.NewParameter("account", smartcontract.ByteArrayType))
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.isBlocked, 1000000, smartcontract.ReadStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
2020-08-03 13:35:05 +00:00
|
|
|
desc = newDescriptor("getMaxBlockSystemFee", smartcontract.IntegerType)
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.getMaxBlockSystemFee, 1000000, smartcontract.ReadStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-08-03 13:35:05 +00:00
|
|
|
|
2020-12-14 09:18:59 +00:00
|
|
|
desc = newDescriptor("getExecFeeFactor", smartcontract.IntegerType)
|
|
|
|
md = newMethodAndPrice(p.getExecFeeFactor, 1000000, smartcontract.ReadStates)
|
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("setExecFeeFactor", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
|
|
|
md = newMethodAndPrice(p.setExecFeeFactor, 3000000, smartcontract.WriteStates)
|
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
2020-12-14 09:41:23 +00:00
|
|
|
desc = newDescriptor("getStoragePrice", smartcontract.IntegerType)
|
|
|
|
md = newMethodAndPrice(p.getStoragePrice, 1000000, smartcontract.ReadStates)
|
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("setStoragePrice", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
|
|
|
md = newMethodAndPrice(p.setStoragePrice, 3000000, smartcontract.WriteStates)
|
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
2020-06-15 18:13:32 +00:00
|
|
|
desc = newDescriptor("setMaxBlockSize", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.setMaxBlockSize, 3000000, smartcontract.WriteStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
desc = newDescriptor("setMaxTransactionsPerBlock", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.setMaxTransactionsPerBlock, 3000000, smartcontract.WriteStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
desc = newDescriptor("setFeePerByte", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.setFeePerByte, 3000000, smartcontract.WriteStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
2020-08-03 13:35:05 +00:00
|
|
|
desc = newDescriptor("setMaxBlockSystemFee", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.setMaxBlockSystemFee, 3000000, smartcontract.WriteStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-08-03 13:35:05 +00:00
|
|
|
|
2020-06-15 18:13:32 +00:00
|
|
|
desc = newDescriptor("blockAccount", smartcontract.BoolType,
|
2020-12-22 14:30:45 +00:00
|
|
|
manifest.NewParameter("account", smartcontract.ByteArrayType))
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.blockAccount, 3000000, smartcontract.WriteStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
desc = newDescriptor("unblockAccount", smartcontract.BoolType,
|
2020-12-22 14:30:45 +00:00
|
|
|
manifest.NewParameter("account", smartcontract.ByteArrayType))
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(p.unblockAccount, 3000000, smartcontract.WriteStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata implements Contract interface.
|
|
|
|
func (p *Policy) Metadata() *interop.ContractMD {
|
|
|
|
return &p.ContractMD
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initializes Policy native contract and implements Contract interface.
|
|
|
|
func (p *Policy) Initialize(ic *interop.Context) error {
|
2020-06-18 18:36:20 +00:00
|
|
|
p.isValid = true
|
|
|
|
p.maxTransactionsPerBlock = defaultMaxTransactionsPerBlock
|
|
|
|
p.maxBlockSize = defaultMaxBlockSize
|
2020-12-14 09:18:59 +00:00
|
|
|
p.execFeeFactor = defaultExecFeeFactor
|
2020-06-18 18:36:20 +00:00
|
|
|
p.feePerByte = defaultFeePerByte
|
2020-08-03 13:35:05 +00:00
|
|
|
p.maxBlockSystemFee = defaultMaxBlockSystemFee
|
2020-07-25 11:32:04 +00:00
|
|
|
p.maxVerificationGas = defaultMaxVerificationGas
|
2020-12-14 09:41:23 +00:00
|
|
|
p.storagePrice = StoragePrice
|
2020-10-06 09:00:42 +00:00
|
|
|
p.blockedAccounts = make([]util.Uint160, 0)
|
2020-06-18 18:36:20 +00:00
|
|
|
|
2020-06-15 18:13:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnPersist implements Contract interface.
|
|
|
|
func (p *Policy) OnPersist(ic *interop.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:36:06 +00:00
|
|
|
// PostPersist implements Contract interface.
|
|
|
|
func (p *Policy) PostPersist(ic *interop.Context) error {
|
2020-12-13 20:30:21 +00:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2020-06-18 18:36:20 +00:00
|
|
|
if p.isValid {
|
2020-10-06 09:00:42 +00:00
|
|
|
return nil
|
2020-06-18 18:36:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 20:30:21 +00:00
|
|
|
p.maxTransactionsPerBlock = getUint32WithKey(p.ContractID, ic.DAO, maxTransactionsPerBlockKey, defaultMaxTransactionsPerBlock)
|
|
|
|
p.maxBlockSize = getUint32WithKey(p.ContractID, ic.DAO, maxBlockSizeKey, defaultMaxBlockSize)
|
2020-12-14 09:18:59 +00:00
|
|
|
p.execFeeFactor = getUint32WithKey(p.ContractID, ic.DAO, execFeeFactorKey, defaultExecFeeFactor)
|
2020-12-13 20:30:21 +00:00
|
|
|
p.feePerByte = getInt64WithKey(p.ContractID, ic.DAO, feePerByteKey, defaultFeePerByte)
|
|
|
|
p.maxBlockSystemFee = getInt64WithKey(p.ContractID, ic.DAO, maxBlockSystemFeeKey, defaultMaxBlockSystemFee)
|
2020-08-03 10:30:51 +00:00
|
|
|
p.maxVerificationGas = defaultMaxVerificationGas
|
2020-12-14 09:41:23 +00:00
|
|
|
p.storagePrice = getUint32WithKey(p.ContractID, ic.DAO, storagePriceKey, StoragePrice)
|
2020-08-03 10:30:51 +00:00
|
|
|
|
2020-10-21 12:51:59 +00:00
|
|
|
p.blockedAccounts = make([]util.Uint160, 0)
|
2020-12-13 20:30:21 +00:00
|
|
|
siMap, err := ic.DAO.GetStorageItemsWithPrefix(p.ContractID, []byte{blockedAccountPrefix})
|
2020-10-06 09:00:42 +00:00
|
|
|
if err != nil {
|
2020-10-21 12:51:59 +00:00
|
|
|
return fmt.Errorf("failed to get blocked accounts from storage: %w", err)
|
2020-10-06 09:00:42 +00:00
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
for key := range siMap {
|
|
|
|
hash, err := util.Uint160DecodeBytesBE([]byte(key))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode blocked account hash: %w", err)
|
|
|
|
}
|
|
|
|
p.blockedAccounts = append(p.blockedAccounts, hash)
|
|
|
|
}
|
|
|
|
sort.Slice(p.blockedAccounts, func(i, j int) bool {
|
|
|
|
return p.blockedAccounts[i].Less(p.blockedAccounts[j])
|
|
|
|
})
|
2020-10-06 09:00:42 +00:00
|
|
|
|
2020-06-18 18:36:20 +00:00
|
|
|
p.isValid = true
|
2020-10-06 09:00:42 +00:00
|
|
|
return nil
|
2020-06-18 18:36:20 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 18:13:32 +00:00
|
|
|
// 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 {
|
2020-06-18 18:36:20 +00:00
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
if p.isValid {
|
|
|
|
return p.maxTransactionsPerBlock
|
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
return getUint32WithKey(p.ContractID, dao, maxTransactionsPerBlockKey, defaultMaxTransactionsPerBlock)
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getMaxBlockSize is Policy contract method and returns maximum block size.
|
|
|
|
func (p *Policy) getMaxBlockSize(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2020-08-03 15:49:22 +00:00
|
|
|
return stackitem.NewBigInteger(big.NewInt(int64(p.GetMaxBlockSizeInternal(ic.DAO))))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMaxBlockSizeInternal returns maximum block size.
|
|
|
|
func (p *Policy) GetMaxBlockSizeInternal(dao dao.DAO) uint32 {
|
2020-06-18 18:36:20 +00:00
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
if p.isValid {
|
2020-08-03 15:49:22 +00:00
|
|
|
return p.maxBlockSize
|
2020-06-18 18:36:20 +00:00
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
return getUint32WithKey(p.ContractID, dao, maxBlockSizeKey, defaultMaxBlockSize)
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getFeePerByte is Policy contract method and returns required transaction's fee
|
|
|
|
// per byte.
|
|
|
|
func (p *Policy) getFeePerByte(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(p.GetFeePerByteInternal(ic.DAO)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeePerByteInternal returns required transaction's fee per byte.
|
|
|
|
func (p *Policy) GetFeePerByteInternal(dao dao.DAO) int64 {
|
2020-06-18 18:36:20 +00:00
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
if p.isValid {
|
|
|
|
return p.feePerByte
|
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
return getInt64WithKey(p.ContractID, dao, feePerByteKey, defaultFeePerByte)
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 11:32:04 +00:00
|
|
|
// GetMaxVerificationGas returns maximum gas allowed to be burned during verificaion.
|
|
|
|
func (p *Policy) GetMaxVerificationGas(_ dao.DAO) int64 {
|
2020-08-03 10:30:51 +00:00
|
|
|
if p.isValid {
|
|
|
|
return p.maxVerificationGas
|
|
|
|
}
|
|
|
|
return defaultMaxVerificationGas
|
2020-07-25 11:32:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 13:35:05 +00:00
|
|
|
// getMaxBlockSystemFee is Policy contract method and returns the maximum overall
|
|
|
|
// system fee per block.
|
|
|
|
func (p *Policy) getMaxBlockSystemFee(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(p.GetMaxBlockSystemFeeInternal(ic.DAO)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMaxBlockSystemFeeInternal the maximum overall system fee per block.
|
|
|
|
func (p *Policy) GetMaxBlockSystemFeeInternal(dao dao.DAO) int64 {
|
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
if p.isValid {
|
|
|
|
return p.maxBlockSystemFee
|
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
return getInt64WithKey(p.ContractID, dao, maxBlockSystemFeeKey, defaultMaxBlockSystemFee)
|
2020-08-03 13:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 09:18:59 +00:00
|
|
|
func (p *Policy) getExecFeeFactor(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(int64(p.GetExecFeeFactorInternal(ic.DAO))))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetExecFeeFactorInternal returns current execution fee factor.
|
|
|
|
func (p *Policy) GetExecFeeFactorInternal(d dao.DAO) int64 {
|
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
if p.isValid {
|
|
|
|
return int64(p.execFeeFactor)
|
|
|
|
}
|
|
|
|
return int64(getUint32WithKey(p.ContractID, d, execFeeFactorKey, defaultExecFeeFactor))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policy) setExecFeeFactor(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
value := toUint32(args[0])
|
|
|
|
if value <= 0 || maxExecFeeFactor < value {
|
|
|
|
panic(fmt.Errorf("ExecFeeFactor must be between 0 and %d", maxExecFeeFactor))
|
|
|
|
}
|
|
|
|
ok, err := checkValidators(ic)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
err = setUint32WithKey(p.ContractID, ic.DAO, execFeeFactorKey, uint32(value))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.isValid = false
|
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
2020-10-21 12:51:59 +00:00
|
|
|
// isBlocked is Policy contract method and checks whether provided account is blocked.
|
|
|
|
func (p *Policy) isBlocked(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
hash := toUint160(args[0])
|
|
|
|
return stackitem.NewBool(p.IsBlockedInternal(ic.DAO, hash))
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 12:51:59 +00:00
|
|
|
// IsBlockedInternal checks whether provided account is blocked
|
|
|
|
func (p *Policy) IsBlockedInternal(dao dao.DAO, hash util.Uint160) bool {
|
2020-10-06 09:00:42 +00:00
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
if p.isValid {
|
2020-10-21 12:51:59 +00:00
|
|
|
length := len(p.blockedAccounts)
|
|
|
|
i := sort.Search(length, func(i int) bool {
|
|
|
|
return !p.blockedAccounts[i].Less(hash)
|
|
|
|
})
|
|
|
|
if length != 0 && i != length && p.blockedAccounts[i].Equals(hash) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
key := append([]byte{blockedAccountPrefix}, hash.BytesBE()...)
|
|
|
|
return dao.GetStorageItem(p.ContractID, key) != nil
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 09:41:23 +00:00
|
|
|
func (p *Policy) getStoragePrice(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(p.GetStoragePriceInternal(ic.DAO)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStoragePriceInternal returns current execution fee factor.
|
|
|
|
func (p *Policy) GetStoragePriceInternal(d dao.DAO) int64 {
|
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
if p.isValid {
|
|
|
|
return int64(p.storagePrice)
|
|
|
|
}
|
|
|
|
return int64(getUint32WithKey(p.ContractID, d, storagePriceKey, StoragePrice))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policy) setStoragePrice(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
value := toUint32(args[0])
|
|
|
|
if value <= 0 || maxStoragePrice < value {
|
|
|
|
panic(fmt.Errorf("StoragePrice must be between 0 and %d", maxStoragePrice))
|
|
|
|
}
|
|
|
|
ok, err := checkValidators(ic)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
err = setUint32WithKey(p.ContractID, ic.DAO, storagePriceKey, uint32(value))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.isValid = false
|
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
2020-06-15 18:13:32 +00:00
|
|
|
// 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 {
|
2020-10-02 15:15:16 +00:00
|
|
|
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))
|
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
ok, err := checkValidators(ic)
|
2020-06-15 18:13:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
2020-06-18 18:36:20 +00:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2020-11-27 10:55:48 +00:00
|
|
|
err = setUint32WithKey(p.ContractID, ic.DAO, maxTransactionsPerBlockKey, value)
|
2020-06-15 18:13:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-18 18:36:20 +00:00
|
|
|
p.isValid = false
|
2020-06-15 18:13:32 +00:00
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setMaxBlockSize is Policy contract method and sets maximum block size.
|
|
|
|
func (p *Policy) setMaxBlockSize(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-10-02 15:15:16 +00:00
|
|
|
value := uint32(toBigInt(args[0]).Int64())
|
|
|
|
if value > payload.MaxSize {
|
|
|
|
panic(fmt.Errorf("MaxBlockSize cannot be more than the maximum payload size = %d", payload.MaxSize))
|
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
ok, err := checkValidators(ic)
|
2020-06-15 18:13:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
2020-06-18 18:36:20 +00:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2020-11-27 10:55:48 +00:00
|
|
|
err = setUint32WithKey(p.ContractID, ic.DAO, maxBlockSizeKey, value)
|
2020-06-15 18:13:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-18 18:36:20 +00:00
|
|
|
p.isValid = false
|
2020-06-15 18:13:32 +00:00
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setFeePerByte is Policy contract method and sets transaction's fee per byte.
|
|
|
|
func (p *Policy) setFeePerByte(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-10-02 15:15:16 +00:00
|
|
|
value := toBigInt(args[0]).Int64()
|
|
|
|
if value < 0 || value > maxFeePerByte {
|
|
|
|
panic(fmt.Errorf("FeePerByte shouldn't be negative or greater than %d", maxFeePerByte))
|
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
ok, err := checkValidators(ic)
|
2020-06-15 18:13:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
2020-06-18 18:36:20 +00:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2020-11-27 10:55:48 +00:00
|
|
|
err = setInt64WithKey(p.ContractID, ic.DAO, feePerByteKey, value)
|
2020-06-15 18:13:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-18 18:36:20 +00:00
|
|
|
p.isValid = false
|
2020-06-15 18:13:32 +00:00
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:35:05 +00:00
|
|
|
// setMaxBlockSystemFee is Policy contract method and sets the maximum system fee per block.
|
|
|
|
func (p *Policy) setMaxBlockSystemFee(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-10-02 15:15:16 +00:00
|
|
|
value := toBigInt(args[0]).Int64()
|
|
|
|
if value <= minBlockSystemFee {
|
|
|
|
panic(fmt.Errorf("MaxBlockSystemFee cannot be less then %d", minBlockSystemFee))
|
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
ok, err := checkValidators(ic)
|
2020-08-03 13:35:05 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2020-11-27 10:55:48 +00:00
|
|
|
err = setInt64WithKey(p.ContractID, ic.DAO, maxBlockSystemFeeKey, value)
|
2020-08-03 13:35:05 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.isValid = false
|
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
2020-06-15 18:13:32 +00:00
|
|
|
// blockAccount is Policy contract method and adds given account hash to the list
|
|
|
|
// of blocked accounts.
|
|
|
|
func (p *Policy) blockAccount(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-11-27 10:55:48 +00:00
|
|
|
ok, err := checkValidators(ic)
|
2020-06-15 18:13:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
hash := toUint160(args[0])
|
|
|
|
if p.IsBlockedInternal(ic.DAO, hash) {
|
2020-06-15 18:13:32 +00:00
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
key := append([]byte{blockedAccountPrefix}, hash.BytesBE()...)
|
2020-10-06 09:00:42 +00:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2020-10-21 12:51:59 +00:00
|
|
|
err = ic.DAO.PutStorageItem(p.ContractID, key, &state.StorageItem{
|
|
|
|
Value: []byte{0x01},
|
2020-06-15 18:13:32 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-06 09:00:42 +00:00
|
|
|
p.isValid = false
|
2020-06-15 18:13:32 +00:00
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unblockAccount is Policy contract method and removes given account hash from
|
|
|
|
// the list of blocked accounts.
|
|
|
|
func (p *Policy) unblockAccount(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-11-27 10:55:48 +00:00
|
|
|
ok, err := checkValidators(ic)
|
2020-06-15 18:13:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
hash := toUint160(args[0])
|
|
|
|
if !p.IsBlockedInternal(ic.DAO, hash) {
|
2020-06-15 18:13:32 +00:00
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
key := append([]byte{blockedAccountPrefix}, hash.BytesBE()...)
|
2020-10-06 09:00:42 +00:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2020-10-21 12:51:59 +00:00
|
|
|
err = ic.DAO.DeleteStorageItem(p.ContractID, key)
|
2020-06-15 18:13:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-06 09:00:42 +00:00
|
|
|
p.isValid = false
|
2020-06-15 18:13:32 +00:00
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:49:54 +00:00
|
|
|
// CheckPolicy checks whether transaction conforms to current policy restrictions
|
|
|
|
// like not being signed by blocked account or not exceeding block-level system
|
|
|
|
// fee limit.
|
2020-08-06 18:39:00 +00:00
|
|
|
func (p *Policy) CheckPolicy(d dao.DAO, tx *transaction.Transaction) error {
|
2020-10-21 12:51:59 +00:00
|
|
|
for _, signer := range tx.Signers {
|
|
|
|
if p.IsBlockedInternal(d, signer.Account) {
|
|
|
|
return fmt.Errorf("account %s is blocked", signer.Account.StringLE())
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-06 18:49:54 +00:00
|
|
|
maxBlockSystemFee := p.GetMaxBlockSystemFeeInternal(d)
|
|
|
|
if maxBlockSystemFee < tx.SystemFee {
|
|
|
|
return fmt.Errorf("transaction's fee can't exceed maximum block system fee %d", maxBlockSystemFee)
|
|
|
|
}
|
2020-08-06 15:34:44 +00:00
|
|
|
return nil
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|