[#XX] Added 'key rules' and key test
Removed unnecessary 'Hello world' contracts Added required key size and prefix Modified Put and Get contracts so that they take a key as a parameter Added test for invalid key case Modified existing tests Signed-off-by: Lebedeva Ekaterina <ekaterina.lebedeva@yadro.com>
This commit is contained in:
parent
b6cdd15be1
commit
caa0544ada
2 changed files with 41 additions and 25 deletions
|
@ -1,39 +1,40 @@
|
|||
package contract
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||
)
|
||||
|
||||
var notificationName string
|
||||
// Prefixes used for contract data storage.
|
||||
const (
|
||||
// prefixNumber contains map from the key to stored number.
|
||||
prefixNumber byte = 0x00
|
||||
)
|
||||
|
||||
// init initializes notificationName before calling any other smart-contract method
|
||||
func init() {
|
||||
notificationName = "Hello world!"
|
||||
}
|
||||
|
||||
// RuntimeNotify sends runtime notification with "Hello world!" name
|
||||
func RuntimeNotify(args []interface{}) {
|
||||
runtime.Notify(notificationName, args)
|
||||
}
|
||||
|
||||
// the answer to the “Great Question” of “Life, the Universe and Everything”
|
||||
func GetNumber() int {
|
||||
const (
|
||||
keySize = 5
|
||||
)
|
||||
|
||||
func GetNumber(key []byte) int {
|
||||
if len(key) != keySize {
|
||||
panic("Invalid key size")
|
||||
}
|
||||
// var num int
|
||||
ctx := storage.GetContext()
|
||||
// it := storage.Find(ctx, 123, storage.ValuesOnly)
|
||||
// for iterator.Next(it) {
|
||||
// num = iterator.Value(it).(int)
|
||||
// }
|
||||
num := storage.Get(ctx, 123)
|
||||
num := storage.Get(ctx, append([]byte{prefixNumber}, key...))
|
||||
if num == nil {
|
||||
panic("Cannot get number")
|
||||
}
|
||||
return num.(int)
|
||||
}
|
||||
|
||||
func PutNumber(num int) {
|
||||
func PutNumber(key []byte, num int) {
|
||||
if len(key) != keySize {
|
||||
panic("Invalid key size")
|
||||
}
|
||||
ctx := storage.GetContext()
|
||||
storage.Put(ctx, 123, num)
|
||||
storage.Put(ctx, append([]byte{prefixNumber}, key...), num)
|
||||
}
|
||||
|
|
|
@ -11,17 +11,23 @@ import (
|
|||
|
||||
const ctrPath = "../contract"
|
||||
|
||||
// Key for tests
|
||||
var (
|
||||
validKey = []byte{1, 2, 3, 4, 5}
|
||||
invalidKey = []byte{1, 2, 3}
|
||||
)
|
||||
|
||||
func newExecutor(t *testing.T) *neotest.Executor {
|
||||
bc, acc := chain.NewSingle(t)
|
||||
return neotest.NewExecutor(t, bc, acc, acc)
|
||||
}
|
||||
|
||||
func TestContract_GetNumber(t *testing.T) {
|
||||
e := newExecutor(t)
|
||||
ctrGetNum := neotest.CompileFile(t, e.CommitteeHash, ctrPath, path.Join(ctrPath, "neo-go.yml"))
|
||||
e.DeployContract(t, ctrGetNum, nil)
|
||||
inv := e.CommitteeInvoker(ctrGetNum.Hash)
|
||||
inv.InvokeFail(t, "Cannot get number", "getNumber")
|
||||
}
|
||||
|
||||
func newExecutor(t *testing.T) *neotest.Executor {
|
||||
bc, acc := chain.NewSingle(t)
|
||||
return neotest.NewExecutor(t, bc, acc, acc)
|
||||
inv.InvokeFail(t, "Cannot get number", "getNumber", validKey)
|
||||
}
|
||||
|
||||
func TestContract_PutNumber(t *testing.T) {
|
||||
|
@ -29,7 +35,16 @@ func TestContract_PutNumber(t *testing.T) {
|
|||
ctrPutGetNum := neotest.CompileFile(t, e.CommitteeHash, ctrPath, path.Join(ctrPath, "neo-go.yml"))
|
||||
e.DeployContract(t, ctrPutGetNum, nil)
|
||||
inv := e.CommitteeInvoker(ctrPutGetNum.Hash)
|
||||
inv.Invoke(t, stackitem.Null{}, "putNumber", 42)
|
||||
inv.Invoke(t, stackitem.Null{}, "putNumber", validKey, 42)
|
||||
|
||||
inv.Invoke(t, 42, "getNumber")
|
||||
inv.Invoke(t, 42, "getNumber", validKey)
|
||||
}
|
||||
|
||||
func TestContract_InvalidKey(t *testing.T) {
|
||||
e := newExecutor(t)
|
||||
ctrPutGetNum := neotest.CompileFile(t, e.CommitteeHash, ctrPath, path.Join(ctrPath, "neo-go.yml"))
|
||||
e.DeployContract(t, ctrPutGetNum, nil)
|
||||
inv := e.CommitteeInvoker(ctrPutGetNum.Hash)
|
||||
inv.InvokeFail(t, "Invalid key size", "putNumber", invalidKey, 42)
|
||||
inv.InvokeFail(t, "Invalid key size", "getNumber", invalidKey)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue