native: support StorageFee

This commit is contained in:
Evgeniy Stratonikov 2021-03-05 14:53:46 +03:00
parent 256cd09228
commit 9cb38a4b1b
6 changed files with 14 additions and 2 deletions

View file

@ -91,6 +91,7 @@ type MethodAndPrice struct {
Func Method Func Method
MD *manifest.Method MD *manifest.Method
CPUFee int64 CPUFee int64
StorageFee int64
SyscallOffset int SyscallOffset int
RequiredFlags callflag.CallFlag RequiredFlags callflag.CallFlag
} }

View file

@ -34,7 +34,9 @@ func Call(ic *interop.Context) error {
return fmt.Errorf("missing call flags for native %d `%s` operation call: %05b vs %05b", return fmt.Errorf("missing call flags for native %d `%s` operation call: %05b vs %05b",
version, m.MD.Name, ic.VM.Context().GetCallFlags(), m.RequiredFlags) version, m.MD.Name, ic.VM.Context().GetCallFlags(), m.RequiredFlags)
} }
if !ic.VM.AddGas(m.CPUFee * ic.Chain.GetPolicer().GetBaseExecFee()) { invokeFee := m.CPUFee*ic.Chain.GetPolicer().GetBaseExecFee() +
m.StorageFee*ic.Chain.GetPolicer().GetStoragePrice()
if !ic.VM.AddGas(invokeFee) {
return errors.New("gas limit exceeded") return errors.New("gas limit exceeded")
} }
ctx := ic.VM.Context() ctx := ic.VM.Context()

View file

@ -136,6 +136,7 @@ func newNameService() *NameService {
manifest.NewParameter("name", smartcontract.StringType), manifest.NewParameter("name", smartcontract.StringType),
manifest.NewParameter("admin", smartcontract.Hash160Type)) manifest.NewParameter("admin", smartcontract.Hash160Type))
md = newMethodAndPrice(n.setAdmin, 1<<15, callflag.States) md = newMethodAndPrice(n.setAdmin, 1<<15, callflag.States)
md.StorageFee = 20
n.AddMethod(md, desc) n.AddMethod(md, desc)
desc = newDescriptor("setRecord", smartcontract.VoidType, desc = newDescriptor("setRecord", smartcontract.VoidType,
@ -143,6 +144,7 @@ func newNameService() *NameService {
manifest.NewParameter("type", smartcontract.IntegerType), manifest.NewParameter("type", smartcontract.IntegerType),
manifest.NewParameter("data", smartcontract.StringType)) manifest.NewParameter("data", smartcontract.StringType))
md = newMethodAndPrice(n.setRecord, 1<<15, callflag.States) md = newMethodAndPrice(n.setRecord, 1<<15, callflag.States)
md.StorageFee = 200
n.AddMethod(md, desc) n.AddMethod(md, desc)
desc = newDescriptor("getRecord", smartcontract.StringType, desc = newDescriptor("getRecord", smartcontract.StringType,

View file

@ -72,6 +72,7 @@ func newNEP17Native(name string, id int32) *nep17TokenNative {
append(transferParams, manifest.NewParameter("data", smartcontract.AnyType))..., append(transferParams, manifest.NewParameter("data", smartcontract.AnyType))...,
) )
md = newMethodAndPrice(n.Transfer, 1<<17, callflag.States|callflag.AllowCall|callflag.AllowNotify) md = newMethodAndPrice(n.Transfer, 1<<17, callflag.States|callflag.AllowCall|callflag.AllowNotify)
md.StorageFee = 50
n.AddMethod(md, desc) n.AddMethod(md, desc)
n.AddEvent("Transfer", transferParams...) n.AddEvent("Transfer", transferParams...)

View file

@ -108,6 +108,7 @@ func newNonFungible(name string, id int32, symbol string, decimals byte) *nonfun
manifest.NewParameter("to", smartcontract.Hash160Type), manifest.NewParameter("to", smartcontract.Hash160Type),
manifest.NewParameter("tokenId", smartcontract.ByteArrayType)) manifest.NewParameter("tokenId", smartcontract.ByteArrayType))
md = newMethodAndPrice(n.transfer, 1<<17, callflag.States|callflag.AllowNotify) md = newMethodAndPrice(n.transfer, 1<<17, callflag.States|callflag.AllowNotify)
md.StorageFee = 50
n.AddMethod(md, desc) n.AddMethod(md, desc)
n.AddEvent("Transfer", n.AddEvent("Transfer",

View file

@ -56,7 +56,10 @@ func (bc *Blockchain) registerNative(c interop.Contract) {
bc.contracts.Contracts = append(bc.contracts.Contracts, c) bc.contracts.Contracts = append(bc.contracts.Contracts, c)
} }
const testSumCPUFee = 1 << 15 // same as contract.Call const (
testSumCPUFee = 1 << 15 // same as contract.Call
testSumStorageFee = 200
)
func newTestNative() *testNative { func newTestNative() *testNative {
tn := &testNative{ tn := &testNative{
@ -77,6 +80,7 @@ func newTestNative() *testNative {
md := &interop.MethodAndPrice{ md := &interop.MethodAndPrice{
Func: tn.sum, Func: tn.sum,
CPUFee: testSumCPUFee, CPUFee: testSumCPUFee,
StorageFee: testSumStorageFee,
RequiredFlags: callflag.NoneFlag, RequiredFlags: callflag.NoneFlag,
} }
tn.meta.AddMethod(md, desc) tn.meta.AddMethod(md, desc)
@ -183,6 +187,7 @@ func TestNativeContract_Invoke(t *testing.T) {
// System.Contract.Call + "sum" itself + opcodes for pushing arguments. // System.Contract.Call + "sum" itself + opcodes for pushing arguments.
price := int64(testSumCPUFee * chain.GetBaseExecFee() * 2) price := int64(testSumCPUFee * chain.GetBaseExecFee() * 2)
price += testSumStorageFee * chain.GetStoragePrice()
price += 3 * fee.Opcode(chain.GetBaseExecFee(), opcode.PUSHINT8) price += 3 * fee.Opcode(chain.GetBaseExecFee(), opcode.PUSHINT8)
price += 2 * fee.Opcode(chain.GetBaseExecFee(), opcode.SYSCALL, opcode.PUSHDATA1, opcode.PUSHINT8) price += 2 * fee.Opcode(chain.GetBaseExecFee(), opcode.SYSCALL, opcode.PUSHDATA1, opcode.PUSHINT8)
price += fee.Opcode(chain.GetBaseExecFee(), opcode.PACK) price += fee.Opcode(chain.GetBaseExecFee(), opcode.PACK)