2019-09-03 15:00:10 +00:00
|
|
|
package storagecontract
|
2018-08-20 11:22:46 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-25 14:40:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2018-08-20 11:22:46 +00:00
|
|
|
)
|
|
|
|
|
2020-08-10 10:42:02 +00:00
|
|
|
// ctx holds storage context for contract methods
|
|
|
|
var ctx storage.Context
|
2020-07-03 11:53:29 +00:00
|
|
|
|
2021-10-16 11:10:17 +00:00
|
|
|
// defaultKey represents the default key.
|
|
|
|
var defaultKey = []byte("default")
|
|
|
|
|
2020-08-10 10:42:02 +00:00
|
|
|
// init inits storage context before any other contract method is called
|
|
|
|
func init() {
|
|
|
|
ctx = storage.GetContext()
|
2020-07-03 11:53:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Put puts the value at the key.
|
2020-08-10 10:42:02 +00:00
|
|
|
func Put(key, value []byte) []byte {
|
|
|
|
storage.Put(ctx, key, value)
|
|
|
|
return key
|
2020-07-03 11:53:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// PutDefault puts the value to the default key.
|
2021-10-16 11:10:17 +00:00
|
|
|
func PutDefault(value []byte) []byte {
|
|
|
|
storage.Put(ctx, defaultKey, value)
|
|
|
|
return defaultKey
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Get returns the value at the passed key.
|
2023-04-03 10:34:24 +00:00
|
|
|
func Get(key []byte) any {
|
2020-08-10 10:42:02 +00:00
|
|
|
return storage.Get(ctx, key)
|
2020-07-03 11:53:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 11:10:17 +00:00
|
|
|
// GetDefault returns the value at the default key.
|
2023-04-03 10:34:24 +00:00
|
|
|
func GetDefault() any {
|
2021-10-16 11:10:17 +00:00
|
|
|
return storage.Get(ctx, defaultKey)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Delete deletes the value at the passed key.
|
2020-08-10 10:42:02 +00:00
|
|
|
func Delete(key []byte) bool {
|
2020-07-03 11:53:29 +00:00
|
|
|
storage.Delete(ctx, key)
|
|
|
|
return true
|
|
|
|
}
|
2018-08-20 11:22:46 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Find returns an array of key-value pairs with the key that matches the passed value.
|
2020-08-10 10:42:02 +00:00
|
|
|
func Find(value []byte) []string {
|
2021-01-12 10:39:31 +00:00
|
|
|
iter := storage.Find(ctx, value, storage.None)
|
2020-08-10 10:42:02 +00:00
|
|
|
result := []string{}
|
|
|
|
for iterator.Next(iter) {
|
2021-01-12 09:30:21 +00:00
|
|
|
val := iterator.Value(iter).([]string)
|
|
|
|
result = append(result, val[0]+":"+val[1])
|
2018-08-20 11:22:46 +00:00
|
|
|
}
|
2020-08-10 10:42:02 +00:00
|
|
|
return result
|
2018-08-20 11:22:46 +00:00
|
|
|
}
|
2023-08-08 15:29:32 +00:00
|
|
|
|
|
|
|
// FindReturnIter returns an iterator over key-value pairs with the key that has the specified prefix.
|
|
|
|
func FindReturnIter(prefix []byte) iterator.Iterator {
|
|
|
|
iter := storage.Find(ctx, prefix, storage.None)
|
|
|
|
return iter
|
|
|
|
}
|