From 55966c7e078dfa9dbba53c17e05ded651fcf960c Mon Sep 17 00:00:00 2001 From: Anthony De Meulemeester Date: Fri, 31 Aug 2018 10:23:57 +0200 Subject: [PATCH] Draft of iterator and enumerator (CityOfZion/neo-storm#26) * Draft of iterator and enumerator * Added iterator API to the syscall mapping * Added draft of the enumerator.go file * Added enumerator interop API. * Updated the changelog Imported from CityOfZion/neo-storm (156093318b8612e810965bb1ea26e1babfb46cdd). --- examples/iterator/iterator.go | 18 ++++++++++++++++++ interop/enumerator/enumerator.go | 29 +++++++++++++++++++++++++++++ interop/iterator/iterator.go | 28 ++++++++++++++++++++++++++++ interop/storage/storage.go | 6 ++++-- pkg/vm/compiler/syscall.go | 6 ++++++ 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 examples/iterator/iterator.go create mode 100644 interop/enumerator/enumerator.go create mode 100644 interop/iterator/iterator.go diff --git a/examples/iterator/iterator.go b/examples/iterator/iterator.go new file mode 100644 index 000000000..4ccf996b3 --- /dev/null +++ b/examples/iterator/iterator.go @@ -0,0 +1,18 @@ +package iterator_contract + +import ( + "github.com/CityOfZion/neo-storm/interop/iterator" + "github.com/CityOfZion/neo-storm/interop/runtime" + "github.com/CityOfZion/neo-storm/interop/storage" +) + +func Main() bool { + iter := storage.Find(storage.GetContext(), []byte("foo")) + values := iterator.Values(iter) + keys := iterator.Keys(iter) + + runtime.Notify("found storage values", values) + runtime.Notify("found storage keys", keys) + + return true +} diff --git a/interop/enumerator/enumerator.go b/interop/enumerator/enumerator.go new file mode 100644 index 000000000..e2b906c34 --- /dev/null +++ b/interop/enumerator/enumerator.go @@ -0,0 +1,29 @@ +package enumerator + +// Package enumerator provides function signatures that can be used inside +// smart contracts that are written in the neo-storm framework. + +// TODO: Check enumerator use cases and add them to the examples folder. + +// Enumerator stubs a NEO enumerator type. +type Enumerator struct{} + +// Create creates a new enumerator from the given items. +func Create(items []interface{}) Enumerator { + return Enumerator{} +} + +// Next returns the next item in the iteration. +func Next(e Enumerator) interface{} { + return nil +} + +// Value returns the enumerator value. +func Value(e Enumerator) interface{} { + return nil +} + +// Concat concats the 2 given enumerators. +func Concat(a, b Enumerator) Enumerator { + return Enumerator{} +} diff --git a/interop/iterator/iterator.go b/interop/iterator/iterator.go new file mode 100644 index 000000000..352cf3f17 --- /dev/null +++ b/interop/iterator/iterator.go @@ -0,0 +1,28 @@ +package iterator + +// Package iterator provides function signatures that can be used inside +// smart contracts that are written in the neo-storm framework. + +// Iterator stubs a NEO iterator object type. +type Iterator struct{} + +// Create creates an iterator from the given items. +func Create(items []interface{}) Iterator { + return Iterator{} +} + +// TODO: Better description for this. +// Key returns the iterator key. +func Key(it Iterator) interface{} { + return nil +} + +// Keys returns the iterator keys. +func Keys(it Iterator) []interface{} { + return nil +} + +// Values returns the iterator values. +func Values(it Iterator) []interface{} { + return nil +} diff --git a/interop/storage/storage.go b/interop/storage/storage.go index b280e834f..25cf4ef34 100644 --- a/interop/storage/storage.go +++ b/interop/storage/storage.go @@ -1,5 +1,7 @@ package storage +import "github.com/CityOfZion/neo-storm/interop/iterator" + // Package storage provides function signatures that can be used inside // smart contracts that are written in the neo-storm framework. @@ -18,5 +20,5 @@ func Get(ctx Context, key interface{}) interface{} { return 0 } // Delete key value pair from storage func Delete(ctx Context, key interface{}) {} -// Find values stored on keys partially matching given key -func Find(ctx Context, key interface{}) interface{} { return 0 } +// Find returns an iterator.Iterator over the keys that matched the given key. +func Find(ctx Context, key interface{}) iterator.Iterator { return iterator.Iterator{} } diff --git a/pkg/vm/compiler/syscall.go b/pkg/vm/compiler/syscall.go index 1e42f633f..7a9d05146 100644 --- a/pkg/vm/compiler/syscall.go +++ b/pkg/vm/compiler/syscall.go @@ -81,4 +81,10 @@ var syscalls = map[string]map[string]string{ "GetEntryScriptHash": "System.ExecutionEngine.GetEntryScriptHash", "GetExecutingScriptHash": "System.ExecutionEngine.GetExecutingScriptHash", }, + "iterator": { + "Create": "Neo.Iterator.Create", + "Key": "Neo.Iterator.Key", + "Keys": "Neo.Iterator.Keys", + "Values": "Neo.Iterator.Values", + }, }