diff --git a/pkg/core/native/contract.go b/pkg/core/native/contract.go index 3338551ab..55ff6230a 100644 --- a/pkg/core/native/contract.go +++ b/pkg/core/native/contract.go @@ -76,8 +76,7 @@ func (cs *Contracts) GetNativeInterop(ic *interop.Context) func(uint32) *vm.Inte return func(id uint32) *vm.InteropFuncPrice { if c := cs.ByID(id); c != nil { return &vm.InteropFuncPrice{ - Func: getNativeInterop(ic, c), - Price: 0, // TODO price func + Func: getNativeInterop(ic, c), } } return nil @@ -100,6 +99,9 @@ func getNativeInterop(ic *interop.Context, c interop.Contract) func(v *vm.VM) er if !v.Context().GetCallFlags().Has(m.RequiredFlags) { return errors.New("missing call flags") } + if !v.AddGas(util.Fixed8(m.Price)) { + return errors.New("gas limit exceeded") + } result := m.Func(ic, args) v.Estack().PushVal(result) return nil diff --git a/pkg/core/native_contract_test.go b/pkg/core/native_contract_test.go index b3eed1ae7..a08556a0d 100644 --- a/pkg/core/native_contract_test.go +++ b/pkg/core/native_contract_test.go @@ -47,6 +47,8 @@ func (bc *Blockchain) registerNative(c interop.Contract) { bc.contracts.Contracts = append(bc.contracts.Contracts, c) } +const testSumPrice = 1000000 + func newTestNative() *testNative { tn := &testNative{ meta: *interop.NewContractMD("Test.Native.Sum"), @@ -62,7 +64,7 @@ func newTestNative() *testNative { } md := &interop.MethodAndPrice{ Func: tn.sum, - Price: 1, + Price: testSumPrice, RequiredFlags: smartcontract.NoneFlag, } tn.meta.AddMethod(md, desc, true) @@ -99,7 +101,8 @@ func TestNativeContract_Invoke(t *testing.T) { w := io.NewBufBinWriter() emit.AppCallWithOperationAndArgs(w.BinWriter, tn.Metadata().Hash, "sum", int64(14), int64(28)) script := w.Bytes() - tx := transaction.New(chain.GetConfig().Magic, script, 0) + // System.Contract.Call + "sum" itself + opcodes for pushing arguments (PACK is 7000) + tx := transaction.New(chain.GetConfig().Magic, script, testSumPrice*2+10000) validUntil := chain.blockHeight + 1 tx.ValidUntilBlock = validUntil require.NoError(t, addSender(tx))