Skip to content

Commit

Permalink
fix: set api host on embedded configs (#253)
Browse files Browse the repository at this point in the history
* feat: add api host to serrvice config struct

* chore: add api host to config files

* feat: add api host to service flags

* feat: solver add service options

* refactor(powLogs): set host via init instead of fetching from env

* refacotr(metricsDashboard): set host via init instead of fetching via env and move struct definition to top of file

* chore: init logger when instantiating resource provider service

* chore: init logger when instantiating job creator service

* chore: add services to solver instance params and init logger when instantiating the solver service
  • Loading branch information
AquiGorka authored Jul 11, 2024
1 parent c43a71c commit 0aa29f3
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 16 deletions.
1 change: 1 addition & 0 deletions pkg/data/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
type ServiceConfig struct {
Solver string `json:"solver" toml:"solver"`
Mediator []string `json:"mediator" toml:"mediator"`
APIHost string `json:"api_host" toml:"api_host"`
}

// posted to the solver by a job creator
Expand Down
2 changes: 2 additions & 0 deletions pkg/jobcreator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func NewJobCreatorController(
return nil, err
}

metricsDashboard.Init(options.Offer.Services.APIHost)

controller := &JobCreatorController{
solverClient: solverClient,
options: options,
Expand Down
21 changes: 12 additions & 9 deletions pkg/metricsDashboard/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package metricsDashboard

import (
"encoding/json"
"os"
"time"

"github.com/lilypad-tech/lilypad/pkg/data"
Expand All @@ -15,7 +14,14 @@ const nodeConnectionEndpoint = "uptimes"
const dealsEndpoint = "deals"
const namespace = "metrics-dashboard"

var host = os.Getenv("API_HOST")
var host string

type NodeConnectionParams struct {
Event string
ID string
CountryCode string
IP string
}

type DealPayload struct {
ID string
Expand All @@ -24,6 +30,10 @@ type DealPayload struct {
JobID string
}

func Init(h string) {
host = h
}

func TrackJobOfferUpdate(evOffer data.JobOfferContainer) {
if host == "" {
return
Expand Down Expand Up @@ -68,13 +78,6 @@ func TrackNodeInfo(resourceOffer data.ResourceOffer) {
http.GenericJSONPostClient(url, payload)
}

type NodeConnectionParams struct {
Event string
ID string
CountryCode string
IP string
}

func TrackNodeConnectionEvent(params NodeConnectionParams) {
if host == "" {
return
Expand Down
1 change: 1 addition & 0 deletions pkg/options/configs/dev.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[services]
solver = "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC"
mediator = ["0x90F79bf6EB2c4f870365E785982E1f101E93b906"]
api_host = "http://localhost:8002/"

[web3]
rpc_url = "ws://localhost:8548"
Expand Down
1 change: 1 addition & 0 deletions pkg/options/configs/devnet.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[services]
solver = "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC"
mediator = ["0x90F79bf6EB2c4f870365E785982E1f101E93b906"]
api_host = "https://api-devnet.lilypad.tech/"

[web3]
rpc_url = "wss://devnet-chain-ws.lilypad.tech"
Expand Down
1 change: 1 addition & 0 deletions pkg/options/configs/testnet.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[services]
solver = "0xe05e1b71955da4934938a6b16f97e1db9de6b764"
mediator = ["0x7B49d6ee530B0A538D26E344f3B02E79ACa96De2"]
api_host = "https://api-testnet.lilypad.tech/"

[web3]
rpc_url = "wss://arb-sepolia.g.alchemy.com/v2/4PDb7czJf8E6z7ZjhXB0Z5fdU1XaQT0u"
Expand Down
11 changes: 11 additions & 0 deletions pkg/options/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func GetDefaultServicesOptions() data.ServiceConfig {
return data.ServiceConfig{
Solver: GetDefaultServeOptionString("SERVICE_SOLVER", ""),
Mediator: GetDefaultServeOptionStringArray("SERVICE_MEDIATORS", []string{}),
APIHost: GetDefaultServeOptionString("API_HOST", ""),
}
}

Expand All @@ -23,6 +24,10 @@ func AddServicesCliFlags(cmd *cobra.Command, servicesConfig *data.ServiceConfig)
&servicesConfig.Mediator, "service-mediators", servicesConfig.Mediator,
`The mediators we trust (SERVICE_MEDIATORS)`,
)
cmd.PersistentFlags().StringVar(
&servicesConfig.APIHost, "api-host", servicesConfig.APIHost,
`The api host to connect to (API_HOST)`,
)
}

func ProcessServicesOptions(options data.ServiceConfig, network string) (data.ServiceConfig, error) {
Expand All @@ -38,6 +43,9 @@ func ProcessServicesOptions(options data.ServiceConfig, network string) (data.Se
if len(options.Mediator) == 0 {
options.Mediator = config.ServiceConfig.Mediator
}
if options.APIHost == "" {
options.APIHost = config.ServiceConfig.APIHost
}

return options, nil
}
Expand All @@ -49,5 +57,8 @@ func CheckServicesOptions(options data.ServiceConfig) error {
if len(options.Mediator) == 0 {
return fmt.Errorf("No mediators services specified - please use SERVICE_MEDIATORS or --service-mediators")
}
if len(options.APIHost) == 0 {
return fmt.Errorf("No api host specified - please use API_HOST or --api-host")
}
return nil
}
6 changes: 4 additions & 2 deletions pkg/options/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

func NewSolverOptions() solver.SolverOptions {
options := solver.SolverOptions{
Server: GetDefaultServerOptions(),
Web3: GetDefaultWeb3Options(),
Server: GetDefaultServerOptions(),
Web3: GetDefaultWeb3Options(),
Services: GetDefaultServicesOptions(),
}
options.Web3.Service = system.SolverService
return options
Expand All @@ -18,6 +19,7 @@ func NewSolverOptions() solver.SolverOptions {
func AddSolverCliFlags(cmd *cobra.Command, options *solver.SolverOptions) {
AddWeb3CliFlags(cmd, &options.Web3)
AddServerCliFlags(cmd, &options.Server)
AddServicesCliFlags(cmd, &options.Services)
}

func CheckSolverOptions(options solver.SolverOptions) error {
Expand Down
7 changes: 5 additions & 2 deletions pkg/powLogs/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package powLogs

import (
"encoding/json"
"os"

"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/lilypad-tech/lilypad/pkg/http"
Expand All @@ -22,7 +21,11 @@ const namespace = "pow-logs"
const eventsEndpoint = "events"
const hashrateEndpoint = "hashrates"

var host = os.Getenv("API_HOST")
var host string

func Init(h string) {
host = h
}

func TrackEvent(data PowLog) {
if host == "" {
Expand Down
1 change: 1 addition & 0 deletions pkg/resourceprovider/resourceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func NewResourceProvider(
options: options,
web3SDK: web3SDK,
}
powLogs.Init(options.Offers.Services.APIHost)
return solver, nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/solver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ type solverServer struct {
options http.ServerOptions
controller *SolverController
store store.SolverStore
services data.ServiceConfig
}

func NewSolverServer(
options http.ServerOptions,
controller *SolverController,
store store.SolverStore,
services data.ServiceConfig,
) (*solverServer, error) {
server := &solverServer{
options: options,
controller: controller,
store: store,
}

metricsDashboard.Init(services.APIHost)

return server, nil
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package solver
import (
"context"

"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/lilypad-tech/lilypad/pkg/http"
"github.com/lilypad-tech/lilypad/pkg/solver/store"
"github.com/lilypad-tech/lilypad/pkg/system"
Expand All @@ -11,8 +12,9 @@ import (
)

type SolverOptions struct {
Web3 web3.Web3Options
Server http.ServerOptions
Web3 web3.Web3Options
Server http.ServerOptions
Services data.ServiceConfig
}

type Solver struct {
Expand All @@ -32,7 +34,7 @@ func NewSolver(
if err != nil {
return nil, err
}
server, err := NewSolverServer(options.Server, controller, store)
server, err := NewSolverServer(options.Server, controller, store, options.Services)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0aa29f3

Please sign in to comment.