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

     1  package test_env
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/google/uuid"
    10  	"github.com/rs/zerolog"
    11  	"github.com/rs/zerolog/log"
    12  	tc "github.com/testcontainers/testcontainers-go"
    13  
    14  	"github.com/smartcontractkit/chainlink-testing-framework/libs/docker"
    15  	"github.com/smartcontractkit/chainlink-testing-framework/libs/logging"
    16  	"github.com/smartcontractkit/chainlink-testing-framework/libs/mirror"
    17  )
    18  
    19  type ValKeysGeneretor struct {
    20  	EnvComponent
    21  	chainConfig        *EthereumChainConfig
    22  	l                  zerolog.Logger
    23  	valKeysHostDataDir string
    24  	addressesToFund    []string
    25  	t                  *testing.T
    26  }
    27  
    28  func NewValKeysGeneretor(chainConfig *EthereumChainConfig, valKeysHostDataDir string, opts ...EnvComponentOption) (*ValKeysGeneretor, error) {
    29  	// currently it uses latest (no fixed version available)
    30  	dockerImage, err := mirror.GetImage("protolambda/eth2-val-tools:l")
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	parts := strings.Split(dockerImage, ":")
    36  	g := &ValKeysGeneretor{
    37  		EnvComponent: EnvComponent{
    38  			ContainerName:    fmt.Sprintf("%s-%s", "val-keys-generator", uuid.NewString()[0:8]),
    39  			ContainerImage:   parts[0],
    40  			ContainerVersion: parts[1],
    41  		},
    42  		chainConfig:        chainConfig,
    43  		valKeysHostDataDir: valKeysHostDataDir,
    44  		l:                  log.Logger,
    45  		addressesToFund:    []string{},
    46  	}
    47  	g.SetDefaultHooks()
    48  	for _, opt := range opts {
    49  		opt(&g.EnvComponent)
    50  	}
    51  
    52  	return g, nil
    53  }
    54  
    55  func (g *ValKeysGeneretor) WithTestInstance(t *testing.T) *ValKeysGeneretor {
    56  	g.l = logging.GetTestLogger(t)
    57  	g.t = t
    58  	return g
    59  }
    60  
    61  func (g *ValKeysGeneretor) StartContainer() error {
    62  	r, err := g.getContainerRequest(g.Networks)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	l := logging.GetTestContainersGoTestLogger(g.t)
    68  	_, err = docker.StartContainerWithRetry(g.l, tc.GenericContainerRequest{
    69  		ContainerRequest: *r,
    70  		Reuse:            true,
    71  		Started:          true,
    72  		Logger:           l,
    73  	})
    74  	if err != nil {
    75  		return fmt.Errorf("cannot start val keys generation container: %w", err)
    76  	}
    77  
    78  	g.l.Info().Str("containerName", g.ContainerName).
    79  		Msg("Started Val Keys Generation container")
    80  
    81  	return nil
    82  }
    83  
    84  func (g *ValKeysGeneretor) getContainerRequest(networks []string) (*tc.ContainerRequest, error) {
    85  	return &tc.ContainerRequest{
    86  		Name:          g.ContainerName,
    87  		Image:         g.GetImageWithVersion(),
    88  		ImagePlatform: "linux/x86_64",
    89  		Networks:      networks,
    90  		WaitingFor: NewExitCodeStrategy().WithExitCode(0).
    91  			WithPollInterval(1 * time.Second).WithTimeout(10 * time.Second),
    92  		Cmd: []string{"keystores",
    93  			"--insecure",
    94  			fmt.Sprintf("--prysm-pass=%s", WALLET_PASSWORD),
    95  			fmt.Sprintf("--out-loc=%s", NODE_0_DIR_INSIDE_CONTAINER),
    96  			fmt.Sprintf("--source-mnemonic=%s", VALIDATOR_BIP39_MNEMONIC),
    97  			//if we ever have more than 1 node these indexes should be updated, so that we don't generate the same keys
    98  			//e.g. if network has 2 nodes each with 10 validators, then the next source-min should be 10, and max should be 20
    99  			"--source-min=0",
   100  			fmt.Sprintf("--source-max=%d", g.chainConfig.ValidatorCount),
   101  		},
   102  		Mounts: tc.ContainerMounts{
   103  			tc.ContainerMount{
   104  				Source: tc.GenericBindMountSource{
   105  					HostPath: g.valKeysHostDataDir,
   106  				},
   107  				Target: tc.ContainerMountTarget(GENERATED_VALIDATOR_KEYS_DIR_INSIDE_CONTAINER),
   108  			},
   109  		},
   110  		LifecycleHooks: []tc.ContainerLifecycleHooks{
   111  			{
   112  				PostStarts: g.PostStartsHooks,
   113  				PostStops:  g.PostStopsHooks,
   114  			},
   115  		},
   116  	}, nil
   117  }