Skip to content

Commit

Permalink
native: introduce attribute pricing
Browse files Browse the repository at this point in the history
Port the neo-project/neo#2916.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed Nov 21, 2023
1 parent 80fcf81 commit 72f5632
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 22 deletions.
3 changes: 2 additions & 1 deletion internal/testchain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
// Ledger is an interface that abstracts the implementation of the blockchain.
type Ledger interface {
BlockHeight() uint32
CalculateAttributesFee(tx *transaction.Transaction) int64
FeePerByte() int64
GetBaseExecFee() int64
GetHeader(hash util.Uint256) (*block.Header, error)
Expand Down Expand Up @@ -135,7 +136,7 @@ func signTxGeneric(bc Ledger, sign func(hash.Hashable) []byte, verif []byte, txs
netFee, sizeDelta := fee.Calculate(bc.GetBaseExecFee(), verif)
tx.NetworkFee += netFee
size += sizeDelta
tx.NetworkFee += int64(size) * bc.FeePerByte()
tx.NetworkFee += int64(size)*bc.FeePerByte() + bc.CalculateAttributesFee(tx)
tx.Scripts = []transaction.Witness{{
InvocationScript: sign(tx),
VerificationScript: verif,
Expand Down
10 changes: 10 additions & 0 deletions pkg/compiler/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ func TestLedgerVMStates(t *testing.T) {
require.EqualValues(t, ledger.BreakState, vmstate.Break)
}

func TestPolicyAttributeType(t *testing.T) {
require.EqualValues(t, policy.HighPriorityT, transaction.HighPriority)
require.EqualValues(t, policy.OracleResponseT, transaction.OracleResponseT)
require.EqualValues(t, policy.NotValidBeforeT, transaction.NotValidBeforeT)
require.EqualValues(t, policy.ConflictsT, transaction.ConflictsT)
require.EqualValues(t, policy.NotaryAssistedT, transaction.NotaryAssistedT)
}

type nativeTestCase struct {
method string
params []string
Expand Down Expand Up @@ -179,6 +187,8 @@ func TestNativeHelpersCompile(t *testing.T) {
{"setFeePerByte", []string{"42"}},
{"setStoragePrice", []string{"42"}},
{"unblockAccount", []string{u160}},
{"getAttributeFee", []string{"1"}},
{"setAttributeFee", []string{"1", "123"}},
})
runNativeTestCases(t, cs.Ledger.ContractMD, "ledger", []nativeTestCase{
{"currentHash", nil},
Expand Down
1 change: 1 addition & 0 deletions pkg/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Ledger interface {
SubscribeForBlocks(ch chan *coreb.Block)
UnsubscribeFromBlocks(ch chan *coreb.Block)
GetBaseExecFee() int64
CalculateAttributesFee(tx *transaction.Transaction) int64
interop.Ledger
mempool.Feer
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func signTx(t *testing.T, bc Ledger, txs ...*transaction.Transaction) {
netFee, sizeDelta := fee.Calculate(bc.GetBaseExecFee(), rawScript)
tx.NetworkFee += +netFee
size += sizeDelta
tx.NetworkFee += int64(size) * bc.FeePerByte()
tx.NetworkFee += int64(size)*bc.FeePerByte() + bc.CalculateAttributesFee(tx)

buf := io.NewBufBinWriter()
for _, key := range privNetKeys {
Expand Down
53 changes: 43 additions & 10 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2479,7 +2479,7 @@ func (bc *Blockchain) verifyAndPoolTx(t *transaction.Transaction, pool *mempool.
if size > transaction.MaxTransactionSize {
return fmt.Errorf("%w: (%d > MaxTransactionSize %d)", ErrTxTooBig, size, transaction.MaxTransactionSize)
}
needNetworkFee := int64(size) * bc.FeePerByte()
needNetworkFee := int64(size)*bc.FeePerByte() + bc.CalculateAttributesFee(t)
if bc.P2PSigExtensionsEnabled() {
attrs := t.GetAttributes(transaction.NotaryAssistedT)
if len(attrs) != 0 {
Expand All @@ -2502,7 +2502,7 @@ func (bc *Blockchain) verifyAndPoolTx(t *transaction.Transaction, pool *mempool.
return err
}
}
err = bc.verifyTxWitnesses(t, nil, isPartialTx)
err = bc.verifyTxWitnesses(t, nil, isPartialTx, netFee)
if err != nil {
return err
}
Expand Down Expand Up @@ -2530,6 +2530,32 @@ func (bc *Blockchain) verifyAndPoolTx(t *transaction.Transaction, pool *mempool.
return nil
}

// CalculateAttributesFee returns network fee for all transaction attributes that should be
// paid according to native Policy.
func (bc *Blockchain) CalculateAttributesFee(tx *transaction.Transaction) int64 {
var (
feeCache map[transaction.AttrType]int64
feeSum int64
)
for _, attr := range tx.Attributes {
if feeCache == nil {
feeCache = make(map[transaction.AttrType]int64)
}
base, ok := feeCache[attr.Type]
if !ok {
base = bc.contracts.Policy.GetAttributeFeeInternal(bc.dao, attr.Type)
feeCache[attr.Type] = base
}
switch attr.Type {
case transaction.ConflictsT:
feeSum += base * int64(len(tx.Signers))
default:
feeSum += base
}
}
return feeSum
}

func (bc *Blockchain) verifyTxAttributes(d *dao.Simple, tx *transaction.Transaction, isPartialTx bool) error {
for i := range tx.Attributes {
switch attrType := tx.Attributes[i].Type; attrType {
Expand Down Expand Up @@ -2889,17 +2915,24 @@ func (bc *Blockchain) verifyHashAgainstScript(hash util.Uint160, witness *transa
// transaction. It can reorder them by ScriptHash, because that's required to
// match a slice of script hashes from the Blockchain. Block parameter
// is used for easy interop access and can be omitted for transactions that are
// not yet added into any block.
// not yet added into any block. verificationFee argument can be provided to
// restrict the maximum amount of GAS allowed to spend on transaction
// verification.
// Golang implementation of VerifyWitnesses method in C# (https://github.com/neo-project/neo/blob/master/neo/SmartContract/Helper.cs#L87).
func (bc *Blockchain) verifyTxWitnesses(t *transaction.Transaction, block *block.Block, isPartialTx bool) error {
func (bc *Blockchain) verifyTxWitnesses(t *transaction.Transaction, block *block.Block, isPartialTx bool, verificationFee ...int64) error {
interopCtx := bc.newInteropContext(trigger.Verification, bc.dao, block, t)
gasLimit := t.NetworkFee - int64(t.Size())*bc.FeePerByte()
if bc.P2PSigExtensionsEnabled() {
attrs := t.GetAttributes(transaction.NotaryAssistedT)
if len(attrs) != 0 {
na := attrs[0].Value.(*transaction.NotaryAssisted)
gasLimit -= (int64(na.NKeys) + 1) * bc.contracts.Notary.GetNotaryServiceFeePerKey(bc.dao)
var gasLimit int64
if len(verificationFee) == 0 {
gasLimit = t.NetworkFee - int64(t.Size())*bc.FeePerByte() - bc.CalculateAttributesFee(t)
if bc.P2PSigExtensionsEnabled() {
attrs := t.GetAttributes(transaction.NotaryAssistedT)
if len(attrs) != 0 {
na := attrs[0].Value.(*transaction.NotaryAssisted)
gasLimit -= (int64(na.NKeys) + 1) * bc.contracts.Notary.GetNotaryServiceFeePerKey(bc.dao)
}
}
} else {
gasLimit = verificationFee[0]
}
for i := range t.Signers {
gasConsumed, err := bc.verifyHashAgainstScript(t.Signers[i].Account, &t.Scripts[i], interopCtx, gasLimit)
Expand Down
18 changes: 15 additions & 3 deletions pkg/core/native/native_nep17.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,30 @@ func toUint160(s stackitem.Item) util.Uint160 {
return u
}

func toUint32(s stackitem.Item) uint32 {
func toUint64(s stackitem.Item) uint64 {
bigInt := toBigInt(s)
if !bigInt.IsUint64() {
panic("bigint is not an uint64")
panic("bigint is not a uint64")
}
uint64Value := bigInt.Uint64()
return bigInt.Uint64()
}

func toUint32(s stackitem.Item) uint32 {
uint64Value := toUint64(s)
if uint64Value > math.MaxUint32 {
panic("bigint does not fit into uint32")
}
return uint32(uint64Value)
}

func toUint8(s stackitem.Item) uint8 {
uint64Value := toUint64(s)
if uint64Value > math.MaxUint8 {
panic("bigint does not fit into uint8")
}
return uint8(uint64Value)
}

func toInt64(s stackitem.Item) int64 {
bigInt := toBigInt(s)
if !bigInt.IsInt64() {
Expand Down
67 changes: 67 additions & 0 deletions pkg/core/native/native_test/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import (
"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/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)

func newPolicyClient(t *testing.T) *neotest.ContractInvoker {
Expand Down Expand Up @@ -39,6 +45,67 @@ func TestPolicy_StoragePriceCache(t *testing.T) {
testGetSetCache(t, newPolicyClient(t), "StoragePrice", native.DefaultStoragePrice)
}

func TestPolicy_AttributeFee(t *testing.T) {
c := newPolicyClient(t)
getName := "getAttributeFee"
setName := "setAttributeFee"

randomInvoker := c.WithSigners(c.NewAccount(t))
committeeInvoker := c.WithSigners(c.Committee)

t.Run("set, not signed by committee", func(t *testing.T) {
randomInvoker.InvokeFail(t, "invalid committee signature", setName, byte(transaction.ConflictsT), 123)
})
t.Run("get, unknown attribute", func(t *testing.T) {
randomInvoker.InvokeFail(t, "invalid attribute type: 84", getName, byte(0x54))
})
t.Run("get, default value", func(t *testing.T) {
randomInvoker.Invoke(t, 0, getName, byte(transaction.ConflictsT))
})
t.Run("set, too large value", func(t *testing.T) {
committeeInvoker.InvokeFail(t, "out of range", setName, byte(transaction.ConflictsT), 10_0000_0001)
})
t.Run("set, unknown attribute", func(t *testing.T) {
committeeInvoker.InvokeFail(t, "invalid attribute type: 84", setName, 0x54, 5)
})
t.Run("set, success", func(t *testing.T) {
// Set and get in the same block.
txSet := committeeInvoker.PrepareInvoke(t, setName, byte(transaction.ConflictsT), 1)
txGet := randomInvoker.PrepareInvoke(t, getName, byte(transaction.ConflictsT))
c.AddNewBlock(t, txSet, txGet)
c.CheckHalt(t, txSet.Hash(), stackitem.Null{})
c.CheckHalt(t, txGet.Hash(), stackitem.Make(1))
// Get in the next block.
randomInvoker.Invoke(t, 1, getName, byte(transaction.ConflictsT))
})
}

func TestPolicy_AttributeFeeCache(t *testing.T) {
c := newPolicyClient(t)
getName := "getAttributeFee"
setName := "setAttributeFee"

committeeInvoker := c.WithSigners(c.Committee)

// Change fee, abort the transaction and check that contract cache wasn't persisted
// for FAULTed tx at the same block.
w := io.NewBufBinWriter()
emit.AppCall(w.BinWriter, committeeInvoker.Hash, setName, callflag.All, byte(transaction.ConflictsT), 5)
emit.Opcodes(w.BinWriter, opcode.ABORT)
tx1 := committeeInvoker.PrepareInvocation(t, w.Bytes(), committeeInvoker.Signers)
tx2 := committeeInvoker.PrepareInvoke(t, getName, byte(transaction.ConflictsT))
committeeInvoker.AddNewBlock(t, tx1, tx2)
committeeInvoker.CheckFault(t, tx1.Hash(), "ABORT")
committeeInvoker.CheckHalt(t, tx2.Hash(), stackitem.Make(0))

// Change fee and check that change is available for the next tx.
tx1 = committeeInvoker.PrepareInvoke(t, setName, byte(transaction.ConflictsT), 5)
tx2 = committeeInvoker.PrepareInvoke(t, getName, byte(transaction.ConflictsT))
committeeInvoker.AddNewBlock(t, tx1, tx2)
committeeInvoker.CheckHalt(t, tx1.Hash())
committeeInvoker.CheckHalt(t, tx2.Hash(), stackitem.Make(5))
}

func TestPolicy_BlockedAccounts(t *testing.T) {
c := newPolicyClient(t)
e := c.Executor
Expand Down
Loading

0 comments on commit 72f5632

Please sign in to comment.