Skip to content

Commit

Permalink
chore: improvements to v4-ics branch (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey authored Nov 30, 2023
1 parent ba30bbf commit cfbca40
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 41 deletions.
92 changes: 59 additions & 33 deletions relayer/hermes/hermes_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ func NewConfig(chainConfigs ...ChainConfig) Config {
}

chains = append(chains, Chain{
ID: chainCfg.ChainID,
RPCAddr: hermesCfg.rpcAddr,
GrpcAddr: fmt.Sprintf("http://%s", hermesCfg.grpcAddr),
WebsocketAddr: strings.ReplaceAll(fmt.Sprintf("%s/websocket", hermesCfg.rpcAddr), "http", "ws"),
ID: chainCfg.ChainID,
Type: "CosmosSdk",
CCVConsumerChain: false,
RPCAddr: hermesCfg.rpcAddr,
GrpcAddr: fmt.Sprintf("http://%s", hermesCfg.grpcAddr),
EventSource: EventSource{
Mode: "push",
URL: strings.ReplaceAll(fmt.Sprintf("%s/websocket", hermesCfg.rpcAddr), "http", "ws"),
BatchDelay: "500ms",
},
RPCTimeout: "10s",
TrustedNode: false,
AccountPrefix: chainCfg.Bech32Prefix,
KeyName: hermesCfg.keyName,
AddressType: AddressType{
Expand Down Expand Up @@ -80,16 +87,20 @@ func NewConfig(chainConfigs ...ChainConfig) Config {
Telemetry: Telemetry{
Enabled: false,
},
TracingServer: TracingServer{
Enabled: false,
},
Chains: chains,
}
}

type Config struct {
Global Global `toml:"global"`
Mode Mode `toml:"mode"`
Rest Rest `toml:"rest"`
Telemetry Telemetry `toml:"telemetry"`
Chains []Chain `toml:"chains"`
Global Global `toml:"global"`
Mode Mode `toml:"mode"`
Rest Rest `toml:"rest"`
Telemetry Telemetry `toml:"telemetry"`
TracingServer TracingServer `toml:"tracing_server"`
Chains []Chain `toml:"chains"`
}

type Global struct {
Expand All @@ -111,10 +122,11 @@ type Channels struct {
}

type Packets struct {
Enabled bool `toml:"enabled"`
ClearInterval int `toml:"clear_interval"`
ClearOnStart bool `toml:"clear_on_start"`
TxConfirmation bool `toml:"tx_confirmation"`
Enabled bool `toml:"enabled"`
ClearInterval int `toml:"clear_interval"`
ClearOnStart bool `toml:"clear_on_start"`
TxConfirmation bool `toml:"tx_confirmation"`
AutoRegisterCounterpartyPayee bool `toml:"auto_register_counterparty_payee"`
}

type Mode struct {
Expand All @@ -136,6 +148,17 @@ type Telemetry struct {
Port int `toml:"port"`
}

type TracingServer struct {
Enabled bool `toml:"enabled"`
Port int `toml:"port"`
}

type EventSource struct {
Mode string `toml:"mode"`
URL string `toml:"url"`
BatchDelay string `toml:"batch_delay"`
}

type AddressType struct {
Derivation string `toml:"derivation"`
}
Expand All @@ -151,24 +174,27 @@ type TrustThreshold struct {
}

type Chain struct {
ID string `toml:"id"`
RPCAddr string `toml:"rpc_addr"`
GrpcAddr string `toml:"grpc_addr"`
WebsocketAddr string `toml:"websocket_addr"`
RPCTimeout string `toml:"rpc_timeout"`
AccountPrefix string `toml:"account_prefix"`
KeyName string `toml:"key_name"`
AddressType AddressType `toml:"address_type"`
StorePrefix string `toml:"store_prefix"`
DefaultGas int `toml:"default_gas"`
MaxGas int `toml:"max_gas"`
GasPrice GasPrice `toml:"gas_price"`
GasMultiplier float64 `toml:"gas_multiplier"`
MaxMsgNum int `toml:"max_msg_num"`
MaxTxSize int `toml:"max_tx_size"`
ClockDrift string `toml:"clock_drift"`
MaxBlockTime string `toml:"max_block_time"`
TrustingPeriod string `toml:"trusting_period"`
TrustThreshold TrustThreshold `toml:"trust_threshold"`
MemoPrefix string `toml:"memo_prefix,omitempty"`
ID string `toml:"id"`
Type string `toml:"type"`
CCVConsumerChain bool `toml:"ccv_consumer_chain"`
RPCAddr string `toml:"rpc_addr"`
GrpcAddr string `toml:"grpc_addr"`
EventSource EventSource `toml:"event_source"`
RPCTimeout string `toml:"rpc_timeout"`
TrustedNode bool `toml:"trusted_node"`
AccountPrefix string `toml:"account_prefix"`
KeyName string `toml:"key_name"`
AddressType AddressType `toml:"address_type"`
StorePrefix string `toml:"store_prefix"`
DefaultGas int `toml:"default_gas"`
MaxGas int `toml:"max_gas"`
GasPrice GasPrice `toml:"gas_price"`
GasMultiplier float64 `toml:"gas_multiplier"`
MaxMsgNum int `toml:"max_msg_num"`
MaxTxSize int `toml:"max_tx_size"`
ClockDrift string `toml:"clock_drift"`
MaxBlockTime string `toml:"max_block_time"`
TrustingPeriod string `toml:"trusting_period"`
TrustThreshold TrustThreshold `toml:"trust_threshold"`
MemoPrefix string `toml:"memo_prefix,omitempty"`
}
53 changes: 45 additions & 8 deletions relayer/hermes/hermes_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
const (
hermes = "hermes"
defaultContainerImage = "ghcr.io/informalsystems/hermes"
DefaultContainerVersion = "1.4.0"
DefaultContainerVersion = "v1.7.1"

hermesDefaultUidGid = "1000:1000"
hermesHome = "/home/hermes"
Expand Down Expand Up @@ -94,6 +94,34 @@ func (r *Relayer) AddChainConfiguration(ctx context.Context, rep ibc.RelayerExec
return r.validateConfig(ctx, rep)
}

func (r *Relayer) MarkChainAsConsumer(ctx context.Context, chainID string) error {
bz, err := r.ReadFileFromHomeDir(ctx, hermesConfigPath)
if err != nil {
return err
}

var cfg Config
err = toml.Unmarshal(bz, &cfg)
if err != nil {
return err
}

for i, chain := range cfg.Chains {
if chain.ID == chainID {
chain.CCVConsumerChain = true
cfg.Chains[i] = chain
break
}
}

bz, err = toml.Marshal(cfg)
if err != nil {
return err
}

return r.WriteFileToHomeDir(ctx, hermesConfigPath, bz)
}

// LinkPath performs the operations that happen when a path is linked. This includes creating clients, creating connections
// and establishing a channel. This happens across multiple operations rather than a single link path cli command.
func (r *Relayer) LinkPath(ctx context.Context, rep ibc.RelayerExecReporter, pathName string, channelOpts ibc.CreateChannelOptions, clientOpts ibc.CreateClientOptions) error {
Expand Down Expand Up @@ -141,7 +169,7 @@ func (r *Relayer) CreateConnections(ctx context.Context, rep ibc.RelayerExecRepo
return res.Err
}

chainAConnectionID, chainBConnectionID, err := getConnectionIDsFromStdout(res.Stdout)
chainAConnectionID, chainBConnectionID, err := GetConnectionIDsFromStdout(res.Stdout)
if err != nil {
return err
}
Expand Down Expand Up @@ -178,7 +206,7 @@ func (r *Relayer) CreateClients(ctx context.Context, rep ibc.RelayerExecReporter
return res.Err
}

chainAClientId, err := getClientIdFromStdout(res.Stdout)
chainAClientId, err := GetClientIdFromStdout(res.Stdout)
if err != nil {
return err
}
Expand All @@ -193,7 +221,7 @@ func (r *Relayer) CreateClients(ctx context.Context, rep ibc.RelayerExecReporter
return res.Err
}

chainBClientId, err := getClientIdFromStdout(res.Stdout)
chainBClientId, err := GetClientIdFromStdout(res.Stdout)
if err != nil {
return err
}
Expand Down Expand Up @@ -293,24 +321,33 @@ func extractJsonResult(stdout []byte) []byte {
return []byte(jsonOutput)
}

// getClientIdFromStdout extracts the client ID from stdout.
func getClientIdFromStdout(stdout []byte) (string, error) {
// GetClientIdFromStdout extracts the client ID from stdout.
func GetClientIdFromStdout(stdout []byte) (string, error) {
var clientCreationResult ClientCreationResponse
if err := json.Unmarshal(extractJsonResult(stdout), &clientCreationResult); err != nil {
return "", err
}
return clientCreationResult.Result.CreateClient.ClientID, nil
}

// getConnectionIDsFromStdout extracts the connectionIDs on both ends from the stdout.
func getConnectionIDsFromStdout(stdout []byte) (string, string, error) {
// GetConnectionIDsFromStdout extracts the connectionIDs on both ends from the stdout.
func GetConnectionIDsFromStdout(stdout []byte) (string, string, error) {
var connectionResponse ConnectionResponse
if err := json.Unmarshal(extractJsonResult(stdout), &connectionResponse); err != nil {
return "", "", err
}
return connectionResponse.Result.ASide.ConnectionID, connectionResponse.Result.BSide.ConnectionID, nil
}

// GetChannelIDsFromStdout extracts the channelIDs on both ends from stdout.
func GetChannelIDsFromStdout(stdout []byte) (string, string, error) {
var channelResponse ChannelCreationResponse
if err := json.Unmarshal(extractJsonResult(stdout), &channelResponse); err != nil {
return "", "", err
}
return channelResponse.Result.ASide.ChannelID, channelResponse.Result.BSide.ChannelID, nil
}

// parseRestoreKeyOutput extracts the address from the hermes output.
func parseRestoreKeyOutput(stdout string) string {
fullMatchIdx, addressGroupIdx := 0, 1
Expand Down
14 changes: 14 additions & 0 deletions relayer/hermes/hermes_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ type ConnectionSide struct {
ConnectionID string `json:"connection_id"`
}

// ChannelCreationResponse contains the minimum required values to extract the channel ids from a hermes response.
type ChannelCreationResponse struct {
Result CreateChannelResult `json:"result"`
}

type CreateChannelResult struct {
ASide ChannelSide `json:"a_side"`
BSide ChannelSide `json:"b_side"`
}

type ChannelSide struct {
ChannelID string `json:"channel_id"`
}

// ChannelOutputResult contains the minimum required channel values.
type ChannelOutputResult struct {
Result []ChannelResult `json:"result"`
Expand Down

0 comments on commit cfbca40

Please sign in to comment.