diff --git a/cmd/zetatool/config/config.go b/cmd/zetatool/config/config.go index eac4d4e2f7..1fbf991a66 100644 --- a/cmd/zetatool/config/config.go +++ b/cmd/zetatool/config/config.go @@ -2,21 +2,24 @@ package config import ( "encoding/json" - "os" + + "github.com/spf13/afero" ) +var AppFs = afero.NewOsFs() + const ( - Flag = "config" - defaultCfgFileName = "InboundTxFilter_config.json" - ZetaURL = "http://46.4.15.110:1317" //http://100.71.167.102:26657 - TssAddressBTC = "bc1qm24wp577nk8aacckv8np465z3dvmu7ry45el6y" - TssAddressEVM = "0x70e967acfcc17c3941e87562161406d41676fd83" - BtcExplorer = "https://blockstream.info/api/address/bc1qm24wp577nk8aacckv8np465z3dvmu7ry45el6y/txs" - EthRPC = "https://rpc.ankr.com/eth/2da24e4a1fd28f2bec1569eceb2c38a5694b7f5c83fd24c69ae714a89a514f9b" - ConnectorAddress = "0x000007Cf399229b2f5A4D043F20E90C9C98B7C6a" - CustodyAddress = "0x0000030Ec64DF25301d8414eE5a29588C4B0dE10" - EvmStartBlock = 19200110 - EvmMaxRange = 1000 + Flag = "config" + defaultCfgFileName = "InboundTxFilter_config.json" + ZetaURL = "http://46.4.15.110:1317" //http://100.71.167.102:26657 + TssAddressBTC = "bc1qm24wp577nk8aacckv8np465z3dvmu7ry45el6y" + TssAddressEVM = "0x70e967acfcc17c3941e87562161406d41676fd83" + BtcExplorer = "https://blockstream.info/api/address/bc1qm24wp577nk8aacckv8np465z3dvmu7ry45el6y/txs" + EthRPC = "https://rpc.ankr.com/eth/2da24e4a1fd28f2bec1569eceb2c38a5694b7f5c83fd24c69ae714a89a514f9b" + ConnectorAddress = "0x000007Cf399229b2f5A4D043F20E90C9C98B7C6a" + CustodyAddress = "0x0000030Ec64DF25301d8414eE5a29588C4B0dE10" + EvmStartBlock uint64 = 19200110 + EvmMaxRange uint64 = 1000 ) type Config struct { @@ -50,12 +53,12 @@ func (c *Config) Save() error { if err != nil { return err } - err = os.WriteFile(defaultCfgFileName, file, 0600) + err = afero.WriteFile(AppFs, defaultCfgFileName, file, 0600) return err } func (c *Config) Read(filename string) error { - data, err := os.ReadFile(filename) + data, err := afero.ReadFile(AppFs, filename) if err != nil { return err } diff --git a/cmd/zetatool/config/config_test.go b/cmd/zetatool/config/config_test.go new file mode 100644 index 0000000000..1dda4936ce --- /dev/null +++ b/cmd/zetatool/config/config_test.go @@ -0,0 +1,80 @@ +package config + +import ( + "github.com/spf13/afero" + "github.com/stretchr/testify/require" + "testing" +) + +func TestDefaultConfig(t *testing.T) { + cfg := DefaultConfig() + require.Equal(t, cfg.EthRPC, EthRPC) + require.Equal(t, cfg.ZetaURL, ZetaURL) + require.Equal(t, cfg.TssAddressBTC, TssAddressBTC) + require.Equal(t, cfg.TssAddressEVM, TssAddressEVM) + require.Equal(t, cfg.BtcExplorer, BtcExplorer) + require.Equal(t, cfg.ConnectorAddress, ConnectorAddress) + require.Equal(t, cfg.CustodyAddress, CustodyAddress) + require.Equal(t, cfg.EvmStartBlock, EvmStartBlock) + require.Equal(t, cfg.EvmMaxRange, EvmMaxRange) +} + +func TestGetConfig(t *testing.T) { + AppFs = afero.NewMemMapFs() + defaultCfg := DefaultConfig() + + t.Run("No config file specified", func(t *testing.T) { + cfg, err := GetConfig("") + require.NoError(t, err) + require.Equal(t, cfg, defaultCfg) + + exists, err := afero.Exists(AppFs, defaultCfgFileName) + require.NoError(t, err) + require.True(t, exists) + }) + + t.Run("config file specified", func(t *testing.T) { + cfg, err := GetConfig(defaultCfgFileName) + require.NoError(t, err) + require.Equal(t, cfg, defaultCfg) + }) +} + +func TestConfig_Read(t *testing.T) { + AppFs = afero.NewMemMapFs() + cfg, err := GetConfig("") + require.NoError(t, err) + + t.Run("read existing file", func(t *testing.T) { + c := &Config{} + err := c.Read(defaultCfgFileName) + require.NoError(t, err) + require.Equal(t, c, cfg) + }) + + t.Run("read non-existent file", func(t *testing.T) { + err := AppFs.Remove(defaultCfgFileName) + require.NoError(t, err) + c := &Config{} + err = c.Read(defaultCfgFileName) + require.ErrorContains(t, err, "file does not exist") + require.NotEqual(t, c, cfg) + }) +} + +func TestConfig_Save(t *testing.T) { + AppFs = afero.NewMemMapFs() + cfg := DefaultConfig() + cfg.EvmMaxRange = uint64(2000) + + t.Run("save modified cfg", func(t *testing.T) { + err := cfg.Save() + require.NoError(t, err) + + newCfg, err := GetConfig(defaultCfgFileName) + require.NoError(t, err) + require.Equal(t, cfg, newCfg) + }) + + // Should test invalid json encoding but currently not able to without interface +}