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
|
|
|
)
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// Main is a very useful function.
|
2018-08-20 11:22:46 +00:00
|
|
|
func Main(operation string, args []interface{}) interface{} {
|
2018-08-21 07:29:28 +00:00
|
|
|
ctx := storage.GetContext()
|
2018-08-20 11:22:46 +00:00
|
|
|
|
|
|
|
// Puts value at key
|
|
|
|
if operation == "put" {
|
|
|
|
if checkArgs(args, 2) {
|
|
|
|
key := args[0].([]byte)
|
|
|
|
value := args[1].([]byte)
|
|
|
|
storage.Put(ctx, key, value)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the value at passed key
|
|
|
|
if operation == "get" {
|
|
|
|
if checkArgs(args, 1) {
|
|
|
|
key := args[0].([]byte)
|
|
|
|
return storage.Get(ctx, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes the value at passed key
|
|
|
|
if operation == "delete" {
|
|
|
|
key := args[0].([]byte)
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-03-25 14:40:58 +00:00
|
|
|
// Returns an array of key-value pairs with key that matched the passed value
|
|
|
|
if operation == "find" {
|
|
|
|
if checkArgs(args, 1) {
|
|
|
|
value := args[0].([]byte)
|
|
|
|
iter := storage.Find(ctx, value)
|
|
|
|
result := []string{}
|
|
|
|
for iterator.Next(iter) {
|
|
|
|
val := iterator.Value(iter)
|
|
|
|
key := iterator.Key(iter)
|
|
|
|
result = append(result, key.(string)+":"+val.(string))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
2018-08-20 11:22:46 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkArgs(args []interface{}, length int) bool {
|
|
|
|
if len(args) == length {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|