github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/native/util.go (about)

     1  package native
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"math/big"
     7  
     8  	"github.com/nspcc-dev/neo-go/pkg/core/dao"
     9  	"github.com/nspcc-dev/neo-go/pkg/core/storage"
    10  	"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
    11  	"github.com/nspcc-dev/neo-go/pkg/util"
    12  	"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
    13  )
    14  
    15  var intOne = big.NewInt(1)
    16  var intTwo = big.NewInt(2)
    17  
    18  func getConvertibleFromDAO(id int32, d *dao.Simple, key []byte, conv stackitem.Convertible) error {
    19  	si := d.GetStorageItem(id, key)
    20  	if si == nil {
    21  		return storage.ErrKeyNotFound
    22  	}
    23  	return stackitem.DeserializeConvertible(si, conv)
    24  }
    25  
    26  func putConvertibleToDAO(id int32, d *dao.Simple, key []byte, conv stackitem.Convertible) error {
    27  	item, err := conv.ToStackItem()
    28  	if err != nil {
    29  		return err
    30  	}
    31  	data, err := d.GetItemCtx().Serialize(item, false)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	d.PutStorageItem(id, key, data)
    36  	return nil
    37  }
    38  
    39  func setIntWithKey(id int32, dao *dao.Simple, key []byte, value int64) {
    40  	dao.PutBigInt(id, key, big.NewInt(value))
    41  }
    42  
    43  func getIntWithKey(id int32, dao *dao.Simple, key []byte) int64 {
    44  	si := dao.GetStorageItem(id, key)
    45  	if si == nil {
    46  		panic(fmt.Errorf("item with id = %d and key = %s is not initialized", id, hex.EncodeToString(key)))
    47  	}
    48  	return bigint.FromBytes(si).Int64()
    49  }
    50  
    51  // makeUint160Key creates a key from the account script hash.
    52  func makeUint160Key(prefix byte, h util.Uint160) []byte {
    53  	k := make([]byte, util.Uint160Size+1)
    54  	k[0] = prefix
    55  	copy(k[1:], h.BytesBE())
    56  	return k
    57  }
    58  
    59  func toString(item stackitem.Item) string {
    60  	s, err := stackitem.ToString(item)
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  	return s
    65  }