Skip to content

Commit

Permalink
frdrpc+frdrpcserver: move RPC server into own package
Browse files Browse the repository at this point in the history
To avoid the frdrpc package having dependencies to other libraries and
making using the JSON/WASM stubs hard, we extract all the business logic
and RPC server code into its own package called frdrpcserver.
  • Loading branch information
guggero committed May 2, 2022
1 parent 0c08671 commit 6f4c01d
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 100 deletions.
6 changes: 3 additions & 3 deletions faraday.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/lightningnetwork/lnd/signal"

"github.com/lightninglabs/faraday/chain"
"github.com/lightninglabs/faraday/frdrpc"
"github.com/lightninglabs/faraday/frdrpcserver"
)

// MinLndVersion is the minimum lnd version required. Note that apis that are
Expand Down Expand Up @@ -85,7 +85,7 @@ func Main() error {
defer client.Close()

// Instantiate the faraday gRPC server.
cfg := &frdrpc.Config{
cfg := &frdrpcserver.Config{
Lnd: client.LndServices,
RPCListen: config.RPCListen,
RESTListen: config.RESTListen,
Expand All @@ -104,7 +104,7 @@ func Main() error {
}
}

server := frdrpc.NewRPCServer(cfg)
server := frdrpcserver.NewRPCServer(cfg)

// Start the server.
if err := server.Start(); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions frdrpc/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package frdrpc contains the proto files and generated code for faraday's
// grpc server which serves requests for close recommendations.
package frdrpc
11 changes: 6 additions & 5 deletions frdrpc/channel_insights.go → frdrpcserver/channel_insights.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package frdrpc
package frdrpcserver

import (
"context"
"time"

"github.com/lightninglabs/faraday/frdrpc"
"github.com/lightninglabs/faraday/insights"
"github.com/lightninglabs/faraday/lndwrap"
"github.com/lightninglabs/faraday/revenue"
Expand Down Expand Up @@ -41,12 +42,12 @@ func channelInsights(ctx context.Context,
}

func rpcChannelInsightsResponse(
insights []*insights.ChannelInfo) *ChannelInsightsResponse {
insights []*insights.ChannelInfo) *frdrpc.ChannelInsightsResponse {

rpcInsights := make([]*ChannelInsight, 0, len(insights))
rpcInsights := make([]*frdrpc.ChannelInsight, 0, len(insights))

for _, i := range insights {
insight := &ChannelInsight{
insight := &frdrpc.ChannelInsight{
ChanPoint: i.ChannelPoint,
MonitoredSeconds: uint64(i.MonitoredFor.Seconds()),
UptimeSeconds: uint64(i.Uptime.Seconds()),
Expand All @@ -60,5 +61,5 @@ func rpcChannelInsightsResponse(
rpcInsights = append(rpcInsights, insight)
}

return &ChannelInsightsResponse{ChannelInsights: rpcInsights}
return &frdrpc.ChannelInsightsResponse{ChannelInsights: rpcInsights}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package frdrpc
package frdrpcserver

import (
"context"
"sort"
"time"

"github.com/lightninglabs/faraday/frdrpc"
"github.com/lightninglabs/faraday/insights"
"github.com/lightninglabs/faraday/recommend"
)

// parseRecommendationRequest parses a close recommendation request and
// returns the config required to get recommendations.
func parseRecommendationRequest(ctx context.Context, cfg *Config,
req *CloseRecommendationRequest) *recommend.CloseRecommendationConfig {
req *frdrpc.CloseRecommendationRequest) *recommend.CloseRecommendationConfig {

// Create a close recommendations config with the minimum monitored
// value provided in the request and the default outlier multiplier.
Expand All @@ -27,19 +28,19 @@ func parseRecommendationRequest(ctx context.Context, cfg *Config,
// Get the metric that the recommendations are being calculated based
// on.
switch req.Metric {
case CloseRecommendationRequest_UPTIME:
case frdrpc.CloseRecommendationRequest_UPTIME:
recCfg.Metric = recommend.UptimeMetric

case CloseRecommendationRequest_REVENUE:
case frdrpc.CloseRecommendationRequest_REVENUE:
recCfg.Metric = recommend.RevenueMetric

case CloseRecommendationRequest_INCOMING_VOLUME:
case frdrpc.CloseRecommendationRequest_INCOMING_VOLUME:
recCfg.Metric = recommend.IncomingVolume

case CloseRecommendationRequest_OUTGOING_VOLUME:
case frdrpc.CloseRecommendationRequest_OUTGOING_VOLUME:
recCfg.Metric = recommend.OutgoingVolume

case CloseRecommendationRequest_TOTAL_VOLUME:
case frdrpc.CloseRecommendationRequest_TOTAL_VOLUME:
recCfg.Metric = recommend.Volume
}

Expand All @@ -49,7 +50,7 @@ func parseRecommendationRequest(ctx context.Context, cfg *Config,
// parseOutlierRequest parses a rpc outlier recommendation request and returns
// the close recommendation config and multiplier required.
func parseOutlierRequest(ctx context.Context, cfg *Config,
req *OutlierRecommendationsRequest) (
req *frdrpc.OutlierRecommendationsRequest) (
*recommend.CloseRecommendationConfig, float64) {

multiplier := recommend.DefaultOutlierMultiplier
Expand All @@ -65,7 +66,7 @@ func parseOutlierRequest(ctx context.Context, cfg *Config,
// threshold boolean is inverted to allow for
// a default that returns values below a threshold.
func parseThresholdRequest(ctx context.Context, cfg *Config,
req *ThresholdRecommendationsRequest) (
req *frdrpc.ThresholdRecommendationsRequest) (
*recommend.CloseRecommendationConfig, float64) {

return parseRecommendationRequest(ctx, cfg, req.RecRequest),
Expand All @@ -74,15 +75,15 @@ func parseThresholdRequest(ctx context.Context, cfg *Config,

// rpcResponse parses the response obtained getting a close recommendation
// and converts it to a close recommendation response.
func rpcResponse(report *recommend.Report) *CloseRecommendationsResponse {
resp := &CloseRecommendationsResponse{
func rpcResponse(report *recommend.Report) *frdrpc.CloseRecommendationsResponse {
resp := &frdrpc.CloseRecommendationsResponse{
TotalChannels: int32(report.TotalChannels),
ConsideredChannels: int32(report.ConsideredChannels),
}

for chanPoint, rec := range report.Recommendations {
resp.Recommendations = append(
resp.Recommendations, &Recommendation{
resp.Recommendations, &frdrpc.Recommendation{
ChanPoint: chanPoint,
Value: float32(rec.Value),
RecommendClose: rec.RecommendClose,
Expand Down
9 changes: 6 additions & 3 deletions frdrpc/close_report.go → frdrpcserver/close_report.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package frdrpc
package frdrpcserver

import (
"context"
Expand All @@ -8,6 +8,7 @@ import (
"github.com/lightninglabs/lndclient"

"github.com/lightninglabs/faraday/fees"
"github.com/lightninglabs/faraday/frdrpc"
"github.com/lightninglabs/faraday/resolutions"
)

Expand All @@ -28,8 +29,10 @@ func parseCloseReportRequest(ctx context.Context, cfg *Config) *resolutions.Conf
}
}

func rpcCloseReportResponse(report *resolutions.CloseReport) *CloseReportResponse {
return &CloseReportResponse{
func rpcCloseReportResponse(
report *resolutions.CloseReport) *frdrpc.CloseReportResponse {

return &frdrpc.CloseReportResponse{
ChannelPoint: report.ChannelPoint.String(),
ChannelInitiator: report.ChannelInitiator,
CloseType: report.CloseType.String(),
Expand Down
55 changes: 29 additions & 26 deletions frdrpc/exchange_rate.go → frdrpcserver/exchange_rate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package frdrpc
package frdrpcserver

import (
"errors"
Expand All @@ -7,11 +7,12 @@ import (
"time"

"github.com/lightninglabs/faraday/fiat"
"github.com/lightninglabs/faraday/frdrpc"
)

func priceCfgFromRPC(rpcBackend FiatBackend, rpcGranularity Granularity,
disable bool, start, end time.Time, prices []*BitcoinPrice) (
*fiat.PriceSourceConfig, error) {
func priceCfgFromRPC(rpcBackend frdrpc.FiatBackend,
rpcGranularity frdrpc.Granularity, disable bool, start, end time.Time,
prices []*frdrpc.BitcoinPrice) (*fiat.PriceSourceConfig, error) {

backend, err := fiatBackendFromRPC(rpcBackend)
if err != nil {
Expand Down Expand Up @@ -59,7 +60,7 @@ func priceCfgFromRPC(rpcBackend FiatBackend, rpcGranularity Granularity,

// granularityFromRPC gets a granularity enum value from a rpc request,
// defaulting getting the best granularity for the period being queried.
func granularityFromRPC(g Granularity, disableFiat bool,
func granularityFromRPC(g frdrpc.Granularity, disableFiat bool,
duration time.Duration) (*fiat.Granularity, error) {

// If we do not need fiat prices, we can return nil granularity.
Expand All @@ -70,58 +71,58 @@ func granularityFromRPC(g Granularity, disableFiat bool,
switch g {
// If granularity is not set, allow it to default to the best
// granularity that we can get for the query period.
case Granularity_UNKNOWN_GRANULARITY:
case frdrpc.Granularity_UNKNOWN_GRANULARITY:
best, err := fiat.BestGranularity(duration)
if err != nil {
return nil, err
}

return &best, nil

case Granularity_MINUTE:
case frdrpc.Granularity_MINUTE:
return &fiat.GranularityMinute, nil

case Granularity_FIVE_MINUTES:
case frdrpc.Granularity_FIVE_MINUTES:
return &fiat.Granularity5Minute, nil

case Granularity_FIFTEEN_MINUTES:
case frdrpc.Granularity_FIFTEEN_MINUTES:
return &fiat.Granularity15Minute, nil

case Granularity_THIRTY_MINUTES:
case frdrpc.Granularity_THIRTY_MINUTES:
return &fiat.Granularity30Minute, nil

case Granularity_HOUR:
case frdrpc.Granularity_HOUR:
return &fiat.GranularityHour, nil

case Granularity_SIX_HOURS:
case frdrpc.Granularity_SIX_HOURS:
return &fiat.Granularity6Hour, nil

case Granularity_TWELVE_HOURS:
case frdrpc.Granularity_TWELVE_HOURS:
return &fiat.Granularity12Hour, nil

case Granularity_DAY:
case frdrpc.Granularity_DAY:
return &fiat.GranularityDay, nil

default:
return nil, fmt.Errorf("unknown granularity: %v", g)
}
}

func fiatBackendFromRPC(backend FiatBackend) (fiat.PriceBackend, error) {
func fiatBackendFromRPC(backend frdrpc.FiatBackend) (fiat.PriceBackend, error) {
switch backend {
case FiatBackend_UNKNOWN_FIATBACKEND:
case frdrpc.FiatBackend_UNKNOWN_FIATBACKEND:
return fiat.UnknownPriceBackend, nil

case FiatBackend_COINCAP:
case frdrpc.FiatBackend_COINCAP:
return fiat.CoinCapPriceBackend, nil

case FiatBackend_COINDESK:
case frdrpc.FiatBackend_COINDESK:
return fiat.CoinDeskPriceBackend, nil

case FiatBackend_CUSTOM:
case frdrpc.FiatBackend_CUSTOM:
return fiat.CustomPriceBackend, nil

case FiatBackend_COINGECKO:
case frdrpc.FiatBackend_COINGECKO:
return fiat.CoinGeckoPriceBackend, nil

default:
Expand All @@ -130,7 +131,7 @@ func fiatBackendFromRPC(backend FiatBackend) (fiat.PriceBackend, error) {
}
}

func parseExchangeRateRequest(req *ExchangeRateRequest) ([]time.Time,
func parseExchangeRateRequest(req *frdrpc.ExchangeRateRequest) ([]time.Time,
*fiat.PriceSourceConfig, error) {

if len(req.Timestamps) == 0 {
Expand Down Expand Up @@ -164,21 +165,23 @@ func parseExchangeRateRequest(req *ExchangeRateRequest) ([]time.Time,
return timestamps, cfg, nil
}

func exchangeRateResponse(prices map[time.Time]*fiat.Price) *ExchangeRateResponse {
fiatVals := make([]*ExchangeRate, 0, len(prices))
func exchangeRateResponse(
prices map[time.Time]*fiat.Price) *frdrpc.ExchangeRateResponse {

fiatVals := make([]*frdrpc.ExchangeRate, 0, len(prices))

for ts, price := range prices {
fiatVals = append(fiatVals, &ExchangeRate{
fiatVals = append(fiatVals, &frdrpc.ExchangeRate{
Timestamp: uint64(ts.Unix()),
BtcPrice: &BitcoinPrice{
BtcPrice: &frdrpc.BitcoinPrice{
Price: price.Price.String(),
PriceTimestamp: uint64(price.Timestamp.Unix()),
Currency: price.Currency,
},
})
}

return &ExchangeRateResponse{
return &frdrpc.ExchangeRateResponse{
Rates: fiatVals,
}
}
2 changes: 1 addition & 1 deletion frdrpc/log.go → frdrpcserver/log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package frdrpc
package frdrpcserver

import (
"github.com/btcsuite/btclog"
Expand Down
2 changes: 1 addition & 1 deletion frdrpc/macaroons.go → frdrpcserver/macaroons.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package frdrpc
package frdrpcserver

import (
"context"
Expand Down
Loading

0 comments on commit 6f4c01d

Please sign in to comment.