From 37cb60a0b5d929f4a677cbe20d6f7af01855daf8 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 26 Mar 2020 16:34:54 +0300 Subject: [PATCH 1/2] compiler/interop: add missing methods to interop.Iterator Add Next() and Value() to interop.Iterator and corresponding syscalls to compiler --- pkg/compiler/syscall.go | 2 ++ pkg/interop/iterator/iterator.go | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/compiler/syscall.go b/pkg/compiler/syscall.go index 62e24f4c9..01234955b 100644 --- a/pkg/compiler/syscall.go +++ b/pkg/compiler/syscall.go @@ -86,6 +86,8 @@ var syscalls = map[string]map[string]string{ "Create": "Neo.Iterator.Create", "Key": "Neo.Iterator.Key", "Keys": "Neo.Iterator.Keys", + "Next": "Neo.Iterator.Next", + "Value": "Neo.Iterator.Value", "Values": "Neo.Iterator.Values", }, } diff --git a/pkg/interop/iterator/iterator.go b/pkg/interop/iterator/iterator.go index 2dd17d2eb..84e85310e 100644 --- a/pkg/interop/iterator/iterator.go +++ b/pkg/interop/iterator/iterator.go @@ -22,6 +22,17 @@ func Keys(it Iterator) []interface{} { return nil } +// Next advances the iterator, return true if it is was successful +// and false otherwise. +func Next(it Iterator) bool { + return true +} + +// Value returns the current iterator value. +func Value(it Iterator) interface{} { + return nil +} + // Values returns the iterator values. func Values(it Iterator) []interface{} { return nil From 0a5298b3ee967b9421c61f0d54c497cf80e77e49 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 25 Mar 2020 17:40:58 +0300 Subject: [PATCH 2/2] examples: expand storage smartcontract with find() usage example closes #364 --- examples/storage/storage.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/examples/storage/storage.go b/examples/storage/storage.go index 72450fd36..a117f9f3b 100644 --- a/examples/storage/storage.go +++ b/examples/storage/storage.go @@ -1,6 +1,7 @@ package storagecontract import ( + "github.com/nspcc-dev/neo-go/pkg/interop/iterator" "github.com/nspcc-dev/neo-go/pkg/interop/storage" ) @@ -33,7 +34,20 @@ func Main(operation string, args []interface{}) interface{} { return true } - // TODO: storage.Find() + // Returns an array of key-value pairs with key that matched the passed value + if operation == "find" { + if checkArgs(args, 1) { + value := args[0].([]byte) + iter := storage.Find(ctx, value) + result := []string{} + for iterator.Next(iter) { + val := iterator.Value(iter) + key := iterator.Key(iter) + result = append(result, key.(string)+":"+val.(string)) + } + return result + } + } return false }