cosmos-sdk/types/stdlib/stdlib.go

233 lines
4.7 KiB
Go
Raw Normal View History

2018-03-14 11:20:15 -07:00
package types
import (
"errors"
2018-03-15 02:59:36 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-03-14 11:20:15 -07:00
wire "github.com/cosmos/cosmos-sdk/wire"
)
type ListMapper interface { // Solidity list like structure
2018-03-15 02:59:36 -07:00
Len(sdk.Context) int64
Get(sdk.Context, int64, interface{})
Set(sdk.Context, int64, interface{})
Push(sdk.Context, interface{})
Iterate(sdk.Context, interface{}, func(sdk.Context, int64))
2018-03-14 11:20:15 -07:00
}
type listMapper struct {
2018-03-15 02:59:36 -07:00
key sdk.StoreKey
2018-03-14 11:20:15 -07:00
cdc *wire.Codec
lk []byte
}
2018-03-15 02:59:36 -07:00
func NewListMapper(cdc *wire.Codec, key sdk.StoreKey) ListMapper {
2018-03-14 11:20:15 -07:00
lk, err := cdc.MarshalBinary(int64(-1))
if err != nil {
panic(err)
}
return listMapper{
key: key,
cdc: cdc,
lk: lk,
}
}
2018-03-15 02:59:36 -07:00
func (lm listMapper) Len(ctx sdk.Context) int64 {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(lm.key)
bz := store.Get(lm.lk)
if bz == nil {
zero, err := lm.cdc.MarshalBinary(0)
if err != nil {
panic(err)
}
store.Set(lm.lk, zero)
return 0
}
var res int64
if err := lm.cdc.UnmarshalBinary(bz, &res); err != nil {
panic(err)
}
return res
}
2018-03-15 02:59:36 -07:00
func (lm listMapper) Get(ctx sdk.Context, index int64, ptr interface{}) {
2018-03-14 11:20:15 -07:00
if index < 0 {
panic(errors.New(""))
}
store := ctx.KVStore(lm.key)
bz := store.Get(marshalInt64(lm.cdc, index))
if err := lm.cdc.UnmarshalBinary(bz, ptr); err != nil {
panic(err)
}
}
2018-03-15 02:59:36 -07:00
func (lm listMapper) Set(ctx sdk.Context, index int64, value interface{}) {
2018-03-14 11:20:15 -07:00
if index < 0 {
panic(errors.New(""))
}
store := ctx.KVStore(lm.key)
bz, err := lm.cdc.MarshalBinary(value)
if err != nil {
panic(err)
}
store.Set(marshalInt64(lm.cdc, index), bz)
}
2018-03-15 02:59:36 -07:00
func (lm listMapper) Push(ctx sdk.Context, value interface{}) {
2018-03-14 11:20:15 -07:00
length := lm.Len(ctx)
lm.Set(ctx, length, value)
store := ctx.KVStore(lm.key)
store.Set(lm.lk, marshalInt64(lm.cdc, length+1))
}
2018-03-15 02:59:36 -07:00
func (lm listMapper) Iterate(ctx sdk.Context, ptr interface{}, fn func(sdk.Context, int64)) {
2018-03-14 11:20:15 -07:00
length := lm.Len(ctx)
for i := int64(0); i < length; i++ {
lm.Get(ctx, i, ptr)
fn(ctx, i)
}
}
type QueueMapper interface {
2018-03-15 02:59:36 -07:00
Push(sdk.Context, interface{})
Peek(sdk.Context, interface{})
Pop(sdk.Context)
IsEmpty(sdk.Context) bool
Iterate(sdk.Context, interface{}, func(sdk.Context) bool)
2018-03-14 11:20:15 -07:00
}
type queueMapper struct {
2018-03-15 02:59:36 -07:00
key sdk.StoreKey
2018-03-14 11:20:15 -07:00
cdc *wire.Codec
ik []byte
}
2018-03-15 02:59:36 -07:00
func NewQueueMapper(cdc *wire.Codec, key sdk.StoreKey) QueueMapper {
2018-03-14 11:20:15 -07:00
ik, err := cdc.MarshalBinary(int64(-1))
if err != nil {
panic(err)
}
return queueMapper{
key: key,
cdc: cdc,
ik: ik,
}
}
type queueInfo struct {
// begin <= elems < end
Begin int64
End int64
}
func (info queueInfo) validateBasic() error {
if info.End < info.Begin || info.Begin < 0 || info.End < 0 {
return errors.New("")
}
return nil
}
func (info queueInfo) isEmpty() bool {
return info.Begin == info.End
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) getQueueInfo(store sdk.KVStore) queueInfo {
2018-03-14 11:20:15 -07:00
bz := store.Get(qm.ik)
if bz == nil {
store.Set(qm.ik, marshalQueueInfo(qm.cdc, queueInfo{0, 0}))
return queueInfo{0, 0}
}
var info queueInfo
if err := qm.cdc.UnmarshalBinary(bz, &info); err != nil {
panic(err)
}
if err := info.validateBasic(); err != nil {
panic(err)
}
return info
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) setQueueInfo(store sdk.KVStore, info queueInfo) {
2018-03-14 11:20:15 -07:00
bz, err := qm.cdc.MarshalBinary(info)
if err != nil {
panic(err)
}
store.Set(qm.ik, bz)
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) Push(ctx sdk.Context, value interface{}) {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
bz, err := qm.cdc.MarshalBinary(value)
if err != nil {
panic(err)
}
store.Set(marshalInt64(qm.cdc, info.End), bz)
info.End++
qm.setQueueInfo(store, info)
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) Peek(ctx sdk.Context, ptr interface{}) {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
bz := store.Get(marshalInt64(qm.cdc, info.Begin))
if err := qm.cdc.UnmarshalBinary(bz, ptr); err != nil {
panic(err)
}
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) Pop(ctx sdk.Context) {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
store.Delete(marshalInt64(qm.cdc, info.Begin))
info.Begin++
qm.setQueueInfo(store, info)
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) IsEmpty(ctx sdk.Context) bool {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
return info.isEmpty()
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) Iterate(ctx sdk.Context, ptr interface{}, fn func(sdk.Context) bool) {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
var i int64
for i = info.Begin; i < info.End; i++ {
key := marshalInt64(qm.cdc, i)
bz := store.Get(key)
if err := qm.cdc.UnmarshalBinary(bz, ptr); err != nil {
panic(err)
}
store.Delete(key)
if fn(ctx) {
break
}
}
info.Begin = i
qm.setQueueInfo(store, info)
}
func marshalQueueInfo(cdc *wire.Codec, info queueInfo) []byte {
bz, err := cdc.MarshalBinary(info)
if err != nil {
panic(err)
}
return bz
}
func marshalInt64(cdc *wire.Codec, i int64) []byte {
bz, err := cdc.MarshalBinary(i)
if err != nil {
panic(err)
}
return bz
}