|
| 1 | +package cosmos |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "math/big" |
| 8 | + "strings" |
| 9 | + |
| 10 | + codectypes "github.com/cosmos/cosmos-sdk/codec/types" |
| 11 | + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" |
| 12 | + "github.com/docker/docker/client" |
| 13 | + "github.com/icon-project/ibc-integration/test/internal/dockerutil" |
| 14 | + "github.com/icon-project/ibc-integration/test/testsuite/testconfig" |
| 15 | + "github.com/strangelove-ventures/interchaintest/v7/ibc" |
| 16 | + "go.uber.org/zap" |
| 17 | +) |
| 18 | + |
| 19 | +// CosmosRemoteChain is a local docker testnet for a Cosmos SDK chain. |
| 20 | +// Implements the ibc.Chain interface. |
| 21 | +type CosmosRemoteChain struct { |
| 22 | + testName string |
| 23 | + cfg ibc.ChainConfig |
| 24 | + log *zap.Logger |
| 25 | + Client *client.Client |
| 26 | + Network string |
| 27 | + testConfig *testconfig.Chain |
| 28 | +} |
| 29 | + |
| 30 | +func NewCosmosHeighlinerChainConfig(name string, |
| 31 | + binary string, |
| 32 | + bech32Prefix string, |
| 33 | + denom string, |
| 34 | + gasPrices string, |
| 35 | + gasAdjustment float64, |
| 36 | + trustingPeriod string, |
| 37 | + noHostMount bool) ibc.ChainConfig { |
| 38 | + return ibc.ChainConfig{ |
| 39 | + Type: "cosmos", |
| 40 | + Name: name, |
| 41 | + Bech32Prefix: bech32Prefix, |
| 42 | + Denom: denom, |
| 43 | + GasPrices: gasPrices, |
| 44 | + GasAdjustment: gasAdjustment, |
| 45 | + TrustingPeriod: trustingPeriod, |
| 46 | + NoHostMount: noHostMount, |
| 47 | + Images: []ibc.DockerImage{ |
| 48 | + { |
| 49 | + Repository: fmt.Sprintf("ghcr.io/strangelove-ventures/heighliner/%s", name), |
| 50 | + UidGid: dockerutil.GetHeighlinerUserString(), |
| 51 | + }, |
| 52 | + }, |
| 53 | + Bin: binary, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func NewCosmosRemoteChain(testName string, chainConfig ibc.ChainConfig, client *client.Client, network string, log *zap.Logger, testConfig *testconfig.Chain) *CosmosRemoteChain { |
| 58 | + if chainConfig.EncodingConfig == nil { |
| 59 | + cfg := DefaultEncoding() |
| 60 | + chainConfig.EncodingConfig = &cfg |
| 61 | + } |
| 62 | + |
| 63 | + registry := codectypes.NewInterfaceRegistry() |
| 64 | + cryptocodec.RegisterInterfaces(registry) |
| 65 | + return &CosmosRemoteChain{ |
| 66 | + testName: testName, |
| 67 | + cfg: chainConfig, |
| 68 | + log: log, |
| 69 | + Client: client, |
| 70 | + Network: network, |
| 71 | + testConfig: testConfig, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// // Implements Chain interface |
| 76 | +func (c *CosmosRemoteChain) Config() ibc.ChainConfig { |
| 77 | + return c.cfg |
| 78 | +} |
| 79 | + |
| 80 | +// // Implements Chain interface |
| 81 | +func (c *CosmosRemoteChain) Initialize(ctx context.Context, testName string, cli *client.Client, networkID string) error { |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// // Implements Chain interface |
| 86 | +func (c *CosmosRemoteChain) GetRPCAddress() string { |
| 87 | + return c.testConfig.RPCUri |
| 88 | +} |
| 89 | + |
| 90 | +// // Implements Chain interface |
| 91 | +func (c *CosmosRemoteChain) GetGRPCAddress() string { |
| 92 | + return c.testConfig.RPCUri |
| 93 | +} |
| 94 | + |
| 95 | +// // GetHostRPCAddress returns the address of the RPC server accessible by the host. |
| 96 | +// // This will not return a valid address until the chain has been started. |
| 97 | +func (c *CosmosRemoteChain) GetHostRPCAddress() string { |
| 98 | + return c.testConfig.RPCUri |
| 99 | +} |
| 100 | + |
| 101 | +// // GetHostGRPCAddress returns the address of the gRPC server accessible by the host. |
| 102 | +// // This will not return a valid address until the chain has been started. |
| 103 | +func (c *CosmosRemoteChain) GetHostGRPCAddress() string { |
| 104 | + return c.testConfig.RPCUri |
| 105 | +} |
| 106 | + |
| 107 | +// // HomeDir implements ibc.Chain. |
| 108 | +func (c *CosmosRemoteChain) HomeDir() string { |
| 109 | + panic("not implemented") |
| 110 | +} |
| 111 | + |
| 112 | +// // Implements Chain interface |
| 113 | +func (c *CosmosRemoteChain) CreateKey(ctx context.Context, keyName string) error { |
| 114 | + panic("not implemented") |
| 115 | +} |
| 116 | + |
| 117 | +// Implements Chain interface |
| 118 | +func (c *CosmosRemoteChain) RecoverKey(ctx context.Context, keyName, mnemonic string) error { |
| 119 | + panic("not implemented") |
| 120 | +} |
| 121 | + |
| 122 | +// // Implements Chain interface |
| 123 | +func (c *CosmosRemoteChain) GetAddress(ctx context.Context, keyName string) ([]byte, error) { |
| 124 | + panic("not implemented") |
| 125 | +} |
| 126 | + |
| 127 | +// // BuildWallet will return a Cosmos wallet |
| 128 | +// // If mnemonic != "", it will restore using that mnemonic |
| 129 | +// // If mnemonic == "", it will create a new key |
| 130 | +func (c *CosmosRemoteChain) BuildWallet(ctx context.Context, keyName string, mnemonic string) (ibc.Wallet, error) { |
| 131 | + panic("not implemented") |
| 132 | +} |
| 133 | + |
| 134 | +// // BuildRelayerWallet will return a Cosmos wallet populated with the mnemonic so that the wallet can |
| 135 | +// // be restored in the relayer node using the mnemonic. After it is built, that address is included in |
| 136 | +// // genesis with some funds. |
| 137 | +func (c *CosmosRemoteChain) BuildRelayerWallet(ctx context.Context, keyName string) (ibc.Wallet, error) { |
| 138 | + panic("not implemented") |
| 139 | +} |
| 140 | + |
| 141 | +// // Implements Chain interface |
| 142 | +func (c *CosmosRemoteChain) SendFunds(ctx context.Context, keyName string, amount ibc.WalletAmount) error { |
| 143 | + panic("not implemented") |
| 144 | +} |
| 145 | + |
| 146 | +// // Implements Chain interface |
| 147 | +func (c *CosmosRemoteChain) SendIBCTransfer( |
| 148 | + ctx context.Context, |
| 149 | + channelID string, |
| 150 | + keyName string, |
| 151 | + amount ibc.WalletAmount, |
| 152 | + options ibc.TransferOptions, |
| 153 | +) (tx ibc.Tx, _ error) { |
| 154 | + panic("not implemented") |
| 155 | +} |
| 156 | + |
| 157 | +// // StoreContract takes a file path to smart contract and stores it on-chain. Returns the contracts code id. |
| 158 | +func (c *CosmosRemoteChain) StoreContract(ctx context.Context, keyName string, fileName string) (string, error) { |
| 159 | + panic("not implemented") |
| 160 | +} |
| 161 | + |
| 162 | +// // InstantiateContract takes a code id for a smart contract and initialization message and returns the instantiated contract address. |
| 163 | +func (c *CosmosRemoteChain) InstantiateContract(ctx context.Context, keyName string, codeID string, initMessage string, needsNoAdminFlag bool, extraExecTxArgs ...string) (string, error) { |
| 164 | + panic("not implemented") |
| 165 | +} |
| 166 | + |
| 167 | +// // QueryContract performs a smart query, taking in a query struct and returning a error with the response struct populated. |
| 168 | +func (c *CosmosRemoteChain) QueryContract(ctx context.Context, contractAddress string, query any, response any) error { |
| 169 | + panic("not implemented") |
| 170 | +} |
| 171 | + |
| 172 | +// // ExportState exports the chain state at specific height. |
| 173 | +// // Implements Chain interface |
| 174 | +func (c *CosmosRemoteChain) ExportState(ctx context.Context, height int64) (string, error) { |
| 175 | + panic("not implemented") |
| 176 | +} |
| 177 | + |
| 178 | +func (c *CosmosRemoteChain) GetGasFeesInNativeDenom(gasPaid int64) int64 { |
| 179 | + panic("not implemented") |
| 180 | +} |
| 181 | + |
| 182 | +func (c *CosmosRemoteChain) UpgradeVersion(ctx context.Context, cli *client.Client, containerRepo, version string) { |
| 183 | + panic("not implemented") |
| 184 | +} |
| 185 | + |
| 186 | +// // Bootstraps the chain and starts it from genesis |
| 187 | +func (c *CosmosRemoteChain) Start(testName string, ctx context.Context, additionalGenesisWallets ...ibc.WalletAmount) error { |
| 188 | + panic("not implemented") |
| 189 | +} |
| 190 | + |
| 191 | +// Height implements ibc.Chain |
| 192 | +func (c *CosmosRemoteChain) Height(ctx context.Context) (uint64, error) { |
| 193 | + panic("not implemented") |
| 194 | +} |
| 195 | + |
| 196 | +// // Acknowledgements implements ibc.Chain, returning all acknowledgments in block at height |
| 197 | +func (c *CosmosRemoteChain) Acknowledgements(ctx context.Context, height uint64) ([]ibc.PacketAcknowledgement, error) { |
| 198 | + panic("not implemented") |
| 199 | +} |
| 200 | + |
| 201 | +// // Timeouts implements ibc.Chain, returning all timeouts in block at height |
| 202 | +func (c *CosmosRemoteChain) Timeouts(ctx context.Context, height uint64) ([]ibc.PacketTimeout, error) { |
| 203 | + panic("not implemented") |
| 204 | +} |
| 205 | + |
| 206 | +// // Exec implements ibc.Chain. |
| 207 | +func (c *CosmosRemoteChain) Exec(ctx context.Context, cmd []string, env []string) (stdout, stderr []byte, err error) { |
| 208 | + cmd = append([]string{"centaurid"}, cmd...) |
| 209 | + job := dockerutil.NewImage(c.log, c.Client, c.Network, c.testName, c.cfg.Images[0].Repository, c.cfg.Images[0].Version) |
| 210 | + opts := dockerutil.ContainerOptions{ |
| 211 | + Binds: []string{ |
| 212 | + c.testConfig.ContractsPath + ":/contracts", |
| 213 | + c.testConfig.ConfigPath + ":/centauri/.banksy", |
| 214 | + }, |
| 215 | + Env: env, |
| 216 | + } |
| 217 | + |
| 218 | + res := job.Run(ctx, cmd, opts) |
| 219 | + return res.Stdout, res.Stderr, res.Err |
| 220 | +} |
| 221 | + |
| 222 | +func (c *CosmosRemoteChain) extractAmount(text string) (*big.Int, error) { |
| 223 | + entries := strings.Split(text, "\n") |
| 224 | + for _, entry := range entries { |
| 225 | + values := strings.Split(entry, ":") |
| 226 | + key := values[0] |
| 227 | + value := values[1] |
| 228 | + if key == "amount" { |
| 229 | + var balance string |
| 230 | + json.Unmarshal([]byte(value), &balance) |
| 231 | + n := new(big.Int) |
| 232 | + n, ok := n.SetString(balance, 10) |
| 233 | + if !ok { |
| 234 | + return nil, fmt.Errorf("error converting") |
| 235 | + |
| 236 | + } |
| 237 | + return n, nil |
| 238 | + |
| 239 | + } |
| 240 | + } |
| 241 | + return nil, fmt.Errorf("balance not found") |
| 242 | +} |
| 243 | + |
| 244 | +// // GetBalance fetches the current balance for a specific account address and denom. |
| 245 | +// // Implements Chain interface |
| 246 | +func (c *CosmosRemoteChain) GetWalletBalance(ctx context.Context, address string, denom string) (*big.Int, error) { |
| 247 | + commands := []string{"query", "bank", "balances", address} |
| 248 | + commands = append(commands, |
| 249 | + "--node", c.GetHostRPCAddress(), |
| 250 | + "--denom", denom, |
| 251 | + ) |
| 252 | + stdout, _, err := c.Exec(ctx, commands, nil) |
| 253 | + if err != nil { |
| 254 | + return nil, err |
| 255 | + } |
| 256 | + amount, err := c.extractAmount(string(stdout[:])) |
| 257 | + return amount, err |
| 258 | +} |
0 commit comments