Skip to content

Commit 2cb3b86

Browse files
committed
relayer-config: add yaml and based struct configuration for forward
1 parent bcd3f0f commit 2cb3b86

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

pkg/relayer/cmd/cmd_start.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ Totals:
197197
}
198198
}
199199

200+
// --- Serve forward endpoint if enabled ---
201+
// enable the forward authenticated server to send request to a supplier for a given service name. It can be useful for operational purposes.
202+
// More information available here: https://dev.poktroll.com/operate/configs/relayminer_config#forward
203+
if relayMinerConfig.Forward.Enabled {
204+
if err = relayMiner.ServeForward(ctx, "tcp", relayMinerConfig.Forward.Addr, relayMinerConfig.Forward.AuthToken); err != nil {
205+
return fmt.Errorf("serve forward: %w", err)
206+
}
207+
}
208+
200209
// --- Start the relay miner ---
201210
logger.Info().Msg("Starting relay miner...")
202211

pkg/relayer/config/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ var (
1313
ErrRelayMinerConfigInvalidServer = sdkerrors.Register(codespace, 2106, "invalid server in RelayMiner config")
1414
ErrRelayMinerConfigInvalidRequestTimeout = sdkerrors.Register(codespace, 2107, "invalid request timeout specified in RelayMiner config")
1515
ErrRelayMinerConfigInvalidMaxBodySize = sdkerrors.Register(codespace, 2108, "invalid max body size specified in RelayMiner config")
16+
ErrRelayerMinerWrongForwardToken = sdkerrors.Register(codespace, 2109, "wrong or empty forward.token in configuration file. Expect a 32 bytes hexadecimal string (you can use 'make relayminer_forward_token_gen' command to generate a token)")
1617
)

pkg/relayer/config/relayminer_configs_reader.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
import (
4+
"regexp"
5+
46
"github.com/docker/go-units"
57
yaml "gopkg.in/yaml.v2"
68

@@ -87,6 +89,19 @@ func ParseRelayMinerConfigs(logger polylog.Logger, configContent []byte) (*Relay
8789
Addr: yamlRelayMinerConfig.Ping.Addr,
8890
}
8991

92+
if yamlRelayMinerConfig.Forward.Enabled {
93+
// accepts 32 bytes hexadecimal
94+
if matched, _ := regexp.MatchString(`^[a-fA-F0-9]{64}$`, yamlRelayMinerConfig.Forward.AuthToken); !matched {
95+
return nil, ErrRelayerMinerWrongForwardToken
96+
}
97+
}
98+
99+
relayMinerConfig.Forward = &RelayMinerForwardConfig{
100+
Enabled: yamlRelayMinerConfig.Forward.Enabled,
101+
Addr: yamlRelayMinerConfig.Forward.Addr,
102+
AuthToken: yamlRelayMinerConfig.Forward.AuthToken,
103+
}
104+
90105
// Hydrate the pocket node urls
91106
if err := relayMinerConfig.HydratePocketNodeUrls(&yamlRelayMinerConfig.PocketNode); err != nil {
92107
return nil, err

pkg/relayer/config/types.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"net/http"
45
"net/url"
56

67
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
@@ -32,6 +33,7 @@ type YAMLRelayMinerConfig struct {
3233
Suppliers []YAMLRelayMinerSupplierConfig `yaml:"suppliers"`
3334
Ping YAMLRelayMinerPingConfig `yaml:"ping"`
3435
EnableOverServicing bool `yaml:"enable_over_servicing"`
36+
Forward YAMLRelayMinerForwardConfig `yaml:"forward"`
3537

3638
// TODO_IMPROVE: Add a EnableErrorPropagation flag to control whether errors (i.e. non-2XX HTTP status codes)
3739
// are propagated back to the client or masked as internal errors.
@@ -52,6 +54,19 @@ type YAMLRelayMinerPingConfig struct {
5254
Addr string `yaml:"addr"`
5355
}
5456

57+
// YAMLRelayMinerForwardConfig represents the configuration to expose a forward
58+
// request server.
59+
type YAMLRelayMinerForwardConfig struct {
60+
Enabled bool `yaml:"enabled"`
61+
62+
// Addr is the address to bind to (format: 'hostname:port') where 'hostname' can be a DNS name or an IP.
63+
Addr string `yaml:"addr"`
64+
65+
// AuthToken is a 32 bytes hexadecimal string to authenticate request to the forward server. (mandatory flag when enabling forwarding capabilities).
66+
// You can use 'make relayminer_forward_token_gen' command to print a well-formatted token.
67+
AuthToken string `yaml:"auth_token"`
68+
}
69+
5570
// YAMLRelayMinerPocketNodeConfig is the structure used to unmarshal the pocket
5671
// node URLs section of the RelayMiner config file.
5772
type YAMLRelayMinerPocketNodeConfig struct {
@@ -116,6 +131,7 @@ type RelayMinerConfig struct {
116131
SmtStorePath string
117132
Ping *RelayMinerPingConfig
118133
EnableOverServicing bool
134+
Forward *RelayMinerForwardConfig
119135
}
120136

121137
// TODO_TECHDEBT(@red-0ne): Remove this structure altogether. See the discussion here for ref:
@@ -128,6 +144,17 @@ type RelayMinerPingConfig struct {
128144
Addr string
129145
}
130146

147+
// RelayMinerForwardConfig is the structure resulting from parsing the
148+
// forward server configuration.
149+
type RelayMinerForwardConfig struct {
150+
Enabled bool
151+
// Addr is the address to bind to (format: 'hostname:port') where 'hostname' can be a DNS name or an IP.
152+
Addr string
153+
// AuthToken is a 32 bytes hexadecimal string to authenticate request to the forward server. (mandatory flag when enabling forwarding capabilities)
154+
// You can use 'make relayminer_forward_token_gen' command to print a well-formatted token.
155+
AuthToken string
156+
}
157+
131158
// RelayMinerPocketNodeConfig is the structure resulting from parsing the pocket
132159
// node URLs section of the RelayMiner config file
133160
type RelayMinerPocketNodeConfig struct {
@@ -227,6 +254,17 @@ type RelayMinerSupplierServiceConfig struct {
227254
ForwardPocketHeaders bool
228255
}
229256

257+
// GetHeadersHTTP returns the supplier's service config http header.
258+
func (c RelayMinerSupplierServiceConfig) GetHeadersHTTP() http.Header {
259+
header := make(http.Header)
260+
261+
for k, v := range c.Headers {
262+
header.Add(k, v)
263+
}
264+
265+
return header
266+
}
267+
230268
// RelayMinerSupplierServiceAuthentication is the structure resulting from parsing
231269
// the supplier service basic auth of the RelayMiner config file when the
232270
// supplier is of type "http".

0 commit comments

Comments
 (0)