Skip to content

Commit

Permalink
feat: yaml config support
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Apr 17, 2024
1 parent dbef0d7 commit 04aa5cf
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 88 deletions.
33 changes: 33 additions & 0 deletions local-interchain/chains/hub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
chains:
- name: gaia
chain_id: localcosmos-1
denom: uatom
binary: gaiad
bech32_prefix: cosmos
docker_image:
version: v10.0.1
gas_prices: 0%DENOM%
chain_type: cosmos
coin_type: 118
trusting_period: 112h
gas_adjustment: 2
number_vals: 1
number_node: 0
debugging: true
block_time: 500ms
genesis:
modify:
- key: app_state.gov.voting_params.voting_period
value: 15s
- key: app_state.gov.deposit_params.max_deposit_period
value: 15s
- key: app_state.gov.deposit_params.min_deposit.0.denom
value: uatom
accounts:
- name: acc0
address: cosmos1hj5fveer5cjtn4wd6wstzugjfdxzl0xpxvjjvr
amount: 10000000000%DENOM%
mnemonic: decorate bright ozone fork gallery riot bus exhaust worth way bone
indoor calm squirrel merry zero scheme cotton until shop any excess stage
laundry
4 changes: 2 additions & 2 deletions local-interchain/cmd/local-ic/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

type chains struct {
Configs []string `json:"chain_configs"`
Configs []string `json:"chain_configs" yaml:"chain_configs"`
}

var chainsCmd = &cobra.Command{
Use: "chains [config.json]",
Use: "chains [config.(json|yaml)]",
Short: "List all current chains or outputs a current config information",
Args: cobra.RangeArgs(0, 1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
2 changes: 1 addition & 1 deletion local-interchain/cmd/local-ic/new_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
var reader = bufio.NewReader(os.Stdin)

type Chains struct {
Chains []ictypes.Chain `json:"chains"`
Chains []ictypes.Chain `json:"chains" yaml:"chains"`
}

var newChainCmd = &cobra.Command{
Expand Down
34 changes: 29 additions & 5 deletions local-interchain/cmd/local-ic/start_chain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -57,13 +59,14 @@ local-ic start https://pastebin.com/raw/Ummk4DTM
// last part of the URL to be the test name
configPath = configPath[strings.LastIndex(configPath, "/")+1:]
} else {
configPath, err = GetConfigWithExtension(parentDir, configPath)
if err != nil {
panic(err)
}

config, err = interchain.LoadConfig(parentDir, configPath)
if err != nil {
// try again with .json, then if it still fails - panic
config, err = interchain.LoadConfig(parentDir, configPath+".json")
if err != nil {
panic(err)
}
panic(err)
}
}

Expand Down Expand Up @@ -94,6 +97,27 @@ local-ic start https://pastebin.com/raw/Ummk4DTM
},
}

// GetConfigWithExtension returns the config with the file extension attached if one was not provided.
// If "hub" is passed it, it will search for hub.yaml, hub.yml, or hub.json.
// If an extension is already applied, it will use that.
func GetConfigWithExtension(parentDir, config string) (string, error) {
if path.Ext(config) != "" {
return config, nil
}

extensions := []string{".yaml", ".yml", ".json"}
for _, ext := range extensions {
fp := path.Join(parentDir, interchain.ChainDir, config+ext)
if _, err := os.Stat(fp); err != nil {
continue
}

return config + ext, nil
}

return "", fmt.Errorf("could not find a file with an accepted extension: %s. (%+v)", config, extensions)
}

func init() {
startCmd.Flags().String(FlagAPIAddressOverride, "127.0.0.1", "override the default API address")
startCmd.Flags().Uint16(FlagAPIPortOverride, 8080, "override the default API port")
Expand Down
24 changes: 16 additions & 8 deletions local-interchain/interchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package interchain
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand All @@ -14,13 +14,16 @@ import (

types "github.com/strangelove-ventures/localinterchain/interchain/types"
"github.com/strangelove-ventures/localinterchain/interchain/util"
"gopkg.in/yaml.v3"

"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
)

const ChainDir = "chains"

func LoadConfig(installDir, chainCfgFile string) (*types.Config, error) {
var config types.Config

Expand All @@ -29,18 +32,23 @@ func LoadConfig(installDir, chainCfgFile string) (*types.Config, error) {
configFile = chainCfgFile
}

// Chains Folder
chainsDir := filepath.Join(installDir, "chains")
// A nested "chains" dir is required within the parent you specify.
chainsDir := filepath.Join(installDir, ChainDir)
cfgFilePath := filepath.Join(chainsDir, configFile)

bytes, err := os.ReadFile(cfgFilePath)
bz, err := os.ReadFile(cfgFilePath)
if err != nil {
return nil, err
}

err = json.Unmarshal(bytes, &config)
if err != nil {
return nil, err
if strings.HasSuffix(chainCfgFile, ".json") {
if err = json.Unmarshal(bz, &config); err != nil {
return nil, fmt.Errorf("error unmarshalling json config: %w", err)
}
} else {
if err := yaml.Unmarshal(bz, &config); err != nil {
return nil, fmt.Errorf("error unmarshalling yaml config: %w", err)
}
}

log.Println("Using directory:", installDir)
Expand All @@ -56,7 +64,7 @@ func LoadConfigFromURL(url string) (*types.Config, error) {
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
Expand Down
6 changes: 3 additions & 3 deletions local-interchain/interchain/handlers/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func NewInfo(
}

type GetInfo struct {
Logs types.MainLogs `json:"logs"`
Chains []types.Chain `json:"chains"`
Relay types.Relayer `json:"relayer"`
Logs types.MainLogs `json:"logs" yaml:"logs"`
Chains []types.Chain `json:"chains" yaml:"chains"`
Relay types.Relayer `json:"relayer" yaml:"relayer"`
}

func (i *info) GetInfo(w http.ResponseWriter, r *http.Request) {
Expand Down
20 changes: 10 additions & 10 deletions local-interchain/interchain/handlers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
)

type IbcChainConfigAlias struct {
Type string `json:"type"`
Name string `json:"name"`
ChainID string `json:"chain_id"`
Bin string `json:"bin"`
Bech32Prefix string `json:"bech32_prefix"`
Denom string `json:"denom"`
CoinType string `json:"coin_type"`
GasPrices string `json:"gas_prices"`
GasAdjustment float64 `json:"gas_adjustment"`
TrustingPeriod string `json:"trusting_period"`
Type string `json:"type" yaml:"type"`
Name string `json:"name" yaml:"name"`
ChainID string `json:"chain_id" yaml:"chain_id"`
Bin string `json:"bin" yaml:"bin"`
Bech32Prefix string `json:"bech32_prefix" yaml:"bech32_prefix"`
Denom string `json:"denom" yaml:"denom"`
CoinType string `json:"coin_type" yaml:"coin_type"`
GasPrices string `json:"gas_prices" yaml:"gas_prices"`
GasAdjustment float64 `json:"gas_adjustment" yaml:"gas_adjustment"`
TrustingPeriod string `json:"trusting_period" yaml:"trusting_period"`
}

func (c *IbcChainConfigAlias) Marshal() ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions local-interchain/interchain/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

type Route struct {
Path string `json:"path"`
Methods []string `json:"methods"`
Path string `json:"path" yaml:"path"`
Methods []string `json:"methods" yaml:"methods"`
}

func NewRouter(
Expand Down
21 changes: 16 additions & 5 deletions local-interchain/interchain/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"math"
"net/http"
"path"
"strings"

"github.com/strangelove-ventures/interchaintest/v8"
Expand Down Expand Up @@ -77,9 +78,9 @@ func StartChain(installDir, chainCfgFile string, ac *types.AppStartConfig) {
// Create chain factory for all the chains
cf := interchaintest.NewBuiltinChainFactory(logger, chainSpecs)

// Get chains from the chain factory
name := strings.ReplaceAll(chainCfgFile, ".json", "") + "ic"
chains, err := cf.Chains(name)
testName := GetTestName(chainCfgFile)

chains, err := cf.Chains(testName)
if err != nil {
log.Fatal("cf.Chains", err)
}
Expand All @@ -90,7 +91,7 @@ func StartChain(installDir, chainCfgFile string, ac *types.AppStartConfig) {
ic.AdditionalGenesisWallets = SetupGenesisWallets(config, chains)

fakeT := FakeTesting{
FakeName: name,
FakeName: testName,
}

// Base setup
Expand Down Expand Up @@ -125,7 +126,7 @@ func StartChain(installDir, chainCfgFile string, ac *types.AppStartConfig) {

// Build all chains & begin.
err = ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: name,
TestName: testName,
Client: client,
NetworkID: network,
SkipPathCreation: false,
Expand Down Expand Up @@ -197,3 +198,13 @@ func StartChain(installDir, chainCfgFile string, ac *types.AppStartConfig) {
log.Fatal("WaitForBlocks StartChain: ", err)
}
}

func GetTestName(chainCfgFile string) string {
name := chainCfgFile
fExt := path.Ext(name)
if fExt != "" {
name = strings.ReplaceAll(chainCfgFile, fExt, "")
}

return name + "ic"
}
48 changes: 22 additions & 26 deletions local-interchain/interchain/types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,36 @@ import (

// ConfigFileOverrides overrides app configuration files.
type ConfigFileOverrides struct {
File string `json:"file"`
Paths testutil.Toml `json:"paths"`
File string `json:"file" yaml:"file"`
Paths testutil.Toml `json:"paths" yaml:"paths"`
}

type Chain struct {
// ibc chain config (optional)
ChainType string `json:"chain_type" validate:"min=1"`
CoinType int `json:"coin_type" validate:"gt=0"`
Binary string `json:"binary" validate:"min=1"`
Bech32Prefix string `json:"bech32_prefix" validate:"min=1"`
Denom string `json:"denom" validate:"min=1"`
TrustingPeriod string `json:"trusting_period"`
Debugging bool `json:"debugging"`
BlockTime string `json:"block_time"`

HostPortOverride map[string]string `json:"host_port_override"`
ChainType string `json:"chain_type" yaml:"chain_type" validate:"min=1"`
CoinType int `json:"coin_type" yaml:"coin_type" validate:"gt=0"`
Binary string `json:"binary" yaml:"binary" validate:"min=1"`
Bech32Prefix string `json:"bech32_prefix" yaml:"bech32_prefix" validate:"min=1"`
Denom string `json:"denom" yaml:"denom" validate:"min=1"`
TrustingPeriod string `json:"trusting_period" yaml:"trusting_period"`
Debugging bool `json:"debugging" yaml:"debugging"`
BlockTime string `json:"block_time" yaml:"block_time"`
HostPortOverride map[string]string `json:"host_port_override" yaml:"host_port_override"`

// Required
Name string `json:"name" validate:"min=1"`
ChainID string `json:"chain_id" validate:"min=3"`

DockerImage DockerImage `json:"docker_image" validate:"url"`

GasPrices string `json:"gas_prices"`
GasAdjustment float64 `json:"gas_adjustment"`
NumberVals int `json:"number_vals" validate:"gte=1"`
NumberNode int `json:"number_node"`
IBCPaths []string `json:"ibc_paths"`
Genesis Genesis `json:"genesis"`

ConfigFileOverrides []ConfigFileOverrides `json:"config_file_overrides,omitempty"`
Name string `json:"name" yaml:"name" validate:"min=1"`
ChainID string `json:"chain_id" yaml:"chain_id" validate:"min=3"`
DockerImage DockerImage `json:"docker_image" yaml:"docker_image" validate:"url"`
GasPrices string `json:"gas_prices" yaml:"gas_prices"`
GasAdjustment float64 `json:"gas_adjustment" yaml:"gas_adjustment"`
NumberVals int `json:"number_vals" yaml:"number_vals" validate:"gte=1"`
NumberNode int `json:"number_node" yaml:"number_node"`
IBCPaths []string `json:"ibc_paths" yaml:"ibc_paths"`
Genesis Genesis `json:"genesis" yaml:"genesis"`
ConfigFileOverrides []ConfigFileOverrides `json:"config_file_overrides,omitempty" yaml:"config_file_overrides,omitempty"`

// EVM
EVMLoadStatePath string `json:"evm_load_state_path,omitempty"`
EVMLoadStatePath string `json:"evm_load_state_path,omitempty" yaml:"evm_load_state_path,omitempty"`
}

func (chain *Chain) Validate() error {
Expand Down
7 changes: 3 additions & 4 deletions local-interchain/interchain/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ type GenesisAccount struct {

type Genesis struct {
// Only apart of my fork for now.
Modify []cosmos.GenesisKV `json:"modify"` // 'key' & 'val' in the config.
Modify []cosmos.GenesisKV `json:"modify" yaml:"modify"` // 'key' & 'val' in the config.

Accounts []GenesisAccount `json:"accounts"`
Accounts []GenesisAccount `json:"accounts" yaml:"accounts"`

// A list of commands which run after chains are good to go.
// May need to move out of genesis into its own section? Seems silly though.
StartupCommands []string `json:"startup_commands"`
StartupCommands []string `json:"startup_commands" yaml:"startup_commands"`
}

20 changes: 10 additions & 10 deletions local-interchain/interchain/types/logs.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package types

type MainLogs struct {
StartTime uint64 `json:"start_time"`
Chains []LogOutput `json:"chains"`
Channels []IBCChannel `json:"ibc_channels"`
StartTime uint64 `json:"start_time" yaml:"start_time"`
Chains []LogOutput `json:"chains" yaml:"chains"`
Channels []IBCChannel `json:"ibc_channels" yaml:"ibc_channels"`
}

type LogOutput struct {
ChainID string `json:"chain_id"`
ChainName string `json:"chain_name"`
RPCAddress string `json:"rpc_address"`
RESTAddress string `json:"rest_address"`
GRPCAddress string `json:"grpc_address"`
P2PAddress string `json:"p2p_address"`
IBCPath []string `json:"ibc_paths"`
ChainID string `json:"chain_id" yaml:"chain_id"`
ChainName string `json:"chain_name" yaml:"chain_name"`
RPCAddress string `json:"rpc_address" yaml:"rpc_address"`
RESTAddress string `json:"rest_address" yaml:"rest_address"`
GRPCAddress string `json:"grpc_address" yaml:"grpc_address"`
P2PAddress string `json:"p2p_address" yaml:"p2p_address"`
IBCPath []string `json:"ibc_paths" yaml:"ibc_paths"`
}
Loading

0 comments on commit 04aa5cf

Please sign in to comment.