Compare commits

...
Sign in to create a new pull request.

3 commits

Author SHA1 Message Date
0f7bbb95dc [#XX] Added basic coverage functionality to PutNumber()
It probably doesn't work properly.

Signed-off-by: Lebedeva Ekaterina <ekaterina.lebedeva@yadro.com>
2023-07-26 17:15:56 +03:00
3e8e119a22 [#XX] Added replace directive into go.mod
Signed-off-by: Lebedeva Ekaterina <ekaterina.lebedeva@yadro.com>
2023-07-24 18:43:06 +03:00
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
3 changed files with 128 additions and 33 deletions

View file

@ -18,12 +18,7 @@ func GetNumber(key []byte) int {
if len(key) != keySize {
panic("Invalid key size")
}
// var num int
ctx := storage.GetContext()
// it := storage.Find(ctx, 123, storage.ValuesOnly)
// for iterator.Next(it) {
// num = iterator.Value(it).(int)
// }
num := storage.Get(ctx, append([]byte{prefixNumber}, key...))
if num == nil {
panic("Cannot get number")

View file

@ -4,9 +4,11 @@ go 1.20
require (
github.com/nspcc-dev/neo-go v0.101.3
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20221202075445-cb5c18dc73eb
//github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20221202075445-cb5c18dc73eb
)
//replace github.com/nspcc-dev/neo-go => /Users/ekaterina.lebedeva/neo-go-0.101.3/
require (
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect

View file

@ -4,15 +4,17 @@ import (
"fmt"
"os"
"path"
"strings"
"testing"
"github.com/davecgh/go-spew/spew"
_ "github.com/nspcc-dev/neo-go/pkg/compiler"
"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/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
const ctrPath = "../contract"
@ -36,14 +38,121 @@ func TestContract_GetNumber(t *testing.T) {
inv.InvokeFail(t, "Cannot get number", "getNumber", validKey)
}
type Coverage struct {
Document string
MethodID string
StartLine int
StartCol int
EndLine int
EndCol int
Counter int
}
func TestContract_PutNumber(t *testing.T) {
e := newExecutor(t)
ctrPutGetNum := neotest.CompileFile(t, e.CommitteeHash, ctrPath, path.Join(ctrPath, "neo-go.yml"))
e.DeployContract(t, ctrPutGetNum, nil)
inv := e.CommitteeInvoker(ctrPutGetNum.Hash)
inv.Invoke(t, stackitem.Null{}, "putNumber", validKey, 42)
di := ctrPutGetNum.DebugInfo
inv.Invoke(t, 42, "getNumber", validKey)
// Find contract.go doc
iDoc := len(di.Documents)
for i, cDoc := range di.Documents {
if strings.Contains(cDoc, "contract.go") {
iDoc = i
}
}
if iDoc == len(di.Documents) {
fmt.Fprintf(os.Stderr, "No such file\n")
t.FailNow()
}
// Get sequence points for current method
iPutMethod := len(di.Methods)
for i := 0; i < len(di.Methods); i++ {
if di.Methods[i].ID == "PutNumber" {
iPutMethod = i
break
}
}
if iPutMethod == len(di.Methods) {
fmt.Fprintf(os.Stderr, "No \"PutNumber\" method in %s contract\n", ctrPutGetNum.Manifest.Name)
t.FailNow()
}
//seqPoints := make([]compiler.DebugSeqPoint, len(di.Methods[iPutMethod].SeqPoints))
//copy(seqPoints, di.Methods[iPutMethod].SeqPoints)
var seqPoints []compiler.DebugSeqPoint
for _, curPoint := range di.Methods[iPutMethod].SeqPoints {
if curPoint.Document == iDoc {
seqPoints = append(seqPoints, curPoint)
}
}
spew.Dump(seqPoints)
ic, err := e.Chain.GetTestVM(trigger.Application, nil, nil)
require.NoError(t, err)
fmt.Println(ic.VM.SyscallHandler == nil)
ic.VM.LoadNEFMethod(ctrPutGetNum.NEF, ctrPutGetNum.Hash, ctrPutGetNum.Hash, callflag.All, false, 101, -1, nil)
ic.VM.Context().Estack().PushVal(2)
ic.VM.Context().Estack().PushVal(validKey)
fmt.Println("\nPrinting debug info for PutNumber:")
for !ic.VM.HasStopped() {
_, curInstr := ic.VM.Context().NextInstr()
for i := 0; i < len(seqPoints); i++ {
if int(curInstr) == seqPoints[i].Opcode {
fmt.Printf("%s: %d.%d, %d.%d hooray!!\n", di.Documents[iDoc], seqPoints[i].StartLine, seqPoints[i].StartCol, seqPoints[i].EndLine, seqPoints[i].EndCol)
}
}
//fmt.Println(nStr, " ", curInstr)
ic.VM.Step()
}
if ic.VM.HasFailed() {
//govm.SyscallHandler()
fmt.Fprintln(os.Stderr, "Something went wrong")
t.FailNow()
}
fmt.Println("\n Done printing!")
// e.DeployContract(t, ctrPutGetNum, nil)
// // inv := e.CommitteeInvoker(ctrPutGetNum.Hash)
// // inv.Invoke(t, stackitem.Null{}, "putNumber", validKey, 42)
// // inv.Invoke(t, 42, "getNumber", validKey)
// fmt.Println("\n I'm printing debug info for compiled file!")
// spew.Dump(ctrPutGetNum.DebugInfo)
// fmt.Println()
// //fmt.Println(json.MarshalIndent(ctrPutGetNum.Manifest, "", " "))
// govm := vm.New()
// govm.LoadNEFMethod(ctrPutGetNum.NEF, ctrPutGetNum.Hash, ctrPutGetNum.Hash, callflag.All, false, 101, -1, nil)
// fmt.Println("PrintOps:")
// govm.PrintOps(os.Stdout)
// govm.Context().Estack().PushVal(2)
// govm.Context().Estack().PushVal(validKey)
// fmt.Println("\nPrinting opcodes for PutNumber:")
// for !govm.HasStopped() {
// nStr, curInstr := govm.Context().NextInstr()
// fmt.Println(nStr, " ", curInstr)
// govm.Step()
// }
// if govm.HasFailed() {
// //govm.SyscallHandler()
// fmt.Fprintln(os.Stderr, "Something went wrong")
// t.FailNow()
// }
// govm.LoadNEFMethod(ctrPutGetNum.NEF, ctrPutGetNum.Hash, ctrPutGetNum.Hash, callflag.All, true, 0, -1, nil)
// govm.Context().Estack().PushVal(validKey)
// fmt.Println("\nPrinting opcodes for GetNumber:")
// for !govm.HasStopped() {
// nStr, curInstr := govm.Context().NextInstr()
// fmt.Println(nStr, " ", curInstr)
// govm.Step()
// }
// res := govm.Estack().Pop().Value()
// fmt.Println("\nresult = ", res)
// fmt.Println("\n Done printing!")
}
func TestContract_InvalidKey(t *testing.T) {
@ -59,31 +168,23 @@ 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)
spew.Dump(ctrAdd.Manifest)
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(3)
//fmt.Println(govm.Context().CurrInstr())
_, curInstr := govm.Context().CurrInstr()
//while curInstr != RET do
for curInstr != 0x40 {
fmt.Println(curInstr)
govm.Context().Estack().PushVal(2)
fmt.Println("\nPrinting opcodes:")
for !govm.HasStopped() {
nStr, curInstr := govm.Context().NextInstr()
fmt.Println(nStr, " ", curInstr)
govm.Step()
_, curInstr = govm.Context().CurrInstr()
}
fmt.Println(govm.Context().CurrInstr())
//govm.DumpEStack()
//govm.Run()
res := govm.Context().Estack().Pop().Value()
fmt.Println(res)
// manif, _ := json.MarshalIndent(ctrAdd.Manifest, "", " ")
// fmt.Println(string(manif))
inv := e.CommitteeInvoker(ctrAdd.Hash)
inv.Invoke(t, 3, "add", 1, 2)
res := govm.Estack().Pop().Value()
fmt.Println("\nresult = ", res)
}
func Test(t *testing.T) {
@ -94,13 +195,10 @@ func Test(t *testing.T) {
t.FailNow()
}
govm.PrintOps(os.Stdout)
//step in a loop for debug
//
govm.Context().Jump(170)
govm.Context().Estack().PushVal(1)
govm.Context().Estack().PushVal(2)
govm.Run()
res := govm.Context().Estack().Pop().Value()
res := govm.Estack().Pop().Value()
fmt.Println(res)
}