Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: external chain problem using geth #435

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 60 additions & 7 deletions command/bridge/server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"archive/tar"
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -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"
Expand All @@ -28,7 +30,7 @@ import (
)

const (
gethConsoleImage = "ghcr.io/0xpolygon/go-ethereum-console:latest"
gethConsoleImage = "0xethernal/go-ethereum-console:v0.0.1"
gethImage = "ethereum/client-go:v1.9.25"

defaultHostIP = "127.0.0.1"
Expand Down Expand Up @@ -164,14 +166,24 @@ func runExternalChain(ctx context.Context, outputter command.OutputFormatter, cl
image = gethImage
}

// try to pull the image
reader, err := dockerClient.ImagePull(ctx, image, dockerImg.PullOptions{})
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
}

build, err := dockerClient.ImageBuild(ctx, buildContext, types.ImageBuildOptions{
Tags: []string{imageName},
})
if err != nil {
return err
}
defer reader.Close()

if _, err = io.Copy(outputter, reader); err != nil {
defer build.Body.Close()

if _, err = io.Copy(outputter, build.Body); err != nil {
return fmt.Errorf("cannot copy: %w", err)
}

Expand Down Expand Up @@ -208,7 +220,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",
Expand Down Expand Up @@ -271,6 +283,47 @@ 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"
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)
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
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)),
}

if err := tarWriter.WriteHeader(fileInfo); err != nil {
return nil, fmt.Errorf("failed to write tar header: %w", err)
}

if _, err := tarWriter.Write([]byte(dockerfileContent)); err != nil {
return nil, fmt.Errorf("failed to write Dockerfile content: %w", err)
}

if err := tarWriter.Close(); err != nil {
return nil, fmt.Errorf("failed to close tar writer: %w", err)
}

return &buf, nil
}

func gatherLogs(ctx context.Context, outputter command.OutputFormatter) error {
opts := container.LogsOptions{
ShowStderr: true,
Expand Down
Loading