github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/docker/test_env/genesis_generator.go (about)

     1  package test_env
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/google/uuid"
    11  	"github.com/rs/zerolog"
    12  	"github.com/rs/zerolog/log"
    13  	tc "github.com/testcontainers/testcontainers-go"
    14  	tcwait "github.com/testcontainers/testcontainers-go/wait"
    15  
    16  	"github.com/smartcontractkit/chainlink-testing-framework/libs/docker"
    17  	"github.com/smartcontractkit/chainlink-testing-framework/libs/logging"
    18  	"github.com/smartcontractkit/chainlink-testing-framework/libs/mirror"
    19  )
    20  
    21  type EthGenesisGeneretor struct {
    22  	EnvComponent
    23  	chainConfig          EthereumChainConfig
    24  	l                    zerolog.Logger
    25  	generatedDataHostDir string
    26  	t                    *testing.T
    27  }
    28  
    29  func NewEthGenesisGenerator(chainConfig EthereumChainConfig, generatedDataHostDir string, opts ...EnvComponentOption) (*EthGenesisGeneretor, error) {
    30  	// currently it uses 2.0.5
    31  	dockerImage, err := mirror.GetImage("tofelb/ethereum-genesis-generator:2.0.5")
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	parts := strings.Split(dockerImage, ":")
    37  	g := &EthGenesisGeneretor{
    38  		EnvComponent: EnvComponent{
    39  			ContainerName:    fmt.Sprintf("%s-%s", "eth-genesis-generator", uuid.NewString()[0:8]),
    40  			ContainerImage:   parts[0],
    41  			ContainerVersion: parts[1],
    42  		},
    43  		chainConfig:          chainConfig,
    44  		generatedDataHostDir: generatedDataHostDir,
    45  		l:                    log.Logger,
    46  	}
    47  	g.SetDefaultHooks()
    48  	for _, opt := range opts {
    49  		opt(&g.EnvComponent)
    50  	}
    51  	return g, nil
    52  }
    53  
    54  func (g *EthGenesisGeneretor) WithTestInstance(t *testing.T) *EthGenesisGeneretor {
    55  	g.l = logging.GetTestLogger(t)
    56  	g.t = t
    57  	return g
    58  }
    59  
    60  func (g *EthGenesisGeneretor) StartContainer() error {
    61  	r, err := g.getContainerRequest(g.Networks)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	l := logging.GetTestContainersGoTestLogger(g.t)
    67  	_, err = docker.StartContainerWithRetry(g.l, tc.GenericContainerRequest{
    68  		ContainerRequest: *r,
    69  		Reuse:            true,
    70  		Started:          true,
    71  		Logger:           l,
    72  	})
    73  	if err != nil {
    74  		return fmt.Errorf("cannot start eth genesis generation container: %w", err)
    75  	}
    76  
    77  	g.l.Info().Str("containerName", g.ContainerName).
    78  		Msg("Started Eth Genesis container")
    79  
    80  	return nil
    81  }
    82  
    83  func (g *EthGenesisGeneretor) getContainerRequest(networks []string) (*tc.ContainerRequest, error) {
    84  	valuesEnv, err := os.CreateTemp("", "values.env")
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	bc, err := generateEnvValues(&g.chainConfig)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	_, err = valuesEnv.WriteString(bc)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	elGenesisFile, err := os.CreateTemp("", "genesis-config.yaml")
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	_, err = elGenesisFile.WriteString(elGenesisConfig)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	clGenesisFile, err := os.CreateTemp("", "config.yaml")
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	_, err = clGenesisFile.WriteString(clGenesisConfig)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	mnemonicsFile, err := os.CreateTemp("", "mnemonics.yaml")
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	_, err = mnemonicsFile.WriteString(mnemonics)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	return &tc.ContainerRequest{
   126  		Name:          g.ContainerName,
   127  		Image:         g.GetImageWithVersion(),
   128  		ImagePlatform: "linux/x86_64",
   129  		Networks:      networks,
   130  		WaitingFor: tcwait.ForAll(
   131  			tcwait.ForLog("+ terminalTotalDifficulty=0"),
   132  			tcwait.ForLog("+ sed -i 's/TERMINAL_TOTAL_DIFFICULTY:.*/TERMINAL_TOTAL_DIFFICULTY: 0/' /data/custom_config_data/config.yaml").
   133  				WithStartupTimeout(20*time.Second).
   134  				WithPollInterval(1*time.Second),
   135  		),
   136  		Cmd: []string{"all"},
   137  		Files: []tc.ContainerFile{
   138  			{
   139  				HostFilePath:      valuesEnv.Name(),
   140  				ContainerFilePath: "/config/values.env",
   141  				FileMode:          0644,
   142  			},
   143  			{
   144  				HostFilePath:      elGenesisFile.Name(),
   145  				ContainerFilePath: "/config/el/genesis-config.yaml",
   146  				FileMode:          0644,
   147  			},
   148  			{
   149  				HostFilePath:      clGenesisFile.Name(),
   150  				ContainerFilePath: "/config/cl/config.yaml",
   151  				FileMode:          0644,
   152  			},
   153  			{
   154  				HostFilePath:      mnemonicsFile.Name(),
   155  				ContainerFilePath: "/config/cl/mnemonics.yaml",
   156  				FileMode:          0644,
   157  			},
   158  		},
   159  		Mounts: tc.ContainerMounts{
   160  			tc.ContainerMount{
   161  				Source: tc.GenericBindMountSource{
   162  					HostPath: g.generatedDataHostDir,
   163  				},
   164  				Target: tc.ContainerMountTarget(GENERATED_DATA_DIR_INSIDE_CONTAINER),
   165  			},
   166  		},
   167  		LifecycleHooks: []tc.ContainerLifecycleHooks{
   168  			{
   169  				PostStarts: g.PostStartsHooks,
   170  				PostStops:  g.PostStopsHooks,
   171  			},
   172  		},
   173  	}, nil
   174  }