simple-contract/FirstContract/contract/contract.go
Lebedeva Ekaterina f4795cfb31 [#XX] Problem with estack
Unexpected "runtime error: invalid memory address or nil pointer
dereference" in TestContract_Add

Signed-off-by: Lebedeva Ekaterina <ekaterina.lebedeva@yadro.com>
2023-07-24 14:28:23 +03:00

44 lines
867 B
Go

package contract
import (
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
// Prefixes used for contract data storage.
const (
// prefixNumber contains map from the key to stored number.
prefixNumber byte = 0x00
)
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, append([]byte{prefixNumber}, key...))
if num == nil {
panic("Cannot get number")
}
return num.(int)
}
func PutNumber(key []byte, num int) {
if len(key) != keySize {
panic("Invalid key size")
}
ctx := storage.GetContext()
storage.Put(ctx, append([]byte{prefixNumber}, key...), num)
}
func Add(a, b int) int {
return a + b
}