2020-05-18 15:06:12 +00:00
|
|
|
/*
|
|
|
|
Package storage provides functions to access and modify contract's storage.
|
|
|
|
Neo storage's model follows simple key-value DB pattern, this storage is a part
|
|
|
|
of blockchain state, so you can use it between various invocations of the same
|
|
|
|
contract.
|
|
|
|
*/
|
2018-08-20 11:22:46 +00:00
|
|
|
package storage
|
|
|
|
|
2021-02-05 16:02:09 +00:00
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
|
|
|
|
)
|
2018-08-31 08:23:57 +00:00
|
|
|
|
2020-05-18 15:06:12 +00:00
|
|
|
// Context represents storage context that is mandatory for Put/Get/Delete
|
|
|
|
// operations. It's an opaque type that can only be created properly by
|
2020-05-18 15:13:45 +00:00
|
|
|
// GetContext, GetReadOnlyContext or ConvertContextToReadOnly. It's similar
|
|
|
|
// to Neo .net framework's StorageContext class.
|
2018-08-21 07:11:25 +00:00
|
|
|
type Context struct{}
|
2018-08-20 11:22:46 +00:00
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
// FindFlags represents parameters to `Find` iterator.
|
|
|
|
type FindFlags byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
// None is default option. Iterator values are key-value pairs.
|
|
|
|
None FindFlags = 0
|
|
|
|
// KeysOnly is used for iterating over keys.
|
|
|
|
KeysOnly FindFlags = 1 << 0
|
|
|
|
// RemovePrefix is used for stripping 1-byte prefix from keys.
|
|
|
|
RemovePrefix FindFlags = 1 << 1
|
|
|
|
// ValuesOnly is used for iterating over values.
|
|
|
|
ValuesOnly FindFlags = 1 << 2
|
2021-01-12 12:09:05 +00:00
|
|
|
// DeserializeValues is used for deserializing values on-the-fly.
|
|
|
|
// It can be combined with other options.
|
|
|
|
DeserializeValues FindFlags = 1 << 3
|
2021-01-12 12:32:27 +00:00
|
|
|
// PickField0 is used to get first field in a serialized struct or array.
|
|
|
|
PickField0 FindFlags = 1 << 4
|
|
|
|
// PickField1 is used to get second field in a serialized struct or array.
|
|
|
|
PickField1 FindFlags = 1 << 5
|
2021-01-12 10:39:31 +00:00
|
|
|
)
|
|
|
|
|
2020-05-18 15:13:45 +00:00
|
|
|
// ConvertContextToReadOnly returns new context from the given one, but with
|
|
|
|
// writing capability turned off, so that you could only invoke Get and Find
|
|
|
|
// using this new Context. If Context is already read-only this function is a
|
2020-06-10 08:55:56 +00:00
|
|
|
// no-op. It uses `System.Storage.AsReadOnly` syscall.
|
2021-02-05 16:02:09 +00:00
|
|
|
func ConvertContextToReadOnly(ctx Context) Context {
|
|
|
|
return neogointernal.Syscall0("System.Storage.AsReadOnly").(Context)
|
|
|
|
}
|
2020-05-18 15:13:45 +00:00
|
|
|
|
2020-05-18 15:06:12 +00:00
|
|
|
// GetContext returns current contract's (that invokes this function) storage
|
2020-06-10 08:55:56 +00:00
|
|
|
// context. It uses `System.Storage.GetContext` syscall.
|
2021-02-05 16:02:09 +00:00
|
|
|
func GetContext() Context {
|
|
|
|
return neogointernal.Syscall0("System.Storage.GetContext").(Context)
|
|
|
|
}
|
2018-08-20 11:22:46 +00:00
|
|
|
|
2020-05-18 15:13:45 +00:00
|
|
|
// GetReadOnlyContext returns current contract's (that invokes this function)
|
|
|
|
// storage context in read-only mode, you can use this context for Get and Find
|
|
|
|
// functions, but using it for Put and Delete will fail. It uses
|
2020-06-10 08:55:56 +00:00
|
|
|
// `System.Storage.GetReadOnlyContext` syscall.
|
2021-02-05 16:02:09 +00:00
|
|
|
func GetReadOnlyContext() Context {
|
|
|
|
return neogointernal.Syscall0("System.Storage.GetReadOnlyContext").(Context)
|
|
|
|
}
|
2020-05-18 15:13:45 +00:00
|
|
|
|
2020-05-18 15:06:12 +00:00
|
|
|
// Put saves given value with given key in the storage using given Context.
|
|
|
|
// Even though it accepts interface{} for both, you can only pass simple types
|
|
|
|
// there like string, []byte, int or bool (not structures or slices of more
|
|
|
|
// complex types). To put more complex types there serialize them first using
|
2020-06-10 08:55:56 +00:00
|
|
|
// runtime.Serialize. This function uses `System.Storage.Put` syscall.
|
2021-02-05 16:02:09 +00:00
|
|
|
func Put(ctx Context, key interface{}, value interface{}) {
|
|
|
|
neogointernal.Syscall3NoReturn("System.Storage.Put", ctx, key, value)
|
|
|
|
}
|
2018-08-20 11:22:46 +00:00
|
|
|
|
2020-07-22 08:22:58 +00:00
|
|
|
// PutEx is an advanced version of Put which saves given value with given key
|
|
|
|
// and given ReadOnly flag in the storage using given Context. `flag` argument
|
|
|
|
// can either be odd for constant storage items or even for variable storage items.
|
|
|
|
// Refer to Put function description for details on how to pass the remaining
|
|
|
|
// arguments. This function uses `System.Storage.PutEx` syscall.
|
2021-02-05 16:02:09 +00:00
|
|
|
func PutEx(ctx Context, key interface{}, value interface{}, flag int64) {
|
|
|
|
neogointernal.Syscall4NoReturn("System.Storage.PutEx", ctx, key, value, flag)
|
|
|
|
}
|
2020-07-22 08:22:58 +00:00
|
|
|
|
2020-05-18 15:06:12 +00:00
|
|
|
// Get retrieves value stored for the given key using given Context. See Put
|
2020-06-23 21:06:42 +00:00
|
|
|
// documentation on possible key and value types. If the value is not present in
|
|
|
|
// the database it returns nil. This function uses `System.Storage.Get` syscall.
|
2021-02-05 16:02:09 +00:00
|
|
|
func Get(ctx Context, key interface{}) interface{} {
|
|
|
|
return neogointernal.Syscall2("System.Storage.Get", ctx, key)
|
|
|
|
}
|
2018-08-20 11:22:46 +00:00
|
|
|
|
2020-05-18 15:06:12 +00:00
|
|
|
// Delete removes key-value pair from storage by the given key using given
|
|
|
|
// Context. See Put documentation on possible key types. This function uses
|
2020-06-10 08:55:56 +00:00
|
|
|
// `System.Storage.Delete` syscall.
|
2021-02-05 16:02:09 +00:00
|
|
|
func Delete(ctx Context, key interface{}) {
|
|
|
|
neogointernal.Syscall2NoReturn("System.Storage.Delete", ctx, key)
|
|
|
|
}
|
2018-08-20 11:22:46 +00:00
|
|
|
|
2020-05-18 15:06:12 +00:00
|
|
|
// Find returns an iterator.Iterator over key-value pairs in the given Context
|
|
|
|
// that match the given key (contain it as a prefix). See Put documentation on
|
|
|
|
// possible key types and iterator package documentation on how to use the
|
2020-06-10 08:55:56 +00:00
|
|
|
// returned value. This function uses `System.Storage.Find` syscall.
|
2021-01-12 10:39:31 +00:00
|
|
|
func Find(ctx Context, key interface{}, options FindFlags) iterator.Iterator {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall3("System.Storage.Find", ctx, key, options).(iterator.Iterator)
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|