From 9d983ec77bd066dbdf00ec839833708deb566415 Mon Sep 17 00:00:00 2001 From: Jeroen Peeters Date: Mon, 20 Aug 2018 13:22:46 +0200 Subject: [PATCH] feat: add storage APi's and example Imported from CityOfZion/neo-storm (86ac5c215a2c6ec710f2fd913e0ace63d6ea993e). --- examples/{ => check_witness}/check_witness.go | 0 examples/storage/storage.go | 46 +++++++++++++++++++ interop/storage/storage.go | 19 ++++++++ 3 files changed, 65 insertions(+) rename examples/{ => check_witness}/check_witness.go (100%) create mode 100644 examples/storage/storage.go create mode 100644 interop/storage/storage.go diff --git a/examples/check_witness.go b/examples/check_witness/check_witness.go similarity index 100% rename from examples/check_witness.go rename to examples/check_witness/check_witness.go diff --git a/examples/storage/storage.go b/examples/storage/storage.go new file mode 100644 index 000000000..26a7f5dc7 --- /dev/null +++ b/examples/storage/storage.go @@ -0,0 +1,46 @@ +package storage_contract + +import ( + "github.com/CityOfZion/neo-go-sc/interop/storage" +) + +func Main(operation string, args []interface{}) interface{} { + var ctx = storage.GetContext() + + // 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 + } + + // TODO: storage.Find() + + return false +} + +func checkArgs(args []interface{}, length int) bool { + if len(args) == length { + return true + } + + return false +} diff --git a/interop/storage/storage.go b/interop/storage/storage.go new file mode 100644 index 000000000..de803e87c --- /dev/null +++ b/interop/storage/storage.go @@ -0,0 +1,19 @@ +package storage + +// Context represents the storage context +type Context interface{} + +// GetContext returns the storage context +func GetContext() interface{} { return nil } + +// Put value at given key +func Put(ctx interface{}, key interface{}, value interface{}) {} + +// Get value matching given key +func Get(ctx interface{}, key interface{}) interface{} { return 0 } + +// Delete key value pair from storage +func Delete(ctx interface{}, key interface{}) {} + +// Find values stored on keys partially matching given key +func Find(ctx interface{}, key interface{}) interface{} { return 0 }