Skip to content

Commit

Permalink
feat(da): config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keruch committed Jul 2, 2024
1 parent 4bf7af9 commit 111346f
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 31 deletions.
2 changes: 2 additions & 0 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ da_config = "{{ .DAConfig }}"
# da_config = "{\"base_url\":\"http:\/\/127.0.0.1:26658\",\"timeout\":30000000000,\"gas_prices\":0.1,\"auth_token\":\"TOKEN\",\"backoff\":{\"initial_delay\":6000000000,\"max_delay\":6000000000,\"growth_factor\":2},\"retry_attempts\":4,\"retry_delay\":3000000000}"
# Avail config example:
# da_config = "{\"seed\": \"MNEMONIC\", \"api_url\": \"wss://kate.avail.tools/ws\", \"app_id\": 0, \"tip\":10}"
# Interchain DA config example:
# da_config = "{\"client_id\":\"dym-interchain\",\"chain_id\":\"interchain-da-test\",\"keyring_backend\":\"test\",\"keyring_home_dir\":\"/Users/keruch/.simapp\",\"address_prefix\":\"cosmos\",\"account_name\":\"sequencer\",\"node_address\":\"http://127.0.0.1:36657\",\"gas_prices\":\"10stake\",\"da_params\":{\"cost_per_byte\":{\"amount\":\"0\"}},\"retry_min_delay\":100000000,\"retry_max_delay\":2000000000,\"retry_attempts\":10}"
### p2p config ###
Expand Down
56 changes: 27 additions & 29 deletions da/interchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (
)

type DAConfig struct {
ClientID string `mapstructure:"client_id"` // IBC client ID between the Hub and DA layer
ChainID string `mapstructure:"chain_id"` // Chain ID of the DA layer

KeyringBackend string `mapstructure:"keyring_backend"`
KeyringHomeDir string `mapstructure:"keyring_home_dir"`
AddressPrefix string `mapstructure:"address_prefix"`
AccountName string `mapstructure:"account_name"`
NodeAddress string `mapstructure:"node_address"`
GasLimit uint64 `mapstructure:"gas_limit"`
GasPrices string `mapstructure:"gas_prices"`
GasFees string `mapstructure:"gas_fees"`
DAParams interchainda.Params `mapstructure:"da_params"`

RetryMinDelay time.Duration `mapstructure:"retry_min_delay"`
RetryMaxDelay time.Duration `mapstructure:"retry_min_delay"`
RetryAttempts uint `mapstructure:"retry_attempts"`
ClientID string `mapstructure:"client_id" json:"client_id,omitempty"` // IBC client ID between the Hub and DA layer
ChainID string `mapstructure:"chain_id" json:"chain_id,omitempty"` // Chain ID of the DA layer

KeyringBackend string `mapstructure:"keyring_backend" json:"keyring_backend,omitempty"`
KeyringHomeDir string `mapstructure:"keyring_home_dir" json:"keyring_home_dir,omitempty"`
AddressPrefix string `mapstructure:"address_prefix" json:"address_prefix,omitempty"`
AccountName string `mapstructure:"account_name" json:"account_name,omitempty"`
NodeAddress string `mapstructure:"node_address" json:"node_address,omitempty"`
GasLimit uint64 `mapstructure:"gas_limit" json:"gas_limit,omitempty"`
GasPrices string `mapstructure:"gas_prices" json:"gas_prices,omitempty"`
GasFees string `mapstructure:"gas_fees" json:"gas_fees,omitempty"`
DAParams interchainda.Params `mapstructure:"da_params" json:"da_params"`

RetryMinDelay time.Duration `mapstructure:"retry_min_delay" json:"retry_min_delay,omitempty"`
RetryMaxDelay time.Duration `mapstructure:"retry_min_delay" json:"retry_max_delay,omitempty"`
RetryAttempts uint `mapstructure:"retry_attempts" json:"retry_attempts,omitempty"`
}

func (c *DAConfig) Verify() error {
Expand All @@ -37,32 +37,30 @@ func (c *DAConfig) Verify() error {
}

if c.ChainID == "" {
return errors.New("missing Chain ID of the DA layer")
return errors.New("missing chain ID of the DA layer")
}

if c.KeyringBackend == "" {
return errors.New("missing Keyring Backend")
return errors.New("missing keyring backend")
}

if c.KeyringHomeDir == "" {
return errors.New("missing Keyring HomeDir")
return errors.New("missing keyring home dir")
}

if c.AddressPrefix == "" {
return errors.New("missing Address Prefix")
return errors.New("missing address prefix")
}

if c.AccountName == "" {
return errors.New("missing Account Name")
return errors.New("missing account name")
}

if c.NodeAddress == "" {
return errors.New("missing Node Address")
return errors.New("missing node address")
}

if c.GasLimit == 0 {
return errors.New("missing Gas Limit")
}
// GasLimit may be 0

if c.GasPrices == "" && c.GasFees == "" {
return errors.New("either gas prices or gas_prices are required")
Expand All @@ -75,15 +73,15 @@ func (c *DAConfig) Verify() error {
// DAParams are set during Init

if c.RetryMinDelay.Nanoseconds() == 0 {
return errors.New("missing Retry MinDelay")
return errors.New("missing retry min delay")
}

if c.RetryMaxDelay.Nanoseconds() == 0 {
return errors.New("missing Retry MaxDelay")
return errors.New("missing retry max delay")
}

if c.RetryAttempts == 0 {
return errors.New("missing Retry Attempts")
return errors.New("missing retry attempts")
}

return nil
Expand All @@ -93,7 +91,7 @@ func DefaultDAConfig() DAConfig {
home, _ := os.UserHomeDir()
keyringHomeDir := filepath.Join(home, ".simapp")
return DAConfig{
ClientID: "",
ClientID: "dym-interchain",
ChainID: "interchain-da-test",
KeyringBackend: keyring.BackendTest,
KeyringHomeDir: keyringHomeDir,
Expand Down
159 changes: 159 additions & 0 deletions da/interchain/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package interchain_test

import (
"encoding/json"
"errors"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/dymensionxyz/dymint/da/interchain"
interchainda "github.com/dymensionxyz/dymint/types/pb/interchain_da"
)

func TestDAConfig_Verify(t *testing.T) {
defaultConfig := interchain.DefaultDAConfig()

tests := map[string]struct {
DaConfig func(interchain.DAConfig) interchain.DAConfig
expectedError error
}{
"valid configuration": {
DaConfig: func(interchain.DAConfig) interchain.DAConfig {
return interchain.DAConfig{
ClientID: "test client id",
ChainID: "test chain id",
KeyringBackend: "test keyring backend",
KeyringHomeDir: "test keyring home dir",
AddressPrefix: "/",
AccountName: "test account name",
NodeAddress: "test node address",
GasLimit: 1,
GasPrices: "1.00",
GasFees: "",
DAParams: interchainda.Params{},
RetryMaxDelay: time.Second * 1,
RetryMinDelay: time.Second * 1,
RetryAttempts: 1,
}
},
expectedError: nil,
},

"default is valid": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
return d
},
expectedError: nil,
},
"missing client ID": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.ClientID = ""
return d
},
expectedError: errors.New("missing IBC client ID"),
},
"missing chain ID": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.ChainID = ""
return d
},
expectedError: errors.New("missing chain ID of the DA layer"),
},
"missing keyring backend": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.KeyringBackend = ""
return d
},
expectedError: errors.New("missing keyring backend"),
},
"missing keyring home dir": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.KeyringHomeDir = ""
return d
},
expectedError: errors.New("missing keyring home dir"),
},
"missing address prefix": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.AddressPrefix = ""
return d
},
expectedError: errors.New("missing address prefix"),
},
"missing account name": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.AccountName = ""
return d
},
expectedError: errors.New("missing account name"),
},
"missing node address": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.NodeAddress = ""
return d
},
expectedError: errors.New("missing node address"),
},
"both gas prices and gas fees are missing": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.GasPrices = ""
d.GasFees = ""
return d
},
expectedError: errors.New("either gas prices or gas_prices are required"),
},
"both gas prices and gas fees are specified": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.GasPrices = "10stake"
d.GasFees = "10stake"
return d
},
expectedError: errors.New("cannot provide both fees and gas prices"),
},
"missing retry min delay": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.RetryMinDelay = 0
return d
},
expectedError: errors.New("missing retry min delay"),
},
"missing retry max delay": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.RetryMaxDelay = 0
return d
},
expectedError: errors.New("missing retry max delay"),
},
"missing retry attempts": {
DaConfig: func(d interchain.DAConfig) interchain.DAConfig {
d.RetryAttempts = 0
return d
},
expectedError: errors.New("missing retry attempts"),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Generate test case config
tcConf := tc.DaConfig(defaultConfig)

err := tcConf.Verify()

if tc.expectedError != nil {
require.ErrorContains(t, err, tc.expectedError.Error())
} else {
require.NoError(t, err)
}
})
}
}

func Test(t *testing.T) {
conf := interchain.DefaultDAConfig()
data, err := json.Marshal(conf)
require.NoError(t, err)
t.Log(string(data))
}
5 changes: 3 additions & 2 deletions da/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package registry_test
import (
"testing"

"github.com/dymensionxyz/dymint/da/registry"
"github.com/stretchr/testify/assert"

"github.com/dymensionxyz/dymint/da/registry"
)

func TestRegistery(t *testing.T) {
assert := assert.New(t)

expected := []string{"mock", "grpc", "celestia", "avail"}
expected := []string{"mock", "grpc", "celestia", "avail", "interchain"}
actual := registry.RegisteredClients()

assert.ElementsMatch(expected, actual)
Expand Down

0 comments on commit 111346f

Please sign in to comment.