Compare commits

..

No commits in common. "master" and "dnw-add" have entirely different histories.

3 changed files with 33 additions and 128 deletions

View file

@ -18,7 +18,12 @@ 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,11 +4,9 @@ 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,17 +4,15 @@ 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/stretchr/testify/require"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
const ctrPath = "../contract"
@ -38,121 +36,14 @@ 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)
di := ctrPutGetNum.DebugInfo
inv := e.CommitteeInvoker(ctrPutGetNum.Hash)
inv.Invoke(t, stackitem.Null{}, "putNumber", validKey, 42)
// 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!")
inv.Invoke(t, 42, "getNumber", validKey)
}
func TestContract_InvalidKey(t *testing.T) {
@ -168,23 +59,31 @@ 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)
spew.Dump(ctrAdd.Manifest)
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.Context().Estack().PushVal(3)
//fmt.Println(govm.Context().CurrInstr())
_, curInstr := govm.Context().CurrInstr()
//while curInstr != RET do
for curInstr != 0x40 {
fmt.Println(curInstr)
govm.Step()
_, curInstr = govm.Context().CurrInstr()
}
fmt.Println(govm.Context().CurrInstr())
res := govm.Estack().Pop().Value()
fmt.Println("\nresult = ", res)
//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)
}
func Test(t *testing.T) {
@ -195,10 +94,13 @@ 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.Estack().Pop().Value()
res := govm.Context().Estack().Pop().Value()
fmt.Println(res)
}