Skip to content

Commit

Permalink
made celestia not a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial committed Aug 14, 2023
1 parent 3f2ef33 commit 82320ed
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 43 deletions.
4 changes: 4 additions & 0 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/da-light-client/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 18 additions & 34 deletions data_layer/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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{}
}
2 changes: 1 addition & 1 deletion data_layer/da_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions test/config/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package initconfig_test

import (
"github.com/dymensionxyz/roller/config"
"github.com/dymensionxyz/roller/data_layer/celestia"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 82320ed

Please sign in to comment.