2022-12-05 10:27:41 +00:00
|
|
|
package beacon_api
|
|
|
|
|
|
|
|
import (
|
2023-01-06 03:32:13 +00:00
|
|
|
"context"
|
2022-12-05 10:27:41 +00:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/config/params"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/validator/client/beacon-api/mock"
|
2024-02-21 18:37:17 +00:00
|
|
|
"go.uber.org/mock/gomock"
|
2022-12-05 10:27:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetDomainData_ValidDomainData(t *testing.T) {
|
|
|
|
const genesisValidatorRoot = "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"
|
|
|
|
forkVersion := params.BeaconConfig().AltairForkVersion
|
|
|
|
epoch := params.BeaconConfig().AltairForkEpoch
|
|
|
|
domainType := params.BeaconConfig().DomainBeaconProposer
|
|
|
|
|
|
|
|
genesisValidatorRootBytes, err := hexutil.Decode(genesisValidatorRoot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedForkDataRoot, err := (ðpb.ForkData{
|
|
|
|
CurrentVersion: forkVersion,
|
|
|
|
GenesisValidatorsRoot: genesisValidatorRootBytes,
|
|
|
|
}).HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2023-01-06 03:32:13 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-12-05 10:27:41 +00:00
|
|
|
// Make sure that GetGenesis() is called exactly once
|
2023-11-21 16:42:55 +00:00
|
|
|
genesisProvider := mock.NewMockGenesisProvider(ctrl)
|
2023-01-06 03:32:13 +00:00
|
|
|
genesisProvider.EXPECT().GetGenesis(ctx).Return(
|
2024-02-03 11:57:01 +00:00
|
|
|
&structs.Genesis{GenesisValidatorsRoot: genesisValidatorRoot},
|
2022-12-05 10:27:41 +00:00
|
|
|
nil,
|
|
|
|
).Times(1)
|
|
|
|
|
|
|
|
validatorClient := &beaconApiValidatorClient{genesisProvider: genesisProvider}
|
2023-01-06 03:32:13 +00:00
|
|
|
resp, err := validatorClient.getDomainData(ctx, epoch, domainType)
|
2022-12-05 10:27:41 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
|
|
|
|
var expectedSignatureDomain []byte
|
|
|
|
expectedSignatureDomain = append(expectedSignatureDomain, domainType[:]...)
|
|
|
|
expectedSignatureDomain = append(expectedSignatureDomain, expectedForkDataRoot[:28]...)
|
|
|
|
|
|
|
|
assert.Equal(t, len(expectedSignatureDomain), len(resp.SignatureDomain))
|
|
|
|
assert.DeepEqual(t, expectedSignatureDomain, resp.SignatureDomain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDomainData_GenesisError(t *testing.T) {
|
|
|
|
epoch := params.BeaconConfig().AltairForkEpoch
|
|
|
|
domainType := params.BeaconConfig().DomainBeaconProposer
|
|
|
|
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2023-01-06 03:32:13 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-12-05 10:27:41 +00:00
|
|
|
// Make sure that GetGenesis() is called exactly once
|
2023-11-21 16:42:55 +00:00
|
|
|
genesisProvider := mock.NewMockGenesisProvider(ctrl)
|
2023-12-22 22:39:20 +00:00
|
|
|
genesisProvider.EXPECT().GetGenesis(ctx).Return(nil, errors.New("foo error")).Times(1)
|
2022-12-05 10:27:41 +00:00
|
|
|
|
|
|
|
validatorClient := &beaconApiValidatorClient{genesisProvider: genesisProvider}
|
2023-01-06 03:32:13 +00:00
|
|
|
_, err := validatorClient.getDomainData(ctx, epoch, domainType)
|
2022-12-05 10:27:41 +00:00
|
|
|
assert.ErrorContains(t, "failed to get genesis info", err)
|
2022-12-08 14:38:56 +00:00
|
|
|
assert.ErrorContains(t, "foo error", err)
|
2022-12-05 10:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDomainData_InvalidGenesisRoot(t *testing.T) {
|
|
|
|
epoch := params.BeaconConfig().AltairForkEpoch
|
|
|
|
domainType := params.BeaconConfig().DomainBeaconProposer
|
|
|
|
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2023-01-06 03:32:13 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-12-05 10:27:41 +00:00
|
|
|
// Make sure that GetGenesis() is called exactly once
|
2023-11-21 16:42:55 +00:00
|
|
|
genesisProvider := mock.NewMockGenesisProvider(ctrl)
|
2023-01-06 03:32:13 +00:00
|
|
|
genesisProvider.EXPECT().GetGenesis(ctx).Return(
|
2024-02-03 11:57:01 +00:00
|
|
|
&structs.Genesis{GenesisValidatorsRoot: "foo"},
|
2022-12-05 10:27:41 +00:00
|
|
|
nil,
|
|
|
|
).Times(1)
|
|
|
|
|
|
|
|
validatorClient := &beaconApiValidatorClient{genesisProvider: genesisProvider}
|
2023-01-06 03:32:13 +00:00
|
|
|
_, err := validatorClient.getDomainData(ctx, epoch, domainType)
|
2022-12-05 10:27:41 +00:00
|
|
|
assert.ErrorContains(t, "invalid genesis validators root: foo", err)
|
|
|
|
}
|