Skip to content

Commit

Permalink
Don't go through json
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpringle committed Feb 7, 2025
1 parent 616890e commit 453fa0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
17 changes: 17 additions & 0 deletions messages/teleporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ type Config struct {
RewardAddress string `json:"reward-address"`
}

func ConfigFromMap(m map[string]interface{}) (*Config, error) {
rewardAddress, ok := m["reward-address"].(string)
if !ok {
return nil, fmt.Errorf("reward-address not found")
}

c := &Config{
RewardAddress: rewardAddress,
}

if err := c.Validate(); err != nil {
return nil, err
}

return c, nil
}

func (c *Config) Validate() error {
if !common.IsHexAddress(c.RewardAddress) {
return fmt.Errorf("invalid reward address for EVM source subnet: %s", c.RewardAddress)
Expand Down
16 changes: 2 additions & 14 deletions messages/teleporter/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package teleporter

import (
"context"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -34,7 +33,7 @@ const (
)

type factory struct {
messageConfig Config
messageConfig *Config
protocolAddress common.Address
logger logging.Logger
deciderClient pbDecider.DeciderServiceClient
Expand Down Expand Up @@ -65,19 +64,8 @@ func NewMessageHandlerFactory(
messageProtocolConfig config.MessageProtocolConfig,
deciderClientConn *grpc.ClientConn,
) (messages.MessageHandlerFactory, error) {
// Marshal the map and unmarshal into the Teleporter config
data, err := json.Marshal(messageProtocolConfig.Settings)
messageConfig, err := ConfigFromMap(messageProtocolConfig.Settings)
if err != nil {
logger.Error("Failed to marshal Teleporter config")
return nil, err
}
var messageConfig Config
if err := json.Unmarshal(data, &messageConfig); err != nil {
logger.Error("Failed to unmarshal Teleporter config")
return nil, err
}

if err := messageConfig.Validate(); err != nil {
logger.Error(
"Invalid Teleporter config.",
zap.Error(err),
Expand Down

0 comments on commit 453fa0b

Please sign in to comment.