From 2f99eda2c8b73c16bdfeeff294cd615c354a595e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Fri, 16 Aug 2024 14:47:56 +0300 Subject: [PATCH 1/2] Fix loading of custom currencies. Empty list if file not specified. --- cmd/rosetta/config.go | 5 +++-- cmd/rosetta/config_test.go | 4 ++-- cmd/rosetta/main.go | 11 ++++++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/rosetta/config.go b/cmd/rosetta/config.go index 9f3e3274..ed17aa4c 100644 --- a/cmd/rosetta/config.go +++ b/cmd/rosetta/config.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "os" "github.com/multiversx/mx-chain-rosetta/server/resources" @@ -10,14 +11,14 @@ import ( func loadConfigOfCustomCurrencies(configFile string) ([]resources.Currency, error) { fileContent, err := os.ReadFile(configFile) if err != nil { - return nil, err + return nil, fmt.Errorf("error when reading custom currencies config file: %w", err) } var customCurrencies []resources.Currency err = json.Unmarshal(fileContent, &customCurrencies) if err != nil { - return nil, err + return nil, fmt.Errorf("error when loading custom currencies from file: %w", err) } return customCurrencies, nil diff --git a/cmd/rosetta/config_test.go b/cmd/rosetta/config_test.go index 3e407f73..be31c7ae 100644 --- a/cmd/rosetta/config_test.go +++ b/cmd/rosetta/config_test.go @@ -26,11 +26,11 @@ func TestLoadConfigOfCustomCurrencies(t *testing.T) { t.Run("with error (missing file)", func(t *testing.T) { _, err := loadConfigOfCustomCurrencies("testdata/missing-file.json") - require.Error(t, err) + require.ErrorContains(t, err, "error when reading custom currencies config file") }) t.Run("with error (invalid file)", func(t *testing.T) { _, err := loadConfigOfCustomCurrencies("testdata/custom-currencies-bad.json") - require.Error(t, err) + require.ErrorContains(t, err, "error when loading custom currencies from file") }) } diff --git a/cmd/rosetta/main.go b/cmd/rosetta/main.go index 904a6658..cf327622 100644 --- a/cmd/rosetta/main.go +++ b/cmd/rosetta/main.go @@ -10,6 +10,7 @@ import ( "github.com/coinbase/rosetta-sdk-go/server" "github.com/multiversx/mx-chain-rosetta/server/factory" + "github.com/multiversx/mx-chain-rosetta/server/resources" "github.com/multiversx/mx-chain-rosetta/version" "github.com/urfave/cli" ) @@ -53,7 +54,7 @@ func startRosetta(ctx *cli.Context) error { return err } - customCurrencies, err := loadConfigOfCustomCurrencies(cliFlags.configFileCustomCurrencies) + customCurrencies, err := decideCustomCurrencies(cliFlags.configFileCustomCurrencies) if err != nil { return err } @@ -123,6 +124,14 @@ func startRosetta(ctx *cli.Context) error { return nil } +func decideCustomCurrencies(configFileCustomCurrencies string) ([]resources.Currency, error) { + if len(configFileCustomCurrencies) == 0 { + return make([]resources.Currency, 0), nil + } + + return loadConfigOfCustomCurrencies(configFileCustomCurrencies) +} + func createHttpServer(port int, routers ...server.Router) (*http.Server, error) { router := server.NewRouter( routers..., From 3786f549998ce224ff604da82ef153693486776d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Fri, 16 Aug 2024 14:51:52 +0300 Subject: [PATCH 2/2] Refactoring, tests. --- cmd/rosetta/config.go | 8 ++++++++ cmd/rosetta/config_test.go | 14 ++++++++++++++ cmd/rosetta/main.go | 9 --------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/cmd/rosetta/config.go b/cmd/rosetta/config.go index ed17aa4c..b5346326 100644 --- a/cmd/rosetta/config.go +++ b/cmd/rosetta/config.go @@ -8,6 +8,14 @@ import ( "github.com/multiversx/mx-chain-rosetta/server/resources" ) +func decideCustomCurrencies(configFileCustomCurrencies string) ([]resources.Currency, error) { + if len(configFileCustomCurrencies) == 0 { + return make([]resources.Currency, 0), nil + } + + return loadConfigOfCustomCurrencies(configFileCustomCurrencies) +} + func loadConfigOfCustomCurrencies(configFile string) ([]resources.Currency, error) { fileContent, err := os.ReadFile(configFile) if err != nil { diff --git a/cmd/rosetta/config_test.go b/cmd/rosetta/config_test.go index be31c7ae..6d6c7978 100644 --- a/cmd/rosetta/config_test.go +++ b/cmd/rosetta/config_test.go @@ -7,6 +7,20 @@ import ( "github.com/stretchr/testify/require" ) +func TestDecideCustomCurrencies(t *testing.T) { + t.Run("with success (file provided)", func(t *testing.T) { + customCurrencies, err := decideCustomCurrencies("testdata/custom-currencies.json") + require.NoError(t, err) + require.Len(t, customCurrencies, 2) + }) + + t.Run("with success (file not provided)", func(t *testing.T) { + customCurrencies, err := decideCustomCurrencies("") + require.NoError(t, err) + require.Empty(t, customCurrencies) + }) +} + func TestLoadConfigOfCustomCurrencies(t *testing.T) { t.Run("with success", func(t *testing.T) { customCurrencies, err := loadConfigOfCustomCurrencies("testdata/custom-currencies.json") diff --git a/cmd/rosetta/main.go b/cmd/rosetta/main.go index cf327622..fc51576c 100644 --- a/cmd/rosetta/main.go +++ b/cmd/rosetta/main.go @@ -10,7 +10,6 @@ import ( "github.com/coinbase/rosetta-sdk-go/server" "github.com/multiversx/mx-chain-rosetta/server/factory" - "github.com/multiversx/mx-chain-rosetta/server/resources" "github.com/multiversx/mx-chain-rosetta/version" "github.com/urfave/cli" ) @@ -124,14 +123,6 @@ func startRosetta(ctx *cli.Context) error { return nil } -func decideCustomCurrencies(configFileCustomCurrencies string) ([]resources.Currency, error) { - if len(configFileCustomCurrencies) == 0 { - return make([]resources.Currency, 0), nil - } - - return loadConfigOfCustomCurrencies(configFileCustomCurrencies) -} - func createHttpServer(port int, routers ...server.Router) (*http.Server, error) { router := server.NewRouter( routers...,