Skip to content

Commit d09ca9f

Browse files
committed
simplify port forwarding, use only x:y format
1 parent ca45d89 commit d09ca9f

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

framework/components/blockchain/aptos.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"path/filepath"
7+
"strings"
78

89
"github.com/docker/docker/api/types/container"
910
"github.com/testcontainers/testcontainers-go"
@@ -12,6 +13,11 @@ import (
1213
"github.com/smartcontractkit/chainlink-testing-framework/framework"
1314
)
1415

16+
const (
17+
DefaultAptosAPIPort = "8080"
18+
DefaultAptosFaucetPort = "8081"
19+
)
20+
1521
var (
1622
DefaultAptosAccount = "0xa337b42bd0eecf8fb59ee5929ea4541904b3c35a642040223f3d26ab57f59d6e"
1723
DefaultAptosPrivateKey = "0xd477c65f88ed9e6d4ec6e2014755c3cfa3e0c44e521d0111a02868c5f04c41d4"
@@ -21,11 +27,15 @@ func defaultAptos(in *Input) {
2127
if in.Image == "" {
2228
in.Image = "aptoslabs/tools:aptos-node-v1.27.2"
2329
}
24-
framework.L.Warn().Msg("Aptos node API can only be exposed on port 8080!")
30+
framework.L.Warn().Msgf("Aptos node API can only be exposed on port %s!", DefaultAptosAPIPort)
31+
if in.Port == "" {
32+
// enable default API exposed port
33+
in.Port = DefaultAptosAPIPort
34+
}
2535
if in.CustomPorts == nil {
26-
in.CustomPorts = append(in.CustomPorts, "8080:8080", "8081:8081")
36+
// enable default API and faucet forwarding
37+
in.CustomPorts = append(in.CustomPorts, fmt.Sprintf("%s:%s", in.Port, DefaultAptosAPIPort), fmt.Sprintf("%s:%s", DefaultAptosFaucetPort, DefaultAptosFaucetPort))
2738
}
28-
in.Port = "8080"
2939
}
3040

3141
func newAptos(in *Input) (*Output, error) {
@@ -42,6 +52,7 @@ func newAptos(in *Input) (*Output, error) {
4252
if err != nil {
4353
return nil, err
4454
}
55+
exposedPorts = append(exposedPorts, in.Port)
4556

4657
cmd := []string{
4758
"aptos",
@@ -102,13 +113,20 @@ func newAptos(in *Input) (*Output, error) {
102113
if err != nil {
103114
return nil, err
104115
}
116+
// expose default API port if remapped
117+
var exposedAPIPort string
118+
for _, portPair := range in.CustomPorts {
119+
if strings.Contains(portPair, fmt.Sprintf(":%s", DefaultAptosAPIPort)) {
120+
exposedAPIPort = strings.Split(portPair, ":")[0]
121+
}
122+
}
105123
return &Output{
106124
UseCache: true,
107125
Family: "aptos",
108126
ContainerName: containerName,
109127
Nodes: []*Node{
110128
{
111-
ExternalHTTPUrl: fmt.Sprintf("http://%s:%s", host, in.Port),
129+
ExternalHTTPUrl: fmt.Sprintf("http://%s:%s", host, exposedAPIPort),
112130
InternalHTTPUrl: fmt.Sprintf("http://%s:%s", containerName, in.Port),
113131
},
114132
},

framework/docker.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -421,32 +421,22 @@ func GenerateCustomPortsData(portsProvided []string) ([]string, nat.PortMap, err
421421
portBindings := nat.PortMap{}
422422
customPorts := make([]string, 0)
423423
for _, p := range portsProvided {
424-
if strings.Contains(p, ":") {
425-
pp := strings.Split(p, ":")
426-
if len(pp) != 2 {
427-
return nil, nil, fmt.Errorf("custom_ports has ':' but you must provide both ports, you provided: %s", pp)
428-
}
429-
customPorts = append(customPorts, fmt.Sprintf("%s/tcp", pp[1]))
430-
431-
dockerPort := nat.Port(fmt.Sprintf("%s/tcp", pp[1]))
432-
hostPort := pp[0]
433-
portBindings[dockerPort] = []nat.PortBinding{
434-
{
435-
HostIP: "0.0.0.0",
436-
HostPort: hostPort,
437-
},
438-
}
439-
} else {
440-
customPorts = append(customPorts, fmt.Sprintf("%s/tcp", p))
441-
442-
dockerPort := nat.Port(fmt.Sprintf("%s/tcp", p))
443-
hostPort := p
444-
portBindings[dockerPort] = []nat.PortBinding{
445-
{
446-
HostIP: "0.0.0.0",
447-
HostPort: hostPort,
448-
},
449-
}
424+
if !strings.Contains(p, ":") {
425+
return nil, nil, fmt.Errorf("custom ports must have format external_port:internal_port, you provided: %s", p)
426+
}
427+
pp := strings.Split(p, ":")
428+
if len(pp) != 2 {
429+
return nil, nil, fmt.Errorf("custom_ports has ':' but you must provide both ports, you provided: %s", pp)
430+
}
431+
customPorts = append(customPorts, fmt.Sprintf("%s/tcp", pp[1]))
432+
433+
dockerPort := nat.Port(fmt.Sprintf("%s/tcp", pp[1]))
434+
hostPort := pp[0]
435+
portBindings[dockerPort] = []nat.PortBinding{
436+
{
437+
HostIP: "0.0.0.0",
438+
HostPort: hostPort,
439+
},
450440
}
451441
}
452442
exposedPorts = append(exposedPorts, customPorts...)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[blockchain_a]
22
type = "aptos"
3-
custom_ports = ["2020", "4050:4050", "8080:8080", "8081:8081"]
3+
custom_ports = ["9005:8080", "9006:8081"]
44
docker_cmd_params = ["--skip-metadata-apply"]

0 commit comments

Comments
 (0)