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"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
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
|
|
|
)
|
|
|
|
|
2020-06-15 08:39:15 +00:00
|
|
|
var errGasLimitExceeded = errors.New("gas limit exceeded")
|
|
|
|
|
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()
|
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 {
|
2020-06-03 12:55:06 +00:00
|
|
|
filteredMap.Add(stackitem.NewByteArray(append(prefix, []byte(k)...)), stackitem.NewByteArray(v.Value))
|
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
|
|
|
|
2019-12-26 11:34:38 +00:00
|
|
|
item := vm.NewMapIterator(filteredMap)
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().PushVal(item)
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|