github.com/prysmaticlabs/prysm@v1.4.4/slasher/beaconclient/chain_data_test.go (about)

     1  package beaconclient
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    10  	"github.com/prysmaticlabs/prysm/shared/mock"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    13  	logTest "github.com/sirupsen/logrus/hooks/test"
    14  )
    15  
    16  func TestService_ChainHead(t *testing.T) {
    17  	ctrl := gomock.NewController(t)
    18  	defer ctrl.Finish()
    19  	client := mock.NewMockBeaconChainClient(ctrl)
    20  
    21  	bs := Service{
    22  		cfg: &Config{BeaconClient: client},
    23  	}
    24  	wanted := &ethpb.ChainHead{
    25  		HeadSlot:      4,
    26  		HeadEpoch:     0,
    27  		HeadBlockRoot: make([]byte, 32),
    28  	}
    29  	client.EXPECT().GetChainHead(gomock.Any(), gomock.Any()).Return(wanted, nil)
    30  	res, err := bs.ChainHead(context.Background())
    31  	require.NoError(t, err)
    32  	require.DeepEqual(t, wanted, res)
    33  }
    34  
    35  func TestService_GenesisValidatorsRoot(t *testing.T) {
    36  	ctrl := gomock.NewController(t)
    37  	defer ctrl.Finish()
    38  
    39  	client := mock.NewMockNodeClient(ctrl)
    40  	bs := Service{
    41  		cfg: &Config{NodeClient: client},
    42  	}
    43  	wanted := &ethpb.Genesis{
    44  		GenesisValidatorsRoot: []byte("I am genesis"),
    45  	}
    46  	client.EXPECT().GetGenesis(gomock.Any(), gomock.Any()).Return(wanted, nil)
    47  	res, err := bs.GenesisValidatorsRoot(context.Background())
    48  	require.NoError(t, err)
    49  	assert.DeepEqual(t, wanted.GenesisValidatorsRoot, res, "Wanted %#x, received %#x", wanted.GenesisValidatorsRoot, res)
    50  	// test next fetch uses memory and not the rpc call.
    51  	res, err = bs.GenesisValidatorsRoot(context.Background())
    52  	require.NoError(t, err)
    53  	assert.DeepEqual(t, wanted.GenesisValidatorsRoot, res, "Wanted %#x, received %#x", wanted.GenesisValidatorsRoot, res)
    54  }
    55  
    56  func TestService_QuerySyncStatus(t *testing.T) {
    57  	hook := logTest.NewGlobal()
    58  	ctrl := gomock.NewController(t)
    59  	defer ctrl.Finish()
    60  	client := mock.NewMockNodeClient(ctrl)
    61  
    62  	bs := Service{
    63  		cfg: &Config{NodeClient: client},
    64  	}
    65  	syncStatusPollingInterval = time.Millisecond
    66  	client.EXPECT().GetSyncStatus(gomock.Any(), gomock.Any()).Return(&ethpb.SyncStatus{
    67  		Syncing: true,
    68  	}, nil)
    69  	client.EXPECT().GetSyncStatus(gomock.Any(), gomock.Any()).Return(&ethpb.SyncStatus{
    70  		Syncing: false,
    71  	}, nil)
    72  	bs.querySyncStatus(context.Background())
    73  	require.LogsContain(t, hook, "Waiting for beacon node to be fully synced...")
    74  	require.LogsContain(t, hook, "Beacon node is fully synced")
    75  }