Compare commits

..

2 commits

Author SHA1 Message Date
eea212af58 [#XX] Fixed problem with TestContract_Add
Panic occurred due to absence of context.
Changed the loop condition, CurrInstr() changed to NextInstr(),
removed an attempt to get context from vm before poppint value
from the estack (also in Test).

Signed-off-by: Lebedeva Ekaterina <ekaterina.lebedeva@yadro.com>
2023-07-24 16:27:39 +03:00
f4795cfb31 [#XX] Problem with estack
Unexpected "runtime error: invalid memory address or nil pointer
dereference" in TestContract_Add

Signed-off-by: Lebedeva Ekaterina <ekaterina.lebedeva@yadro.com>
2023-07-24 14:28:23 +03:00
4 changed files with 48 additions and 8 deletions

View file

@ -38,3 +38,7 @@ func PutNumber(key []byte, num int) {
ctx := storage.GetContext()
storage.Put(ctx, append([]byte{prefixNumber}, key...), num)
}
func Add(a, b int) int {
return a + b
}

Binary file not shown.

View file

@ -1,11 +1,3 @@
name: FirstContract
sourceurl: http://example.com/
safemethods: []
supportedstandards: []
events:
- name: Hello world!
parameters:
- name: args
type: Array
permissions:
- methods: '*'

View file

@ -1,11 +1,16 @@
package tests
import (
"fmt"
"os"
"path"
"testing"
_ "github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
@ -48,3 +53,42 @@ func TestContract_InvalidKey(t *testing.T) {
inv.InvokeFail(t, "Invalid key size", "putNumber", invalidKey, 42)
inv.InvokeFail(t, "Invalid key size", "getNumber", invalidKey)
}
func TestContract_Add(t *testing.T) {
e := newExecutor(t)
ctrAdd := neotest.CompileFile(t, e.CommitteeHash, ctrPath, path.Join(ctrPath, "neo-go.yml"))
e.DeployContract(t, ctrAdd, nil)
inv := e.CommitteeInvoker(ctrAdd.Hash)
inv.Invoke(t, 3, "add", 1, 2)
govm := vm.New()
govm.LoadNEFMethod(ctrAdd.NEF, ctrAdd.Hash, ctrAdd.Hash, callflag.All, true, 170, -1, nil)
govm.Context().Estack().PushVal(1)
govm.Context().Estack().PushVal(2)
fmt.Println("\nPrinting opcodes:")
for !govm.HasStopped() {
nStr, curInstr := govm.Context().NextInstr()
fmt.Println(nStr, " ", curInstr)
govm.Step()
}
res := govm.Estack().Pop().Value()
fmt.Println("\nresult = ", res)
}
func Test(t *testing.T) {
govm := vm.New()
err := govm.LoadFileWithFlags(path.Join(ctrPath, "contract.nef"), callflag.All)
if err != nil {
fmt.Fprintln(os.Stderr, "NEF loading failed")
t.FailNow()
}
govm.PrintOps(os.Stdout)
govm.Context().Jump(170)
govm.Context().Estack().PushVal(1)
govm.Context().Estack().PushVal(2)
govm.Run()
res := govm.Estack().Pop().Value()
fmt.Println(res)
}