From 82320ed4129ceb3f2a37fc3bb9389413c9c0b955 Mon Sep 17 00:00:00 2001 From: Itay Levy Date: Mon, 14 Aug 2023 17:43:30 +0300 Subject: [PATCH] made celestia not a singleton --- cmd/config/init/init.go | 4 ++ cmd/da-light-client/start/start.go | 1 + data_layer/celestia/celestia.go | 52 +++++++------------ data_layer/da_layer.go | 2 +- .../init_with_flags/rollapp/config/app.toml | 6 +-- .../rollapp/config/app.toml | 6 +-- test/config/init/init_test.go | 2 - 7 files changed, 30 insertions(+), 43 deletions(-) diff --git a/cmd/config/init/init.go b/cmd/config/init/init.go index fe1bc3db..bd4049d8 100644 --- a/cmd/config/init/init.go +++ b/cmd/config/init/init.go @@ -127,6 +127,10 @@ func runInit(cmd *cobra.Command, args []string) error { /* ------------------------ Initialize DA light node ------------------------ */ damanager := datalayer.NewDAManager(initConfig.DA, initConfig.Home) + err = damanager.InitializeLightNodeConfig() + if err != nil { + return err + } daAddress, err := damanager.GetDAAccountAddress() if err != nil { return err diff --git a/cmd/da-light-client/start/start.go b/cmd/da-light-client/start/start.go index 99479b72..eb207358 100644 --- a/cmd/da-light-client/start/start.go +++ b/cmd/da-light-client/start/start.go @@ -32,6 +32,7 @@ func Cmd() *cobra.Command { utils.PrettifyErrorIfExists(errors.New("metrics endpoint can only be set for celestia")) } damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home) + insufficientBalances, err := damanager.CheckDABalance() utils.PrettifyErrorIfExists(err) utils.PrintInsufficientBalancesIfAny(insufficientBalances, rollappConfig) diff --git a/data_layer/celestia/celestia.go b/data_layer/celestia/celestia.go index 557dd55b..677c07b8 100644 --- a/data_layer/celestia/celestia.go +++ b/data_layer/celestia/celestia.go @@ -3,15 +3,13 @@ package celestia import ( "encoding/json" "fmt" - "math/big" - "os/exec" - "path/filepath" - "sync" - "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/config" globalutils "github.com/dymensionxyz/roller/utils" + "math/big" + "os/exec" + "path/filepath" ) // TODO: test how much is enough to run the LC for one day and set the minimum balance accordingly. @@ -32,22 +30,10 @@ type Celestia struct { RPCPort string } -var instance *Celestia -var once sync.Once - -func GetInstance(home string) *Celestia { - once.Do(func() { - cel := &Celestia{ - Root: home, - } - err := cel.InitializeLightNodeConfig() - utils.PrettifyErrorIfExists(err) - rpcPort, err := cel.ReadLightNodePort() - utils.PrettifyErrorIfExists(err) - cel.RPCPort = rpcPort - instance = cel - }) - return instance +func NewCelestia(home string) *Celestia { + return &Celestia{ + Root: home, + } } func (c2 *Celestia) GetPrivateKey() (string, error) { @@ -90,17 +76,21 @@ func (c *Celestia) GetStatus(rlpCfg config.RollappConfig) string { } } -func (c *Celestia) GetLightNodeEndpoint() string { - return fmt.Sprintf("http://localhost:%s", c.RPCPort) -} - -func (c *Celestia) ReadLightNodePort() (string, error) { +func (c *Celestia) getRPCPort() string { + if c.RPCPort != "" { + return c.RPCPort + } port, err := globalutils.GetKeyFromTomlFile(filepath.Join(c.Root, consts.ConfigDirName.DALightNode, "config.toml"), "Gateway.Port") if err != nil { - return "", err + panic(err) } - return port, nil + c.RPCPort = port + return port +} + +func (c *Celestia) GetLightNodeEndpoint() string { + return fmt.Sprintf("http://localhost:%s", c.getRPCPort()) } // GetDAAccountAddress implements datalayer.DataLayer. @@ -221,9 +211,3 @@ func (c *Celestia) GetSequencerDAConfig() string { return fmt.Sprintf(`{"base_url": "%s", "timeout": 60000000000, "gas_prices":0.1, "gas_adjustment": 1.3, "namespace_id":"000000000000ffff"}`, lcEndpoint) } - -// UnsafeResetInstance resets the singleton instance. Use only in tests. -func UnsafeResetInstance() { - instance = nil - once = sync.Once{} -} diff --git a/data_layer/da_layer.go b/data_layer/da_layer.go index db04bfae..9cd97969 100644 --- a/data_layer/da_layer.go +++ b/data_layer/da_layer.go @@ -36,7 +36,7 @@ func NewDAManager(datype config.DAType, home string) *DAManager { switch datype { case config.Celestia: - dalayer = celestia.GetInstance(home) + dalayer = celestia.NewCelestia(home) case config.Avail: dalayer = avail.NewAvail(home) case config.Mock: diff --git a/test/config/init/goldens/init_with_flags/rollapp/config/app.toml b/test/config/init/goldens/init_with_flags/rollapp/config/app.toml index 49696427..a1301add 100644 --- a/test/config/init/goldens/init_with_flags/rollapp/config/app.toml +++ b/test/config/init/goldens/init_with_flags/rollapp/config/app.toml @@ -8,9 +8,9 @@ index-events = [] inter-block-cache = true min-retain-blocks = 0 minimum-gas-prices = "0udym" -pruning = "default" -pruning-interval = "0" -pruning-keep-recent = "0" +pruning = "custom" +pruning-interval = "18000" +pruning-keep-recent = "6048000" [api] address = "tcp://0.0.0.0:1317" diff --git a/test/config/init/goldens/init_without_flags/rollapp/config/app.toml b/test/config/init/goldens/init_without_flags/rollapp/config/app.toml index 49696427..a1301add 100644 --- a/test/config/init/goldens/init_without_flags/rollapp/config/app.toml +++ b/test/config/init/goldens/init_without_flags/rollapp/config/app.toml @@ -8,9 +8,9 @@ index-events = [] inter-block-cache = true min-retain-blocks = 0 minimum-gas-prices = "0udym" -pruning = "default" -pruning-interval = "0" -pruning-keep-recent = "0" +pruning = "custom" +pruning-interval = "18000" +pruning-keep-recent = "6048000" [api] address = "tcp://0.0.0.0:1317" diff --git a/test/config/init/init_test.go b/test/config/init/init_test.go index c30e69b2..bd9cabca 100644 --- a/test/config/init/init_test.go +++ b/test/config/init/init_test.go @@ -2,7 +2,6 @@ package initconfig_test import ( "github.com/dymensionxyz/roller/config" - "github.com/dymensionxyz/roller/data_layer/celestia" "path/filepath" "testing" @@ -39,7 +38,6 @@ func TestInitCmd(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - celestia.UnsafeResetInstance() assert := assert.New(t) tempDir, err := os.MkdirTemp(os.TempDir(), "test") tempDir = filepath.Join(tempDir, ".roller")