-
Notifications
You must be signed in to change notification settings - Fork 580
/
keys.go
159 lines (125 loc) · 5.64 KB
/
keys.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package types
import (
"fmt"
"strconv"
"strings"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
)
const (
// ModuleName defines the 29-fee name
ModuleName = "feeibc"
// StoreKey is the store key string for IBC fee module
StoreKey = ModuleName
// RouterKey is the message route for IBC fee module
RouterKey = ModuleName
// QuerierRoute is the querier route for IBC fee module
QuerierRoute = ModuleName
Version = "ics29-1"
// FeeEnabledPrefix is the key prefix for storing fee enabled flag
FeeEnabledKeyPrefix = "feeEnabled"
// PayeeKeyPrefix is the key prefix for the fee payee address stored in state
PayeeKeyPrefix = "payee"
// CounterpartyPayeeKeyPrefix is the key prefix for the counterparty payee address mapping
CounterpartyPayeeKeyPrefix = "counterpartyPayee"
// FeesInEscrowPrefix is the key prefix for fee in escrow mapping
FeesInEscrowPrefix = "feesInEscrow"
// ForwardRelayerPrefix is the key prefix for forward relayer addresses stored in state for async acknowledgements
ForwardRelayerPrefix = "forwardRelayer"
)
// KeyLocked returns the key used to lock and unlock the fee module. This key is used
// in the presence of a severe bug.
func KeyLocked() []byte {
return []byte("locked")
}
// KeyFeeEnabled returns the key that stores a flag to determine if fee logic should
// be enabled for the given port and channel identifiers.
func KeyFeeEnabled(portID, channelID string) []byte {
return []byte(fmt.Sprintf("%s/%s/%s", FeeEnabledKeyPrefix, portID, channelID))
}
// ParseKeyFeeEnabled parses the key used to indicate if the fee logic should be
// enabled for the given port and channel identifiers.
func ParseKeyFeeEnabled(key string) (portID, channelID string, err error) {
keySplit := strings.Split(key, "/")
if len(keySplit) != 3 {
return "", "", sdkerrors.Wrapf(
sdkerrors.ErrLogic, "key provided is incorrect: the key split has incorrect length, expected %d, got %d", 3, len(keySplit),
)
}
if keySplit[0] != FeeEnabledKeyPrefix {
return "", "", sdkerrors.Wrapf(sdkerrors.ErrLogic, "key prefix is incorrect: expected %s, got %s", FeeEnabledKeyPrefix, keySplit[0])
}
portID = keySplit[1]
channelID = keySplit[2]
return portID, channelID, nil
}
// KeyPayee returns the key for relayer address -> payee address mapping
func KeyPayee(relayerAddr, channelID string) []byte {
return []byte(fmt.Sprintf("%s/%s/%s", PayeeKeyPrefix, relayerAddr, channelID))
}
// ParseKeyPayeeAddress returns the registered relayer addresss and channelID used to the store the fee payee address
func ParseKeyPayeeAddress(key string) (relayerAddr, channelID string, err error) {
keySplit := strings.Split(key, "/")
if len(keySplit) != 3 {
return "", "", sdkerrors.Wrapf(
sdkerrors.ErrLogic, "key provided is incorrect: the key split has incorrect length, expected %d, got %d", 3, len(keySplit),
)
}
return keySplit[1], keySplit[2], nil
}
// KeyCounterpartyPayee returns the key for relayer address -> counterparty payee address mapping
func KeyCounterpartyPayee(address, channelID string) []byte {
return []byte(fmt.Sprintf("%s/%s/%s", CounterpartyPayeeKeyPrefix, address, channelID))
}
// ParseKeyCounterpartyPayee returns the registered relayer address and channelID used to store the counterparty payee address
func ParseKeyCounterpartyPayee(key string) (address string, channelID string, error error) {
keySplit := strings.Split(key, "/")
if len(keySplit) != 3 {
return "", "", sdkerrors.Wrapf(
sdkerrors.ErrLogic, "key provided is incorrect: the key split has incorrect length, expected %d, got %d", 3, len(keySplit),
)
}
return keySplit[1], keySplit[2], nil
}
// KeyRelayerAddressForAsyncAck returns the key for packetID -> forwardAddress mapping
func KeyRelayerAddressForAsyncAck(packetID channeltypes.PacketId) []byte {
return []byte(fmt.Sprintf("%s/%s/%s/%d", ForwardRelayerPrefix, packetID.PortId, packetID.ChannelId, packetID.Sequence))
}
// ParseKeyRelayerAddressForAsyncAck parses the key used to store the forward relayer address and returns the packetID
func ParseKeyRelayerAddressForAsyncAck(key string) (channeltypes.PacketId, error) {
keySplit := strings.Split(key, "/")
if len(keySplit) != 4 {
return channeltypes.PacketId{}, sdkerrors.Wrapf(
sdkerrors.ErrLogic, "key provided is incorrect: the key split has incorrect length, expected %d, got %d", 4, len(keySplit),
)
}
seq, err := strconv.ParseUint(keySplit[3], 10, 64)
if err != nil {
return channeltypes.PacketId{}, err
}
packetID := channeltypes.NewPacketID(keySplit[1], keySplit[2], seq)
return packetID, nil
}
// KeyFeesInEscrow returns the key for escrowed fees
func KeyFeesInEscrow(packetID channeltypes.PacketId) []byte {
return []byte(fmt.Sprintf("%s/%d", KeyFeesInEscrowChannelPrefix(packetID.PortId, packetID.ChannelId), packetID.Sequence))
}
// ParseKeyFeesInEscrow parses the key used to store fees in escrow and returns the packet id
func ParseKeyFeesInEscrow(key string) (channeltypes.PacketId, error) {
keySplit := strings.Split(key, "/")
if len(keySplit) != 4 {
return channeltypes.PacketId{}, sdkerrors.Wrapf(
sdkerrors.ErrLogic, "key provided is incorrect: the key split has incorrect length, expected %d, got %d", 4, len(keySplit),
)
}
seq, err := strconv.ParseUint(keySplit[3], 10, 64)
if err != nil {
return channeltypes.PacketId{}, err
}
packetID := channeltypes.NewPacketID(keySplit[1], keySplit[2], seq)
return packetID, nil
}
// KeyFeesInEscrowChannelPrefix returns the key prefix for escrowed fees on the given channel
func KeyFeesInEscrowChannelPrefix(portID, channelID string) []byte {
return []byte(fmt.Sprintf("%s/%s/%s", FeesInEscrowPrefix, portID, channelID))
}