github.com/prysmaticlabs/prysm@v1.4.4/endtoend/components/slasher.go (about)

     1  package components
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/bazelbuild/rules_go/go/tools/bazel"
    11  	"github.com/prysmaticlabs/prysm/endtoend/helpers"
    12  	e2e "github.com/prysmaticlabs/prysm/endtoend/params"
    13  	e2etypes "github.com/prysmaticlabs/prysm/endtoend/types"
    14  )
    15  
    16  var _ e2etypes.ComponentRunner = (*SlasherNode)(nil)
    17  var _ e2etypes.ComponentRunner = (*SlasherNodeSet)(nil)
    18  
    19  // SlasherNodeSet represents set of slasher nodes.
    20  type SlasherNodeSet struct {
    21  	e2etypes.ComponentRunner
    22  	config  *e2etypes.E2EConfig
    23  	started chan struct{}
    24  }
    25  
    26  // NewSlasherNodeSet creates and returns a set of slasher nodes.
    27  func NewSlasherNodeSet(config *e2etypes.E2EConfig) *SlasherNodeSet {
    28  	return &SlasherNodeSet{
    29  		config:  config,
    30  		started: make(chan struct{}, 1),
    31  	}
    32  }
    33  
    34  // Start starts all the slasher nodes in set.
    35  func (s *SlasherNodeSet) Start(ctx context.Context) error {
    36  	// Create slasher nodes.
    37  	nodes := make([]e2etypes.ComponentRunner, e2e.TestParams.BeaconNodeCount)
    38  	for i := 0; i < e2e.TestParams.BeaconNodeCount; i++ {
    39  		nodes[i] = NewSlasherNode(s.config, i)
    40  	}
    41  
    42  	// Wait for all nodes to finish their job (blocking).
    43  	// Once nodes are ready passed in handler function will be called.
    44  	return helpers.WaitOnNodes(ctx, nodes, func() {
    45  		// All nodes stated, close channel, so that all services waiting on a set, can proceed.
    46  		close(s.started)
    47  	})
    48  }
    49  
    50  // Started checks whether beacon node set is started and all nodes are ready to be queried.
    51  func (s *SlasherNodeSet) Started() <-chan struct{} {
    52  	return s.started
    53  }
    54  
    55  // SlasherNode represents a slasher node.
    56  type SlasherNode struct {
    57  	e2etypes.ComponentRunner
    58  	index   int
    59  	started chan struct{}
    60  }
    61  
    62  // NewSlasherNode creates and returns slasher node.
    63  func NewSlasherNode(_ *e2etypes.E2EConfig, index int) *SlasherNode {
    64  	return &SlasherNode{
    65  		index:   index,
    66  		started: make(chan struct{}, 1),
    67  	}
    68  }
    69  
    70  // Start starts slasher clients for use within E2E, connected to all beacon nodes.
    71  func (node *SlasherNode) Start(ctx context.Context) error {
    72  	binaryPath, found := bazel.FindBinary("cmd/slasher", "slasher")
    73  	if !found {
    74  		log.Info(binaryPath)
    75  		return errors.New("slasher binary not found")
    76  	}
    77  
    78  	stdOutFile, err := helpers.DeleteAndCreateFile(e2e.TestParams.LogPath, fmt.Sprintf(e2e.SlasherLogFileName, node.index))
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	args := []string{
    84  		fmt.Sprintf("--datadir=%s/slasher-data-%d/", e2e.TestParams.TestPath, node.index),
    85  		fmt.Sprintf("--log-file=%s", stdOutFile.Name()),
    86  		fmt.Sprintf("--rpc-port=%d", e2e.TestParams.SlasherRPCPort+node.index),
    87  		fmt.Sprintf("--monitoring-port=%d", e2e.TestParams.SlasherMetricsPort+node.index),
    88  		fmt.Sprintf("--beacon-rpc-provider=localhost:%d", e2e.TestParams.BeaconNodeRPCPort+node.index),
    89  		"--force-clear-db",
    90  		"--e2e-config",
    91  		"--accept-terms-of-use",
    92  	}
    93  
    94  	log.Infof("Starting slasher %d with flags: %s", node.index, strings.Join(args[2:], " "))
    95  	cmd := exec.CommandContext(ctx, binaryPath, args...)
    96  	if err = cmd.Start(); err != nil {
    97  		return fmt.Errorf("failed to start slasher client: %w", err)
    98  	}
    99  
   100  	if err = helpers.WaitForTextInFile(stdOutFile, "Starting slasher client"); err != nil {
   101  		return fmt.Errorf("could not find starting logs for slasher %d, this means it had issues starting: %w", node.index, err)
   102  	}
   103  
   104  	// Mark node as ready.
   105  	close(node.started)
   106  
   107  	return cmd.Wait()
   108  }
   109  
   110  // Started checks whether slasher node is started and ready to be queried.
   111  func (node *SlasherNode) Started() <-chan struct{} {
   112  	return node.started
   113  }