Skip to content

Commit 90096d9

Browse files
committed
merge 105 and 107 PRs
2 parents 6a4d269 + 6ed42e9 commit 90096d9

File tree

5 files changed

+96
-58
lines changed

5 files changed

+96
-58
lines changed

internal/artifacts.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ func (b *ArtifactsBuilder) Build() (*Artifacts, error) {
276276
}
277277
}
278278

279+
// Update the allocs to include the same prefunded accounts as the L1 genesis.
280+
allocs := make(map[string]interface{})
281+
input["alloc"] = allocs
282+
for _, privStr := range prefundedAccounts {
283+
priv, err := getPrivKey(privStr)
284+
if err != nil {
285+
return nil, err
286+
}
287+
addr := ecrypto.PubkeyToAddress(priv.PublicKey)
288+
allocs[addr.String()] = map[string]interface{}{
289+
"balance": "0x10000000000000000000000",
290+
"nonce": "0x1",
291+
}
292+
}
293+
279294
newOpGenesis, err := overrideJSON(opGenesis, input)
280295
if err != nil {
281296
return nil, err

internal/components.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (o *OpNode) Run(service *service, ctx *ExContext) {
8787
"--rpc.port", `{{Port "http" 8549}}`,
8888
"--p2p.listen.ip", "0.0.0.0",
8989
"--p2p.listen.tcp", `{{Port "p2p" 9003}}`,
90-
"--p2p.listen.udp", `{{Port "p2p" 9003}}`,
90+
"--p2p.listen.udp", `{{PortUDP "p2p" 9003}}`,
9191
"--p2p.scoring.peers", "light",
9292
"--p2p.ban.peers", "true",
9393
"--metrics.enabled",
@@ -303,11 +303,11 @@ func (l *LighthouseBeaconNode) Run(svc *service, ctx *ExContext) {
303303
"--disable-peer-scoring",
304304
"--staking",
305305
"--enr-address", "10.0.2.2",
306-
"--enr-udp-port", `{{Port "p2p" 9000}}`,
306+
"--enr-udp-port", `{{PortUDP "p2p" 9000}}`,
307307
"--enr-tcp-port", `{{Port "p2p" 9000}}`,
308-
"--enr-quic-port", `{{Port "quic-p2p" 9100}}`,
309-
"--port", `{{Port "p2p" 9000}}`,
310-
"--quic-port", `{{Port "quic-p2p" 9100}}`,
308+
"--enr-quic-port", `{{PortUDP "quic-p2p" 9100}}`,
309+
"--port", `{{PortUDP "p2p" 9000}}`,
310+
"--quic-port", `{{PortUDP "quic-p2p" 9100}}`,
311311
"--http",
312312
"--http-port", `{{Port "http" 3500}}`,
313313
"--http-address", "0.0.0.0",
@@ -440,7 +440,7 @@ func (b *BuilderHubPostgres) Run(service *service, ctx *ExContext) {
440440
service.
441441
WithImage("docker.io/flashbots/builder-hub-db").
442442
WithTag("0.2.1").
443-
WithLocalPort("postgres", 5432).
443+
WithPort("postgres", 5432).
444444
WithEnv("POSTGRES_USER", "postgres").
445445
WithEnv("POSTGRES_PASSWORD", "postgres").
446446
WithEnv("POSTGRES_DB", "postgres").
@@ -487,7 +487,7 @@ func (b *BuilderHubMockProxy) Run(service *service, ctx *ExContext) {
487487
service.
488488
WithImage("nginx").
489489
WithTag("1.27").
490-
WithLocalPort("http", 8888).
490+
WithPort("http", 8888).
491491
DependsOnRunning(b.TargetService).
492492
WithEntrypoint("/bin/sh").
493493
WithArgs("-c", fmt.Sprintf(`cat > /etc/nginx/conf.d/default.conf << 'EOF'

internal/local_runner.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ type LocalRunner struct {
5959
tasksMtx sync.Mutex
6060
tasks map[string]*task
6161
taskUpdateCh chan struct{}
62+
63+
// wether to bind the ports to the local interface
64+
bindHostPortsLocally bool
6265
}
6366

6467
type task struct {
@@ -88,12 +91,14 @@ func newDockerClient() (*client.Client, error) {
8891
return client, nil
8992
}
9093

91-
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool) (*LocalRunner, error) {
94+
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool) (*LocalRunner, error) {
9295
client, err := newDockerClient()
9396
if err != nil {
9497
return nil, fmt.Errorf("failed to create docker client: %w", err)
9598
}
9699

100+
fmt.Println(bindHostPortsLocally)
101+
97102
// merge the overrides with the manifest overrides
98103
if overrides == nil {
99104
overrides = make(map[string]string)
@@ -134,15 +139,16 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
134139
}
135140

136141
d := &LocalRunner{
137-
out: out,
138-
manifest: manifest,
139-
client: client,
140-
reservedPorts: map[int]bool{},
141-
overrides: overrides,
142-
handles: []*exec.Cmd{},
143-
tasks: tasks,
144-
taskUpdateCh: make(chan struct{}),
145-
exitErr: make(chan error, 2),
142+
out: out,
143+
manifest: manifest,
144+
client: client,
145+
reservedPorts: map[int]bool{},
146+
overrides: overrides,
147+
handles: []*exec.Cmd{},
148+
tasks: tasks,
149+
taskUpdateCh: make(chan struct{}),
150+
exitErr: make(chan error, 2),
151+
bindHostPortsLocally: bindHostPortsLocally,
146152
}
147153

148154
if interactive {
@@ -380,6 +386,16 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, map[string]string, er
380386
}
381387
}
382388

389+
resolvePort := func(name string, defaultPort int, protocol string) int {
390+
// For {{Port "name" "defaultPort"}}:
391+
// - Service runs on host: return the host port
392+
// - Service runs inside docker: return the docker port
393+
if d.isHostService(s.Name) {
394+
return s.MustGetPort(name).HostPort
395+
}
396+
return defaultPort
397+
}
398+
383399
funcs := template.FuncMap{
384400
"Service": func(name string, portLabel, protocol string) string {
385401
protocolPrefix := ""
@@ -412,13 +428,10 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, map[string]string, er
412428
}
413429
},
414430
"Port": func(name string, defaultPort int) int {
415-
// For {{Port "name" "defaultPort"}}:
416-
// - Service runs on host: return the host port
417-
// - Service runs inside docker: return the docker port
418-
if d.isHostService(s.Name) {
419-
return s.MustGetPort(name).HostPort
420-
}
421-
return defaultPort
431+
return resolvePort(name, defaultPort, ProtocolTCP)
432+
},
433+
"PortUDP": func(name string, defaultPort int) int {
434+
return resolvePort(name, defaultPort, ProtocolUDP)
422435
},
423436
}
424437

@@ -583,14 +596,15 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
583596
if len(s.ports) > 0 {
584597
ports := []string{}
585598
for _, p := range s.ports {
586-
if p.Local {
587-
// Bind only to localhost (127.0.0.1)
588-
ports = append(ports, fmt.Sprintf("127.0.0.1:%d:%d/tcp", p.HostPort, p.Port))
589-
ports = append(ports, fmt.Sprintf("127.0.0.1:%d:%d/udp", p.HostPort, p.Port))
599+
protocol := ""
600+
if p.Protocol == ProtocolUDP {
601+
protocol = "/udp"
602+
}
603+
604+
if d.bindHostPortsLocally {
605+
ports = append(ports, fmt.Sprintf("127.0.0.1:%d:%d%s", p.HostPort, p.Port, protocol))
590606
} else {
591-
// Bind to all interfaces (default)
592-
ports = append(ports, fmt.Sprintf("%d:%d/tcp", p.HostPort, p.Port))
593-
ports = append(ports, fmt.Sprintf("%d:%d/udp", p.HostPort, p.Port))
607+
ports = append(ports, fmt.Sprintf("%d:%d%s", p.HostPort, p.Port, protocol))
594608
}
595609
}
596610
service["ports"] = ports

internal/manifest.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ func (s *Manifest) Validate() error {
210210
return nil
211211
}
212212

213+
const (
214+
ProtocolUDP = "udp"
215+
ProtocolTCP = "tcp"
216+
)
217+
213218
// Port describes a port that a service exposes
214219
type Port struct {
215220
// Name is the name of the port
@@ -218,6 +223,9 @@ type Port struct {
218223
// Port is the port number
219224
Port int
220225

226+
// Protocol (tcp or udp)
227+
Protocol string
228+
221229
// HostPort is the port number assigned on the host machine for this
222230
// container port. It is populated by the local runner
223231
// TODO: We might want to move this to the runner itself.
@@ -359,37 +367,39 @@ func (s *service) WithTag(tag string) *service {
359367
return s
360368
}
361369

362-
func (s *service) WithPort(name string, portNumber int, local bool) *service {
370+
func (s *service) WithPort(name string, portNumber int, protocolVar ...string) *service {
371+
protocol := ProtocolTCP
372+
if len(protocolVar) > 0 {
373+
if protocolVar[0] != ProtocolTCP && protocolVar[0] != ProtocolUDP {
374+
panic(fmt.Sprintf("protocol %s not supported", protocolVar[0]))
375+
}
376+
protocol = protocolVar[0]
377+
}
378+
363379
// add the port if not already present with the same name.
364380
// if preset with the same name, they must have same port number
365381
for _, p := range s.ports {
366382
if p.Name == name {
367383
if p.Port != portNumber {
368384
panic(fmt.Sprintf("port %s already defined with different port number", name))
369385
}
386+
if p.Protocol != protocol {
387+
// If they have different protocols they are different ports
388+
continue
389+
}
370390
return s
371391
}
372392
}
373-
s.ports = append(s.ports, &Port{Name: name, Port: portNumber, Local: local})
393+
s.ports = append(s.ports, &Port{Name: name, Port: portNumber, Protocol: protocol})
374394
return s
375395
}
376396

377-
// WithLocalPort is a convenience method to add a port that should only be bound to localhost
378-
func (s *service) WithLocalPort(name string, portNumber int) *service {
379-
return s.WithPort(name, portNumber, true)
380-
}
381-
382-
// WithPublicPort is a convenience method to add a port that should be bound to all interfaces (default behavior)
383-
func (s *service) WithPublicPort(name string, portNumber int) *service {
384-
return s.WithPort(name, portNumber, false)
385-
}
386-
387397
func (s *service) applyTemplate(arg string) {
388398
var port []Port
389399
var nodeRef []NodeRef
390400
_, port, nodeRef = applyTemplate(arg)
391401
for _, p := range port {
392-
s.WithPort(p.Name, p.Port, false) // Default to non-local ports for backward compatibility
402+
s.WithPort(p.Name, p.Port, p.Protocol)
393403
}
394404
for _, n := range nodeRef {
395405
s.nodeRefs = append(s.nodeRefs, &n)
@@ -458,9 +468,13 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) {
458468
return fmt.Sprintf(`{{Service "%s" "%s"}}`, name, portLabel)
459469
},
460470
"Port": func(name string, defaultPort int) string {
461-
portRef = append(portRef, Port{Name: name, Port: defaultPort, Local: false})
471+
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: ProtocolTCP})
462472
return fmt.Sprintf(`{{Port "%s" %d}}`, name, defaultPort)
463473
},
474+
"PortUDP": func(name string, defaultPort int) string {
475+
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: ProtocolUDP})
476+
return fmt.Sprintf(`{{PortUDP "%s" %d}}`, name, defaultPort)
477+
},
464478
}
465479

466480
tpl, err := template.New("").Funcs(funcs).Parse(templateStr)

main.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var dryRun bool
2424
var interactive bool
2525
var timeout time.Duration
2626
var logLevelFlag string
27-
var localPortsFlag bool
27+
var bindExternal bool
2828

2929
var rootCmd = &cobra.Command{
3030
Use: "playground",
@@ -168,7 +168,7 @@ func main() {
168168
recipeCmd.Flags().BoolVar(&interactive, "interactive", false, "interactive mode")
169169
recipeCmd.Flags().DurationVar(&timeout, "timeout", 0, "") // Used for CI
170170
recipeCmd.Flags().StringVar(&logLevelFlag, "log-level", "info", "log level")
171-
recipeCmd.Flags().BoolVar(&localPortsFlag, "local-ports", false, "bind all ports to localhost only (127.0.0.1) for enhanced security")
171+
recipeCmd.Flags().BoolVar(&bindExternal, "bind-external", false, "bind host ports to external interface")
172172

173173
cookCmd.AddCommand(recipeCmd)
174174
}
@@ -219,15 +219,6 @@ func runIt(recipe internal.Recipe) error {
219219
return fmt.Errorf("failed to validate manifest: %w", err)
220220
}
221221

222-
// set the local ports flag
223-
if localPortsFlag {
224-
for _, svc := range svcManager.Services() {
225-
for _, port := range svc.Ports() {
226-
port.Local = true
227-
}
228-
}
229-
}
230-
231222
// generate the dot graph
232223
dotGraph := svcManager.GenerateDotGraph()
233224
if err := artifacts.Out.WriteFile("graph.dot", dotGraph); err != nil {
@@ -245,7 +236,7 @@ func runIt(recipe internal.Recipe) error {
245236
}
246237
}
247238

248-
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive)
239+
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal)
249240
if err != nil {
250241
return fmt.Errorf("failed to create docker runner: %w", err)
251242
}
@@ -275,7 +266,11 @@ func runIt(recipe internal.Recipe) error {
275266

276267
portsStr := []string{}
277268
for _, p := range ports {
278-
portsStr = append(portsStr, fmt.Sprintf("%s: %d/%d", p.Name, p.Port, p.HostPort))
269+
protocol := ""
270+
if p.Protocol == internal.ProtocolUDP {
271+
protocol = "/udp"
272+
}
273+
portsStr = append(portsStr, fmt.Sprintf("%s: %d/%d%s", p.Name, p.Port, p.HostPort, protocol))
279274
}
280275
fmt.Printf("- %s (%s)\n", ss.Name, strings.Join(portsStr, ", "))
281276
}

0 commit comments

Comments
 (0)