This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
273 lines (227 loc) · 7.58 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/avast/retry-go/v4"
sdktypes "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/gogoproto/proto"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
chantypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
tendermint "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
"github.com/ghodss/yaml"
)
const (
configPath = "./config/config.yaml"
targetChainID = "osmosis-1"
maxWorkers = 1000 // Maximum number of goroutines that will run concurrently while querying escrow information.
)
var (
// targetChannels should be an empty slice if you want to run the escrow checker against every escrow account.
// Otherwise, add the channel-ids associated with escrow accounts you want to target.
targetChannels = []string{}
retries = uint(7)
retryAttempts = retry.Attempts(retries)
retryDelay = retry.Delay(time.Millisecond * 1000)
retryError = retry.LastErrorOnly(true)
)
type Info struct {
Channel *chantypes.IdentifiedChannel
EscrowAddress string
Balances sdktypes.Coins
CounterpartyChainID string
}
func main() {
cfg, err := readConfig(configPath)
if err != nil {
panic(err)
}
clients, err := clientsFromConfig(cfg)
if err != nil {
panic(err)
}
c, err := clients.clientByChainID(targetChainID)
if err != nil {
panic(err)
}
ctx := context.Background()
fmt.Println("Querying channels...")
var channels []*chantypes.IdentifiedChannel
if err := retry.Do(func() error {
channels, err = queryChannels(ctx, c)
return err
}, retry.Context(ctx), retryAttempts, retryDelay, retryError, retry.OnRetry(func(n uint, err error) {
fmt.Printf("Failed to query channels, retrying (%d/%d): %s \n", n+1, retries, err.Error())
})); err != nil {
panic(err)
}
fmt.Printf("Number of channels: %d \n", len(channels))
// For every channel query the associated escrow account address, the escrow account balances,
// and the channel's associated client state in order to identify the counterparty chain.
var (
sem = make(chan struct{}, maxWorkers)
wg = sync.WaitGroup{}
mu = sync.Mutex{}
infos = make([]*Info, 0)
)
fmt.Println("Querying escrow account information for each channel...")
for i, channel := range channels {
channel := channel
i := i
wg.Add(1)
sem <- struct{}{}
fmt.Printf("Starting worker number %d for channel %s \n", i+1, channel.ChannelId)
go func() {
defer func() {
wg.Done()
<-sem
}()
var (
addr string
bals *banktypes.QueryAllBalancesResponse
res *chantypes.QueryChannelClientStateResponse
)
if err := retry.Do(func() error {
addr, err = c.QueryEscrowAddress(ctx, channel.PortId, channel.ChannelId)
return err
}, retry.Context(ctx), retryAttempts, retryDelay, retryError, retry.OnRetry(func(n uint, err error) {
fmt.Printf("Failed to query escrow address for %s, retrying (%d/%d): %s \n", channel.ChannelId, n+1, retries, err.Error())
})); err != nil {
panic(err)
}
if err := retry.Do(func() error {
bals, err = c.QueryBalances(ctx, addr)
return err
}, retry.Context(ctx), retryAttempts, retryDelay, retryError, retry.OnRetry(func(n uint, err error) {
fmt.Printf("Failed to query escrow balance for %s, retrying (%d/%d): %s \n", addr, n+1, retries, err.Error())
})); err != nil {
panic(err)
}
if err = retry.Do(func() error {
res, err = c.QueryChannelClientState(channel.PortId, channel.ChannelId)
return err
}, retry.Context(ctx), retryAttempts, retryDelay, retryError, retry.OnRetry(func(n uint, err error) {
fmt.Printf("Failed to query channel client state for %s, retrying (%d/%d): %s \n", channel.ChannelId, n+1, retries, err.Error())
})); err != nil {
panic(err)
}
cs := &tendermint.ClientState{}
err = proto.Unmarshal(res.IdentifiedClientState.ClientState.Value, cs)
if err != nil {
panic(err)
}
mu.Lock()
infos = append(infos, &Info{
Channel: channel,
EscrowAddress: addr,
Balances: bals.Balances,
CounterpartyChainID: cs.ChainId,
})
mu.Unlock()
}()
}
wg.Wait()
fmt.Println("Finished querying escrow account information.")
// For each token balance in the escrow accounts, query the IBC denom trace from the hash,
// then compose the denom on the counterparty chain and query the tokens total supply.
// Assert that the balance in the escrow account is equal to the total supply on the counterparty.
fmt.Println("Querying counterparty total supply for each token found in an escrow account...")
for _, info := range infos {
client, err := clients.clientByChainID(info.CounterpartyChainID)
if err != nil {
fmt.Println(err)
continue
}
for _, bal := range info.Balances {
var (
hash string
denom *transfertypes.DenomTrace
amount sdktypes.Coin
)
if strings.Contains(bal.Denom, "ibc/") {
parts := strings.Split(bal.Denom, "/")
hash = parts[1]
} else {
// Found a native denom or non-IBC denom.
continue
}
if err := retry.Do(func() error {
denom, err = c.QueryDenomTrace(ctx, hash)
return err
}, retry.Context(ctx), retryAttempts, retryDelay, retryError, retry.OnRetry(func(n uint, err error) {
fmt.Printf("Failed to query denom trace for %s, retrying (%d/%d): %s \n", hash, n+1, retries, err.Error())
})); err != nil {
panic(err)
}
path := fmt.Sprintf("%s/%s/%s", info.Channel.Counterparty.PortId, info.Channel.Counterparty.ChannelId, denom.Path)
counterpartyDenom := transfertypes.ParseDenomTrace(fmt.Sprintf("%s/%s", path, denom.BaseDenom))
ibcDenom := counterpartyDenom.IBCDenom()
if err := retry.Do(func() error {
amount, err = client.QueryBankTotalSupply(ctx, ibcDenom)
return err
}, retry.Context(ctx), retryAttempts, retryDelay, retryError, retry.OnRetry(func(n uint, err error) {
fmt.Printf("Failed to query total supply of %s, retrying (%d/%d): %s \n", ibcDenom, n+1, retries, err.Error())
})); err != nil {
panic(err)
}
if !bal.Amount.Equal(amount.Amount) {
fmt.Println("--------------------------------------------")
fmt.Println("Discrepancy found!")
fmt.Printf("Counterparty Chain ID: %s \n", info.CounterpartyChainID)
fmt.Printf("Escrow Account Address: %s \n", info.EscrowAddress)
fmt.Printf("Asset Base Denom: %s \n", denom.BaseDenom)
fmt.Printf("Asset IBC Denom: %s \n", bal.Denom)
fmt.Printf("Escrow Balance: %s \n", bal.Amount)
fmt.Printf("Counterparty Total Supply: %s \n", amount)
}
}
}
}
func readConfig(path string) (*Config, error) {
cfgFile, err := os.ReadFile(path)
if err != nil {
return nil, err
}
cfg := &Config{}
err = yaml.Unmarshal(cfgFile, cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
func clientsFromConfig(cfg *Config) (Clients, error) {
clients := make([]*Client, len(cfg.Chains))
for i, c := range cfg.Chains {
t, err := time.ParseDuration(c.Timeout)
if err != nil {
return nil, err
}
clients[i] = NewClient(c.ChainID, c.RPCAddress, c.AccountPrefix, t)
}
return clients, nil
}
func queryChannels(ctx context.Context, c *Client) ([]*chantypes.IdentifiedChannel, error) {
var (
channels []*chantypes.IdentifiedChannel
err error
)
if len(targetChannels) == 0 {
channels, err = c.QueryChannels(ctx)
if err != nil {
return nil, err
}
} else {
for _, id := range targetChannels {
channel, err := c.QueryChannel(ctx, id)
if err != nil {
return nil, err
}
channels = append(channels, channel)
}
}
return channels, nil
}