Skip to content

Commit f148b7f

Browse files
authored
Use template to define ports (#41)
1 parent 4513c3c commit f148b7f

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

main.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,13 @@ func setupServices(svcManager *serviceManager, out *output) error {
440440
// p2p config. Use a default discovery key and disable public discovery and connections
441441
"--p2p-secret-key", defaultRethDiscoveryPrivKeyLoc,
442442
"--addr", "127.0.0.1",
443-
"--port", "30303",
443+
"--port", `{{Port "rpc" 30303}}`,
444444
// "--disable-discovery",
445445
// http config
446446
"--http",
447447
"--http.api", "admin,eth,net,web3",
448-
"--http.port", "8545",
449-
"--authrpc.port", "8551",
448+
"--http.port", `{{Port "http" 8545}}`,
449+
"--authrpc.port", `{{Port "authrpc" 8551}}`,
450450
"--authrpc.jwtsecret", "{{.Dir}}/jwtsecret",
451451
// For reth version 1.2.0 the "legacy" engine was removed, so we now require these arguments:
452452
"--engine.persistence-threshold", "0", "--engine.memory-block-buffer-target", "0",
@@ -455,9 +455,6 @@ func setupServices(svcManager *serviceManager, out *output) error {
455455
If(useRethForValidation, func(s *service) *service {
456456
return s.WithReplacementArgs("--http.api", "admin,eth,web3,net,rpc,flashbots")
457457
}).
458-
WithPort("rpc", 30303).
459-
WithPort("http", 8545).
460-
WithPort("authrpc", 8551).
461458
Build()
462459

463460
lightHouseVersion := func() string {
@@ -503,13 +500,13 @@ func setupServices(svcManager *serviceManager, out *output) error {
503500
"--disable-peer-scoring",
504501
"--staking",
505502
"--enr-address", "127.0.0.1",
506-
"--enr-udp-port", "9000",
507-
"--enr-tcp-port", "9000",
508-
"--enr-quic-port", "9100",
509-
"--port", "9000",
510-
"--quic-port", "9100",
503+
"--enr-udp-port", `{{Port "p2p" 9000}}`,
504+
"--enr-tcp-port", `{{Port "p2p" 9000}}`,
505+
"--enr-quic-port", `{{Port "quic" 9100}}`,
506+
"--port", `{{Port "p2p" 9000}}`,
507+
"--quic-port", `{{Port "quic" 9100}}`,
511508
"--http",
512-
"--http-port", "3500",
509+
"--http-port", `{{Port "http" 3500}}`,
513510
"--http-allow-origin", "*",
514511
"--disable-packet-filter",
515512
"--target-peers", "0",
@@ -536,7 +533,6 @@ func setupServices(svcManager *serviceManager, out *output) error {
536533
return s.WithArgs("--suggested-fee-recipient", "0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990")
537534
},
538535
).
539-
WithPort("http", 3500).
540536
Build()
541537

542538
// start validator client
@@ -894,6 +890,12 @@ func (s *serviceManager) NewService(name string) *service {
894890
}
895891

896892
func (s *service) WithPort(name string, portNumber int) *service {
893+
// add the port if not already present
894+
for _, p := range s.ports {
895+
if p.name == name {
896+
return s
897+
}
898+
}
897899
s.ports = append(s.ports, &port{name: name, port: portNumber})
898900
return s
899901
}
@@ -902,7 +904,10 @@ func (s *service) WithArgs(args ...string) *service {
902904
// use template substitution to load constants
903905
tmplVars := s.tmplVars()
904906
for i, arg := range args {
905-
args[i] = applyTemplate(arg, tmplVars)
907+
var port *port
908+
if args[i], port = applyTemplate(arg, tmplVars); port != nil {
909+
s.WithPort(port.name, port.port)
910+
}
906911
}
907912

908913
s.args = append(s.args, args...)
@@ -929,7 +934,10 @@ func (s *service) WithReplacementArgs(args ...string) *service {
929934
// use template substitution to load constants
930935
tmplVars := s.tmplVars()
931936
for i, arg := range args {
932-
args[i] = applyTemplate(arg, tmplVars)
937+
var port *port
938+
if args[i], port = applyTemplate(arg, tmplVars); port != nil {
939+
s.WithPort(port.name, port.port)
940+
}
933941
}
934942

935943
if i := slices.Index(s.args, args[0]); i != -1 {
@@ -951,8 +959,16 @@ func (s *service) Build() {
951959
s.srvMng.Build(s)
952960
}
953961

954-
func applyTemplate(templateStr string, input interface{}) string {
955-
tpl, err := template.New("").Parse(templateStr)
962+
func applyTemplate(templateStr string, input interface{}) (string, *port) {
963+
var found_port *port
964+
funcs := template.FuncMap{
965+
"Port": func(name string, num int) int {
966+
found_port = &port{name: name, port: num}
967+
return num
968+
},
969+
}
970+
971+
tpl, err := template.New("").Funcs(funcs).Parse(templateStr)
956972
if err != nil {
957973
panic(fmt.Sprintf("BUG: failed to parse template, err: %s", err))
958974
}
@@ -961,7 +977,7 @@ func applyTemplate(templateStr string, input interface{}) string {
961977
if err := tpl.Execute(&out, input); err != nil {
962978
panic(fmt.Sprintf("BUG: failed to execute template, err: %s", err))
963979
}
964-
return out.String()
980+
return out.String(), found_port
965981
}
966982

967983
func convert(config *params.BeaconChainConfig) ([]byte, error) {

0 commit comments

Comments
 (0)