Skip to content

Commit

Permalink
core,vm: remove System.Enumerator.* interops
Browse files Browse the repository at this point in the history
Map iterator now returns key-value pair, while array/byte-array
iterators work like old enumerators.
Follow neo-project/neo#2190.
  • Loading branch information
fyrchik authored and roman-khimov committed Jan 15, 2021
1 parent d04b000 commit 2130e17
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 418 deletions.
11 changes: 3 additions & 8 deletions examples/iterator/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ import (
// NotifyKeysAndValues sends notification with `foo` storage keys and values
func NotifyKeysAndValues() bool {
iter := storage.Find(storage.GetContext(), []byte("foo"))
values := iterator.Values(iter)
keys := iterator.Keys(iter)

runtime.Notify("found storage values", values)
// For illustration purposes event is emitted with 'Any' type.
var typedKeys interface{} = keys
runtime.Notify("found storage keys", typedKeys)

for iterator.Next(iter) {
runtime.Notify("found storage key-value pair", iterator.Value(iter))
}
return true
}
8 changes: 2 additions & 6 deletions examples/iterator/iterator.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
name: "Iterator example"
supportedstandards: []
events:
- name: found storage values
- name: found storage key-value pair
parameters:
- name: values
type: InteropInterface
- name: found storage keys
parameters:
- name: keys
- name: value
type: Any
5 changes: 2 additions & 3 deletions examples/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func Find(value []byte) []string {
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))
val := iterator.Value(iter).([]string)
result = append(result, val[0]+":"+val[1])
}
return result
}
12 changes: 2 additions & 10 deletions pkg/compiler/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,10 @@ var syscalls = map[string]map[string]string{
"RIPEMD160": interopnames.NeoCryptoRIPEMD160,
"SHA256": interopnames.NeoCryptoSHA256,
},
"enumerator": {
"Create": interopnames.SystemEnumeratorCreate,
"Next": interopnames.SystemEnumeratorNext,
"Value": interopnames.SystemEnumeratorValue,
},
"iterator": {
"Create": interopnames.SystemIteratorCreate,
"Key": interopnames.SystemIteratorKey,
"Keys": interopnames.SystemIteratorKeys,
"Next": interopnames.SystemEnumeratorNext,
"Value": interopnames.SystemEnumeratorValue,
"Values": interopnames.SystemIteratorValues,
"Next": interopnames.SystemIteratorNext,
"Value": interopnames.SystemIteratorValue,
},
"json": {
"Deserialize": interopnames.SystemJSONDeserialize,
Expand Down
22 changes: 0 additions & 22 deletions pkg/core/interop/enumerator/interop.go

This file was deleted.

32 changes: 0 additions & 32 deletions pkg/core/interop/enumerator/interop_test.go

This file was deleted.

16 changes: 4 additions & 12 deletions pkg/core/interop/interopnames/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ const (
SystemContractGetCallFlags = "System.Contract.GetCallFlags"
SystemContractNativeOnPersist = "System.Contract.NativeOnPersist"
SystemContractNativePostPersist = "System.Contract.NativePostPersist"
SystemEnumeratorCreate = "System.Enumerator.Create"
SystemEnumeratorNext = "System.Enumerator.Next"
SystemEnumeratorValue = "System.Enumerator.Value"
SystemIteratorCreate = "System.Iterator.Create"
SystemIteratorKey = "System.Iterator.Key"
SystemIteratorKeys = "System.Iterator.Keys"
SystemIteratorValues = "System.Iterator.Values"
SystemIteratorNext = "System.Iterator.Next"
SystemIteratorValue = "System.Iterator.Value"
SystemJSONDeserialize = "System.Json.Deserialize"
SystemJSONSerialize = "System.Json.Serialize"
SystemRuntimeCheckWitness = "System.Runtime.CheckWitness"
Expand Down Expand Up @@ -89,13 +85,9 @@ var names = []string{
SystemContractGetCallFlags,
SystemContractNativeOnPersist,
SystemContractNativePostPersist,
SystemEnumeratorCreate,
SystemEnumeratorNext,
SystemEnumeratorValue,
SystemIteratorCreate,
SystemIteratorKey,
SystemIteratorKeys,
SystemIteratorValues,
SystemIteratorNext,
SystemIteratorValue,
SystemJSONDeserialize,
SystemJSONSerialize,
SystemRuntimeCheckWitness,
Expand Down
19 changes: 8 additions & 11 deletions pkg/core/interop/iterator/interop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ func Create(ic *interop.Context) error {
return vm.IteratorCreate(ic.VM)
}

// Key returns current iterator key.
func Key(ic *interop.Context) error {
return vm.IteratorKey(ic.VM)
// Next advances the iterator, pushes true on success and false otherwise.
func Next(ic *interop.Context) error {
return vm.IteratorNext(ic.VM)
}

// Keys returns keys of the iterator.
func Keys(ic *interop.Context) error {
return vm.IteratorKeys(ic.VM)
}

// Values returns values of the iterator.
func Values(ic *interop.Context) error {
return vm.IteratorValues(ic.VM)
// Value returns current iterator value and depends on iterator type:
// For slices the result is just value.
// For maps the result is key-value pair packed in a struct.
func Value(ic *interop.Context) error {
return vm.IteratorValue(ic.VM)
}
32 changes: 11 additions & 21 deletions pkg/core/interop/iterator/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,19 @@ func TestIterator(t *testing.T) {
require.NoError(t, Create(ic))

res := ic.VM.Estack().Pop().Item()
ic.VM.Estack().PushVal(res)
require.NoError(t, vm.EnumeratorNext(ic.VM))
require.True(t, ic.VM.Estack().Pop().Bool())

ic.VM.Estack().PushVal(res)
require.NoError(t, Key(ic))
require.Equal(t, big.NewInt(0), ic.VM.Estack().Pop().BigInt())
for i := range full {
ic.VM.Estack().PushVal(res)
require.NoError(t, Next(ic))
require.True(t, ic.VM.Estack().Pop().Bool())

ic.VM.Estack().PushVal(res)
require.NoError(t, vm.EnumeratorValue(ic.VM))
require.Equal(t, big.NewInt(int64(full[0])), ic.VM.Estack().Pop().BigInt())
ic.VM.Estack().PushVal(res)
require.NoError(t, Value(ic))

ic.VM.Estack().PushVal(res)
require.NoError(t, vm.EnumeratorNext(ic.VM))
require.True(t, ic.VM.Estack().Pop().Bool())

ic.VM.Estack().PushVal(res)
require.NoError(t, Keys(ic))
require.NoError(t, vm.EnumeratorValue(ic.VM))
require.Equal(t, big.NewInt(1), ic.VM.Estack().Pop().BigInt())
value := ic.VM.Estack().Pop().Item().Value()
require.Equal(t, big.NewInt(int64(full[i])), value)
}

ic.VM.Estack().PushVal(res)
require.NoError(t, Values(ic))
require.NoError(t, vm.EnumeratorValue(ic.VM))
require.Equal(t, big.NewInt(int64(full[1])), ic.VM.Estack().Pop().BigInt())
require.NoError(t, Next(ic))
require.False(t, false, ic.VM.Estack().Pop().Bool())
}
41 changes: 16 additions & 25 deletions pkg/core/interop_neo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/enumerator"
"github.com/nspcc-dev/neo-go/pkg/core/interop/iterator"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
Expand Down Expand Up @@ -67,33 +66,25 @@ func TestStorageFind(t *testing.T) {
require.NoError(t, err)

var iter *stackitem.Interop
require.NotPanics(t, func() { iter = v.Estack().Top().Interop() })
require.NotPanics(t, func() { iter = v.Estack().Pop().Interop() })

require.NoError(t, enumerator.Next(context))
require.True(t, v.Estack().Pop().Bool())
for _, id := range []int{2, 0} { // sorted indices with mathing prefix
v.Estack().PushVal(iter)
require.NoError(t, iterator.Next(context))
require.True(t, v.Estack().Pop().Bool())

v.Estack().PushVal(iter)
require.NoError(t, iterator.Key(context))
require.Equal(t, []byte{0x01, 0x01}, v.Estack().Pop().Bytes())

v.Estack().PushVal(iter)
require.NoError(t, enumerator.Value(context))
require.Equal(t, []byte{0x03, 0x04, 0x05, 0x06}, v.Estack().Pop().Bytes())

v.Estack().PushVal(iter)
require.NoError(t, enumerator.Next(context))
require.True(t, v.Estack().Pop().Bool())

v.Estack().PushVal(iter)
require.NoError(t, iterator.Key(context))
require.Equal(t, []byte{0x01, 0x02}, v.Estack().Pop().Bytes())
v.Estack().PushVal(iter)
require.NoError(t, iterator.Value(context))

v.Estack().PushVal(iter)
require.NoError(t, enumerator.Value(context))
require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, v.Estack().Pop().Bytes())
kv, ok := v.Estack().Pop().Value().([]stackitem.Item)
require.True(t, ok)
require.Len(t, kv, 2)
require.Equal(t, skeys[id], kv[0].Value())
require.Equal(t, items[id].Value, kv[1].Value())
}

v.Estack().PushVal(iter)
require.NoError(t, enumerator.Next(context))
require.NoError(t, iterator.Next(context))
require.False(t, v.Estack().Pop().Bool())
})

Expand All @@ -104,7 +95,7 @@ func TestStorageFind(t *testing.T) {
err := storageFind(context)
require.NoError(t, err)

require.NoError(t, enumerator.Next(context))
require.NoError(t, iterator.Next(context))
require.False(t, v.Estack().Pop().Bool())
})

Expand All @@ -122,7 +113,7 @@ func TestStorageFind(t *testing.T) {
v.Estack().PushVal(stackitem.NewInterop(&StorageContext{ID: invalidID}))

require.NoError(t, storageFind(context))
require.NoError(t, enumerator.Next(context))
require.NoError(t, iterator.Next(context))
require.False(t, v.Estack().Pop().Bool())
})
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/core/interops.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/core/interop/crypto"
"github.com/nspcc-dev/neo-go/pkg/core/interop/enumerator"
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
"github.com/nspcc-dev/neo-go/pkg/core/interop/iterator"
"github.com/nspcc-dev/neo-go/pkg/core/interop/json"
Expand Down Expand Up @@ -58,13 +57,9 @@ var systemInterops = []interop.Function{
{Name: interopnames.SystemContractGetCallFlags, Func: contractGetCallFlags, Price: 1 << 10},
{Name: interopnames.SystemContractNativeOnPersist, Func: native.OnPersist, Price: 0, RequiredFlags: callflag.WriteStates},
{Name: interopnames.SystemContractNativePostPersist, Func: native.PostPersist, Price: 0, RequiredFlags: callflag.WriteStates},
{Name: interopnames.SystemEnumeratorCreate, Func: enumerator.Create, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemEnumeratorNext, Func: enumerator.Next, Price: 1 << 15, ParamCount: 1},
{Name: interopnames.SystemEnumeratorValue, Func: enumerator.Value, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemIteratorCreate, Func: iterator.Create, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemIteratorKey, Func: iterator.Key, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemIteratorKeys, Func: iterator.Keys, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemIteratorValues, Func: iterator.Values, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemIteratorNext, Func: iterator.Next, Price: 1 << 15, ParamCount: 1},
{Name: interopnames.SystemIteratorValue, Func: iterator.Value, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemJSONDeserialize, Func: json.Deserialize, Price: 1 << 14, ParamCount: 1},
{Name: interopnames.SystemJSONSerialize, Func: json.Serialize, Price: 1 << 12, ParamCount: 1},
{Name: interopnames.SystemRuntimeCheckWitness, Func: runtime.CheckWitness, Price: 1 << 10,
Expand Down
32 changes: 0 additions & 32 deletions pkg/interop/enumerator/enumerator.go

This file was deleted.

Loading

0 comments on commit 2130e17

Please sign in to comment.