2020-10-01 15:17:09 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2021-02-02 15:46:43 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2020-11-27 10:55:48 +00:00
|
|
|
|
2020-10-01 15:17:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2021-02-02 15:46:43 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-10-01 15:17:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-01-22 12:12:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-10-01 15:17:09 +00:00
|
|
|
)
|
|
|
|
|
2021-02-02 15:46:43 +00:00
|
|
|
var intOne = big.NewInt(1)
|
|
|
|
|
2020-10-01 15:17:09 +00:00
|
|
|
func getSerializableFromDAO(id int32, d dao.DAO, key []byte, item io.Serializable) error {
|
|
|
|
si := d.GetStorageItem(id, key)
|
|
|
|
if si == nil {
|
|
|
|
return storage.ErrKeyNotFound
|
|
|
|
}
|
2021-03-05 14:06:54 +00:00
|
|
|
r := io.NewBinReaderFromBuf(si)
|
2020-10-01 15:17:09 +00:00
|
|
|
item.DecodeBinary(r)
|
|
|
|
return r.Err
|
|
|
|
}
|
2020-11-19 10:00:46 +00:00
|
|
|
|
|
|
|
func putSerializableToDAO(id int32, d dao.DAO, key []byte, item io.Serializable) error {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
item.EncodeBinary(w.BinWriter)
|
|
|
|
if w.Err != nil {
|
|
|
|
return w.Err
|
|
|
|
}
|
2021-03-05 14:06:54 +00:00
|
|
|
return d.PutStorageItem(id, key, w.Bytes())
|
2020-11-19 10:00:46 +00:00
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
|
2021-02-02 15:46:43 +00:00
|
|
|
func setIntWithKey(id int32, dao dao.DAO, key []byte, value int64) error {
|
2021-03-05 14:06:54 +00:00
|
|
|
return dao.PutStorageItem(id, key, bigint.ToBytes(big.NewInt(value)))
|
2021-02-02 15:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getIntWithKey(id int32, dao dao.DAO, key []byte) int64 {
|
|
|
|
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 14:06:54 +00:00
|
|
|
return bigint.FromBytes(si).Int64()
|
2021-02-02 15:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
// makeUint160Key creates a key from account script hash.
|
|
|
|
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 12:12:09 +00:00
|
|
|
|
|
|
|
func toString(item stackitem.Item) string {
|
|
|
|
s, err := stackitem.ToString(item)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|