Skip to content

Commit

Permalink
feat: add api addrs field in context
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Jan 20, 2025
1 parent 5de4f33 commit ad2569f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
20 changes: 11 additions & 9 deletions config/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ type NodeConfig struct {
Type string `mapstructure:"type"` // Type is the service type of the node.
}

func (c *NodeConfig) APIAddrs() []string {
addrs := make([]string, len(c.RemoteAddrs))
port := c.GetAPIPort().OutFrom

for i, addr := range c.RemoteAddrs {
addrs[i] = fmt.Sprintf("https://%s:%d", addr, port)
}

return addrs
}

func (c *NodeConfig) APIListenAddr() string {
return fmt.Sprintf("0.0.0.0:%d", c.APIListenPort())
}
Expand All @@ -40,15 +51,6 @@ func (c *NodeConfig) APIListenPort() uint16 {
return c.GetAPIPort().InFrom
}

func (c *NodeConfig) APIRemoteAddrs() []string {
addrs := make([]string, len(c.RemoteAddrs))
for i, addr := range c.RemoteAddrs {
addrs[i] = fmt.Sprintf("https://%s:%d", addr, c.APIListenPort())
}

return addrs
}

// GetAPIPort returns the APIPort field.
func (c *NodeConfig) GetAPIPort() types.Port {
v, err := types.NewPortFromString(c.APIPort)
Expand Down
13 changes: 13 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
// Context defines the application context, holding configurations and shared components.
type Context struct {
accAddr cosmossdk.AccAddress
apiAddrs []string
client *client.Client
database *gorm.DB
dlSpeed math.Int
Expand Down Expand Up @@ -47,6 +48,11 @@ func (c *Context) AccAddr() cosmossdk.AccAddress {
return c.accAddr.Bytes()
}

// APIAddrs returns the api addresses set in the context.
func (c *Context) APIAddrs() []string {
return c.apiAddrs
}

// Client returns the client instance set in the context.
func (c *Context) Client() *client.Client {
return c.client.WithRPCAddr(c.RPCAddr())
Expand Down Expand Up @@ -172,6 +178,13 @@ func (c *Context) WithAccAddr(addr cosmossdk.AccAddress) *Context {
return c
}

// WithAPIAddrs sets the api addresses in the context and returns the updated context.
func (c *Context) WithAPIAddrs(addrs []string) *Context {
c.checkSealed()
c.apiAddrs = addrs
return c
}

// WithClient sets the client in the context and returns the updated context.
func (c *Context) WithClient(client *client.Client) *Context {
c.checkSealed()
Expand Down
3 changes: 2 additions & 1 deletion context/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ func (c *Context) Setup(cfg *config.Config) error {
log.Info("Setting up node context...")

// Assign configuration values to the context.
c.WithAPIAddrs(cfg.Node.APIAddrs())
c.WithGigabytePrices(cfg.Node.GetGigabytePrices())
c.WithHourlyPrices(cfg.Node.GetHourlyPrices())
c.WithMoniker(cfg.Node.GetMoniker())
c.WithRemoteAddrs(cfg.Node.APIRemoteAddrs())
c.WithRemoteAddrs(cfg.Node.RemoteAddrs)
c.WithRPCAddrs(cfg.RPC.GetAddrs())

// Set up the client for blockchain communication.
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (n *Node) Register(c *context.Context) error {
c.AccAddr(),
c.GigabytePrices(),
c.HourlyPrices(),
c.RemoteAddrs()[0],
c.APIAddrs()[0],
)

// Broadcast the registration transaction.
Expand All @@ -102,7 +102,7 @@ func (n *Node) UpdateDetails(c *context.Context) error {
c.NodeAddr(),
c.GigabytePrices(),
c.HourlyPrices(),
c.RemoteAddrs()[0],
c.APIAddrs()[0],
)

// Broadcast the update transaction.
Expand Down

0 comments on commit ad2569f

Please sign in to comment.