From 30cc2c5783e9d548985ae5e91160677e3decd298 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 8 Apr 2020 14:08:43 +0300 Subject: [PATCH] core: move enumerator/iterator interops to a separate packages --- pkg/core/interop/enumerator/interop.go | 27 +++++++++++++++ pkg/core/interop/iterator/interop.go | 31 +++++++++++++++++ pkg/core/interop_neo.go | 46 -------------------------- pkg/core/interop_neo_test.go | 10 +++--- pkg/core/interops.go | 24 ++++++++------ 5 files changed, 77 insertions(+), 61 deletions(-) create mode 100644 pkg/core/interop/enumerator/interop.go create mode 100644 pkg/core/interop/iterator/interop.go diff --git a/pkg/core/interop/enumerator/interop.go b/pkg/core/interop/enumerator/interop.go new file mode 100644 index 000000000..ecd90efad --- /dev/null +++ b/pkg/core/interop/enumerator/interop.go @@ -0,0 +1,27 @@ +package enumerator + +import ( + "github.com/nspcc-dev/neo-go/pkg/core/interop" + "github.com/nspcc-dev/neo-go/pkg/vm" +) + +// Concat concatenates 2 enumerators into a single one. +func Concat(_ *interop.Context, v *vm.VM) error { + return vm.EnumeratorConcat(v) +} + +// Create creates an enumerator from an array-like stack item. +func Create(_ *interop.Context, v *vm.VM) error { + return vm.EnumeratorCreate(v) +} + +// Next advances the enumerator, pushes true if is it was successful +// and false otherwise. +func Next(_ *interop.Context, v *vm.VM) error { + return vm.EnumeratorNext(v) +} + +// Value returns the current value of the enumerator. +func Value(_ *interop.Context, v *vm.VM) error { + return vm.EnumeratorValue(v) +} diff --git a/pkg/core/interop/iterator/interop.go b/pkg/core/interop/iterator/interop.go new file mode 100644 index 000000000..f04dfa942 --- /dev/null +++ b/pkg/core/interop/iterator/interop.go @@ -0,0 +1,31 @@ +package iterator + +import ( + "github.com/nspcc-dev/neo-go/pkg/core/interop" + "github.com/nspcc-dev/neo-go/pkg/vm" +) + +// Concat concatenates 2 iterators into a single one. +func Concat(_ *interop.Context, v *vm.VM) error { + return vm.IteratorConcat(v) +} + +// Create creates an iterator from array-like or map stack item. +func Create(_ *interop.Context, v *vm.VM) error { + return vm.IteratorCreate(v) +} + +// Key returns current iterator key. +func Key(_ *interop.Context, v *vm.VM) error { + return vm.IteratorKey(v) +} + +// Keys returns keys of the iterator. +func Keys(_ *interop.Context, v *vm.VM) error { + return vm.IteratorKeys(v) +} + +// Values returns values of the iterator. +func Values(_ *interop.Context, v *vm.VM) error { + return vm.IteratorValues(v) +} diff --git a/pkg/core/interop_neo.go b/pkg/core/interop_neo.go index 4dac4d80b..378214447 100644 --- a/pkg/core/interop_neo.go +++ b/pkg/core/interop_neo.go @@ -799,49 +799,3 @@ func runtimeSerialize(_ *interop.Context, v *vm.VM) error { func runtimeDeserialize(_ *interop.Context, v *vm.VM) error { return vm.RuntimeDeserialize(v) } - -// enumeratorConcat concatenates 2 enumerators into a single one. -func enumeratorConcat(_ *interop.Context, v *vm.VM) error { - return vm.EnumeratorConcat(v) -} - -// enumeratorCreate creates an enumerator from an array-like stack item. -func enumeratorCreate(_ *interop.Context, v *vm.VM) error { - return vm.EnumeratorCreate(v) -} - -// enumeratorNext advances the enumerator, pushes true if is it was successful -// and false otherwise. -func enumeratorNext(_ *interop.Context, v *vm.VM) error { - return vm.EnumeratorNext(v) -} - -// enumeratorValue returns the current value of the enumerator. -func enumeratorValue(_ *interop.Context, v *vm.VM) error { - return vm.EnumeratorValue(v) -} - -// iteratorConcat concatenates 2 iterators into a single one. -func iteratorConcat(_ *interop.Context, v *vm.VM) error { - return vm.IteratorConcat(v) -} - -// iteratorCreate creates an iterator from array-like or map stack item. -func iteratorCreate(_ *interop.Context, v *vm.VM) error { - return vm.IteratorCreate(v) -} - -// iteratorKey returns current iterator key. -func iteratorKey(_ *interop.Context, v *vm.VM) error { - return vm.IteratorKey(v) -} - -// iteratorKeys returns keys of the iterator. -func iteratorKeys(_ *interop.Context, v *vm.VM) error { - return vm.IteratorKeys(v) -} - -// iteratorValues returns values of the iterator. -func iteratorValues(_ *interop.Context, v *vm.VM) error { - return vm.IteratorValues(v) -} diff --git a/pkg/core/interop_neo_test.go b/pkg/core/interop_neo_test.go index 868d63b70..26068690f 100644 --- a/pkg/core/interop_neo_test.go +++ b/pkg/core/interop_neo_test.go @@ -7,6 +7,8 @@ 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" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -76,19 +78,19 @@ func TestStorageFind(t *testing.T) { var iter *vm.InteropItem require.NotPanics(t, func() { iter = v.Estack().Top().Interop() }) - require.NoError(t, enumeratorNext(context, v)) + require.NoError(t, enumerator.Next(context, v)) require.True(t, v.Estack().Pop().Bool()) v.Estack().PushVal(iter) - require.NoError(t, iteratorKey(context, v)) + require.NoError(t, iterator.Key(context, v)) require.Equal(t, []byte{0x01, 0x02}, v.Estack().Pop().Bytes()) v.Estack().PushVal(iter) - require.NoError(t, enumeratorValue(context, v)) + require.NoError(t, enumerator.Value(context, v)) require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, v.Estack().Pop().Bytes()) v.Estack().PushVal(iter) - require.NoError(t, enumeratorNext(context, v)) + require.NoError(t, enumerator.Next(context, v)) require.False(t, v.Estack().Pop().Bool()) }) diff --git a/pkg/core/interops.go b/pkg/core/interops.go index 9e046958c..edcdb3310 100644 --- a/pkg/core/interops.go +++ b/pkg/core/interops.go @@ -11,6 +11,8 @@ import ( "sort" "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/smartcontract" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm" @@ -135,10 +137,10 @@ var neoInterops = []interop.Function{ {Name: "Neo.Contract.GetStorageContext", Func: contractGetStorageContext, Price: 1}, {Name: "Neo.Contract.IsPayable", Func: contractIsPayable, Price: 1}, {Name: "Neo.Contract.Migrate", Func: contractMigrate, Price: 0}, - {Name: "Neo.Enumerator.Concat", Func: enumeratorConcat, Price: 1}, - {Name: "Neo.Enumerator.Create", Func: enumeratorCreate, Price: 1}, - {Name: "Neo.Enumerator.Next", Func: enumeratorNext, Price: 1}, - {Name: "Neo.Enumerator.Value", Func: enumeratorValue, Price: 1}, + {Name: "Neo.Enumerator.Concat", Func: enumerator.Concat, Price: 1}, + {Name: "Neo.Enumerator.Create", Func: enumerator.Create, Price: 1}, + {Name: "Neo.Enumerator.Next", Func: enumerator.Next, Price: 1}, + {Name: "Neo.Enumerator.Value", Func: enumerator.Value, Price: 1}, {Name: "Neo.Header.GetConsensusData", Func: headerGetConsensusData, Price: 1}, {Name: "Neo.Header.GetHash", Func: headerGetHash, Price: 1}, {Name: "Neo.Header.GetIndex", Func: headerGetIndex, Price: 1}, @@ -150,11 +152,11 @@ var neoInterops = []interop.Function{ {Name: "Neo.Input.GetHash", Func: inputGetHash, Price: 1}, {Name: "Neo.Input.GetIndex", Func: inputGetIndex, Price: 1}, {Name: "Neo.InvocationTransaction.GetScript", Func: invocationTxGetScript, Price: 1}, - {Name: "Neo.Iterator.Concat", Func: iteratorConcat, Price: 1}, - {Name: "Neo.Iterator.Create", Func: iteratorCreate, Price: 1}, - {Name: "Neo.Iterator.Key", Func: iteratorKey, Price: 1}, - {Name: "Neo.Iterator.Keys", Func: iteratorKeys, Price: 1}, - {Name: "Neo.Iterator.Values", Func: iteratorValues, Price: 1}, + {Name: "Neo.Iterator.Concat", Func: iterator.Concat, Price: 1}, + {Name: "Neo.Iterator.Create", Func: iterator.Create, Price: 1}, + {Name: "Neo.Iterator.Key", Func: iterator.Key, Price: 1}, + {Name: "Neo.Iterator.Keys", Func: iterator.Keys, Price: 1}, + {Name: "Neo.Iterator.Values", Func: iterator.Values, Price: 1}, {Name: "Neo.Output.GetAssetId", Func: outputGetAssetID, Price: 1}, {Name: "Neo.Output.GetScriptHash", Func: outputGetScriptHash, Price: 1}, {Name: "Neo.Output.GetValue", Func: outputGetValue, Price: 1}, @@ -183,8 +185,8 @@ var neoInterops = []interop.Function{ {Name: "Neo.Witness.GetVerificationScript", Func: witnessGetVerificationScript, Price: 100}, // Aliases. - {Name: "Neo.Iterator.Next", Func: enumeratorNext, Price: 1}, - {Name: "Neo.Iterator.Value", Func: enumeratorValue, Price: 1}, + {Name: "Neo.Iterator.Next", Func: enumerator.Next, Price: 1}, + {Name: "Neo.Iterator.Value", Func: enumerator.Value, Price: 1}, // Old compatibility APIs. {Name: "AntShares.Account.GetBalance", Func: accountGetBalance, Price: 1},