cosmos-sdk/x/ibc/mapper.go

96 lines
2.3 KiB
Go
Raw Normal View History

2018-03-14 05:11:33 -07:00
package ibc
import (
2018-03-15 03:36:17 -07:00
"encoding/json"
2018-03-14 05:11:33 -07:00
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-03-15 03:36:17 -07:00
//wire "github.com/cosmos/cosmos-sdk/wire"
2018-03-14 05:11:33 -07:00
)
type IBCMapper struct {
ibcKey sdk.StoreKey
2018-03-14 05:53:26 -07:00
2018-03-15 03:36:17 -07:00
// cdc *wire.Codec
2018-03-14 05:11:33 -07:00
}
func NewIBCMapper(ibcKey sdk.StoreKey) IBCMapper {
2018-03-15 03:36:17 -07:00
// cdc := wire.NewCodec()
2018-03-14 05:53:26 -07:00
2018-03-14 05:11:33 -07:00
return IBCMapper{
ibcKey: ibcKey,
2018-03-15 03:36:17 -07:00
// cdc: cdc,
2018-03-14 05:53:26 -07:00
}
}
func IngressKey(srcChain string) []byte {
return []byte(fmt.Sprintf("ingress/%s", srcChain))
}
func EgressKey(destChain string, index int64) []byte {
return []byte(fmt.Sprintf("egress/%s/%d", destChain, index))
}
func EgressLengthKey(destChain string) []byte {
return []byte(fmt.Sprintf("egress/%s", destChain))
}
func (ibcm IBCMapper) getEgressLength(store sdk.KVStore, destChain string) int64 {
bz := store.Get(EgressLengthKey(destChain))
if bz == nil {
2018-03-15 03:36:17 -07:00
zero, err := json.Marshal(int64(0)) //ibcm.cdc.MarshalBinary(int64(0))
2018-03-14 05:53:26 -07:00
if err != nil {
panic(err)
}
store.Set(EgressLengthKey(destChain), zero)
return 0
}
var res int64
2018-03-15 03:36:17 -07:00
if err := json.Unmarshal(bz, &res); /*ibcm.cdc.UnmarshalBinary(bz, &res)*/ err != nil {
2018-03-14 05:53:26 -07:00
panic(err)
}
return res
}
func (ibcm IBCMapper) GetIngressSequence(ctx sdk.Context, srcChain string) int64 {
store := ctx.KVStore(ibcm.ibcKey)
bz := store.Get(IngressKey(srcChain))
if bz == nil {
2018-03-15 03:36:17 -07:00
zero, err := json.Marshal(int64(0)) //ibcm.cdc.MarshalBinary(int64(0))
2018-03-14 05:53:26 -07:00
if err != nil {
panic(err)
}
store.Set(IngressKey(srcChain), zero)
return 0
}
var res int64
2018-03-15 03:36:17 -07:00
if err := json.Unmarshal(bz, &res); /*ibcm.cdc.UnmarshalBinary(bz, &res)*/ err != nil {
2018-03-14 05:53:26 -07:00
panic(err)
2018-03-14 05:11:33 -07:00
}
2018-03-14 05:53:26 -07:00
return res
2018-03-14 05:11:33 -07:00
}
2018-03-14 05:53:26 -07:00
func (ibcm IBCMapper) SetIngressSequence(ctx sdk.Context, srcChain string, sequence int64) {
store := ctx.KVStore(ibcm.ibcKey)
2018-03-15 03:36:17 -07:00
bz, err := json.Marshal(sequence) // ibcm.cdc.MarshalBinary(sequence)
2018-03-14 05:53:26 -07:00
if err != nil {
panic(err)
}
store.Set(IngressKey(srcChain), bz)
2018-03-14 05:11:33 -07:00
}
2018-03-14 05:53:26 -07:00
func (ibcm IBCMapper) PushPacket(ctx sdk.Context, packet IBCPacket) {
store := ctx.KVStore(ibcm.ibcKey)
len := ibcm.getEgressLength(store, packet.DestChain)
2018-03-15 03:36:17 -07:00
packetbz, err := json.Marshal(packet) // ibcm.cdc.MarshalBinary(packet)
2018-03-14 05:53:26 -07:00
if err != nil {
panic(err)
}
store.Set(EgressKey(packet.DestChain, len), packetbz)
2018-03-15 03:36:17 -07:00
lenbz, err := json.Marshal(int64(len + 1)) // ibcm.cdc.MarshalBinary(int64(len + 1))
2018-03-14 05:53:26 -07:00
if err != nil {
panic(err)
}
store.Set(EgressLengthKey(packet.DestChain), lenbz)
2018-03-14 05:11:33 -07:00
}