frostfs-contract/common/storage.go
Evgenii Stratonikov 1ebeff7f6f [#42] common: Add routines for fixed-width uint64 marshaling
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2023-10-05 15:10:04 +03:00

28 lines
790 B
Go

package common
import (
"github.com/nspcc-dev/neo-go/pkg/interop/convert"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
// SetSerialized serializes data and puts it into contract storage.
func SetSerialized(ctx storage.Context, key interface{}, value interface{}) {
data := std.Serialize(value)
storage.Put(ctx, key, data)
}
// ToFixedWidth64 converts x to bytes such that numbers <= math.MaxUint64
// have constant with of 9.
func ToFixedWidth64(x int) []byte {
data := convert.ToBytes(x)
if x < 0 || len(data) >= 9 {
return data
}
return append(data, make([]byte, 9-len(data))...)
}
// FromFixedWidth64 is a reverse function for ToFixedWidth64.
func FromFixedWidth64(x []byte) int {
return convert.ToInteger(x)
}