Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed namespaceID parsing from toml #373

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions da/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ import (
pb "github.com/dymensionxyz/dymint/types/pb/dymint"
)

const (
defaultTxPollingRetryDelay = 20 * time.Second
defaultSubmitRetryDelay = 10 * time.Second
defaultTxPollingAttempts = 5
)

type CNCClientI interface {
SubmitPFD(ctx context.Context, namespaceID [8]byte, blob []byte, fee int64, gasLimit uint64) (*cnc.TxResponse, error)
NamespacedShares(ctx context.Context, namespaceID [8]byte, height uint64) ([][]byte, error)
Expand All @@ -50,16 +44,6 @@ type DataAvailabilityLayerClient struct {
var _ da.DataAvailabilityLayerClient = &DataAvailabilityLayerClient{}
var _ da.BatchRetriever = &DataAvailabilityLayerClient{}

// Config stores Celestia DALC configuration parameters.
type Config struct {
BaseURL string `json:"base_url"`
AppNodeURL string `json:"app_node_url"`
Timeout time.Duration `json:"timeout"`
Fee int64 `json:"fee"`
GasLimit uint64 `json:"gas_limit"`
NamespaceID [8]byte `json:"namespace_id"`
}

// WithCNCClient sets CNC client.
func WithCNCClient(client CNCClientI) da.Option {
return func(daLayerClient da.DataAvailabilityLayerClient) {
Expand Down Expand Up @@ -99,16 +83,16 @@ func WithSubmitRetryDelay(delay time.Duration) da.Option {
func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger log.Logger, options ...da.Option) error {
c.logger = logger

if len(config) > 0 {
err := json.Unmarshal(config, &c.config)
if err != nil {
return err
}
if len(config) <= 0 {
return errors.New("config is empty")
}
err := json.Unmarshal(config, &c.config)
if err != nil {
return err
}

c.pubsubServer = pubsubServer
// Set defaults
var err error
c.txPollingRetryDelay = defaultTxPollingRetryDelay
c.txPollingAttempts = defaultTxPollingAttempts
c.submitRetryDelay = defaultSubmitRetryDelay
Expand Down
68 changes: 68 additions & 0 deletions da/celestia/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package celestia

import (
"encoding/hex"
"encoding/json"
"time"
)

const (
defaultTxPollingRetryDelay = 20 * time.Second
defaultSubmitRetryDelay = 10 * time.Second
defaultTxPollingAttempts = 5
)

// Config stores Celestia DALC configuration parameters.
type Config struct {
BaseURL string `json:"base_url"`
AppNodeURL string `json:"app_node_url"`
Timeout time.Duration `json:"timeout"`
Fee int64 `json:"fee"`
GasLimit uint64 `json:"gas_limit"`
NamespaceID [8]byte //`json:"namespace_id"`
}

// Define an auxiliary type to prevent infinite recursion in UnmarshalJSON
type auxConfig struct {
Copy link
Contributor

@omritoptix omritoptix Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't really like the duplication here. if I understand correctly the goal is to parse namespace id from string to bytes. I believe previously it has been done in the config file.
why do you think using AuxConfig is preferred?
Another option is possibly to add a field to the main config.
Creating 2 config which are basically the same but one field seems very confusing to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'll figure out better way

BaseURL string `json:"base_url"`
AppNodeURL string `json:"app_node_url"`
Timeout time.Duration `json:"timeout"`
Fee int64 `json:"fee"`
GasLimit uint64 `json:"gas_limit"`
NamespaceID string `json:"namespace_id"` // In the auxiliary type, NamespaceID is a string
}

var CelestiaDefaultConfig = Config{
BaseURL: "http://127.0.0.1:26659",
AppNodeURL: "",
Timeout: 30 * time.Second,
Fee: 20000,
GasLimit: 20000000,
NamespaceID: [8]byte{0, 0, 0, 0, 0, 0, 255, 255},
}

// UnmarshalJSON on Config type
func (c *Config) UnmarshalJSON(data []byte) error {
var aux auxConfig
if err := json.Unmarshal(data, &aux); err != nil {
return err
}

// Decode NamespaceID from string to byte array
namespaceBytes, err := hex.DecodeString(aux.NamespaceID)
if err != nil {
return err
}

// Copy the decoded bytes into NamespaceID
copy(c.NamespaceID[:], namespaceBytes)

// Copy other fields
c.BaseURL = aux.BaseURL
c.AppNodeURL = aux.AppNodeURL
c.Timeout = aux.Timeout
c.Fee = aux.Fee
c.GasLimit = aux.GasLimit

return nil
}
16 changes: 13 additions & 3 deletions da/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,26 @@ func TestLifecycle(t *testing.T) {
t.Skip("TODO")
}
t.Run(dalc, func(t *testing.T) {
doTestLifecycle(t, registry.GetClient(dalc))
doTestLifecycle(t, dalc)
})
}
}

func doTestLifecycle(t *testing.T, dalc da.DataAvailabilityLayerClient) {
func doTestLifecycle(t *testing.T, daType string) {
var err error
require := require.New(t)
pubsubServer := pubsub.NewServer()
pubsubServer.Start()
err := dalc.Init([]byte{}, pubsubServer, nil, test.NewLogger(t))

dacfg := []byte{}
dalc := registry.GetClient(daType)

if daType == "celestia" {
dacfg, err = json.Marshal(celestia.CelestiaDefaultConfig)
require.NoError(err)
}

err = dalc.Init(dacfg, pubsubServer, nil, test.NewLogger(t))
require.NoError(err)

err = dalc.Start()
Expand Down