forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.go
210 lines (166 loc) · 5.74 KB
/
relay.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package cli
import (
"os"
"time"
"github.com/cosmos/cosmos-sdk/client/utils"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/cosmos/cosmos-sdk/x/ibc"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"
)
// flags
const (
FlagFromChainID = "from-chain-id"
FlagFromChainNode = "from-chain-node"
FlagToChainID = "to-chain-id"
FlagToChainNode = "to-chain-node"
)
type relayCommander struct {
cdc *codec.Codec
address sdk.AccAddress
decoder auth.AccountDecoder
mainStore string
ibcStore string
accStore string
logger log.Logger
}
// IBCRelayCmd implements the IBC relay command.
func IBCRelayCmd(cdc *codec.Codec) *cobra.Command {
cmdr := relayCommander{
cdc: cdc,
decoder: context.GetAccountDecoder(cdc),
ibcStore: "ibc",
mainStore: bam.MainStoreKey,
accStore: auth.StoreKey,
logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)),
}
cmd := &cobra.Command{
Use: "relay",
Run: cmdr.runIBCRelay,
}
cmd.Flags().String(FlagFromChainID, "", "Chain ID for ibc node to check outgoing packets")
cmd.Flags().String(FlagFromChainNode, "tcp://localhost:26657", "<host>:<port> to tendermint rpc interface for this chain")
cmd.Flags().String(FlagToChainID, "", "Chain ID for ibc node to broadcast incoming packets")
cmd.Flags().String(FlagToChainNode, "tcp://localhost:36657", "<host>:<port> to tendermint rpc interface for this chain")
cmd.MarkFlagRequired(FlagFromChainID)
cmd.MarkFlagRequired(FlagFromChainNode)
cmd.MarkFlagRequired(FlagToChainID)
cmd.MarkFlagRequired(FlagToChainNode)
viper.BindPFlag(FlagFromChainID, cmd.Flags().Lookup(FlagFromChainID))
viper.BindPFlag(FlagFromChainNode, cmd.Flags().Lookup(FlagFromChainNode))
viper.BindPFlag(FlagToChainID, cmd.Flags().Lookup(FlagToChainID))
viper.BindPFlag(FlagToChainNode, cmd.Flags().Lookup(FlagToChainNode))
return cmd
}
// nolint: unparam
func (c relayCommander) runIBCRelay(cmd *cobra.Command, args []string) {
fromChainID := viper.GetString(FlagFromChainID)
fromChainNode := viper.GetString(FlagFromChainNode)
toChainID := viper.GetString(FlagToChainID)
toChainNode := viper.GetString(FlagToChainNode)
address := context.NewCLIContext().GetFromAddress()
c.address = address
c.loop(fromChainID, fromChainNode, toChainID, toChainNode)
}
// This is nolinted as someone is in the process of refactoring this to remove the goto
func (c relayCommander) loop(fromChainID, fromChainNode, toChainID, toChainNode string) {
cliCtx := context.NewCLIContext()
name := cliCtx.GetFromName()
passphrase, err := keys.ReadPassphraseFromStdin(name)
if err != nil {
panic(err)
}
ingressKey := ibc.IngressSequenceKey(fromChainID)
lengthKey := ibc.EgressLengthKey(toChainID)
OUTER:
for {
time.Sleep(5 * time.Second)
processedbz, err := query(toChainNode, ingressKey, c.ibcStore)
if err != nil {
panic(err)
}
var processed uint64
if processedbz == nil {
processed = 0
} else if err = c.cdc.UnmarshalBinaryLengthPrefixed(processedbz, &processed); err != nil {
panic(err)
}
egressLengthbz, err := query(fromChainNode, lengthKey, c.ibcStore)
if err != nil {
c.logger.Error("error querying outgoing packet list length", "err", err)
continue OUTER // TODO replace with continue (I think it should just to the correct place where OUTER is now)
}
var egressLength uint64
if egressLengthbz == nil {
egressLength = 0
} else if err = c.cdc.UnmarshalBinaryLengthPrefixed(egressLengthbz, &egressLength); err != nil {
panic(err)
}
if egressLength > processed {
c.logger.Info("Detected IBC packet", "number", egressLength-1)
}
seq := c.getSequence(toChainNode)
for i := processed; i < egressLength; i++ {
egressbz, err := query(fromChainNode, ibc.EgressKey(toChainID, i), c.ibcStore)
if err != nil {
c.logger.Error("error querying egress packet", "err", err)
continue OUTER // TODO replace to break, will break first loop then send back to the beginning (aka OUTER)
}
err = c.broadcastTx(toChainNode, c.refine(egressbz, i, seq, passphrase))
seq++
if err != nil {
c.logger.Error("error broadcasting ingress packet", "err", err)
continue OUTER // TODO replace to break, will break first loop then send back to the beginning (aka OUTER)
}
c.logger.Info("Relayed IBC packet", "number", i)
}
}
}
func query(node string, key []byte, storeName string) (res []byte, err error) {
return context.NewCLIContext().WithNodeURI(node).QueryStore(key, storeName)
}
// nolint: unparam
func (c relayCommander) broadcastTx(node string, tx []byte) error {
_, err := context.NewCLIContext().WithNodeURI(node).BroadcastTx(tx)
return err
}
func (c relayCommander) getSequence(node string) uint64 {
res, err := query(node, auth.AddressStoreKey(c.address), c.accStore)
if err != nil {
panic(err)
}
if nil != res {
account, err := c.decoder(res)
if err != nil {
panic(err)
}
return account.GetSequence()
}
return 0
}
func (c relayCommander) refine(bz []byte, ibcSeq, accSeq uint64, passphrase string) []byte {
var packet ibc.IBCPacket
if err := c.cdc.UnmarshalBinaryLengthPrefixed(bz, &packet); err != nil {
panic(err)
}
msg := ibc.MsgIBCReceive{
IBCPacket: packet,
Relayer: c.address,
Sequence: ibcSeq,
}
txBldr := authtxb.NewTxBuilderFromCLI().WithSequence(accSeq).WithTxEncoder(utils.GetTxEncoder(c.cdc))
cliCtx := context.NewCLIContext()
name := cliCtx.GetFromName()
res, err := txBldr.BuildAndSign(name, passphrase, []sdk.Msg{msg})
if err != nil {
panic(err)
}
return res
}