From e2a37c73a97c3ceb9bbbe494a8ee8036263273cd Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Sat, 16 Mar 2024 14:02:58 +0800 Subject: [PATCH] fix --- common/docker-compose/l1/.gitignore | 1 - common/docker-compose/l1/docker-compose.go | 62 ++++++++++++++++----- common/docker-compose/l1/docker-compose.yml | 7 +-- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/common/docker-compose/l1/.gitignore b/common/docker-compose/l1/.gitignore index d12bb78b0..71868865e 100644 --- a/common/docker-compose/l1/.gitignore +++ b/common/docker-compose/l1/.gitignore @@ -3,4 +3,3 @@ consensus/genesis.ssz consensus/validatordata execution/geth execution/geth.ipc -.idea/ diff --git a/common/docker-compose/l1/docker-compose.go b/common/docker-compose/l1/docker-compose.go index 48c66427a..b3ead1dd4 100644 --- a/common/docker-compose/l1/docker-compose.go +++ b/common/docker-compose/l1/docker-compose.go @@ -2,7 +2,9 @@ package dockercompose import ( "context" + "crypto/rand" "fmt" + "math/big" "os" "path/filepath" "time" @@ -15,8 +17,9 @@ import ( // PoSL1TestEnv represents the config needed to test in PoS Layer 1. type PoSL1TestEnv struct { - composeFilePath string - compose tc.ComposeStack + rootDir string + compose tc.ComposeStack + gethHTTPPort int } // NewPoSL1TestEnv creates and initializes a new instance of PoSL1TestEnv with a random HTTP port. @@ -26,28 +29,43 @@ func NewPoSL1TestEnv() (*PoSL1TestEnv, error) { return nil, fmt.Errorf("failed to find project root directory: %v", err) } + getRandomPort := func(min, max int) (int, error) { + maxInt := big.NewInt(int64(max - min)) + var randInt *big.Int + randInt, err = rand.Int(rand.Reader, maxInt) + if err != nil { + return 0, err + } + return int(randInt.Int64()) + min, nil + } + + gethHTTPPort, err := getRandomPort(1024, 65535) + if err != nil { + return nil, fmt.Errorf("failed to generate a secure random port for Geth HTTP: %v", err) + } + + if err := os.Setenv("GETH_HTTP_PORT", fmt.Sprintf("%d", gethHTTPPort)); err != nil { + return nil, fmt.Errorf("failed to set GETH_HTTP_PORT: %v", err) + } + return &PoSL1TestEnv{ - composeFilePath: filepath.Join(rootDir, "common", "docker-compose", "l1", "docker-compose.yml"), + rootDir: filepath.Join(rootDir, "common", "docker-compose", "l1"), + gethHTTPPort: gethHTTPPort, }, nil } // Start starts the PoS L1 test environment by running the associated Docker Compose configuration. func (e *PoSL1TestEnv) Start() error { var err error - defer func() { - if err != nil { - if errStop := e.Stop(); errStop != nil { - log.Error("failed to stop PoS L1 test environment", "err", errStop) - } - } - }() - - e.compose, err = tc.NewDockerCompose([]string{e.composeFilePath}...) + e.compose, err = tc.NewDockerCompose([]string{filepath.Join(e.rootDir, "docker-compose.yml")}...) if err != nil { return fmt.Errorf("failed to create docker compose: %w", err) } - if err = e.compose.WaitForService("geth", wait.NewHTTPStrategy("/").WithPort("8545/tcp").WithStartupTimeout(30*time.Second)).WithOsEnv().Up(context.Background()); err != nil { + if err = e.compose.WaitForService("geth", wait.NewHTTPStrategy("/").WithPort("8545/tcp").WithStartupTimeout(180*time.Second)).WithOsEnv().Up(context.Background()); err != nil { + if errStop := e.Stop(); errStop != nil { + log.Error("failed to stop PoS L1 test environment", "err", errStop) + } return fmt.Errorf("failed to start PoS L1 test environment: %w", err) } @@ -57,16 +75,30 @@ func (e *PoSL1TestEnv) Start() error { // Stop stops the PoS L1 test environment by stopping and removing the associated Docker Compose services. func (e *PoSL1TestEnv) Stop() error { if e.compose != nil { - if err := e.compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal); err != nil { + if err := e.compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveVolumes(true), tc.RemoveImagesAll); err != nil { return fmt.Errorf("failed to stop PoS L1 test environment: %w", err) } } + + dirsToRemove := []string{ + filepath.Join(e.rootDir, "consensus", "beacondata"), + filepath.Join(e.rootDir, "consensus", "validatordata"), + filepath.Join(e.rootDir, "consensus", "genesis.ssz"), + filepath.Join(e.rootDir, "execution", "geth"), + } + + for _, dir := range dirsToRemove { + if err := os.RemoveAll(dir); err != nil { + return fmt.Errorf("failed to remove data directory %s: %w", dir, err) + } + } + return nil } // Endpoint returns the HTTP endpoint for the PoS L1 test environment. func (e *PoSL1TestEnv) Endpoint() string { - return "http://127.0.0.1:8545" + return fmt.Sprintf("http://127.0.0.1:%d", e.gethHTTPPort) } // L1Client returns an ethclient by dialing the running PoS L1 test environment diff --git a/common/docker-compose/l1/docker-compose.yml b/common/docker-compose/l1/docker-compose.yml index d3da0150b..387c89323 100644 --- a/common/docker-compose/l1/docker-compose.yml +++ b/common/docker-compose/l1/docker-compose.yml @@ -69,8 +69,6 @@ services: depends_on: create-beacon-chain-genesis: condition: service_completed_successfully - ports: - - 12345:4000 volumes: - ${HOST_PATH:-../../..}/common/docker-compose/l1/consensus:/consensus - ${HOST_PATH:-../../..}/common/docker-compose/l1/execution:/execution @@ -95,8 +93,7 @@ services: - --nodiscover - --syncmode=full ports: - - 8551:8551 - - 8545:8545 + - ${GETH_HTTP_PORT:-8545}:8545 depends_on: geth-genesis: condition: service_completed_successfully @@ -112,7 +109,7 @@ services: validator: image: "gcr.io/prysmaticlabs/prysm/validator:v5.0.0" command: - - --beacon-rpc-provider=beacon-chain:12345 + - --beacon-rpc-provider=beacon-chain:4000 - --datadir=/consensus/validatordata - --accept-terms-of-use - --interop-num-validators=64