github.com/prysmaticlabs/prysm@v1.4.4/validator/client/metrics_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  
     6  	types "github.com/prysmaticlabs/eth2-types"
     7  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     8  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    10  	logTest "github.com/sirupsen/logrus/hooks/test"
    11  )
    12  
    13  func TestUpdateLogAggregateStats(t *testing.T) {
    14  	v := &validator{
    15  		logValidatorBalances: true,
    16  		startBalances:        make(map[[48]byte]uint64),
    17  		prevBalance:          make(map[[48]byte]uint64),
    18  		voteStats: voteStats{
    19  			startEpoch: 0, // this would otherwise have been previously set in LogValidatorGainsAndLosses()
    20  		},
    21  	}
    22  
    23  	pubKeyBytes := [][48]byte{
    24  		bytesutil.ToBytes48([]byte("000000000000000000000000000000000000000012345678")),
    25  		bytesutil.ToBytes48([]byte("000000000000000000000000000000000000000099999999")),
    26  		bytesutil.ToBytes48([]byte("000000000000000000000000000000000000000055555555")),
    27  	}
    28  
    29  	v.startBalances[pubKeyBytes[0]] = uint64(32100000000)
    30  	v.startBalances[pubKeyBytes[1]] = uint64(32200000000)
    31  	v.startBalances[pubKeyBytes[2]] = uint64(33000000000)
    32  
    33  	responses := []*ethpb.ValidatorPerformanceResponse{
    34  		{
    35  			PublicKeys: [][]byte{
    36  				bytesutil.FromBytes48(pubKeyBytes[0]),
    37  				bytesutil.FromBytes48(pubKeyBytes[1]),
    38  				bytesutil.FromBytes48(pubKeyBytes[2]),
    39  			},
    40  			InclusionSlots:       []types.Slot{types.Slot(^uint64(0)), 10, 11}, // exact slot doesn't matter, only if it is == or != ^uint64(0)
    41  			InclusionDistances:   []types.Slot{0, 5, 2},
    42  			CorrectlyVotedHead:   []bool{false, true, false},
    43  			CorrectlyVotedSource: []bool{false, true, true},
    44  			CorrectlyVotedTarget: []bool{false, true, true},
    45  		},
    46  		{
    47  			PublicKeys: [][]byte{
    48  				bytesutil.FromBytes48(pubKeyBytes[0]),
    49  				bytesutil.FromBytes48(pubKeyBytes[1]),
    50  				bytesutil.FromBytes48(pubKeyBytes[2]),
    51  			},
    52  			InclusionSlots:       []types.Slot{33, 34, 35},
    53  			InclusionDistances:   []types.Slot{1, 2, 3},
    54  			CorrectlyVotedHead:   []bool{true, true, true},
    55  			CorrectlyVotedSource: []bool{true, true, true},
    56  			CorrectlyVotedTarget: []bool{true, true, true},
    57  		},
    58  		{
    59  			PublicKeys: [][]byte{
    60  				bytesutil.FromBytes48(pubKeyBytes[0]),
    61  				bytesutil.FromBytes48(pubKeyBytes[1]),
    62  				bytesutil.FromBytes48(pubKeyBytes[2]),
    63  			},
    64  			InclusionSlots:       []types.Slot{65, types.Slot(^uint64(0)), 67},
    65  			InclusionDistances:   []types.Slot{1, 0, 2},
    66  			CorrectlyVotedHead:   []bool{true, false, true},
    67  			CorrectlyVotedSource: []bool{true, false, true},
    68  			CorrectlyVotedTarget: []bool{false, false, true},
    69  		},
    70  	}
    71  
    72  	v.prevBalance[pubKeyBytes[0]] = uint64(33200000000)
    73  	v.prevBalance[pubKeyBytes[1]] = uint64(33300000000)
    74  	v.prevBalance[pubKeyBytes[2]] = uint64(31000000000)
    75  
    76  	var hook *logTest.Hook
    77  
    78  	for i, val := range responses {
    79  		if i == len(responses)-1 { // Handle last log.
    80  			hook = logTest.NewGlobal()
    81  		}
    82  		v.UpdateLogAggregateStats(val, types.Slot(32*(i+1)))
    83  	}
    84  
    85  	require.LogsContain(t, hook, "msg=\"Previous epoch aggregated voting summary\" attestationInclusionPct=\"67%\" "+
    86  		"correctlyVotedHeadPct=\"100%\" correctlyVotedSourcePct=\"100%\" correctlyVotedTargetPct=\"50%\" epoch=2")
    87  	require.LogsContain(t, hook, "msg=\"Vote summary since launch\" attestationsInclusionPct=\"78%\" "+
    88  		"averageInclusionDistance=\"2.29 slots\" correctlyVotedHeadPct=\"86%\" correctlyVotedSourcePct=\"100%\" "+
    89  		"correctlyVotedTargetPct=\"86%\" numberOfEpochs=3 pctChangeCombinedBalance=\"0.20555%\"")
    90  
    91  }