This commit is contained in:
colinlyguo
2024-03-16 14:02:58 +08:00
parent 36f4700d6a
commit e2a37c73a9
3 changed files with 49 additions and 21 deletions

View File

@@ -3,4 +3,3 @@ consensus/genesis.ssz
consensus/validatordata
execution/geth
execution/geth.ipc
.idea/

View File

@@ -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

View File

@@ -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