From a26df9962486ffca49f24e9a9f05c5cf8e00c318 Mon Sep 17 00:00:00 2001 From: luka-ciric-ethernal Date: Mon, 18 Nov 2024 13:31:57 +0100 Subject: [PATCH 1/3] fix: external chain problem using geth In this commit chain id can be set because of changing go-ethereum-console code & writing dockerfile to build with expose port --- command/bridge/server/server.go | 68 +++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/command/bridge/server/server.go b/command/bridge/server/server.go index c1212c1e77..7b5a2be2fb 100644 --- a/command/bridge/server/server.go +++ b/command/bridge/server/server.go @@ -1,6 +1,8 @@ package server import ( + "archive/tar" + "bytes" "context" "errors" "fmt" @@ -15,8 +17,8 @@ import ( "syscall" "time" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" - dockerImg "github.com/docker/docker/api/types/image" dockerclient "github.com/docker/docker/client" "github.com/docker/docker/pkg/stdcopy" "github.com/docker/go-connections/nat" @@ -28,7 +30,7 @@ import ( ) const ( - gethConsoleImage = "ghcr.io/0xpolygon/go-ethereum-console:latest" + gethConsoleImage = "stefanethernal/go-ethereum-console:v0.0.2" gethImage = "ethereum/client-go:v1.9.25" defaultHostIP = "127.0.0.1" @@ -164,14 +166,22 @@ func runExternalChain(ctx context.Context, outputter command.OutputFormatter, cl image = gethImage } - // try to pull the image - reader, err := dockerClient.ImagePull(ctx, image, dockerImg.PullOptions{}) + dockerfile := fmt.Sprintf("FROM %s\nEXPOSE %d\n", image, params.port) + buildContext, err := createBuildContext(dockerfile) if err != nil { return err } - defer reader.Close() - if _, err = io.Copy(outputter, reader); err != nil { + imageName := fmt.Sprintf("geth-external-chain-%d", params.chainID) + build, err := dockerClient.ImageBuild(ctx, buildContext, types.ImageBuildOptions{ + Tags: []string{imageName}, + }) + if err != nil { + return err + } + defer build.Body.Close() + + if _, err = io.Copy(outputter, build.Body); err != nil { return fmt.Errorf("cannot copy: %w", err) } @@ -208,7 +218,7 @@ func runExternalChain(ctx context.Context, outputter command.OutputFormatter, cl args = append(args, "--authrpc.port", strconv.FormatUint(authPort, 10)) config := &container.Config{ - Image: image, + Image: imageName, Cmd: args, Labels: map[string]string{ "edge-type": "external-chain", @@ -271,6 +281,50 @@ func runExternalChain(ctx context.Context, outputter command.OutputFormatter, cl return nil } +// createBuildContext creates a tar archive with the Dockerfile content, which is used as the build context for the image, after that it removes temporary directory +func createBuildContext(dockerfileContent string) (io.Reader, error) { + tmpDir := "./temp-build-context" + err := os.MkdirAll(tmpDir, 0755) + if err != nil { + return nil, fmt.Errorf("failed to create temporary directory: %v", err) + } + defer os.RemoveAll(tmpDir) + + dockerfilePath := fmt.Sprintf("%s/Dockerfile", tmpDir) + err = os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644) + if err != nil { + return nil, fmt.Errorf("failed to write Dockerfile: %v", err) + } + + // Create the tar archive in memory + var buf bytes.Buffer + tarWriter := tar.NewWriter(&buf) + + // Add the Dockerfile to the tar archive + fileInfo := &tar.Header{ + Name: "Dockerfile", + Mode: 0600, + Size: int64(len(dockerfileContent)), + } + + err = tarWriter.WriteHeader(fileInfo) + if err != nil { + return nil, fmt.Errorf("failed to write tar header: %v", err) + } + + _, err = tarWriter.Write([]byte(dockerfileContent)) + if err != nil { + return nil, fmt.Errorf("failed to write Dockerfile content: %v", err) + } + + err = tarWriter.Close() + if err != nil { + return nil, fmt.Errorf("failed to close tar writer: %v", err) + } + + return &buf, nil +} + func gatherLogs(ctx context.Context, outputter command.OutputFormatter) error { opts := container.LogsOptions{ ShowStderr: true, From b1176c72175fc181daafba542d8bf9c5f74165d9 Mon Sep 17 00:00:00 2001 From: luka-ciric-ethernal Date: Mon, 18 Nov 2024 14:30:49 +0100 Subject: [PATCH 2/3] fix: lint --- command/bridge/server/server.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/command/bridge/server/server.go b/command/bridge/server/server.go index 7b5a2be2fb..c806b897ed 100644 --- a/command/bridge/server/server.go +++ b/command/bridge/server/server.go @@ -179,6 +179,7 @@ func runExternalChain(ctx context.Context, outputter command.OutputFormatter, cl if err != nil { return err } + defer build.Body.Close() if _, err = io.Copy(outputter, build.Body); err != nil { @@ -281,19 +282,19 @@ func runExternalChain(ctx context.Context, outputter command.OutputFormatter, cl return nil } -// createBuildContext creates a tar archive with the Dockerfile content, which is used as the build context for the image, after that it removes temporary directory +// createBuildContext creates a tar archive with the Dockerfile content, which is used as the build context for the image, +// after that it removes temporary directory func createBuildContext(dockerfileContent string) (io.Reader, error) { tmpDir := "./temp-build-context" - err := os.MkdirAll(tmpDir, 0755) - if err != nil { - return nil, fmt.Errorf("failed to create temporary directory: %v", err) + if err := os.MkdirAll(tmpDir, 0755); err != nil { + return nil, fmt.Errorf("failed to create temporary directory: %w", err) } + defer os.RemoveAll(tmpDir) dockerfilePath := fmt.Sprintf("%s/Dockerfile", tmpDir) - err = os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644) - if err != nil { - return nil, fmt.Errorf("failed to write Dockerfile: %v", err) + if err := os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0600); err != nil { + return nil, fmt.Errorf("failed to write Dockerfile: %w", err) } // Create the tar archive in memory @@ -307,19 +308,16 @@ func createBuildContext(dockerfileContent string) (io.Reader, error) { Size: int64(len(dockerfileContent)), } - err = tarWriter.WriteHeader(fileInfo) - if err != nil { - return nil, fmt.Errorf("failed to write tar header: %v", err) + if err := tarWriter.WriteHeader(fileInfo); err != nil { + return nil, fmt.Errorf("failed to write tar header: %w", err) } - _, err = tarWriter.Write([]byte(dockerfileContent)) - if err != nil { - return nil, fmt.Errorf("failed to write Dockerfile content: %v", err) + if _, err := tarWriter.Write([]byte(dockerfileContent)); err != nil { + return nil, fmt.Errorf("failed to write Dockerfile content: %w", err) } - err = tarWriter.Close() - if err != nil { - return nil, fmt.Errorf("failed to close tar writer: %v", err) + if err := tarWriter.Close(); err != nil { + return nil, fmt.Errorf("failed to close tar writer: %w", err) } return &buf, nil From 88c9ef8d22b2e53e2bfb805298f708d0567c3369 Mon Sep 17 00:00:00 2001 From: luka-ciric-ethernal Date: Mon, 18 Nov 2024 16:02:27 +0100 Subject: [PATCH 3/3] change: base docker image --- command/bridge/server/server.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/command/bridge/server/server.go b/command/bridge/server/server.go index c806b897ed..903cfe99b7 100644 --- a/command/bridge/server/server.go +++ b/command/bridge/server/server.go @@ -30,7 +30,7 @@ import ( ) const ( - gethConsoleImage = "stefanethernal/go-ethereum-console:v0.0.2" + gethConsoleImage = "0xethernal/go-ethereum-console:v0.0.1" gethImage = "ethereum/client-go:v1.9.25" defaultHostIP = "127.0.0.1" @@ -166,13 +166,14 @@ func runExternalChain(ctx context.Context, outputter command.OutputFormatter, cl image = gethImage } + imageName := fmt.Sprintf("geth-external-chain-%d", params.chainID) dockerfile := fmt.Sprintf("FROM %s\nEXPOSE %d\n", image, params.port) + buildContext, err := createBuildContext(dockerfile) if err != nil { return err } - imageName := fmt.Sprintf("geth-external-chain-%d", params.chainID) build, err := dockerClient.ImageBuild(ctx, buildContext, types.ImageBuildOptions{ Tags: []string{imageName}, })