2020-06-15 18:13:32 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2023-09-20 13:33:04 +00:00
|
|
|
"encoding/hex"
|
2020-08-06 15:34:44 +00:00
|
|
|
"fmt"
|
2020-06-15 18:13:32 +00:00
|
|
|
"math/big"
|
|
|
|
"sort"
|
|
|
|
|
2023-11-21 09:35:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
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"
|
2022-03-31 15:14:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2020-06-15 18:13:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2023-09-20 13:33:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-06-15 18:13:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-06-15 18:13:32 +00:00
|
|
|
"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 (
|
2021-02-15 15:43:10 +00:00
|
|
|
policyContractID = -7
|
2020-06-15 18:13:32 +00:00
|
|
|
|
2021-02-17 15:22:57 +00:00
|
|
|
defaultExecFeeFactor = interop.DefaultBaseExecFee
|
|
|
|
defaultFeePerByte = 1000
|
2021-07-07 13:14:49 +00:00
|
|
|
defaultMaxVerificationGas = 1_50000000
|
2023-09-20 13:33:04 +00:00
|
|
|
// defaultAttributeFee is a default fee for a transaction attribute those price wasn't set yet.
|
|
|
|
defaultAttributeFee = 0
|
2023-09-21 15:22:32 +00:00
|
|
|
// defaultNotaryAssistedFee is a default fee for a NotaryAssisted transaction attribute per key.
|
|
|
|
defaultNotaryAssistedFee = 1000_0000 // 0.1 GAS
|
2021-02-02 15:46:43 +00:00
|
|
|
// DefaultStoragePrice is the price to pay for 1 byte of storage.
|
|
|
|
DefaultStoragePrice = 100000
|
|
|
|
|
2020-12-14 09:18:59 +00:00
|
|
|
// maxExecFeeFactor is the maximum allowed execution fee factor.
|
2021-07-08 07:06:10 +00:00
|
|
|
maxExecFeeFactor = 100
|
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
|
2023-09-20 13:33:04 +00:00
|
|
|
// maxAttributeFee is the maximum allowed value for a transaction attribute fee.
|
|
|
|
maxAttributeFee = 10_00000000
|
2020-10-21 12:51:59 +00:00
|
|
|
|
|
|
|
// blockedAccountPrefix is a prefix used to store blocked account.
|
|
|
|
blockedAccountPrefix = 15
|
2023-09-20 13:33:04 +00:00
|
|
|
// attributeFeePrefix is a prefix used to store attribute fee.
|
|
|
|
attributeFeePrefix = 20
|
2020-06-15 18:13:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
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}
|
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
|
2022-04-12 14:29:11 +00:00
|
|
|
NEO *NEO
|
2023-09-21 15:22:32 +00:00
|
|
|
|
|
|
|
// p2pSigExtensionsEnabled defines whether the P2P signature extensions logic is relevant.
|
|
|
|
p2pSigExtensionsEnabled bool
|
2022-04-12 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PolicyCache struct {
|
2021-02-17 15:22:57 +00:00
|
|
|
execFeeFactor uint32
|
|
|
|
feePerByte int64
|
|
|
|
maxVerificationGas int64
|
|
|
|
storagePrice uint32
|
2023-09-20 13:33:04 +00:00
|
|
|
attributeFee map[transaction.AttrType]uint32
|
2021-02-17 15:22:57 +00:00
|
|
|
blockedAccounts []util.Uint160
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 14:48:58 +00:00
|
|
|
var (
|
2022-04-20 14:47:48 +00:00
|
|
|
_ interop.Contract = (*Policy)(nil)
|
|
|
|
_ dao.NativeContractCache = (*PolicyCache)(nil)
|
2022-04-15 14:48:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Copy implements NativeContractCache interface.
|
2022-04-20 14:47:48 +00:00
|
|
|
func (c *PolicyCache) Copy() dao.NativeContractCache {
|
2022-04-15 14:48:58 +00:00
|
|
|
cp := &PolicyCache{}
|
|
|
|
copyPolicyCache(c, cp)
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyPolicyCache(src, dst *PolicyCache) {
|
|
|
|
*dst = *src
|
2023-09-20 13:33:04 +00:00
|
|
|
dst.attributeFee = make(map[transaction.AttrType]uint32, len(src.attributeFee))
|
|
|
|
for t, v := range src.attributeFee {
|
|
|
|
dst.attributeFee[t] = v
|
|
|
|
}
|
2022-04-15 14:48:58 +00:00
|
|
|
dst.blockedAccounts = make([]util.Uint160, len(src.blockedAccounts))
|
|
|
|
copy(dst.blockedAccounts, src.blockedAccounts)
|
|
|
|
}
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
// newPolicy returns Policy native contract.
|
2023-09-21 15:22:32 +00:00
|
|
|
func newPolicy(p2pSigExtensionsEnabled bool) *Policy {
|
|
|
|
p := &Policy{
|
|
|
|
ContractMD: *interop.NewContractMD(nativenames.Policy, policyContractID),
|
|
|
|
p2pSigExtensionsEnabled: p2pSigExtensionsEnabled,
|
|
|
|
}
|
2024-04-08 11:29:44 +00:00
|
|
|
defer p.BuildHFSpecificMD(p.ActiveIn())
|
2020-06-15 18:13:32 +00:00
|
|
|
|
2021-02-17 15:33:53 +00:00
|
|
|
desc := newDescriptor("getFeePerByte", smartcontract.IntegerType)
|
2021-03-05 10:30:16 +00:00
|
|
|
md := newMethodAndPrice(p.getFeePerByte, 1<<15, callflag.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,
|
2021-01-12 15:06:27 +00:00
|
|
|
manifest.NewParameter("account", smartcontract.Hash160Type))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(p.isBlocked, 1<<15, callflag.ReadStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
2020-12-14 09:18:59 +00:00
|
|
|
desc = newDescriptor("getExecFeeFactor", smartcontract.IntegerType)
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(p.getExecFeeFactor, 1<<15, callflag.ReadStates)
|
2020-12-14 09:18:59 +00:00
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
2021-01-28 15:01:30 +00:00
|
|
|
desc = newDescriptor("setExecFeeFactor", smartcontract.VoidType,
|
2020-12-14 09:18:59 +00:00
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(p.setExecFeeFactor, 1<<15, callflag.States)
|
2020-12-14 09:18:59 +00:00
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
2020-12-14 09:41:23 +00:00
|
|
|
desc = newDescriptor("getStoragePrice", smartcontract.IntegerType)
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(p.getStoragePrice, 1<<15, callflag.ReadStates)
|
2020-12-14 09:41:23 +00:00
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
2021-01-28 15:01:30 +00:00
|
|
|
desc = newDescriptor("setStoragePrice", smartcontract.VoidType,
|
2020-12-14 09:41:23 +00:00
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(p.setStoragePrice, 1<<15, callflag.States)
|
2020-12-14 09:41:23 +00:00
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
2023-09-20 13:33:04 +00:00
|
|
|
desc = newDescriptor("getAttributeFee", smartcontract.IntegerType,
|
|
|
|
manifest.NewParameter("attributeType", smartcontract.IntegerType))
|
|
|
|
md = newMethodAndPrice(p.getAttributeFee, 1<<15, callflag.ReadStates)
|
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("setAttributeFee", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("attributeType", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
|
|
|
md = newMethodAndPrice(p.setAttributeFee, 1<<15, callflag.States)
|
|
|
|
p.AddMethod(md, desc)
|
|
|
|
|
2021-01-28 15:01:30 +00:00
|
|
|
desc = newDescriptor("setFeePerByte", smartcontract.VoidType,
|
2020-06-15 18:13:32 +00:00
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(p.setFeePerByte, 1<<15, callflag.States)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
desc = newDescriptor("blockAccount", smartcontract.BoolType,
|
2021-01-12 15:06:27 +00:00
|
|
|
manifest.NewParameter("account", smartcontract.Hash160Type))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(p.blockAccount, 1<<15, callflag.States)
|
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,
|
2021-01-12 15:06:27 +00:00
|
|
|
manifest.NewParameter("account", smartcontract.Hash160Type))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(p.unblockAccount, 1<<15, callflag.States)
|
2020-12-08 10:27:41 +00:00
|
|
|
p.AddMethod(md, desc)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Metadata implements the Contract interface.
|
2020-06-15 18:13:32 +00:00
|
|
|
func (p *Policy) Metadata() *interop.ContractMD {
|
|
|
|
return &p.ContractMD
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Initialize initializes Policy native contract and implements the Contract interface.
|
2024-04-09 11:39:19 +00:00
|
|
|
func (p *Policy) Initialize(ic *interop.Context, hf *config.Hardfork, newMD *interop.HFSpecificContractMD) error {
|
2024-03-27 17:48:14 +00:00
|
|
|
if hf != p.ActiveIn() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-16 14:48:15 +00:00
|
|
|
setIntWithKey(p.ID, ic.DAO, feePerByteKey, defaultFeePerByte)
|
|
|
|
setIntWithKey(p.ID, ic.DAO, execFeeFactorKey, defaultExecFeeFactor)
|
|
|
|
setIntWithKey(p.ID, ic.DAO, storagePriceKey, DefaultStoragePrice)
|
2021-02-02 15:46:43 +00:00
|
|
|
|
2022-04-19 15:57:46 +00:00
|
|
|
cache := &PolicyCache{
|
|
|
|
execFeeFactor: defaultExecFeeFactor,
|
|
|
|
feePerByte: defaultFeePerByte,
|
|
|
|
maxVerificationGas: defaultMaxVerificationGas,
|
|
|
|
storagePrice: DefaultStoragePrice,
|
2023-09-20 13:33:04 +00:00
|
|
|
attributeFee: map[transaction.AttrType]uint32{},
|
2022-04-19 15:57:46 +00:00
|
|
|
blockedAccounts: make([]util.Uint160, 0),
|
|
|
|
}
|
2023-09-21 15:22:32 +00:00
|
|
|
if p.p2pSigExtensionsEnabled {
|
|
|
|
setIntWithKey(p.ID, ic.DAO, []byte{attributeFeePrefix, byte(transaction.NotaryAssistedT)}, defaultNotaryAssistedFee)
|
|
|
|
cache.attributeFee[transaction.NotaryAssistedT] = defaultNotaryAssistedFee
|
|
|
|
}
|
2022-04-20 14:47:48 +00:00
|
|
|
ic.DAO.SetCache(p.ID, cache)
|
2020-06-18 18:36:20 +00:00
|
|
|
|
2020-06-15 18:13:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-26 09:52:59 +00:00
|
|
|
func (p *Policy) InitializeCache(blockHeight uint32, d *dao.Simple) error {
|
2022-04-12 14:29:11 +00:00
|
|
|
cache := &PolicyCache{}
|
|
|
|
err := p.fillCacheFromDAO(cache, d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-20 14:47:48 +00:00
|
|
|
d.SetCache(p.ID, cache)
|
2020-06-15 18:13:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:29:11 +00:00
|
|
|
func (p *Policy) fillCacheFromDAO(cache *PolicyCache, d *dao.Simple) error {
|
|
|
|
cache.execFeeFactor = uint32(getIntWithKey(p.ID, d, execFeeFactorKey))
|
|
|
|
cache.feePerByte = getIntWithKey(p.ID, d, feePerByteKey)
|
|
|
|
cache.maxVerificationGas = defaultMaxVerificationGas
|
|
|
|
cache.storagePrice = uint32(getIntWithKey(p.ID, d, storagePriceKey))
|
2020-08-03 10:30:51 +00:00
|
|
|
|
2022-04-12 14:29:11 +00:00
|
|
|
cache.blockedAccounts = make([]util.Uint160, 0)
|
2022-03-31 15:14:11 +00:00
|
|
|
var fErr error
|
2022-04-12 14:29:11 +00:00
|
|
|
d.Seek(p.ID, storage.SeekRange{Prefix: []byte{blockedAccountPrefix}}, func(k, _ []byte) bool {
|
2022-03-31 15:14:11 +00:00
|
|
|
hash, err := util.Uint160DecodeBytesBE(k)
|
2020-10-21 12:51:59 +00:00
|
|
|
if err != nil {
|
2022-03-31 15:14:11 +00:00
|
|
|
fErr = fmt.Errorf("failed to decode blocked account hash: %w", err)
|
|
|
|
return false
|
2020-10-21 12:51:59 +00:00
|
|
|
}
|
2022-04-12 14:29:11 +00:00
|
|
|
cache.blockedAccounts = append(cache.blockedAccounts, hash)
|
2022-03-31 15:14:11 +00:00
|
|
|
return true
|
|
|
|
})
|
2022-04-12 14:29:11 +00:00
|
|
|
if fErr != nil {
|
|
|
|
return fmt.Errorf("failed to initialize blocked accounts: %w", fErr)
|
2020-10-21 12:51:59 +00:00
|
|
|
}
|
2023-09-20 13:33:04 +00:00
|
|
|
|
|
|
|
cache.attributeFee = make(map[transaction.AttrType]uint32)
|
|
|
|
d.Seek(p.ID, storage.SeekRange{Prefix: []byte{attributeFeePrefix}}, func(k, v []byte) bool {
|
|
|
|
if len(k) != 1 {
|
|
|
|
fErr = fmt.Errorf("unexpected attribute type len %d (%s)", len(k), hex.EncodeToString(k))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t := transaction.AttrType(k[0])
|
|
|
|
value := bigint.FromBytes(v)
|
|
|
|
if value == nil {
|
|
|
|
fErr = fmt.Errorf("unexpected attribute value format: key=%s, value=%s", hex.EncodeToString(k), hex.EncodeToString(v))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cache.attributeFee[t] = uint32(value.Int64())
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if fErr != nil {
|
|
|
|
return fmt.Errorf("failed to initialize attribute fees: %w", fErr)
|
|
|
|
}
|
2022-04-12 14:29:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnPersist implements Contract interface.
|
|
|
|
func (p *Policy) OnPersist(ic *interop.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostPersist implements Contract interface.
|
|
|
|
func (p *Policy) PostPersist(ic *interop.Context) error {
|
2022-04-19 15:57:46 +00:00
|
|
|
return nil
|
2020-06-18 18:36:20 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 09:35:18 +00:00
|
|
|
// ActiveIn implements the Contract interface.
|
|
|
|
func (p *Policy) ActiveIn() *config.Hardfork {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// getFeePerByte is a Policy contract method that returns the required transaction's fee
|
2020-06-15 18:13:32 +00:00
|
|
|
// 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.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (p *Policy) GetFeePerByteInternal(dao *dao.Simple) int64 {
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := dao.GetROCache(p.ID).(*PolicyCache)
|
2022-04-19 15:57:46 +00:00
|
|
|
return cache.feePerByte
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetMaxVerificationGas returns the maximum gas allowed to be burned during verification.
|
2022-04-12 14:29:11 +00:00
|
|
|
func (p *Policy) GetMaxVerificationGas(dao *dao.Simple) int64 {
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := dao.GetROCache(p.ID).(*PolicyCache)
|
2022-04-19 15:57:46 +00:00
|
|
|
return cache.maxVerificationGas
|
2020-07-25 11:32:04 +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.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (p *Policy) GetExecFeeFactorInternal(d *dao.Simple) int64 {
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := d.GetROCache(p.ID).(*PolicyCache)
|
2022-04-19 15:57:46 +00:00
|
|
|
return int64(cache.execFeeFactor)
|
2020-12-14 09:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policy) setExecFeeFactor(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
value := toUint32(args[0])
|
|
|
|
if value <= 0 || maxExecFeeFactor < value {
|
2024-04-08 22:51:03 +00:00
|
|
|
panic(fmt.Errorf("ExecFeeFactor must be between 1 and %d", maxExecFeeFactor))
|
2020-12-14 09:18:59 +00:00
|
|
|
}
|
2021-01-21 12:05:15 +00:00
|
|
|
if !p.NEO.checkCommittee(ic) {
|
2021-01-28 15:01:30 +00:00
|
|
|
panic("invalid committee signature")
|
2020-12-14 09:18:59 +00:00
|
|
|
}
|
2022-02-16 14:48:15 +00:00
|
|
|
setIntWithKey(p.ID, ic.DAO, execFeeFactorKey, int64(value))
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := ic.DAO.GetRWCache(p.ID).(*PolicyCache)
|
2022-04-19 15:57:46 +00:00
|
|
|
cache.execFeeFactor = value
|
2021-01-28 15:01:30 +00:00
|
|
|
return stackitem.Null{}
|
2020-12-14 09:18:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// isBlocked is Policy contract method that checks whether provided account is blocked.
|
2020-10-21 12:51:59 +00:00
|
|
|
func (p *Policy) isBlocked(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
hash := toUint160(args[0])
|
2023-11-02 14:16:56 +00:00
|
|
|
_, blocked := p.isBlockedInternal(ic.DAO.GetROCache(p.ID).(*PolicyCache), hash)
|
2022-04-19 15:57:46 +00:00
|
|
|
return stackitem.NewBool(blocked)
|
|
|
|
}
|
|
|
|
|
2023-11-02 14:16:56 +00:00
|
|
|
// IsBlocked checks whether provided account is blocked. Normally it uses Policy
|
|
|
|
// cache, falling back to the DB queries when Policy cache is not available yet
|
|
|
|
// (the only case is native cache initialization pipeline, where native Neo cache
|
|
|
|
// is being initialized before the Policy's one).
|
2022-04-19 15:57:46 +00:00
|
|
|
func (p *Policy) IsBlocked(dao *dao.Simple, hash util.Uint160) bool {
|
2023-11-02 14:16:56 +00:00
|
|
|
cache := dao.GetROCache(p.ID)
|
|
|
|
if cache == nil {
|
|
|
|
key := append([]byte{blockedAccountPrefix}, hash.BytesBE()...)
|
|
|
|
return dao.GetStorageItem(p.ID, key) == nil
|
|
|
|
}
|
|
|
|
_, isBlocked := p.isBlockedInternal(cache.(*PolicyCache), hash)
|
2022-04-19 15:57:46 +00:00
|
|
|
return isBlocked
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 15:57:46 +00:00
|
|
|
// isBlockedInternal checks whether provided account is blocked. It returns position
|
|
|
|
// of the blocked account in the blocked accounts list (or the position it should be
|
|
|
|
// put at).
|
2023-11-02 14:16:56 +00:00
|
|
|
func (p *Policy) isBlockedInternal(roCache *PolicyCache, hash util.Uint160) (int, bool) {
|
|
|
|
length := len(roCache.blockedAccounts)
|
2022-04-19 15:57:46 +00:00
|
|
|
i := sort.Search(length, func(i int) bool {
|
2023-11-02 14:16:56 +00:00
|
|
|
return !roCache.blockedAccounts[i].Less(hash)
|
2022-04-19 15:57:46 +00:00
|
|
|
})
|
2023-11-02 14:16:56 +00:00
|
|
|
if length != 0 && i != length && roCache.blockedAccounts[i].Equals(hash) {
|
2022-04-19 15:57:46 +00:00
|
|
|
return i, true
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
2022-04-19 15:57:46 +00:00
|
|
|
return i, false
|
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.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (p *Policy) GetStoragePriceInternal(d *dao.Simple) int64 {
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := d.GetROCache(p.ID).(*PolicyCache)
|
2022-04-19 15:57:46 +00:00
|
|
|
return int64(cache.storagePrice)
|
2020-12-14 09:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policy) setStoragePrice(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
value := toUint32(args[0])
|
|
|
|
if value <= 0 || maxStoragePrice < value {
|
2024-04-08 22:51:03 +00:00
|
|
|
panic(fmt.Errorf("StoragePrice must be between 1 and %d", maxStoragePrice))
|
2020-12-14 09:41:23 +00:00
|
|
|
}
|
2021-01-21 12:05:15 +00:00
|
|
|
if !p.NEO.checkCommittee(ic) {
|
2021-01-28 15:01:30 +00:00
|
|
|
panic("invalid committee signature")
|
2020-12-14 09:41:23 +00:00
|
|
|
}
|
2022-02-16 14:48:15 +00:00
|
|
|
setIntWithKey(p.ID, ic.DAO, storagePriceKey, int64(value))
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := ic.DAO.GetRWCache(p.ID).(*PolicyCache)
|
2022-04-19 15:57:46 +00:00
|
|
|
cache.storagePrice = value
|
2021-01-28 15:01:30 +00:00
|
|
|
return stackitem.Null{}
|
2020-12-14 09:41:23 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 13:33:04 +00:00
|
|
|
func (p *Policy) getAttributeFee(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
t := transaction.AttrType(toUint8(args[0]))
|
|
|
|
if !transaction.IsValidAttrType(ic.Chain.GetConfig().ReservedAttributes, t) {
|
|
|
|
panic(fmt.Errorf("invalid attribute type: %d", t))
|
|
|
|
}
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(p.GetAttributeFeeInternal(ic.DAO, t)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAttributeFeeInternal returns required transaction's attribute fee.
|
|
|
|
func (p *Policy) GetAttributeFeeInternal(d *dao.Simple, t transaction.AttrType) int64 {
|
|
|
|
cache := d.GetROCache(p.ID).(*PolicyCache)
|
|
|
|
v, ok := cache.attributeFee[t]
|
|
|
|
if !ok {
|
|
|
|
// We may safely omit this part, but let it be here in case if defaultAttributeFee value is changed.
|
|
|
|
v = defaultAttributeFee
|
|
|
|
}
|
|
|
|
return int64(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policy) setAttributeFee(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
t := transaction.AttrType(toUint8(args[0]))
|
|
|
|
value := toUint32(args[1])
|
|
|
|
if !transaction.IsValidAttrType(ic.Chain.GetConfig().ReservedAttributes, t) {
|
|
|
|
panic(fmt.Errorf("invalid attribute type: %d", t))
|
|
|
|
}
|
|
|
|
if value > maxAttributeFee {
|
|
|
|
panic(fmt.Errorf("attribute value is out of range: %d", value))
|
|
|
|
}
|
|
|
|
if !p.NEO.checkCommittee(ic) {
|
|
|
|
panic("invalid committee signature")
|
|
|
|
}
|
|
|
|
setIntWithKey(p.ID, ic.DAO, []byte{attributeFeePrefix, byte(t)}, int64(value))
|
|
|
|
cache := ic.DAO.GetRWCache(p.ID).(*PolicyCache)
|
|
|
|
cache.attributeFee[t] = value
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// setFeePerByte is a Policy contract method that sets transaction's fee per byte.
|
2020-06-15 18:13:32 +00:00
|
|
|
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))
|
|
|
|
}
|
2021-01-21 12:05:15 +00:00
|
|
|
if !p.NEO.checkCommittee(ic) {
|
2021-01-28 15:01:30 +00:00
|
|
|
panic("invalid committee signature")
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
2022-02-16 14:48:15 +00:00
|
|
|
setIntWithKey(p.ID, ic.DAO, feePerByteKey, value)
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := ic.DAO.GetRWCache(p.ID).(*PolicyCache)
|
2022-04-19 15:57:46 +00:00
|
|
|
cache.feePerByte = value
|
2021-01-28 15:01:30 +00:00
|
|
|
return stackitem.Null{}
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// blockAccount is a Policy contract method that adds the given account hash to the list
|
2020-06-15 18:13:32 +00:00
|
|
|
// of blocked accounts.
|
|
|
|
func (p *Policy) blockAccount(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2021-01-21 12:05:15 +00:00
|
|
|
if !p.NEO.checkCommittee(ic) {
|
2021-01-28 15:01:30 +00:00
|
|
|
panic("invalid committee signature")
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
hash := toUint160(args[0])
|
2021-09-03 08:08:30 +00:00
|
|
|
for i := range ic.Natives {
|
|
|
|
if ic.Natives[i].Metadata().Hash == hash {
|
|
|
|
panic("cannot block native contract")
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 10:27:41 +00:00
|
|
|
return stackitem.NewBool(p.blockAccountInternal(ic.DAO, hash))
|
|
|
|
}
|
|
|
|
func (p *Policy) blockAccountInternal(d *dao.Simple, hash util.Uint160) bool {
|
2023-11-02 14:16:56 +00:00
|
|
|
i, blocked := p.isBlockedInternal(d.GetROCache(p.ID).(*PolicyCache), hash)
|
2022-04-19 15:57:46 +00:00
|
|
|
if blocked {
|
2022-05-04 10:27:41 +00:00
|
|
|
return false
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
key := append([]byte{blockedAccountPrefix}, hash.BytesBE()...)
|
2022-05-04 10:27:41 +00:00
|
|
|
d.PutStorageItem(p.ID, key, state.StorageItem{})
|
|
|
|
cache := d.GetRWCache(p.ID).(*PolicyCache)
|
2022-04-19 15:57:46 +00:00
|
|
|
if len(cache.blockedAccounts) == i {
|
|
|
|
cache.blockedAccounts = append(cache.blockedAccounts, hash)
|
|
|
|
} else {
|
|
|
|
cache.blockedAccounts = append(cache.blockedAccounts[:i+1], cache.blockedAccounts[i:]...)
|
|
|
|
cache.blockedAccounts[i] = hash
|
|
|
|
}
|
2022-05-04 10:27:41 +00:00
|
|
|
return true
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// unblockAccount is a Policy contract method that removes the given account hash from
|
2020-06-15 18:13:32 +00:00
|
|
|
// the list of blocked accounts.
|
|
|
|
func (p *Policy) unblockAccount(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2021-01-21 12:05:15 +00:00
|
|
|
if !p.NEO.checkCommittee(ic) {
|
2021-01-28 15:01:30 +00:00
|
|
|
panic("invalid committee signature")
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
hash := toUint160(args[0])
|
2023-11-02 14:16:56 +00:00
|
|
|
i, blocked := p.isBlockedInternal(ic.DAO.GetROCache(p.ID).(*PolicyCache), hash)
|
2022-04-19 15:57:46 +00:00
|
|
|
if !blocked {
|
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()...)
|
2022-02-16 14:48:15 +00:00
|
|
|
ic.DAO.DeleteStorageItem(p.ID, key)
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := ic.DAO.GetRWCache(p.ID).(*PolicyCache)
|
2022-04-19 15:57:46 +00:00
|
|
|
cache.blockedAccounts = append(cache.blockedAccounts[:i], cache.blockedAccounts[i+1:]...)
|
2020-06-15 18:13:32 +00:00
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CheckPolicy checks whether a transaction conforms to the current policy restrictions,
|
|
|
|
// like not being signed by a blocked account or not exceeding the block-level system
|
2020-08-06 18:49:54 +00:00
|
|
|
// fee limit.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (p *Policy) CheckPolicy(d *dao.Simple, tx *transaction.Transaction) error {
|
2023-11-02 14:57:45 +00:00
|
|
|
cache := d.GetROCache(p.ID).(*PolicyCache)
|
2020-10-21 12:51:59 +00:00
|
|
|
for _, signer := range tx.Signers {
|
2023-11-02 14:57:45 +00:00
|
|
|
if _, isBlocked := p.isBlockedInternal(cache, signer.Account); isBlocked {
|
2020-10-21 12:51:59 +00:00
|
|
|
return fmt.Errorf("account %s is blocked", signer.Account.StringLE())
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-06 15:34:44 +00:00
|
|
|
return nil
|
2020-06-15 18:13:32 +00:00
|
|
|
}
|