-
Notifications
You must be signed in to change notification settings - Fork 531
/
chain.go
310 lines (256 loc) · 7.32 KB
/
chain.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package relayer
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/imdario/mergo"
"github.com/ignite/cli/v28/ignite/pkg/cosmosaccount"
"github.com/ignite/cli/v28/ignite/pkg/cosmosclient"
"github.com/ignite/cli/v28/ignite/pkg/cosmosfaucet"
"github.com/ignite/cli/v28/ignite/pkg/errors"
relayerconfig "github.com/ignite/cli/v28/ignite/pkg/relayer/config"
)
const (
TransferPort = "transfer"
TransferVersion = "ics20-1"
OrderingUnordered = "ORDER_UNORDERED"
OrderingOrdered = "ORDER_ORDERED"
)
var errEndpointExistsWithDifferentChainID = errors.New("rpc endpoint already exists with a different chain id")
// Chain represents a chain in relayer.
type Chain struct {
// ID is id of the chain.
ID string
// accountName is account used on the chain.
accountName string
// rpcAddress is the node address of tm.
rpcAddress string
// faucetAddress is the faucet address to get tokens for relayer accounts.
faucetAddress string
// gasPrice is the gas price used when sending transactions to the chain
gasPrice string
// gasLimit is the gas limit used when sending transactions to the chain
gasLimit int64
// addressPrefix is the address prefix of the chain.
addressPrefix string
// clientID is the client id of the chain for relayer connection.
clientID string
r Relayer
}
// Account represents an account in relayer.
type Account struct {
// Address of the account.
Address string `json:"address"`
}
// Option is used to configure Chain.
type Option func(*Chain)
// WithFaucet provides a faucet address for chain to get tokens from.
// when it isn't provided.
func WithFaucet(address string) Option {
return func(c *Chain) {
c.faucetAddress = address
}
}
// WithGasPrice gives the gas price to use to send ibc transactions to the chain.
func WithGasPrice(gasPrice string) Option {
return func(c *Chain) {
c.gasPrice = gasPrice
}
}
// WithGasLimit gives the gas limit to use to send ibc transactions to the chain.
func WithGasLimit(limit int64) Option {
return func(c *Chain) {
c.gasLimit = limit
}
}
// WithAddressPrefix configures the account key prefix used on the chain.
func WithAddressPrefix(addressPrefix string) Option {
return func(c *Chain) {
c.addressPrefix = addressPrefix
}
}
// WithClientID configures the chain client id.
func WithClientID(clientID string) Option {
return func(c *Chain) {
c.clientID = clientID
}
}
// NewChain creates a new chain on relayer or uses the existing matching chain.
func (r Relayer) NewChain(accountName, rpcAddress string, options ...Option) (
*Chain, cosmosaccount.Account, error,
) {
c := &Chain{
accountName: accountName,
rpcAddress: fixRPCAddress(rpcAddress),
r: r,
}
// apply user options.
for _, o := range options {
o(c)
}
account, err := r.ca.GetByName(accountName)
if err != nil {
return nil, cosmosaccount.Account{}, err
}
return c, account, nil
}
// TryRetrieve tries to receive some coins to the account and returns the total balance.
func (c *Chain) TryRetrieve(ctx context.Context) (sdk.Coins, error) {
acc, err := c.r.ca.GetByName(c.accountName)
if err != nil {
return nil, err
}
addr, err := acc.Address(c.addressPrefix)
if err != nil {
return nil, err
}
if err = cosmosfaucet.TryRetrieve(ctx, c.ID, c.rpcAddress, c.faucetAddress, addr); err != nil {
return nil, err
}
return c.r.balance(ctx, c.rpcAddress, c.accountName, c.addressPrefix)
}
func (c *Chain) Config() relayerconfig.Chain {
return relayerconfig.Chain{
ID: c.ID,
Account: c.accountName,
AddressPrefix: c.addressPrefix,
RPCAddress: c.rpcAddress,
GasPrice: c.gasPrice,
GasLimit: c.gasLimit,
ClientID: c.clientID,
}
}
// channelOptions represents options for configuring the IBC channel between two chains.
type channelOptions struct {
sourcePort string
sourceVersion string
targetPort string
targetVersion string
ordering string
}
// newChannelOptions returns default channel options.
func newChannelOptions() channelOptions {
return channelOptions{
sourcePort: TransferPort,
sourceVersion: TransferVersion,
targetPort: TransferPort,
targetVersion: TransferVersion,
ordering: OrderingUnordered,
}
}
// ChannelOption is used to configure relayer IBC connection.
type ChannelOption func(*channelOptions)
// SourcePort configures the source port of the new channel.
func SourcePort(port string) ChannelOption {
return func(c *channelOptions) {
c.sourcePort = port
}
}
// TargetPort configures the target port of the new channel.
func TargetPort(port string) ChannelOption {
return func(c *channelOptions) {
c.targetPort = port
}
}
// SourceVersion configures the source version of the new channel.
func SourceVersion(version string) ChannelOption {
return func(c *channelOptions) {
c.sourceVersion = version
}
}
// TargetVersion configures the target version of the new channel.
func TargetVersion(version string) ChannelOption {
return func(c *channelOptions) {
c.targetVersion = version
}
}
// Ordered sets the new channel as ordered.
func Ordered() ChannelOption {
return func(c *channelOptions) {
c.ordering = OrderingOrdered
}
}
// Connect connects dst chain to c chain and creates a path in between in offline mode.
// it returns the path id on success otherwise, returns with a non-nil error.
func (c *Chain) Connect(dst *Chain, options ...ChannelOption) (id string, err error) {
channelOptions := newChannelOptions()
for _, apply := range options {
apply(&channelOptions)
}
conf, err := relayerconfig.Get()
if err != nil {
return "", err
}
// determine a unique path name from chain ids with incremental numbers. e.g.:
// - src-dst
// - src-dst-2
pathID := PathID(c.ID, dst.ID)
var suffix string
i := 2
for {
guess := pathID + suffix
if _, err := conf.PathByID(guess); err != nil { // guess is unique.
pathID = guess
break
}
suffix = fmt.Sprintf("-%d", i)
i++
}
confPath := relayerconfig.Path{
ID: pathID,
Ordering: channelOptions.ordering,
Src: relayerconfig.PathEnd{
ChainID: c.ID,
PortID: channelOptions.sourcePort,
Version: channelOptions.sourceVersion,
},
Dst: relayerconfig.PathEnd{
ChainID: dst.ID,
PortID: channelOptions.targetPort,
Version: channelOptions.targetVersion,
},
}
conf.Paths = append(conf.Paths, confPath)
if err := relayerconfig.Save(conf); err != nil {
return "", err
}
return pathID, nil
}
// EnsureChainSetup sets up the new or existing chain.
func (c *Chain) EnsureChainSetup(ctx context.Context) error {
client, err := cosmosclient.New(ctx, cosmosclient.WithNodeAddress(c.rpcAddress))
if err != nil {
return err
}
status, err := client.RPC.Status(ctx)
if err != nil {
return err
}
c.ID = status.NodeInfo.Network
confChain := c.Config()
conf, err := relayerconfig.Get()
if err != nil {
return err
}
var found bool
for i, chain := range conf.Chains {
if chain.ID == c.ID {
if chain.RPCAddress != c.rpcAddress {
return errEndpointExistsWithDifferentChainID
}
if err := mergo.Merge(&conf.Chains[i], confChain, mergo.WithOverride); err != nil {
return err
}
found = true
break
}
}
if !found {
conf.Chains = append(conf.Chains, confChain)
}
return relayerconfig.Save(conf)
}
// PathID creates path name from chain ids.
func PathID(srcChainID, dstChainID string) string {
return fmt.Sprintf("%s-%s", srcChainID, dstChainID)
}