forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
130 lines (110 loc) · 3.57 KB
/
mapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package ibc
import (
"fmt"
codec "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// IBC Mapper
type Mapper struct {
key sdk.StoreKey
cdc *codec.Codec
codespace sdk.CodespaceType
}
// XXX: The Mapper should not take a CoinKeeper. Rather have the CoinKeeper
// take an Mapper.
func NewMapper(cdc *codec.Codec, key sdk.StoreKey, codespace sdk.CodespaceType) Mapper {
// XXX: How are these codecs supposed to work?
return Mapper{
key: key,
cdc: cdc,
codespace: codespace,
}
}
// XXX: This is not the public API. This will change in MVP2 and will henceforth
// only be invoked from another module directly and not through a user
// transaction.
// TODO: Handle invalid IBC packets and return errors.
func (ibcm Mapper) PostIBCPacket(ctx sdk.Context, packet IBCPacket) sdk.Error {
// write everything into the state
store := ctx.KVStore(ibcm.key)
index := ibcm.getEgressLength(store, packet.DestChain)
bz, err := ibcm.cdc.MarshalBinaryLengthPrefixed(packet)
if err != nil {
panic(err)
}
store.Set(EgressKey(packet.DestChain, index), bz)
bz, err = ibcm.cdc.MarshalBinaryLengthPrefixed(index + 1)
if err != nil {
panic(err)
}
store.Set(EgressLengthKey(packet.DestChain), bz)
return nil
}
// XXX: In the future every module is able to register it's own handler for
// handling it's own IBC packets. The "ibc" handler will only route the packets
// to the appropriate callbacks.
// XXX: For now this handles all interactions with the CoinKeeper.
// XXX: This needs to do some authentication checking.
func (ibcm Mapper) ReceiveIBCPacket(ctx sdk.Context, packet IBCPacket) sdk.Error {
return nil
}
// --------------------------
// Functions for accessing the underlying KVStore.
func marshalBinaryPanic(cdc *codec.Codec, value interface{}) []byte {
res, err := cdc.MarshalBinaryLengthPrefixed(value)
if err != nil {
panic(err)
}
return res
}
func unmarshalBinaryPanic(cdc *codec.Codec, bz []byte, ptr interface{}) {
err := cdc.UnmarshalBinaryLengthPrefixed(bz, ptr)
if err != nil {
panic(err)
}
}
// TODO add description
func (ibcm Mapper) GetIngressSequence(ctx sdk.Context, srcChain string) uint64 {
store := ctx.KVStore(ibcm.key)
key := IngressSequenceKey(srcChain)
bz := store.Get(key)
if bz == nil {
zero := marshalBinaryPanic(ibcm.cdc, int64(0))
store.Set(key, zero)
return 0
}
var res uint64
unmarshalBinaryPanic(ibcm.cdc, bz, &res)
return res
}
// TODO add description
func (ibcm Mapper) SetIngressSequence(ctx sdk.Context, srcChain string, sequence uint64) {
store := ctx.KVStore(ibcm.key)
key := IngressSequenceKey(srcChain)
bz := marshalBinaryPanic(ibcm.cdc, sequence)
store.Set(key, bz)
}
// Retrieves the index of the currently stored outgoing IBC packets.
func (ibcm Mapper) getEgressLength(store sdk.KVStore, destChain string) uint64 {
bz := store.Get(EgressLengthKey(destChain))
if bz == nil {
zero := marshalBinaryPanic(ibcm.cdc, int64(0))
store.Set(EgressLengthKey(destChain), zero)
return 0
}
var res uint64
unmarshalBinaryPanic(ibcm.cdc, bz, &res)
return res
}
// Stores an outgoing IBC packet under "egress/chain_id/index".
func EgressKey(destChain string, index uint64) []byte {
return []byte(fmt.Sprintf("egress/%s/%d", destChain, index))
}
// Stores the number of outgoing IBC packets under "egress/index".
func EgressLengthKey(destChain string) []byte {
return []byte(fmt.Sprintf("egress/%s", destChain))
}
// Stores the sequence number of incoming IBC packet under "ingress/index".
func IngressSequenceKey(srcChain string) []byte {
return []byte(fmt.Sprintf("ingress/%s", srcChain))
}