Skip to content

Commit

Permalink
fixed the namespaceID parsing solution
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin committed Jun 27, 2023
1 parent e06ae46 commit 081c3c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 41 deletions.
4 changes: 4 additions & 0 deletions da/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.S
if err != nil {
return err
}
err = (&c.config).InitNamespaceID()
if err != nil {
return err
}

c.pubsubServer = pubsubServer
// Set defaults
Expand Down
57 changes: 16 additions & 41 deletions da/celestia/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package celestia

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

Expand All @@ -14,55 +13,31 @@ const (

// 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 {
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
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"`
NamespaceIDStr string `json:"namespace_id"`
NamespaceID [8]byte `json:"-"`
}

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},
BaseURL: "http://127.0.0.1:26659",
AppNodeURL: "",
Timeout: 30 * time.Second,
Fee: 20000,
GasLimit: 20000000,
NamespaceIDStr: "000000000000ffff",
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
}

func (c *Config) InitNamespaceID() error {
// Decode NamespaceID from string to byte array
namespaceBytes, err := hex.DecodeString(aux.NamespaceID)
namespaceBytes, err := hex.DecodeString(c.NamespaceIDStr)
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
}

0 comments on commit 081c3c5

Please sign in to comment.