core: move enumerator/iterator interops to a separate packages

This commit is contained in:
Evgenii Stratonikov 2020-04-08 14:08:43 +03:00
parent 7ffc6c0936
commit 30cc2c5783
5 changed files with 77 additions and 61 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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())
})

View file

@ -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},