2019-10-11 14:00:11 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-05-29 08:02:26 +00:00
|
|
|
"bytes"
|
2019-10-11 14:00:11 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-05-29 08:02:26 +00:00
|
|
|
"sort"
|
2019-10-11 14:00:11 +00:00
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2021-01-12 10:39:31 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2019-10-11 14:00:11 +00:00
|
|
|
)
|
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
var (
|
|
|
|
errGasLimitExceeded = errors.New("gas limit exceeded")
|
|
|
|
errFindInvalidOptions = errors.New("invalid Find options")
|
|
|
|
)
|
2020-06-15 08:39:15 +00:00
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
// storageFind finds stored key-value pair.
|
2020-08-07 11:37:49 +00:00
|
|
|
func storageFind(ic *interop.Context) error {
|
|
|
|
stcInterface := ic.VM.Estack().Pop().Value()
|
2019-10-11 14:00:11 +00:00
|
|
|
stc, ok := stcInterface.(*StorageContext)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not a StorageContext", stcInterface)
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
prefix := ic.VM.Estack().Pop().Bytes()
|
2021-01-12 10:39:31 +00:00
|
|
|
opts := ic.VM.Estack().Pop().BigInt().Int64()
|
|
|
|
if opts&^storage.FindAll != 0 {
|
|
|
|
return fmt.Errorf("%w: unknown flag", errFindInvalidOptions)
|
|
|
|
}
|
2021-01-12 12:32:27 +00:00
|
|
|
if opts&storage.FindKeysOnly != 0 &&
|
|
|
|
opts&(storage.FindDeserialize|storage.FindPick0|storage.FindPick1) != 0 {
|
2021-01-12 12:09:05 +00:00
|
|
|
return fmt.Errorf("%w KeysOnly conflicts with other options", errFindInvalidOptions)
|
|
|
|
}
|
2021-01-12 10:39:31 +00:00
|
|
|
if opts&storage.FindValuesOnly != 0 &&
|
|
|
|
opts&(storage.FindKeysOnly|storage.FindRemovePrefix) != 0 {
|
|
|
|
return fmt.Errorf("%w: KeysOnly conflicts with ValuesOnly", errFindInvalidOptions)
|
|
|
|
}
|
2021-01-12 12:32:27 +00:00
|
|
|
if opts&storage.FindPick0 != 0 && opts&storage.FindPick1 != 0 {
|
|
|
|
return fmt.Errorf("%w: Pick0 conflicts with Pick1", errFindInvalidOptions)
|
|
|
|
}
|
|
|
|
if opts&storage.FindDeserialize == 0 && (opts&storage.FindPick0 != 0 || opts&storage.FindPick1 != 0) {
|
|
|
|
return fmt.Errorf("%w: PickN is specified without Deserialize", errFindInvalidOptions)
|
|
|
|
}
|
2020-06-18 10:50:30 +00:00
|
|
|
siMap, err := ic.DAO.GetStorageItemsWithPrefix(stc.ID, prefix)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
filteredMap := stackitem.NewMap()
|
2019-10-11 14:00:11 +00:00
|
|
|
for k, v := range siMap {
|
2021-04-07 11:52:50 +00:00
|
|
|
key := append(prefix, []byte(k)...)
|
|
|
|
keycopy := make([]byte, len(key))
|
|
|
|
copy(keycopy, key)
|
|
|
|
filteredMap.Add(stackitem.NewByteArray(keycopy), stackitem.NewByteArray(v))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
sort.Slice(filteredMap.Value().([]stackitem.MapElement), func(i, j int) bool {
|
|
|
|
return bytes.Compare(filteredMap.Value().([]stackitem.MapElement)[i].Key.Value().([]byte),
|
|
|
|
filteredMap.Value().([]stackitem.MapElement)[j].Key.Value().([]byte)) == -1
|
2020-05-29 08:02:26 +00:00
|
|
|
})
|
2019-10-11 14:00:11 +00:00
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
item := storage.NewIterator(filteredMap, opts)
|
|
|
|
ic.VM.Estack().PushVal(stackitem.NewInterop(item))
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|