2018-03-14 05:11:33 -07:00
|
|
|
package ibc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-03-15 05:49:18 -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-15 06:54:44 -07:00
|
|
|
cdc *wire.Codec
|
2018-03-14 05:11:33 -07:00
|
|
|
}
|
|
|
|
|
2018-03-15 09:24:31 -07:00
|
|
|
func NewMapper(ibcKey sdk.StoreKey) IBCMapper {
|
2018-03-15 05:49:18 -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 05:49:18 -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 05:49:18 -07:00
|
|
|
zero, err := 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 05:49:18 -07:00
|
|
|
if err := 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 05:49:18 -07:00
|
|
|
zero, err := 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 05:49:18 -07:00
|
|
|
if err := 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 05:49:18 -07:00
|
|
|
bz, err := 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 05:49:18 -07:00
|
|
|
packetbz, err := 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 05:49:18 -07:00
|
|
|
lenbz, err := 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
|
|
|
}
|