2020-10-01 18:17:09 +03:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2021-02-02 18:46:43 +03:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2020-11-27 13:55:48 +03:00
|
|
|
|
2020-10-01 18:17:09 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2021-02-02 18:46:43 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-12-13 18:26:35 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-01-22 15:12:09 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-10-01 18:17:09 +03:00
|
|
|
)
|
|
|
|
|
2021-02-02 18:46:43 +03:00
|
|
|
var intOne = big.NewInt(1)
|
2021-11-30 21:10:48 +03:00
|
|
|
var intTwo = big.NewInt(2)
|
2021-02-02 18:46:43 +03:00
|
|
|
|
2022-02-16 18:04:47 +03:00
|
|
|
func getConvertibleFromDAO(id int32, d *dao.Simple, key []byte, conv stackitem.Convertible) error {
|
2020-10-01 18:17:09 +03:00
|
|
|
si := d.GetStorageItem(id, key)
|
|
|
|
if si == nil {
|
|
|
|
return storage.ErrKeyNotFound
|
|
|
|
}
|
2021-07-17 18:37:33 +03:00
|
|
|
return stackitem.DeserializeConvertible(si, conv)
|
2020-10-01 18:17:09 +03:00
|
|
|
}
|
2020-11-19 13:00:46 +03:00
|
|
|
|
2022-02-16 18:04:47 +03:00
|
|
|
func putConvertibleToDAO(id int32, d *dao.Simple, key []byte, conv stackitem.Convertible) error {
|
2022-05-31 20:10:20 +03:00
|
|
|
item, err := conv.ToStackItem()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := d.GetItemCtx().Serialize(item, false)
|
2021-07-17 18:37:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-19 13:00:46 +03:00
|
|
|
}
|
2022-02-16 17:48:15 +03:00
|
|
|
d.PutStorageItem(id, key, data)
|
|
|
|
return nil
|
2020-11-19 13:00:46 +03:00
|
|
|
}
|
2020-11-27 13:55:48 +03:00
|
|
|
|
2022-02-16 18:04:47 +03:00
|
|
|
func setIntWithKey(id int32, dao *dao.Simple, key []byte, value int64) {
|
2022-05-31 23:10:56 +03:00
|
|
|
dao.PutBigInt(id, key, big.NewInt(value))
|
2021-02-02 18:46:43 +03:00
|
|
|
}
|
|
|
|
|
2022-02-16 18:04:47 +03:00
|
|
|
func getIntWithKey(id int32, dao *dao.Simple, key []byte) int64 {
|
2021-02-02 18:46:43 +03:00
|
|
|
si := dao.GetStorageItem(id, key)
|
|
|
|
if si == nil {
|
|
|
|
panic(fmt.Errorf("item with id = %d and key = %s is not initialized", id, hex.EncodeToString(key)))
|
|
|
|
}
|
2021-03-05 17:06:54 +03:00
|
|
|
return bigint.FromBytes(si).Int64()
|
2021-02-02 18:46:43 +03:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// makeUint160Key creates a key from the account script hash.
|
2020-12-13 18:26:35 +03:00
|
|
|
func makeUint160Key(prefix byte, h util.Uint160) []byte {
|
|
|
|
k := make([]byte, util.Uint160Size+1)
|
|
|
|
k[0] = prefix
|
|
|
|
copy(k[1:], h.BytesBE())
|
|
|
|
return k
|
|
|
|
}
|
2021-01-22 15:12:09 +03:00
|
|
|
|
|
|
|
func toString(item stackitem.Item) string {
|
|
|
|
s, err := stackitem.ToString(item)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|